Skip to content

Commit f60fdc8

Browse files
get_quotes datasource method (#132)
1 parent dd0474d commit f60fdc8

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/dipdup/context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def fire_handler(
7171
self,
7272
name: str,
7373
index: str,
74-
datasource: Datasource,
74+
datasource: TzktDatasource,
7575
fmt: Optional[str] = None,
7676
*args,
7777
**kwargs: Any,
@@ -208,7 +208,7 @@ def __init__(
208208
callbacks: 'CallbackManager',
209209
logger: FormattedLogger,
210210
handler_config: HandlerConfig,
211-
datasource: Datasource,
211+
datasource: TzktDatasource,
212212
) -> None:
213213
super().__init__(datasources, config, callbacks)
214214
self.logger = logger
@@ -246,7 +246,7 @@ async def fire_handler(
246246
ctx: 'DipDupContext',
247247
name: str,
248248
index: str,
249-
datasource: Datasource,
249+
datasource: TzktDatasource,
250250
fmt: Optional[str] = None,
251251
*args,
252252
**kwargs: Any,

src/dipdup/datasources/tzkt/datasource.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323
from dipdup.datasources.datasource import IndexDatasource
2424
from dipdup.datasources.tzkt.enums import TzktMessageType
25-
from dipdup.models import BigMapAction, BigMapData, BlockData, Head, HeadBlockData, OperationData
25+
from dipdup.models import BigMapAction, BigMapData, BlockData, Head, HeadBlockData, OperationData, QuoteData
2626
from dipdup.utils import groupby, split_by_chunks
2727

2828
OperationID = int
@@ -537,6 +537,17 @@ async def get_big_maps(
537537
big_maps.append(self.convert_big_map(bm))
538538
return big_maps
539539

540+
async def get_quotes(self, level: int) -> QuoteData:
541+
"""Get quote for block"""
542+
self._logger.info('Fetching quotes for level %s', level)
543+
quote_json = await self._http.request(
544+
'get',
545+
url='v1/quotes',
546+
params={"level": level},
547+
cache=True,
548+
)
549+
return self.convert_quote(quote_json[0])
550+
540551
async def add_index(self, index_config: ResolvedIndexConfigT) -> None:
541552
"""Register index config in internal mappings and matchers. Find and register subscriptions."""
542553

@@ -866,6 +877,21 @@ def convert_head_block(cls, head_block_json: Dict[str, Any]) -> HeadBlockData:
866877
quote_eth=Decimal(head_block_json['quoteEth']),
867878
)
868879

880+
@classmethod
881+
def convert_quote(cls, quote_json: Dict[str, Any]) -> QuoteData:
882+
"""Convert raw quote message from REST into dataclass"""
883+
return QuoteData(
884+
level=quote_json['level'],
885+
timestamp=cls._parse_timestamp(quote_json['timestamp']),
886+
btc=Decimal(quote_json['btc']),
887+
eur=Decimal(quote_json['eur']),
888+
usd=Decimal(quote_json['usd']),
889+
cny=Decimal(quote_json['cny']),
890+
jpy=Decimal(quote_json['jpy']),
891+
krw=Decimal(quote_json['krw']),
892+
eth=Decimal(quote_json['eth']),
893+
)
894+
869895
async def _send(self, method: str, arguments: List[Dict[str, Any]], on_invocation=None) -> None:
870896
client = self._get_client()
871897
while client.transport.state != ConnectionState.connected:

src/dipdup/models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,21 @@ class HeadBlockData:
249249
quote_eth: Decimal
250250

251251

252+
@dataclass
253+
class QuoteData:
254+
"""Basic structure for quotes from TzKT HTTP response"""
255+
256+
level: int
257+
timestamp: datetime
258+
btc: Decimal
259+
eur: Decimal
260+
usd: Decimal
261+
cny: Decimal
262+
jpy: Decimal
263+
krw: Decimal
264+
eth: Decimal
265+
266+
252267
class Schema(Model):
253268
name = fields.CharField(256, pk=True)
254269
hash = fields.CharField(256)

0 commit comments

Comments
 (0)