Skip to content

Commit 7309e56

Browse files
Update staging API format, fix parsing aliased Pydantic models (#25)
1 parent 0f118ba commit 7309e56

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

src/dipdup/datasources/tzkt/datasource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"hasInternals",
4646
# "quote",
4747
"parameters",
48-
"bigmaps,",
48+
"diffs,",
4949
)
5050

5151

@@ -354,7 +354,7 @@ def convert_operation(cls, operation_json: Dict[str, Any]) -> OperationData:
354354
initiator_address=operation_json['initiator']['address'] if operation_json.get('initiator') else None,
355355
parameter=operation_json.get('parameters'),
356356
storage=storage,
357-
bigmaps=operation_json.get('bigmaps'),
357+
diffs=operation_json.get('diffs'),
358358
)
359359

360360
async def get_latest_block(self) -> Dict[str, Any]:

src/dipdup/models.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ class OperationData:
6262
initiator_address: Optional[str] = None
6363
parameter: Optional[str] = None
6464
storage: Optional[Dict[str, Any]] = None
65-
bigmaps: Optional[List[Dict[str, Any]]] = None
65+
diffs: Optional[List[Dict[str, Any]]] = None
6666

6767
def _merge_bigmapdiffs(self, storage_dict: Dict[str, Any], bigmap_name: str, array: bool) -> None:
68-
if self.bigmaps is None:
68+
if self.diffs is None:
6969
raise Exception('`bigmaps` field missing')
70-
bigmapdiffs = [bm for bm in self.bigmaps if bm['path'] == bigmap_name]
70+
bigmapdiffs = [bm for bm in self.diffs if bm['path'] == bigmap_name]
7171
for diff in bigmapdiffs:
7272
_logger.debug('Applying bigmapdiff: %s', diff)
7373
if diff['action'] in ('add_key', 'update_key'):
74-
key = diff['key']['key']
74+
key = diff['content']['key']
7575
if array is True:
76-
storage_dict[bigmap_name].append({'key': key, 'value': diff['key']['value']})
76+
storage_dict[bigmap_name].append({'key': key, 'value': diff['content']['value']})
7777
else:
78-
storage_dict[bigmap_name][key] = diff['key']['value']
78+
storage_dict[bigmap_name][key] = diff['content']['value']
7979

8080
def get_merged_storage(self, storage_type: Type[StorageType]) -> StorageType:
8181
if self.storage is None:
@@ -85,16 +85,20 @@ def get_merged_storage(self, storage_type: Type[StorageType]) -> StorageType:
8585
_logger.debug('Merging storage')
8686
_logger.debug('Before: %s', storage)
8787
for key, field in storage_type.__fields__.items():
88+
89+
if field.alias:
90+
key = field.alias
91+
8892
# NOTE: TzKT could return bigmaps as object or as array of key-value objects. We need to guess this from storage.
8993
# TODO: This code should be a part of datasource module.
9094
if field.type_ not in (int, bool) and isinstance(storage[key], int):
9195
if hasattr(field.type_, '__fields__') and 'key' in field.type_.__fields__ and 'value' in field.type_.__fields__:
9296
storage[key] = []
93-
if self.bigmaps:
97+
if self.diffs:
9498
self._merge_bigmapdiffs(storage, key, array=True)
9599
else:
96100
storage[key] = {}
97-
if self.bigmaps:
101+
if self.diffs:
98102
self._merge_bigmapdiffs(storage, key, array=False)
99103

100104
_logger.debug('After: %s', storage)

src/dipdup/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import logging
23
import os
34
import re
45
import sys
@@ -12,6 +13,8 @@
1213

1314
from dipdup import __version__
1415

16+
_logger = logging.getLogger(__name__)
17+
1518

1619
def snake_to_camel(value: str) -> str:
1720
return ''.join(map(lambda x: x[0].upper() + x[1:], value.split('_')))
@@ -55,6 +58,8 @@ async def http_request(method: str, **kwargs):
5558
headers=headers,
5659
**kwargs,
5760
) as response:
61+
request_string = kwargs['url'] + '?' + '&'.join([f'{key}={value}' for key, value in kwargs.get('params', {}).items()])
62+
_logger.debug('Calling `%s`', request_string)
5863
yield response
5964

6065

tests/test_dipdup/test_datasources/test_tzkt/operations-storage.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@
7979
"errors": null,
8080
"hasInternals": false,
8181
"parameters": "{\"entrypoint\":\"propose\",\"value\":{\"prim\":\"Pair\",\"args\":[{\"int\":\"10\"},{\"prim\":\"Left\",\"args\":[{\"prim\":\"Pair\",\"args\":[{\"int\":\"0\"},[{\"prim\":\"Pair\",\"args\":[{\"bytes\":\"\"},{\"prim\":\"Some\",\"args\":[{\"bytes\":\"6672\"}]}]}]]}]}]}}",
82-
"bigmaps": [
82+
"diffs": [
8383
{
8484
"bigmap": 36371,
8585
"path": "proposals",
8686
"action": "add_key",
87-
"key": {
87+
"content": {
8888
"hash": "exprtrENeWqsFnt4E4mFvnonvhrhU3jKTuaFXiufCK45XwdgnP7D6p",
8989
"key": "e710c1a066bbbf73692168e783607996785260cec4d60930579827298493b8b9",
9090
"value": {
@@ -112,7 +112,7 @@
112112
"bigmap": 36368,
113113
"path": "ledger",
114114
"action": "update_key",
115-
"key": {
115+
"content": {
116116
"hash": "expruVX2dr7XzPeZwyNsKwVcf88r2FfUrTNHewFZWkDtfPyoHkzrdf",
117117
"key": {
118118
"nat": "0",
@@ -125,7 +125,7 @@
125125
"bigmap": 36368,
126126
"path": "ledger",
127127
"action": "update_key",
128-
"key": {
128+
"content": {
129129
"hash": "exprtjvuiPChDAAipwbGtmwZDA8JcEQ6y4pu2qTy93fpPJFtDkZFap",
130130
"key": {
131131
"nat": "1",

0 commit comments

Comments
 (0)