Got started on materialization code for intermediate datasets

These are the steps required to produce the raw dataset referenced here:
https://github.com/bugout-dev/moonstream/issues/272#issuecomment-926087702

Intermediate steps:
https://github.com/bugout-dev/moonstream/issues/272#issuecomment-927144872
pull/304/head
Neeraj Kashyap 2021-09-25 10:23:33 -07:00
rodzic abae67143a
commit 072a86c8fc
2 zmienionych plików z 75 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,9 @@
from moonstreamdb.db import yield_db_session_ctx
from .materialize import get_rows, EventType
if __name__ == "__main__":
with yield_db_session_ctx() as db_session:
rows = get_rows(db_session, EventType.TRANSFER)
for row in rows:
print(row)

Wyświetl plik

@ -0,0 +1,66 @@
from dataclasses import dataclass
from enum import Enum
from typing import List, Optional
from moonstreamdb.models import (
EthereumAddress,
EthereumLabel,
EthereumTransaction,
EthereumBlock,
)
from sqlalchemy.orm import Session
@dataclass
class BlockBounds:
starting_block: int
ending_block: Optional[int] = None
class EventType(Enum):
TRANSFER = "nft_transfer"
MINT = "nft_mint"
@dataclass
class NFTEvent:
event_type: EventType
nft_address: str
token_id: str
from_address: str
to_address: str
transaction_hash: str
value: Optional[int] = None
block_number: Optional[int] = None
timestamp: Optional[int] = None
def get_rows(
db_session: Session, event_type: EventType, bounds: Optional[BlockBounds] = None
) -> List[NFTEvent]:
query = (
db_session.query(
EthereumLabel.label,
EthereumAddress.address,
EthereumLabel.label_data,
EthereumLabel.transaction_hash,
)
.join(EthereumAddress, EthereumLabel.address_id == EthereumAddress.id)
.outerjoin(
EthereumTransaction,
EthereumLabel.transaction_hash == EthereumTransaction.hash,
)
.filter(EthereumLabel.label == event_type.value)
.limit(10)
)
return [
NFTEvent(
event_type=label,
nft_address=address,
token_id=label_data["tokenId"],
from_address=label_data["from"],
to_address=label_data["to"],
transaction_hash=transaction_hash,
)
for label, address, label_data, transaction_hash in query
]