Skip to content

Commit 1eeb1ff

Browse files
Wizard1209Wizard1209
andauthored
Backport: Fixed parsing TzKT contract event data (#835)
Co-authored-by: Wizard1209 <[email protected]>
1 parent ce1a834 commit 1eeb1ff

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

demos/demo-events/dipdup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ datasources:
1212

1313
contracts:
1414
events_contract:
15-
code_hash: KT1Up6AMehze2VTdt3w85xaZPtrEWn1AeyR3
15+
address: KT1Up6AMehze2VTdt3w85xaZPtrEWn1AeyR3
1616

1717
indexes:
1818
events:

src/dipdup/datasources/tzkt/datasource.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from asyncio import Event
66
from collections import defaultdict
77
from collections import deque
8-
from dataclasses import fields
98
from datetime import datetime
109
from datetime import timezone
1110
from decimal import Decimal
@@ -112,6 +111,16 @@
112111
'originationId',
113112
'migrationId',
114113
)
114+
EVENT_FIELDS = (
115+
'id',
116+
'level',
117+
'timestamp',
118+
'tag',
119+
'payload',
120+
'contract',
121+
'codeHash',
122+
'transactionId',
123+
)
115124

116125

117126
class TzktMessageType(Enum):
@@ -809,21 +818,26 @@ async def get_events(
809818
offset: int | None = None,
810819
limit: int | None = None,
811820
) -> tuple[EventData, ...]:
821+
params = self._get_request_params(
822+
first_level,
823+
last_level,
824+
offset=offset or 0,
825+
limit=limit,
826+
select=EVENT_FIELDS,
827+
values=True,
828+
cursor=True,
829+
**{
830+
'contract.in': ','.join(addresses),
831+
'tag.in': ','.join(tags),
832+
},
833+
)
812834
offset, limit = offset or 0, limit or self.request_limit
813835
raw_events = await self._request_values_dict(
814836
'get',
815837
url='v1/contracts/events',
816-
params={
817-
'contract.in': ','.join(addresses),
818-
'tag.in': ','.join(tags),
819-
'level.ge': first_level,
820-
'level.le': last_level,
821-
'select.values': ','.join(f.name for f in fields(EventData)),
822-
'offset.cr': offset,
823-
'limit': limit,
824-
},
838+
params=params,
825839
)
826-
return tuple(self.convert_event(e) for e in raw_events)
840+
return tuple(EventData.from_json(e) for e in raw_events)
827841

828842
async def iter_events(
829843
self,

src/dipdup/models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from datetime import date
77
from datetime import datetime
88
from datetime import time
9+
from datetime import timezone
910
from decimal import Decimal
1011
from enum import Enum
1112
from functools import cache
@@ -54,6 +55,10 @@
5455
# ===> Dataclasses
5556

5657

58+
def _parse_timestamp(timestamp: str) -> datetime:
59+
return datetime.fromisoformat(timestamp[:-1]).replace(tzinfo=timezone.utc)
60+
61+
5762
@dataclass
5863
class OperationData:
5964
"""Basic structure for operations from TzKT response"""
@@ -251,6 +256,21 @@ class EventData:
251256
contract_code_hash: Optional[int] = None
252257
transaction_id: Optional[int] = None
253258

259+
@classmethod
260+
def from_json(cls, event_json: dict[str, Any]) -> 'EventData':
261+
"""Convert raw event message from WS/REST into dataclass"""
262+
return EventData(
263+
id=event_json['id'],
264+
level=event_json['level'],
265+
timestamp=_parse_timestamp(event_json['timestamp']),
266+
tag=event_json['tag'],
267+
payload=event_json.get('payload'),
268+
contract_address=event_json['contract']['address'],
269+
contract_alias=event_json['contract'].get('alias'),
270+
contract_code_hash=event_json['codeHash'],
271+
transaction_id=event_json.get('transactionId'),
272+
)
273+
254274

255275
@dataclass
256276
class Event(Generic[EventType]):

src/dipdup/projects/demo_events/dipdup.yml.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ datasources:
1212

1313
contracts:
1414
events_contract:
15-
code_hash: KT1Up6AMehze2VTdt3w85xaZPtrEWn1AeyR3
15+
address: KT1Up6AMehze2VTdt3w85xaZPtrEWn1AeyR3
1616

1717
indexes:
1818
events:

0 commit comments

Comments
 (0)