Skip to content

Commit

Permalink
misc typing
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout committed Dec 16, 2024
1 parent c2e631f commit bd931c8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/dipdup/datasources/substrate_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ async def get_block_hash(self, height: int) -> str:

async def get_block_header(self, hash: str) -> BlockHeader:
response = await self._jsonrpc_request('chain_getHeader', [hash])
# FIXME: missing fields
return {
'hash': hash,
'number': int(response['number'], 16),
Expand All @@ -242,16 +243,16 @@ async def get_metadata_header(self, height: int) -> MetadataHeader:
async def get_metadata_header_batch(self, heights: list[int]) -> list[MetadataHeader]:
return await asyncio.gather(*[self.get_metadata_header(h) for h in heights])

async def get_full_block(self, hash: str) -> dict:
async def get_full_block(self, hash: str) -> dict[str, Any]:
return await self._jsonrpc_request('chain_getBlock', [hash])

async def get_events(self, block_hash: str) -> tuple[SubstrateEventDataDict, ...]:
events = await self._interface.get_events(block_hash)

res_events: list[SubstrateEventDataDict] = []
for event in events:
event: dict = event.decode()
res_events.append(
result: list[SubstrateEventDataDict] = []
for raw_event in events:
event: dict[str, Any] = raw_event.decode()
result.append(
{
'name': f'{event['module_id']}.{event['event_id']}',
'index': event['event_index'],
Expand All @@ -262,7 +263,7 @@ async def get_events(self, block_hash: str) -> tuple[SubstrateEventDataDict, ...
}
)

return tuple(res_events)
return tuple(result)

async def find_metadata_versions(
self,
Expand Down
12 changes: 9 additions & 3 deletions src/dipdup/dipdup.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def _log_status(self) -> None:
# NOTE: Resulting message is about 80 chars with the current logging format
msg += ' ' * (48 - len(msg))

def fmt(speed):
def fmt(speed: float) -> str:
return ' 0' if speed < 0.1 else f'{speed:5.{0 if speed >= 1 else 1}f}'

msg += f' {fmt(levels_speed)} L {fmt(objects_speed)} O'
Expand Down Expand Up @@ -557,13 +557,19 @@ async def _on_tzkt_events(self, datasource: TezosTzktDatasource, events: tuple[T
index.push_realtime_message(events)

# TODO: fix data typing
async def _on_substrate_head(self, datasource: SubstrateNodeDatasource, head: HeadBlock) -> None:
async def _on_substrate_head(
self,
datasource: SubstrateNodeDatasource,
head: HeadBlock,
) -> None:
# TODO: any head updates here?
pass

# TODO: fix data typing
async def _on_substrate_events(
self, datasource: SubstrateNodeDatasource, events: tuple[dict[str, Any], ...]
self,
datasource: SubstrateNodeDatasource,
events: tuple[dict[str, Any], ...],
) -> None:
for index in self._indexes.values():
if isinstance(index, SubstrateEventsIndex) and datasource in index.datasources:
Expand Down
2 changes: 1 addition & 1 deletion src/dipdup/indexes/_subsquid.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def _get_node_sync_level(
self,
subsquid_level: int,
index_level: int,
node: JsonRpcDatasource | None = None,
node: JsonRpcDatasource[Any] | None = None,
) -> int | None:
if not self.node_datasources:
return None
Expand Down
9 changes: 7 additions & 2 deletions src/dipdup/models/substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class SubstrateEventDataDict(TypedDict):
index: int
extrinsicIndex: int
callAddress: list[str]
args: list[Any]
args: list[Any] | None
decoded_args: dict[str, Any] | None


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -73,7 +74,11 @@ class SubstrateEvent(Generic[PayloadT]):
# TODO: could be used in other models with typed payload
@cached_property
def payload(self) -> PayloadT:
return self.data.decoded_args or cast(
if self.data.decoded_args is not None:
return self.data.decoded_args

assert self.data.args is not None
return cast(
PayloadT,
self.runtime.decode_event_args(
name=self.name,
Expand Down

0 comments on commit bd931c8

Please sign in to comment.