2018-04-06 20:11:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Fetch information about coins and tokens supported by Trezor and update it in coins_details.json."""
|
|
|
|
import json
|
2018-07-20 14:49:45 +00:00
|
|
|
import logging
|
2019-04-18 14:27:27 +00:00
|
|
|
import os
|
2018-07-30 13:05:19 +00:00
|
|
|
import sys
|
2019-04-18 14:27:27 +00:00
|
|
|
import time
|
2018-04-06 20:11:14 +00:00
|
|
|
|
2018-08-24 15:01:54 +00:00
|
|
|
import click
|
2019-04-18 14:27:27 +00:00
|
|
|
|
|
|
|
import coin_info
|
2020-08-24 16:15:19 +00:00
|
|
|
import marketcap
|
2018-08-24 15:01:54 +00:00
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
OPTIONAL_KEYS = ("links", "notes", "wallet")
|
2018-07-27 10:04:44 +00:00
|
|
|
ALLOWED_SUPPORT_STATUS = ("yes", "no", "planned", "soon")
|
2018-07-20 14:49:45 +00:00
|
|
|
|
2018-11-20 16:20:40 +00:00
|
|
|
WALLETS = coin_info.load_json("wallets.json")
|
2018-07-30 12:25:53 +00:00
|
|
|
OVERRIDES = coin_info.load_json("coins_details.override.json")
|
|
|
|
VERSIONS = coin_info.latest_releases()
|
2018-04-06 20:11:14 +00:00
|
|
|
|
2018-11-20 16:20:40 +00:00
|
|
|
# automatic wallet entries
|
2020-11-14 15:41:03 +00:00
|
|
|
WALLET_TREZOR = {"Trezor Wallet": "https://wallet.trezor.io"}
|
|
|
|
WALLET_ETH_TREZOR = {"Trezor Suite": "https://suite.trezor.io"}
|
2019-10-15 13:57:59 +00:00
|
|
|
WALLET_NEM = {
|
|
|
|
"Nano Wallet": "https://nem.io/downloads/",
|
|
|
|
"Magnum": "https://magnumwallet.co",
|
|
|
|
}
|
2018-11-20 16:20:40 +00:00
|
|
|
WALLETS_ETH_3RDPARTY = {
|
|
|
|
"MyEtherWallet": "https://www.myetherwallet.com",
|
|
|
|
"MyCrypto": "https://mycrypto.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-26 11:20:11 +00:00
|
|
|
TREZOR_KNOWN_URLS = (
|
2020-11-14 15:27:04 +00:00
|
|
|
"https://suite.trezor.io",
|
2018-11-20 16:20:40 +00:00
|
|
|
"https://wallet.trezor.io",
|
|
|
|
)
|
|
|
|
|
2018-08-24 15:01:54 +00:00
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
def update_marketcaps(coins):
|
2018-07-20 14:49:45 +00:00
|
|
|
for coin in coins.values():
|
2020-08-24 16:15:19 +00:00
|
|
|
coin["marketcap_usd"] = marketcap.marketcap(coin) or 0
|
2018-04-06 20:11:14 +00:00
|
|
|
|
2018-06-21 14:35:29 +00:00
|
|
|
|
2018-08-24 15:01:54 +00:00
|
|
|
def summary(coins, api_key):
|
2018-07-27 10:04:44 +00:00
|
|
|
t1_coins = 0
|
|
|
|
t2_coins = 0
|
|
|
|
supported_marketcap = 0
|
|
|
|
for coin in coins.values():
|
|
|
|
if coin.get("hidden"):
|
|
|
|
continue
|
2018-07-20 14:49:45 +00:00
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
t1_enabled = coin["t1_enabled"] == "yes"
|
|
|
|
t2_enabled = coin["t2_enabled"] == "yes"
|
|
|
|
if t1_enabled:
|
|
|
|
t1_coins += 1
|
|
|
|
if t2_enabled:
|
|
|
|
t2_coins += 1
|
|
|
|
if t1_enabled or t2_enabled:
|
|
|
|
supported_marketcap += coin.get("marketcap_usd", 0)
|
2018-05-16 15:30:28 +00:00
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
total_marketcap = None
|
2018-04-06 20:11:14 +00:00
|
|
|
try:
|
2020-08-24 16:15:19 +00:00
|
|
|
ret = marketcap.call("global-metrics/quotes/latest", api_key)
|
2018-08-24 15:01:54 +00:00
|
|
|
total_marketcap = int(ret["data"]["quote"]["USD"]["total_market_cap"])
|
2019-04-18 14:27:27 +00:00
|
|
|
except Exception:
|
2018-04-06 20:11:14 +00:00
|
|
|
pass
|
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
return dict(
|
|
|
|
updated_at=int(time.time()),
|
|
|
|
updated_at_readable=time.asctime(),
|
|
|
|
t1_coins=t1_coins,
|
|
|
|
t2_coins=t2_coins,
|
|
|
|
marketcap_usd=supported_marketcap,
|
|
|
|
total_marketcap_usd=total_marketcap,
|
2018-10-29 17:42:18 +00:00
|
|
|
marketcap_supported="{:.02f} %".format(
|
|
|
|
100 * supported_marketcap / total_marketcap
|
|
|
|
),
|
2018-07-27 10:04:44 +00:00
|
|
|
)
|
2018-04-11 16:37:30 +00:00
|
|
|
|
2018-06-21 14:35:29 +00:00
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
def _is_supported(support, trezor_version):
|
2018-08-24 15:01:54 +00:00
|
|
|
version = VERSIONS[trezor_version]
|
|
|
|
nominal = support.get(trezor_version)
|
2018-07-20 14:49:45 +00:00
|
|
|
if nominal is None:
|
|
|
|
return "no"
|
|
|
|
elif isinstance(nominal, bool):
|
|
|
|
return "yes" if nominal else "no"
|
2018-05-24 14:25:44 +00:00
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
try:
|
|
|
|
nominal_version = tuple(map(int, nominal.split(".")))
|
|
|
|
return "yes" if nominal_version <= version else "soon"
|
|
|
|
except ValueError:
|
|
|
|
return nominal
|
|
|
|
|
|
|
|
|
|
|
|
def _webwallet_support(coin, support):
|
|
|
|
"""Check the "webwallet" support property.
|
|
|
|
If set, check that at least one of the backends run on trezor.io.
|
|
|
|
If yes, assume we support the coin in our wallet.
|
|
|
|
Otherwise it's probably working with a custom backend, which means don't
|
|
|
|
link to our wallet.
|
|
|
|
"""
|
|
|
|
if not support.get("webwallet"):
|
|
|
|
return False
|
2020-11-12 14:15:16 +00:00
|
|
|
return any(".trezor.io" in url for url in coin["blockbook"])
|
2018-07-20 14:49:45 +00:00
|
|
|
|
|
|
|
|
2018-11-20 16:20:40 +00:00
|
|
|
def dict_merge(orig, new):
|
|
|
|
if isinstance(new, dict) and isinstance(orig, dict):
|
|
|
|
for k, v in new.items():
|
|
|
|
orig[k] = dict_merge(orig.get(k), v)
|
|
|
|
return orig
|
|
|
|
else:
|
|
|
|
return new
|
|
|
|
|
|
|
|
|
2018-08-23 15:50:38 +00:00
|
|
|
def update_simple(coins, support_info, type):
|
2018-07-20 14:49:45 +00:00
|
|
|
res = {}
|
|
|
|
for coin in coins:
|
|
|
|
key = coin["key"]
|
|
|
|
support = support_info[key]
|
2018-08-23 15:50:38 +00:00
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
details = dict(
|
2018-08-23 15:50:38 +00:00
|
|
|
name=coin["name"],
|
2018-07-20 14:49:45 +00:00
|
|
|
shortcut=coin["shortcut"],
|
2018-08-23 15:50:38 +00:00
|
|
|
type=type,
|
2018-08-24 13:20:25 +00:00
|
|
|
t1_enabled=_is_supported(support, "trezor1"),
|
|
|
|
t2_enabled=_is_supported(support, "trezor2"),
|
2018-11-20 16:20:40 +00:00
|
|
|
wallet={},
|
2018-08-23 15:50:38 +00:00
|
|
|
)
|
|
|
|
for k in OPTIONAL_KEYS:
|
|
|
|
if k in coin:
|
|
|
|
details[k] = coin[k]
|
|
|
|
|
2018-11-20 16:20:40 +00:00
|
|
|
details["wallet"].update(WALLETS.get(key, {}))
|
|
|
|
|
2018-08-23 15:50:38 +00:00
|
|
|
res[key] = details
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2018-08-24 13:42:06 +00:00
|
|
|
def update_bitcoin(coins, support_info):
|
2018-08-23 15:50:38 +00:00
|
|
|
res = update_simple(coins, support_info, "coin")
|
|
|
|
for coin in coins:
|
|
|
|
key = coin["key"]
|
|
|
|
support = support_info[key]
|
|
|
|
details = dict(
|
|
|
|
name=coin["coin_label"],
|
|
|
|
links=dict(Homepage=coin["website"], Github=coin["github"]),
|
2018-11-20 16:20:40 +00:00
|
|
|
wallet=WALLET_TREZOR if _webwallet_support(coin, support) else {},
|
2018-07-20 14:49:45 +00:00
|
|
|
)
|
2018-11-20 16:20:40 +00:00
|
|
|
dict_merge(res[key], details)
|
2018-07-20 14:49:45 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2018-11-20 17:00:38 +00:00
|
|
|
def update_erc20(coins, networks, support_info):
|
2018-07-20 14:49:45 +00:00
|
|
|
# TODO skip disabled networks?
|
2018-11-20 17:00:38 +00:00
|
|
|
network_support = {n["chain"]: support_info.get(n["key"]) for n in networks}
|
2018-11-29 15:39:31 +00:00
|
|
|
network_testnets = {n["chain"] for n in networks if "Testnet" in n["name"]}
|
2018-08-23 15:50:38 +00:00
|
|
|
res = update_simple(coins, support_info, "erc20")
|
2018-07-20 14:49:45 +00:00
|
|
|
for coin in coins:
|
|
|
|
key = coin["key"]
|
2018-11-20 16:20:40 +00:00
|
|
|
chain = coin["chain"]
|
2018-11-29 15:39:31 +00:00
|
|
|
|
|
|
|
hidden = False
|
|
|
|
if chain in network_testnets:
|
|
|
|
hidden = True
|
|
|
|
if "deprecation" in coin:
|
|
|
|
hidden = True
|
|
|
|
|
2018-11-20 17:00:38 +00:00
|
|
|
if network_support.get(chain, {}).get("webwallet"):
|
2019-10-15 13:57:59 +00:00
|
|
|
wallets = WALLET_ETH_TREZOR
|
2018-11-20 16:20:40 +00:00
|
|
|
else:
|
|
|
|
wallets = WALLETS_ETH_3RDPARTY
|
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
details = dict(
|
2018-11-20 16:20:40 +00:00
|
|
|
network=chain,
|
2018-07-20 14:49:45 +00:00
|
|
|
address=coin["address"],
|
|
|
|
shortcut=coin["shortcut"],
|
|
|
|
links={},
|
2018-11-20 16:20:40 +00:00
|
|
|
wallet=wallets,
|
2018-07-20 14:49:45 +00:00
|
|
|
)
|
2018-11-29 15:39:31 +00:00
|
|
|
if hidden:
|
|
|
|
details["hidden"] = True
|
2018-07-20 14:49:45 +00:00
|
|
|
if coin.get("website"):
|
|
|
|
details["links"]["Homepage"] = coin["website"]
|
|
|
|
if coin.get("social", {}).get("github"):
|
|
|
|
details["links"]["Github"] = coin["social"]["github"]
|
|
|
|
|
2018-11-20 16:20:40 +00:00
|
|
|
dict_merge(res[key], details)
|
2018-04-11 16:37:30 +00:00
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
return res
|
2018-04-06 21:10:35 +00:00
|
|
|
|
2018-05-24 14:25:44 +00:00
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
def update_ethereum_networks(coins, support_info):
|
|
|
|
res = update_simple(coins, support_info, "coin")
|
|
|
|
for coin in coins:
|
2018-08-23 15:50:38 +00:00
|
|
|
key = coin["key"]
|
2018-11-20 17:00:38 +00:00
|
|
|
if support_info[key].get("webwallet"):
|
2019-10-15 13:57:59 +00:00
|
|
|
wallets = WALLET_ETH_TREZOR
|
2018-11-20 16:20:40 +00:00
|
|
|
else:
|
|
|
|
wallets = WALLETS_ETH_3RDPARTY
|
|
|
|
details = dict(links=dict(Homepage=coin.get("url")), wallet=wallets)
|
|
|
|
dict_merge(res[key], details)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
def update_nem_mosaics(coins, support_info):
|
|
|
|
res = update_simple(coins, support_info, "mosaic")
|
|
|
|
for coin in coins:
|
|
|
|
key = coin["key"]
|
|
|
|
details = dict(wallet=WALLET_NEM)
|
|
|
|
dict_merge(res[key], details)
|
2018-08-23 15:50:38 +00:00
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
return res
|
2018-04-11 16:37:30 +00:00
|
|
|
|
2018-06-21 14:35:29 +00:00
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
def check_missing_data(coins):
|
|
|
|
for k, coin in coins.items():
|
2018-05-25 09:29:37 +00:00
|
|
|
hide = False
|
2018-05-24 14:25:44 +00:00
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
if "Homepage" not in coin.get("links", {}):
|
2018-08-28 13:01:48 +00:00
|
|
|
level = logging.WARNING
|
|
|
|
if k.startswith("erc20:"):
|
|
|
|
level = logging.INFO
|
|
|
|
LOG.log(level, f"{k}: Missing homepage")
|
2018-05-25 09:29:37 +00:00
|
|
|
hide = True
|
2018-07-27 10:04:44 +00:00
|
|
|
if coin["t1_enabled"] not in ALLOWED_SUPPORT_STATUS:
|
2018-08-28 13:01:48 +00:00
|
|
|
LOG.warning(f"{k}: Unknown t1_enabled")
|
2018-05-25 09:29:37 +00:00
|
|
|
hide = True
|
2018-07-27 10:04:44 +00:00
|
|
|
if coin["t2_enabled"] not in ALLOWED_SUPPORT_STATUS:
|
2018-08-28 13:01:48 +00:00
|
|
|
LOG.warning(f"{k}: Unknown t2_enabled")
|
2018-05-25 09:29:37 +00:00
|
|
|
hide = True
|
2018-11-20 16:20:40 +00:00
|
|
|
|
|
|
|
# check wallets
|
|
|
|
for wallet in coin["wallet"]:
|
|
|
|
name = wallet.get("name")
|
|
|
|
url = wallet.get("url")
|
|
|
|
if not name or not url:
|
|
|
|
LOG.warning(f"{k}: Bad wallet entry")
|
|
|
|
hide = True
|
|
|
|
continue
|
2019-07-26 11:20:11 +00:00
|
|
|
if "trezor" in name.lower() and url not in TREZOR_KNOWN_URLS:
|
2018-11-20 16:20:40 +00:00
|
|
|
LOG.warning(f"{k}: Strange URL for Trezor Wallet")
|
2018-08-28 13:01:48 +00:00
|
|
|
|
|
|
|
if coin["t1_enabled"] == "no" and coin["t2_enabled"] == "no":
|
|
|
|
LOG.info(f"{k}: Coin not enabled on either device")
|
2018-05-25 09:29:37 +00:00
|
|
|
hide = True
|
2018-04-06 20:11:14 +00:00
|
|
|
|
2018-11-20 16:20:40 +00:00
|
|
|
if len(coin.get("wallet", [])) == 0:
|
2018-08-28 13:01:48 +00:00
|
|
|
LOG.debug(f"{k}: Missing wallet")
|
2018-05-25 09:29:37 +00:00
|
|
|
|
2019-05-15 10:13:53 +00:00
|
|
|
if "Testnet" in coin["name"] or "Regtest" in coin["name"]:
|
2018-08-28 13:01:48 +00:00
|
|
|
LOG.debug(f"{k}: Hiding testnet")
|
2018-07-20 14:49:45 +00:00
|
|
|
hide = True
|
|
|
|
|
2018-08-28 13:01:48 +00:00
|
|
|
if not hide and coin.get("hidden"):
|
|
|
|
LOG.info(f"{k}: Details are OK, but coin is still hidden")
|
2018-06-21 13:14:34 +00:00
|
|
|
|
2018-08-28 13:01:48 +00:00
|
|
|
if hide:
|
2018-07-20 14:49:45 +00:00
|
|
|
coin["hidden"] = 1
|
2018-05-25 09:29:37 +00:00
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
# summary of hidden coins
|
2018-10-29 17:42:18 +00:00
|
|
|
hidden_coins = [k for k, coin in coins.items() if coin.get("hidden")]
|
|
|
|
for key in hidden_coins:
|
|
|
|
del coins[key]
|
2018-11-29 15:39:31 +00:00
|
|
|
LOG.debug(f"{key}: Coin is hidden")
|
2018-05-24 14:25:44 +00:00
|
|
|
|
2018-06-21 14:35:29 +00:00
|
|
|
|
2018-07-20 14:49:45 +00:00
|
|
|
def apply_overrides(coins):
|
|
|
|
for key, override in OVERRIDES.items():
|
|
|
|
if key not in coins:
|
2018-08-24 15:01:54 +00:00
|
|
|
LOG.warning(f"override without coin: {key}")
|
2018-07-20 14:49:45 +00:00
|
|
|
continue
|
|
|
|
|
2018-11-20 16:20:40 +00:00
|
|
|
dict_merge(coins[key], override)
|
2018-07-20 14:49:45 +00:00
|
|
|
|
2018-11-20 16:20:40 +00:00
|
|
|
|
|
|
|
def finalize_wallets(coins):
|
|
|
|
def sort_key(w):
|
|
|
|
if "trezor.io" in w["url"]:
|
|
|
|
return 0, w["name"]
|
|
|
|
else:
|
|
|
|
return 1, w["name"]
|
|
|
|
|
|
|
|
for coin in coins.values():
|
|
|
|
wallets_list = [
|
2019-12-13 14:39:28 +00:00
|
|
|
dict(name=name, url=url) for name, url in coin["wallet"].items() if url
|
2018-11-20 16:20:40 +00:00
|
|
|
]
|
|
|
|
wallets_list.sort(key=sort_key)
|
|
|
|
coin["wallet"] = wallets_list
|
2018-07-20 14:49:45 +00:00
|
|
|
|
|
|
|
|
2018-08-24 15:01:54 +00:00
|
|
|
@click.command()
|
|
|
|
# fmt: off
|
2018-08-30 16:38:57 +00:00
|
|
|
@click.option("-r", "--refresh", "refresh", flag_value=True, default=None, help="Force refresh market cap info")
|
|
|
|
@click.option("-R", "--no-refresh", "refresh", flag_value=False, default=None, help="Force use cached market cap info")
|
2018-08-24 15:01:54 +00:00
|
|
|
@click.option("-A", "--api-key", required=True, envvar="COINMARKETCAP_API_KEY", help="Coinmarketcap API key")
|
2018-08-28 13:01:48 +00:00
|
|
|
@click.option("-v", "--verbose", is_flag=True, help="Display more info")
|
2018-08-24 15:01:54 +00:00
|
|
|
# fmt: on
|
2018-08-28 13:01:48 +00:00
|
|
|
def main(refresh, api_key, verbose):
|
2018-07-30 13:05:19 +00:00
|
|
|
# setup logging
|
2018-08-28 13:01:48 +00:00
|
|
|
log_level = logging.DEBUG if verbose else logging.WARNING
|
2018-07-30 13:05:19 +00:00
|
|
|
root = logging.getLogger()
|
2018-08-28 13:01:48 +00:00
|
|
|
root.setLevel(log_level)
|
2018-07-30 13:05:19 +00:00
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
2018-08-28 13:01:48 +00:00
|
|
|
handler.setLevel(log_level)
|
2018-07-30 13:05:19 +00:00
|
|
|
root.addHandler(handler)
|
|
|
|
|
2020-08-24 16:15:19 +00:00
|
|
|
marketcap.init(api_key, refresh=refresh)
|
2018-08-24 15:01:54 +00:00
|
|
|
|
2018-08-24 13:20:25 +00:00
|
|
|
defs = coin_info.coin_info()
|
2018-08-23 15:50:38 +00:00
|
|
|
support_info = coin_info.support_info(defs)
|
2018-07-20 14:49:45 +00:00
|
|
|
|
|
|
|
coins = {}
|
2018-08-24 13:42:06 +00:00
|
|
|
coins.update(update_bitcoin(defs.bitcoin, support_info))
|
2018-11-20 17:00:38 +00:00
|
|
|
coins.update(update_erc20(defs.erc20, defs.eth, support_info))
|
2018-07-30 12:25:53 +00:00
|
|
|
coins.update(update_ethereum_networks(defs.eth, support_info))
|
2018-11-20 16:20:40 +00:00
|
|
|
coins.update(update_nem_mosaics(defs.nem, support_info))
|
2018-07-30 12:25:53 +00:00
|
|
|
coins.update(update_simple(defs.misc, support_info, "coin"))
|
2018-07-20 14:49:45 +00:00
|
|
|
|
|
|
|
apply_overrides(coins)
|
2018-11-20 16:20:40 +00:00
|
|
|
finalize_wallets(coins)
|
2018-07-27 10:04:44 +00:00
|
|
|
update_marketcaps(coins)
|
2018-11-20 16:20:40 +00:00
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
check_missing_data(coins)
|
2018-04-06 20:11:14 +00:00
|
|
|
|
2018-08-24 15:01:54 +00:00
|
|
|
info = summary(coins, api_key)
|
2018-07-27 10:04:44 +00:00
|
|
|
details = dict(coins=coins, info=info)
|
2018-07-20 14:49:45 +00:00
|
|
|
|
2018-07-27 10:04:44 +00:00
|
|
|
print(json.dumps(info, sort_keys=True, indent=4))
|
2018-07-30 12:25:53 +00:00
|
|
|
with open(os.path.join(coin_info.DEFS_DIR, "coins_details.json"), "w") as f:
|
2018-07-27 10:04:44 +00:00
|
|
|
json.dump(details, f, sort_keys=True, indent=4)
|
2019-08-31 21:43:17 +00:00
|
|
|
f.write("\n")
|
2018-08-24 15:01:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|