mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-06-27 02:12:35 +00:00
coins_details: fetch CMC metadata, match tokens by contract address
This commit is contained in:
parent
8f5026e71e
commit
1ecd56f33b
@ -73,6 +73,19 @@ def coinmarketcap_init(api_key, refresh=None):
|
|||||||
api_key,
|
api_key,
|
||||||
params={"limit": 5000, "convert": "USD"},
|
params={"limit": 5000, "convert": "USD"},
|
||||||
)
|
)
|
||||||
|
by_id = {str(coin["id"]): coin for coin in coinmarketcap_data["data"]}
|
||||||
|
all_ids = list(by_id.keys())
|
||||||
|
while all_ids:
|
||||||
|
first_100 = all_ids[:100]
|
||||||
|
all_ids = all_ids[100:]
|
||||||
|
time.sleep(1)
|
||||||
|
print("Fetching metadata, {} coins remaining...".format(len(all_ids)))
|
||||||
|
metadata = coinmarketcap_call(
|
||||||
|
"cryptocurrency/info", api_key, params={"id": ",".join(first_100)}
|
||||||
|
)
|
||||||
|
for coin_id, meta in metadata["data"].items():
|
||||||
|
by_id[coin_id]["meta"] = meta
|
||||||
|
|
||||||
with open(COINMAKETCAP_CACHE, "w") as f:
|
with open(COINMAKETCAP_CACHE, "w") as f:
|
||||||
json.dump(coinmarketcap_data, f)
|
json.dump(coinmarketcap_data, f)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -81,9 +94,13 @@ def coinmarketcap_init(api_key, refresh=None):
|
|||||||
coin_data = {}
|
coin_data = {}
|
||||||
for coin in coinmarketcap_data["data"]:
|
for coin in coinmarketcap_data["data"]:
|
||||||
slug = coin["slug"]
|
slug = coin["slug"]
|
||||||
|
platform = coin["meta"]["platform"]
|
||||||
market_cap = coin["quote"]["USD"]["market_cap"]
|
market_cap = coin["quote"]["USD"]["market_cap"]
|
||||||
if market_cap is not None:
|
if market_cap is not None:
|
||||||
coin_data[slug] = int(market_cap)
|
coin_data[slug] = int(market_cap)
|
||||||
|
if platform is not None and platform["name"] == "Ethereum":
|
||||||
|
address = platform["token_address"].lower()
|
||||||
|
coin_data[address] = market_cap
|
||||||
|
|
||||||
MARKET_CAPS = coin_data
|
MARKET_CAPS = coin_data
|
||||||
|
|
||||||
@ -92,6 +109,10 @@ def coinmarketcap_init(api_key, refresh=None):
|
|||||||
|
|
||||||
def marketcap(coin):
|
def marketcap(coin):
|
||||||
cap = None
|
cap = None
|
||||||
|
if coin["type"] == "erc20":
|
||||||
|
address = coin["address"].lower()
|
||||||
|
return MARKET_CAPS.get(address)
|
||||||
|
|
||||||
if "coinmarketcap_alias" in coin:
|
if "coinmarketcap_alias" in coin:
|
||||||
cap = MARKET_CAPS.get(coin["coinmarketcap_alias"])
|
cap = MARKET_CAPS.get(coin["coinmarketcap_alias"])
|
||||||
if cap is None:
|
if cap is None:
|
||||||
|
Loading…
Reference in New Issue
Block a user