This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
forked from trezor/trezor-common
-
Notifications
You must be signed in to change notification settings - Fork 1
/
coins_details.py
executable file
·322 lines (265 loc) · 10.7 KB
/
coins_details.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python3
"""Fetch information about coins and tokens supported by Trezor and update it in coins_details.json."""
import time
import json
import requests
import pprint
import ethereum_tokens_gen
COINS = {}
def coinmarketcap_init():
global COINS
try:
COINS = json.load(open('coinmarketcap.json', 'r'))
except FileNotFoundError:
pass
else:
if COINS["1"]["last_updated"] > time.time() - 3600:
print("Using local cache of coinmarketcap")
return
print("Updating coins from coinmarketcap")
total = None
COINS = {}
while total is None or len(COINS) < total:
url = 'https://api.coinmarketcap.com/v2/ticker/?start=%d&convert=USD&limit=100' % (len(COINS)+1)
data = requests.get(url).json()
COINS.update(data['data'])
if total is None:
total = data['metadata']['num_cryptocurrencies']
print("Fetched %d of %d coins" % (len(COINS), total))
time.sleep(1)
json.dump(COINS, open('coinmarketcap.json', 'w'), sort_keys=True, indent=4)
def coinmarketcap_info(shortcut):
global COINS
shortcut = shortcut.replace(' ', '-').lower()
for _id in COINS:
coin = COINS[_id]
#print(shortcut, coin['website_slug'])
if shortcut == coin['website_slug']:
#print(coin)
return coin
def update_marketcap(obj, shortcut):
try:
obj['marketcap_usd'] = int(float(coinmarketcap_info(shortcut)['quotes']['USD']['market_cap']))
except:
pass
# print("Marketcap info not found for", shortcut)
def coinmarketcap_global():
url = 'https://api.coinmarketcap.com/v2/global'
ret = requests.get(url)
data = ret.json()
return data
def set_default(obj, key, default_value):
obj[key] = obj.setdefault(key, default_value)
def update_info(details):
details['info']['updated_at'] = int(time.time())
details['info']['updated_at_readable'] = time.asctime()
details['info']['t1_coins'] = len([True for _, c in details['coins'].items() if c['t1_enabled'] == 'yes' and not c.get('hidden', False)])
details['info']['t2_coins'] = len([True for _, c in details['coins'].items() if c['t2_enabled'] == 'yes' and not c.get('hidden', False)])
try:
details['info']['total_marketcap_usd'] = int(coinmarketcap_global()['data']['quotes']['USD']['total_market_cap'])
except:
pass
marketcap = 0
for k, c in details['coins'].items():
if c['t1_enabled'] == 'yes' or c['t2_enabled'] == 'yes':
marketcap += details['coins'][k].setdefault('marketcap_usd', 0)
details['info']['marketcap_usd'] = marketcap
def check_unsupported(details, prefix, supported):
for k in details['coins'].keys():
if not k.startswith(prefix):
continue
if k not in supported:
print("%s not supported by Trezor? (Possible manual entry)" % k)
def update_coins(details):
coins = json.load(open('coins.json', 'r'))
supported = []
for coin in coins:
if coin['firmware'] != 'stable':
continue
# print("Updating", coin['coin_label'], coin['coin_shortcut'])
key = "coin:%s" % coin['coin_shortcut']
supported.append(key)
out = details['coins'].setdefault(key, {})
out['type'] = 'coin'
set_default(out, 'shortcut', coin['coin_shortcut'])
set_default(out, 'name', coin['coin_label'])
set_default(out, 'links', {})
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, coin.get('coinmarketcap_alias', coin['coin_label']))
check_unsupported(details, 'coin:', supported)
def update_erc20(details):
networks = ['eth',
'exp',
# 'rop',
# 'rin',
'ubq',
# 'rsk',
# 'kov',
'etc',
]
LATEST_T1 = 'https://raw.githubusercontent.com/trezor/trezor-mcu/v1.6.1/firmware/ethereum_tokens.c'
LATEST_T2 = 'https://raw.githubusercontent.com/trezor/trezor-core/v2.0.6/src/apps/ethereum/tokens.py'
tokens = ethereum_tokens_gen.get_tokens()
tokens_t1 = requests.get(LATEST_T1).text
tokens_t2 = requests.get(LATEST_T2).text
supported = []
for t in tokens:
# print('Updating', t['symbol'])
if t['chain'] not in networks:
print('Skipping, %s is disabled' % t['chain'])
continue
key = "erc20:%s:%s" % (t['chain'], t['symbol'])
supported.append(key)
out = details['coins'].setdefault(key, {})
out['type'] = 'erc20'
out['network'] = t['chain']
out['address'] = t['address']
set_default(out, 'shortcut', t['symbol'])
set_default(out, 'name', t['name'])
set_default(out, 'links', {})
if "\" %s\"" % t['symbol'] in tokens_t1:
out['t1_enabled'] = 'yes'
else:
out['t1_enabled'] = 'soon'
if "'%s'" % t['symbol'] in tokens_t2:
out['t2_enabled'] = 'yes'
else:
out['t2_enabled'] = 'soon'
out['links']['MyCrypto Wallet'] = 'https://mycrypto.com'
out['links']['MyEtherWallet'] = 'https://www.myetherwallet.com'
if t['website']:
out['links']['Homepage'] = t['website']
if t.get('social', {}).get('github', None):
out['links']['Github'] = t['social']['github']
update_marketcap(out, out.get('coinmarketcap_alias', t['symbol']))
check_unsupported(details, 'erc20:', supported)
def update_ethereum(details):
out = details['coins'].setdefault('coin2:ETH', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'ETH')
set_default(out, 'name', 'Ethereum')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'ethereum')
out = details['coins'].setdefault('coin2:ETC', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'ETC')
set_default(out, 'name', 'Ethereum Classic')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'ethereum-classic')
out = details['coins'].setdefault('coin2:RSK', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'RSK')
set_default(out, 'name', 'Rootstock')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'rootstock')
out = details['coins'].setdefault('coin2:EXP', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'EXP')
set_default(out, 'name', 'Expanse')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'expanse')
out = details['coins'].setdefault('coin2:UBQ', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'UBQ')
set_default(out, 'name', 'Ubiq')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'ubiq')
out = details['coins'].setdefault('coin2:ELLA', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'ELLA')
set_default(out, 'name', 'Ellaism')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'ellaism')
out = details['coins'].setdefault('coin2:EGEM', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'EGEM')
set_default(out, 'name', 'EtherGem')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'egem')
out = details['coins'].setdefault('coin2:ETSC', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'ETSC')
set_default(out, 'name', 'EthereumSocial')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'etsc')
ut = details['coins'].setdefault('coin2:EOSC', {})
out['type'] = 'coin'
set_default(out, 'shortcut', 'EOSC')
set_default(out, 'name', 'EOS Classic')
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, 'eosc')
def update_mosaics(details):
d = json.load(open('defs/nem/nem_mosaics.json'))
supported = []
for mosaic in d:
# print('Updating', mosaic['name'], mosaic['ticker'])
key = "mosaic:%s" % mosaic['ticker'].strip()
supported.append(key)
out = details['coins'].setdefault(key, {})
out['type'] = 'mosaic'
set_default(out, 'shortcut', mosaic['ticker'].strip())
set_default(out, 'name', mosaic['name'])
set_default(out, 't1_enabled', 'yes')
set_default(out, 't2_enabled', 'yes')
update_marketcap(out, out.get('coinmarketcap_alias', out['name']))
check_unsupported(details, 'mosaic:', supported)
def check_missing_details(details):
for k in details['coins'].keys():
coin = details['coins'][k]
hide = False
if 'links' not in coin:
print("%s: Missing links" % k)
hide = True
if 'Homepage' not in coin['links']:
print("%s: Missing homepage" % k)
hide = True
if coin['t1_enabled'] not in ('yes', 'no', 'planned', 'soon'):
print("%s: Unknown t1_enabled" % k)
hide = True
if coin['t2_enabled'] not in ('yes', 'no', 'planned', 'soon'):
print("%s: Unknown t2_enabled" % k)
hide = True
if 'TREZOR Wallet' in coin['links'] and coin['links']['TREZOR Wallet'] != 'https://wallet.trezor.io':
print("%s: Strange URL for TREZOR Wallet" % k)
hide = True
for w in [ x.lower() for x in coin['links'].keys() ]:
if 'wallet' in w or 'electrum' in w:
break
else:
if coin['t1_enabled'] == 'yes' or coin['t2_enabled'] == 'yes':
print("%s: Missing wallet" % k)
hide = True
else:
print("%s: Missing wallet, but not hiding" % k)
if hide:
# If any of important detail is missing, hide coin from list
coin['hidden'] = 1
if not hide and coin.get('hidden'):
print("%s: Details are OK, but coin is still hidden" % k)
for k in details['coins'].keys():
if details['coins'][k].get('hidden') == 1:
print("%s: Coin is hidden" % k)
if __name__ == '__main__':
try:
details = json.load(open('coins_details.json', 'r'))
except FileNotFoundError:
details = {'coins': {}, 'info': {}}
coinmarketcap_init()
update_coins(details)
update_erc20(details)
update_ethereum(details)
update_mosaics(details)
update_info(details)
check_missing_details(details)
print(json.dumps(details['info'], sort_keys=True, indent=4))
json.dump(details, open('coins_details.json', 'w'), sort_keys=True, indent=4)