diff --git a/moonstreamdb/alembic/env.py b/moonstreamdb/alembic/env.py index 6dbe9aa8..2d4c8c37 100644 --- a/moonstreamdb/alembic/env.py +++ b/moonstreamdb/alembic/env.py @@ -25,6 +25,9 @@ target_metadata = MoonstreamBase.metadata # my_important_option = config.get_main_option("my_important_option") # ... etc. from moonstreamdb.models import ( + AmoyBlock, + AmoyTransaction, + AmoyLabel, ArbitrumNovaBlock, ArbitrumNovaLabel, ArbitrumNovaTransaction, @@ -90,6 +93,9 @@ def include_symbol(tablename, schema): MumbaiBlock.__tablename__, MumbaiTransaction.__tablename__, MumbaiLabel.__tablename__, + AmoyBlock.__tablename__, + AmoyTransaction.__tablename__, + AmoyLabel.__tablename__, ESDFunctionSignature.__tablename__, ESDEventSignature.__tablename__, OpenSeaCrawlingState.__tablename__, diff --git a/moonstreamdb/moonstreamdb/blockchain.py b/moonstreamdb/moonstreamdb/blockchain.py index 8b317961..376c8ef2 100644 --- a/moonstreamdb/moonstreamdb/blockchain.py +++ b/moonstreamdb/moonstreamdb/blockchain.py @@ -2,6 +2,9 @@ from enum import Enum from typing import Type, Union from .models import ( + AmoyBlock, + AmoyLabel, + AmoyTransaction, ArbitrumNovaBlock, ArbitrumNovaLabel, ArbitrumNovaTransaction, @@ -57,6 +60,7 @@ class AvailableBlockchainType(Enum): ETHEREUM = "ethereum" POLYGON = "polygon" MUMBAI = "mumbai" + AMOY = "amoy" XDAI = "xdai" WYRM = "wyrm" ZKSYNC_ERA = "zksync_era" @@ -79,6 +83,7 @@ def get_block_model( EthereumBlock, PolygonBlock, MumbaiBlock, + AmoyBlock, XDaiBlock, WyrmBlock, ZkSyncEraTestnetBlock, @@ -95,14 +100,14 @@ def get_block_model( ] ]: """ - Depends on provided blockchain type: Ethereum, Polygon, Mumbai, XDai, Wyrm, ZkSyncEra, ZkSyncEraTestnet, ArbitrumNova, ArbitrumSepolia, Xai, XaiSepolia - set proper blocks model. + Depends on provided blockchain type set proper blocks model. """ block_model: Type[ Union[ EthereumBlock, PolygonBlock, MumbaiBlock, + AmoyBlock, XDaiBlock, WyrmBlock, ZkSyncEraTestnetBlock, @@ -124,6 +129,8 @@ def get_block_model( block_model = PolygonBlock elif blockchain_type == AvailableBlockchainType.MUMBAI: block_model = MumbaiBlock + elif blockchain_type == AvailableBlockchainType.AMOY: + block_model = AmoyBlock elif blockchain_type == AvailableBlockchainType.XDAI: block_model = XDaiBlock elif blockchain_type == AvailableBlockchainType.WYRM: @@ -163,6 +170,7 @@ def get_label_model( EthereumLabel, PolygonLabel, MumbaiLabel, + AmoyLabel, XDaiLabel, WyrmLabel, ZkSyncEraTestnetLabel, @@ -179,14 +187,14 @@ def get_label_model( ] ]: """ - Depends on provided blockchain type: Ethereum, Polygon, Mumbai, XDai, Wyrm, ZkSyncEra, ZkSyncEraTestnet, ArbitrumNova, ArbitrumSepolia, Xai, XaiSepolia - set proper block label model. + Depends on provided blockchain type set proper block label model. """ label_model: Type[ Union[ EthereumLabel, PolygonLabel, MumbaiLabel, + AmoyLabel, XDaiLabel, WyrmLabel, ZkSyncEraTestnetLabel, @@ -208,6 +216,8 @@ def get_label_model( label_model = PolygonLabel elif blockchain_type == AvailableBlockchainType.MUMBAI: label_model = MumbaiLabel + elif blockchain_type == AvailableBlockchainType.AMOY: + label_model = AmoyLabel elif blockchain_type == AvailableBlockchainType.XDAI: label_model = XDaiLabel elif blockchain_type == AvailableBlockchainType.WYRM: @@ -247,6 +257,7 @@ def get_transaction_model( EthereumTransaction, PolygonTransaction, MumbaiTransaction, + AmoyTransaction, XDaiTransaction, WyrmTransaction, ZkSyncEraTestnetTransaction, @@ -263,14 +274,14 @@ def get_transaction_model( ] ]: """ - Depends on provided blockchain type: Ethereum, Polygon, Mumbai, XDai, Wyrm, ZkSyncEra, ZkSyncEraTestnet, ArbitrumNova, ArbitrumSepolia, Xai, XaiSepolia - set proper block transactions model. + Depends on provided blockchain type set proper block transactions model. """ transaction_model: Type[ Union[ EthereumTransaction, PolygonTransaction, MumbaiTransaction, + AmoyTransaction, XDaiTransaction, WyrmTransaction, ZkSyncEraTestnetTransaction, @@ -292,6 +303,8 @@ def get_transaction_model( transaction_model = PolygonTransaction elif blockchain_type == AvailableBlockchainType.MUMBAI: transaction_model = MumbaiTransaction + elif blockchain_type == AvailableBlockchainType.AMOY: + transaction_model = AmoyTransaction elif blockchain_type == AvailableBlockchainType.XDAI: transaction_model = XDaiTransaction elif blockchain_type == AvailableBlockchainType.WYRM: diff --git a/moonstreamdb/moonstreamdb/models.py b/moonstreamdb/moonstreamdb/models.py index c5c3b8d9..fa6c1380 100644 --- a/moonstreamdb/moonstreamdb/models.py +++ b/moonstreamdb/moonstreamdb/models.py @@ -368,6 +368,113 @@ class MumbaiLabel(Base): # type: ignore ) +class AmoyBlock(Base): # type: ignore + __tablename__ = "amoy_blocks" + + block_number = Column( + BigInteger, primary_key=True, unique=True, nullable=False, index=True + ) + difficulty = Column(BigInteger) + extra_data = Column(VARCHAR(128)) + gas_limit = Column(BigInteger) + gas_used = Column(BigInteger) + base_fee_per_gas = Column(Numeric(precision=78, scale=0), nullable=True) + hash = Column(VARCHAR(256), index=True) + logs_bloom = Column(VARCHAR(1024)) + miner = Column(VARCHAR(256)) + nonce = Column(VARCHAR(256)) + parent_hash = Column(VARCHAR(256)) + receipt_root = Column(VARCHAR(256)) + uncles = Column(VARCHAR(256)) + size = Column(Integer) + state_root = Column(VARCHAR(256)) + timestamp = Column(BigInteger, index=True) + total_difficulty = Column(VARCHAR(256)) + transactions_root = Column(VARCHAR(256)) + + indexed_at = Column( + DateTime(timezone=True), server_default=utcnow(), nullable=False + ) + + +class AmoyTransaction(Base): # type: ignore + __tablename__ = "amoy_transactions" + + hash = Column( + VARCHAR(256), primary_key=True, unique=True, nullable=False, index=True + ) + block_number = Column( + BigInteger, + ForeignKey("amoy_blocks.block_number", ondelete="CASCADE"), + nullable=False, + index=True, + ) + from_address = Column(VARCHAR(256), index=True) + to_address = Column(VARCHAR(256), index=True) + gas = Column(Numeric(precision=78, scale=0), index=True) + gas_price = Column(Numeric(precision=78, scale=0), index=True) + max_fee_per_gas = Column(Numeric(precision=78, scale=0), nullable=True) + max_priority_fee_per_gas = Column(Numeric(precision=78, scale=0), nullable=True) + input = Column(Text) + nonce = Column(VARCHAR(256)) + transaction_index = Column(BigInteger) + transaction_type = Column(Integer, nullable=True) + value = Column(Numeric(precision=78, scale=0), index=True) + + indexed_at = Column( + DateTime(timezone=True), server_default=utcnow(), nullable=False + ) + + +class AmoyLabel(Base): # type: ignore + __tablename__ = "amoy_labels" + + __table_args__ = ( + Index( + "ix_amoy_labels_address_block_number", + "address", + "block_number", + unique=False, + ), + Index( + "ix_amoy_labels_address_block_timestamp", + "address", + "block_timestamp", + unique=False, + ), + ) + + id = Column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + unique=True, + nullable=False, + ) + label = Column(VARCHAR(256), nullable=False, index=True) + block_number = Column( + BigInteger, + nullable=True, + index=True, + ) + address = Column( + VARCHAR(256), + nullable=True, + index=True, + ) + transaction_hash = Column( + VARCHAR(256), + nullable=True, + index=True, + ) + label_data = Column(JSONB, nullable=True) + block_timestamp = Column(BigInteger, index=True) + log_index = Column(Integer, nullable=True) + created_at = Column( + DateTime(timezone=True), server_default=utcnow(), nullable=False + ) + + class XDaiBlock(Base): # type: ignore __tablename__ = "xdai_blocks" diff --git a/moonstreamdb/moonstreamdb/networks.py b/moonstreamdb/moonstreamdb/networks.py index a70e9a78..5d665813 100644 --- a/moonstreamdb/moonstreamdb/networks.py +++ b/moonstreamdb/moonstreamdb/networks.py @@ -1,7 +1,11 @@ from enum import Enum from typing import Dict, Union +from .blockchain import AvailableBlockchainType from .models import ( + AmoyBlock, + AmoyLabel, + AmoyTransaction, ArbitrumNovaBlock, ArbitrumNovaLabel, ArbitrumNovaTransaction, @@ -58,6 +62,7 @@ class Network(Enum): ethereum = "ethereum" polygon = "polygon" mumbai = "mumbai" + amoy = "amoy" xdai = "xdai" wyrm = "wyrm" zksync_era_testnet = "zksync_era_testnet" @@ -76,6 +81,7 @@ class Network(Enum): tx_raw_types = Union[ EthereumTransaction, MumbaiTransaction, + AmoyTransaction, PolygonTransaction, WyrmTransaction, XDaiTransaction, @@ -103,6 +109,11 @@ MODELS: Dict[Network, Dict[str, Base]] = { "labels": MumbaiLabel, "transactions": MumbaiTransaction, }, + Network.amoy: { + "blocks": AmoyBlock, + "labels": AmoyLabel, + "transactions": AmoyTransaction, + }, Network.polygon: { "blocks": PolygonBlock, "labels": PolygonLabel, @@ -174,3 +185,44 @@ MODELS: Dict[Network, Dict[str, Base]] = { "transactions": BlastSepoliaTransaction, }, } + + +def blockchain_type_to_network_type( + blockchain_type: AvailableBlockchainType, +) -> Network: + if blockchain_type == AvailableBlockchainType.ETHEREUM: + return Network.ethereum + elif blockchain_type == AvailableBlockchainType.POLYGON: + return Network.polygon + elif blockchain_type == AvailableBlockchainType.MUMBAI: + return Network.mumbai + elif blockchain_type == AvailableBlockchainType.AMOY: + return Network.amoy + elif blockchain_type == AvailableBlockchainType.XDAI: + return Network.xdai + elif blockchain_type == AvailableBlockchainType.WYRM: + return Network.wyrm + elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA_TESTNET: + return Network.zksync_era_testnet + elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA: + return Network.zksync_era + elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA_SEPOLIA: + return Network.zksync_era_sepolia + elif blockchain_type == AvailableBlockchainType.ARBITRUM_NOVA: + return Network.arbitrum_nova + elif blockchain_type == AvailableBlockchainType.ARBITRUM_SEPOLIA: + return Network.arbitrum_sepolia + elif blockchain_type == AvailableBlockchainType.XAI: + return Network.xai + elif blockchain_type == AvailableBlockchainType.XAI_SEPOLIA: + return Network.xai_sepolia + elif blockchain_type == AvailableBlockchainType.AVALANCHE: + return Network.avalanche + elif blockchain_type == AvailableBlockchainType.AVALANCHE_FUJI: + return Network.avalanche_fuji + elif blockchain_type == AvailableBlockchainType.BLAST: + return Network.blast + elif blockchain_type == AvailableBlockchainType.BLAST_SEPOLIA: + return Network.blast_sepolia + else: + raise ValueError(f"Unknown blockchain type: {blockchain_type}") diff --git a/moonstreamdb/moonstreamdb/subscriptions.py b/moonstreamdb/moonstreamdb/subscriptions.py new file mode 100644 index 00000000..2ad58bfd --- /dev/null +++ b/moonstreamdb/moonstreamdb/subscriptions.py @@ -0,0 +1,122 @@ +from enum import Enum + +from .blockchain import AvailableBlockchainType + + +class SubscriptionTypes(Enum): + POLYGON_BLOCKCHAIN = "polygon_smartcontract" + ETHEREUM_BLOCKCHAIN = "ethereum_smartcontract" + MUMBAI_BLOCKCHAIN = "mumbai_smartcontract" + AMOY_BLOCKCHAIN = "amoy_smartcontract" + XDAI_BLOCKCHAIN = "xdai_smartcontract" + WYRM_BLOCKCHAIN = "wyrm_smartcontract" + ZKSYNC_ERA_TESTNET_BLOCKCHAIN = "zksync_era_testnet_smartcontract" + ZKSYNC_ERA_BLOCKCHAIN = "zksync_era_smartcontract" + ZKSYNC_ERA_SEPOLIA_BLOCKCHAIN = "zksync_era_sepolia_smartcontract" + ARBITRUM_NOVA_BLOCKCHAIN = "arbitrum_nova_smartcontract" + ARBITRUM_SEPOLIA_BLOCKCHAIN = "arbitrum_sepolia_smartcontract" + XAI_BLOCKCHAIN = "xai_smartcontract" + XAI_SEPOLIA_BLOCKCHAIN = "xai_sepolia_smartcontract" + AVALANCHE_BLOCKCHAIN = "avalanche_smartcontract" + AVALANCHE_FUJI_BLOCKCHAIN = "avalanche_fuji_smartcontract" + BLAST_BLOCKCHAIN = "blast_smartcontract" + BLAST_SEPOLIA_BLOCKCHAIN = "blast_sepolia_smartcontract" + + +def blockchain_type_to_subscription_type( + blockchain_type: AvailableBlockchainType, +) -> SubscriptionTypes: + if blockchain_type == AvailableBlockchainType.ETHEREUM: + return SubscriptionTypes.ETHEREUM_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.POLYGON: + return SubscriptionTypes.POLYGON_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.MUMBAI: + return SubscriptionTypes.MUMBAI_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.AMOY: + return SubscriptionTypes.AMOY_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.XDAI: + return SubscriptionTypes.XDAI_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.WYRM: + return SubscriptionTypes.WYRM_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA_TESTNET: + return SubscriptionTypes.ZKSYNC_ERA_TESTNET_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA: + return SubscriptionTypes.ZKSYNC_ERA_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA_SEPOLIA: + return SubscriptionTypes.ZKSYNC_ERA_SEPOLIA_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.ARBITRUM_NOVA: + return SubscriptionTypes.ARBITRUM_NOVA_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.ARBITRUM_SEPOLIA: + return SubscriptionTypes.ARBITRUM_SEPOLIA_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.XAI: + return SubscriptionTypes.XAI_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.XAI_SEPOLIA: + return SubscriptionTypes.XAI_SEPOLIA_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.AVALANCHE: + return SubscriptionTypes.AVALANCHE_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.AVALANCHE_FUJI: + return SubscriptionTypes.AVALANCHE_FUJI_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.BLAST: + return SubscriptionTypes.BLAST_BLOCKCHAIN + elif blockchain_type == AvailableBlockchainType.BLAST_SEPOLIA: + return SubscriptionTypes.BLAST_SEPOLIA_BLOCKCHAIN + else: + raise ValueError(f"Unknown blockchain type: {blockchain_type}") + + +subscription_id_by_blockchain = { + "ethereum": "ethereum_smartcontract", + "polygon": "polygon_smartcontract", + "mumbai": "mumbai_smartcontract", + "amoy": "amoy_smartcontract", + "xdai": "xdai_smartcontract", + "wyrm": "wyrm_smartcontract", + "zksync_era_testnet": "zksync_era_testnet_smartcontract", + "zksync_era": "zksync_era_smartcontract", + "zksync_era_sepolia": "zksync_era_sepolia_smartcontract", + "arbitrum_nova": "arbitrum_nova_smartcontract", + "arbitrum_sepolia": "arbitrum_sepolia_smartcontract", + "xai": "xai_smartcontract", + "xai_sepolia": "xai_sepolia_smartcontract", + "avalanche": "avalanche_smartcontract", + "avalanche_fuji": "avalanche_fuji_smartcontract", + "blast": "blast_smartcontract", + "blast_sepolia": "blast_sepolia_smartcontract", +} + +blockchain_by_subscription_id = { + "ethereum_blockchain": "ethereum", + "polygon_blockchain": "polygon", + "mumbai_blockchain": "mumbai", + "amoy_blockchain": "amoy", + "xdai_blockchain": "xdai", + "wyrm_blockchain": "wyrm", + "zksync_era_testnet_blockchain": "zksync_era_testnet", + "zksync_era_blockchain": "zksync_era", + "zksync_era_sepolia_blockchain": "zksync_era_sepolia", + "arbitrum_nova_blockchain": "arbitrum_nova", + "arbitrum_sepolia_blockchain": "arbitrum_sepolia", + "xai_blockchain": "xai", + "xai_sepolia_blockchain": "xai_sepolia", + "avalanche_blockchain": "avalanche", + "avalanche_fuji_blockchain": "avalanche_fuji", + "blast_blockchain": "blast", + "blast_sepolia_blockchain": "blast_sepolia", + "ethereum_smartcontract": "ethereum", + "polygon_smartcontract": "polygon", + "mumbai_smartcontract": "mumbai", + "amoy_smartcontract": "amoy", + "xdai_smartcontract": "xdai", + "wyrm_smartcontract": "wyrm", + "zksync_era_testnet_smartcontract": "zksync_era_testnet", + "zksync_era_smartcontract": "zksync_era", + "zksync_era_sepolia_smartcontract": "zksync_era_sepolia", + "arbitrum_nova_smartcontract": "arbitrum_nova", + "arbitrum_sepolia_smartcontract": "arbitrum_sepolia", + "xai_smartcontract": "xai", + "xai_sepolia_smartcontract": "xai_sepolia", + "avalanche_smartcontract": "avalanche", + "avalanche_fuji_smartcontract": "avalanche_fuji", + "blast_smartcontract": "blast", + "blast_sepolia_smartcontract": "blast_sepolia", +} diff --git a/moonstreamdb/moonstreamdb/version.py b/moonstreamdb/moonstreamdb/version.py index f3fc1065..232cb871 100644 --- a/moonstreamdb/moonstreamdb/version.py +++ b/moonstreamdb/moonstreamdb/version.py @@ -2,4 +2,4 @@ Moonstream database version. """ -MOONSTREAMDB_VERSION = "0.3.12" +MOONSTREAMDB_VERSION = "0.4.1"