Skip to content

Commit 5876965

Browse files
committed
refactor!: improve coingecko mapping format
1 parent 6839050 commit 5876965

File tree

3 files changed

+578
-77
lines changed

3 files changed

+578
-77
lines changed

pyth_observer/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from pyth_observer.check import State
2121
from pyth_observer.check.price_feed import PriceFeedState
2222
from pyth_observer.check.publisher import PublisherState
23-
from pyth_observer.coingecko import Symbol, get_coingecko_prices
23+
from pyth_observer.coingecko import get_coingecko_prices
2424
from pyth_observer.dispatch import Dispatch
2525
from pyth_observer.metrics import metrics
2626
from pyth_observer.models import Publisher
@@ -54,7 +54,7 @@ def __init__(
5454
self,
5555
config: Dict[str, Any],
5656
publishers: Dict[str, Publisher],
57-
coingecko_mapping: Dict[str, Symbol],
57+
coingecko_mapping: Dict[str, str],
5858
) -> None:
5959
self.config = config
6060
self.dispatch = Dispatch(config, publishers)
@@ -95,7 +95,7 @@ async def run(self) -> None:
9595

9696
for product in products:
9797
# Skip tombstone accounts with blank metadata
98-
if "base" not in product.attrs:
98+
if "symbol" not in product.attrs:
9999
continue
100100

101101
if not product.first_price_account_key:
@@ -139,9 +139,11 @@ async def run(self) -> None:
139139
latest_trading_slot=price_account.last_slot,
140140
price_aggregate=price_account.aggregate_price_info.price,
141141
confidence_interval_aggregate=price_account.aggregate_price_info.confidence_interval,
142-
coingecko_price=coingecko_prices.get(product.attrs["base"]),
142+
coingecko_price=coingecko_prices.get(
143+
product.attrs["symbol"]
144+
),
143145
coingecko_update=coingecko_updates.get(
144-
product.attrs["base"]
146+
product.attrs["symbol"]
145147
),
146148
)
147149

pyth_observer/coingecko.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
from typing import Any, Dict, TypedDict
1+
from typing import Any, Dict
22

33
from loguru import logger
44
from pycoingecko import CoinGeckoAPI
55
from requests.exceptions import HTTPError
66
from throttler import throttle
77

88

9-
class Symbol(TypedDict):
10-
api: str
11-
market: str
12-
13-
149
# CoinGecko free API limit: 10-50 (varies) https://www.coingecko.com/en/api/pricing
1510
# However prices are updated every 1-10 minutes: https://www.coingecko.com/en/faq
1611
# Hence we only have to query once every minute.
17-
@throttle(rate_limit=1, period=60)
12+
@throttle(rate_limit=1, period=10)
1813
async def get_coingecko_prices(
19-
mapping: Dict[str, Symbol],
14+
symbol_to_ticker: Dict[str, str],
2015
) -> Dict[str, Dict[str, Any]]:
21-
inverted_mapping = {mapping[x]["api"]: x for x in mapping}
22-
ids = [mapping[x]["api"] for x in mapping]
16+
ticker_to_symbol = {v: k for k, v in symbol_to_ticker.items()}
17+
ids = list(ticker_to_symbol.keys())
2318

2419
try:
2520
prices = CoinGeckoAPI().get_price(
@@ -32,6 +27,4 @@ async def get_coingecko_prices(
3227
)
3328
prices = {}
3429

35-
# remap to symbol -> prices
36-
prices_mapping = {inverted_mapping[x]: prices[x] for x in prices}
37-
return prices_mapping
30+
return {ticker_to_symbol[x]: prices[x] for x in prices}

0 commit comments

Comments
 (0)