Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starknet blockchain in database #936

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions moonstreamdb/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
ZkSyncEraTestnetBlock,
ZkSyncEraTestnetLabel,
ZkSyncEraTestnetTransaction,
StarknetBlock,
StarknetTransaction,
StarknetLabel,
)


Expand All @@ -72,6 +75,9 @@ def include_symbol(tablename, schema):
ZkSyncEraTestnetBlock.__tablename__,
ZkSyncEraTestnetLabel.__tablename__,
ZkSyncEraTestnetTransaction.__tablename__,
StarknetBlock.__tablename__,
StarknetTransaction.__tablename__,
StarknetLabel.__tablename__,
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""Starknet blocks, txs, labels

Revision ID: 6f8c4b9e3970
Revises: 0f8ee1ebb45f
Create Date: 2023-10-31 13:46:35.536476

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '6f8c4b9e3970'
down_revision = '0f8ee1ebb45f'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('starknet_blocks',
sa.Column('block_number', sa.BigInteger(), nullable=False),
sa.Column('sequencer_address', sa.VARCHAR(length=256), nullable=True),
sa.Column('status', sa.VARCHAR(length=256), nullable=True),
sa.Column('hash', sa.VARCHAR(length=256), nullable=False),
sa.Column('parent_hash', sa.VARCHAR(length=256), nullable=True),
sa.Column('new_root', sa.VARCHAR(length=256), nullable=True),
sa.Column('timestamp', sa.BigInteger(), nullable=False),
sa.Column('indexed_at', sa.DateTime(timezone=True), server_default=sa.text("TIMEZONE('utc', statement_timestamp())"), nullable=False),
sa.PrimaryKeyConstraint('block_number', name=op.f('pk_starknet_blocks'))
)
op.create_index(op.f('ix_starknet_blocks_block_number'), 'starknet_blocks', ['block_number'], unique=True)
op.create_index(op.f('ix_starknet_blocks_hash'), 'starknet_blocks', ['hash'], unique=False)
op.create_index(op.f('ix_starknet_blocks_timestamp'), 'starknet_blocks', ['timestamp'], unique=False)
op.create_table('starknet_labels',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('label', sa.VARCHAR(length=256), nullable=False),
sa.Column('block_number', sa.BigInteger(), nullable=True),
sa.Column('address', sa.VARCHAR(length=256), nullable=True),
sa.Column('transaction_hash', sa.VARCHAR(length=256), nullable=True),
sa.Column('label_data', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('block_timestamp', sa.BigInteger(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text("TIMEZONE('utc', statement_timestamp())"), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_starknet_labels')),
sa.UniqueConstraint('id', name=op.f('uq_starknet_labels_id'))
)
op.create_index(op.f('ix_starknet_labels_address'), 'starknet_labels', ['address'], unique=False)
op.create_index(op.f('ix_starknet_labels_block_number'), 'starknet_labels', ['block_number'], unique=False)
op.create_index(op.f('ix_starknet_labels_block_timestamp'), 'starknet_labels', ['block_timestamp'], unique=False)
op.create_index(op.f('ix_starknet_labels_label'), 'starknet_labels', ['label'], unique=False)
op.create_index(op.f('ix_starknet_labels_transaction_hash'), 'starknet_labels', ['transaction_hash'], unique=False)
op.create_table('starknet_transactions',
sa.Column('hash', sa.VARCHAR(length=256), nullable=False),
sa.Column('calldata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('address', sa.VARCHAR(length=256), nullable=False),
sa.Column('entry_point_selector', sa.VARCHAR(length=256), nullable=True),
sa.Column('max_fee', sa.Numeric(precision=78, scale=0), nullable=True),
sa.Column('nonce', sa.VARCHAR(length=256), nullable=True),
sa.Column('signature', sa.ARRAY(sa.Text()), nullable=True),
sa.Column('type', sa.VARCHAR(length=256), nullable=True),
sa.Column('version', sa.VARCHAR(length=256), nullable=True),
sa.Column('indexed_at', sa.DateTime(timezone=True), server_default=sa.text("TIMEZONE('utc', statement_timestamp())"), nullable=False),
sa.PrimaryKeyConstraint('hash', name=op.f('pk_starknet_transactions'))
)
op.create_index(op.f('ix_starknet_transactions_address'), 'starknet_transactions', ['address'], unique=False)
op.create_index(op.f('ix_starknet_transactions_hash'), 'starknet_transactions', ['hash'], unique=True)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_starknet_transactions_hash'), table_name='starknet_transactions')
op.drop_index(op.f('ix_starknet_transactions_address'), table_name='starknet_transactions')
op.drop_table('starknet_transactions')
op.drop_index(op.f('ix_starknet_labels_transaction_hash'), table_name='starknet_labels')
op.drop_index(op.f('ix_starknet_labels_label'), table_name='starknet_labels')
op.drop_index(op.f('ix_starknet_labels_block_timestamp'), table_name='starknet_labels')
op.drop_index(op.f('ix_starknet_labels_block_number'), table_name='starknet_labels')
op.drop_index(op.f('ix_starknet_labels_address'), table_name='starknet_labels')
op.drop_table('starknet_labels')
op.drop_index(op.f('ix_starknet_blocks_timestamp'), table_name='starknet_blocks')
op.drop_index(op.f('ix_starknet_blocks_hash'), table_name='starknet_blocks')
op.drop_index(op.f('ix_starknet_blocks_block_number'), table_name='starknet_blocks')
op.drop_table('starknet_blocks')
# ### end Alembic commands ###
16 changes: 16 additions & 0 deletions moonstreamdb/moonstreamdb/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
ZkSyncEraBlock,
ZkSyncEraLabel,
ZkSyncEraTransaction,
StarknetBlock,
StarknetTransaction,
StarknetLabel,
)


Expand All @@ -34,6 +37,7 @@ class AvailableBlockchainType(Enum):
WYRM = "wyrm"
ZKSYNC_ERA_TESTNET = "zksync_era_testnet"
ZKSYNC_ERA = "zksync_era"
STARKNET = "starknet"


def get_block_model(
Expand All @@ -47,6 +51,7 @@ def get_block_model(
WyrmBlock,
ZkSyncEraTestnetBlock,
ZkSyncEraBlock,
StarknetBlock,
]
]:
"""
Expand All @@ -62,6 +67,7 @@ def get_block_model(
WyrmBlock,
ZkSyncEraTestnetBlock,
ZkSyncEraBlock,
StarknetBlock,
]
]
if blockchain_type == AvailableBlockchainType.ETHEREUM:
Expand All @@ -78,6 +84,8 @@ def get_block_model(
block_model = ZkSyncEraTestnetBlock
elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA:
block_model = ZkSyncEraBlock
elif blockchain_type == AvailableBlockchainType.STARKNET:
block_model = StarknetBlock
else:
raise Exception("Unsupported blockchain type provided")

Expand All @@ -95,6 +103,7 @@ def get_label_model(
WyrmLabel,
ZkSyncEraTestnetLabel,
ZkSyncEraLabel,
StarknetLabel,
]
]:
"""
Expand All @@ -110,6 +119,7 @@ def get_label_model(
WyrmLabel,
ZkSyncEraTestnetLabel,
ZkSyncEraLabel,
StarknetLabel,
]
]
if blockchain_type == AvailableBlockchainType.ETHEREUM:
Expand All @@ -126,6 +136,8 @@ def get_label_model(
label_model = ZkSyncEraTestnetLabel
elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA:
label_model = ZkSyncEraLabel
elif blockchain_type == AvailableBlockchainType.STARKNET:
label_model = StarknetLabel
else:
raise Exception("Unsupported blockchain type provided")

Expand All @@ -143,6 +155,7 @@ def get_transaction_model(
WyrmTransaction,
ZkSyncEraTestnetTransaction,
ZkSyncEraTransaction,
StarknetTransaction,
]
]:
"""
Expand All @@ -158,6 +171,7 @@ def get_transaction_model(
WyrmTransaction,
ZkSyncEraTestnetTransaction,
ZkSyncEraTransaction,
StarknetTransaction,
]
]
if blockchain_type == AvailableBlockchainType.ETHEREUM:
Expand All @@ -174,6 +188,8 @@ def get_transaction_model(
transaction_model = ZkSyncEraTestnetTransaction
elif blockchain_type == AvailableBlockchainType.ZKSYNC_ERA:
transaction_model = ZkSyncEraTransaction
elif blockchain_type == AvailableBlockchainType.STARKNET:
transaction_model = StarknetTransaction
else:
raise Exception("Unsupported blockchain type provided")

Expand Down
87 changes: 87 additions & 0 deletions moonstreamdb/moonstreamdb/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import uuid

from sqlalchemy import (
ARRAY,
VARCHAR,
BigInteger,
Column,
Expand Down Expand Up @@ -923,3 +924,89 @@ class OpenSeaCrawlingState(Base): # type: ignore
)

total_count = Column(Integer, nullable=False)


class StarknetBlock(Base): # type: ignore
__tablename__ = "starknet_blocks"

block_number = Column(
BigInteger, primary_key=True, unique=True, nullable=False, index=True
)
sequencer_address = Column(VARCHAR(256))
status = Column(VARCHAR(256))
hash = Column(VARCHAR(256), index=True, nullable=False)
parent_hash = Column(VARCHAR(256))
new_root = Column(VARCHAR(256))
timestamp = Column(BigInteger, index=True, nullable=False)
indexed_at = Column(
DateTime(timezone=True), server_default=utcnow(), nullable=False
)


class StarknetTransaction(Base): # type: ignore
__tablename__ = "starknet_transactions"

hash = Column(
VARCHAR(256), primary_key=True, unique=True, nullable=False, index=True
)
calldata = Column(JSONB, nullable=True)
address = Column(VARCHAR(256), index=True, nullable=False)
entry_point_selector = Column(VARCHAR(256))
max_fee = Column(Numeric(precision=78, scale=0), nullable=True)
nonce = Column(VARCHAR(256))
signature = Column(ARRAY(Text))
type = Column(VARCHAR(256))
version = Column(VARCHAR(256))

indexed_at = Column(
DateTime(timezone=True), server_default=utcnow(), nullable=False
)


class StarknetLabel(Base): # type: ignore
__tablename__ = "starknet_labels"

__table_args__ = (
Index(
"ix_polygon_labels_address_block_number",
"address",
"block_number",
unique=False,
),
Index(
"ix_polygon_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)

created_at = Column(
DateTime(timezone=True), server_default=utcnow(), nullable=False
)
10 changes: 10 additions & 0 deletions moonstreamdb/moonstreamdb/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
ZkSyncEraBlock,
ZkSyncEraLabel,
ZkSyncEraTransaction,
StarknetBlock,
StarknetTransaction,
StarknetLabel,
)


Expand All @@ -35,6 +38,7 @@ class Network(Enum):
wyrm = "wyrm"
zksync_era_testnet = "zksync_era_testnet"
zksync_era = "zksync_era"
starknet = "starknet"


tx_raw_types = Union[
Expand All @@ -45,6 +49,7 @@ class Network(Enum):
XDaiTransaction,
ZkSyncEraTestnetTransaction,
ZkSyncEraTransaction,
StarknetTransaction,
]

MODELS: Dict[Network, Dict[str, Base]] = {
Expand Down Expand Up @@ -83,4 +88,9 @@ class Network(Enum):
"labels": ZkSyncEraLabel,
"transactions": ZkSyncEraTransaction,
},
Network.starknet: {
"blocks": StarknetBlock,
"labels": StarknetLabel,
"transactions": StarknetTransaction,
},
}
2 changes: 1 addition & 1 deletion moonstreamdb/moonstreamdb/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Moonstream database version.
"""

MOONSTREAMDB_VERSION = "0.3.5"
MOONSTREAMDB_VERSION = "0.3.6"
Loading