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
/
Copy pathethereum_tokens_gen.py
executable file
·71 lines (54 loc) · 1.79 KB
/
ethereum_tokens_gen.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
#!/usr/bin/env python3
import sys
import os
import json
def get_tokens():
tokens = []
for s, i in networks:
try:
files = os.scandir('defs/ethereum/tokens/tokens/%s' % s)
except FileNotFoundError:
continue
for f in files:
if not f.path.endswith('.json'):
continue
# print('Processing', f.path)
data = json.load(open(f.path, 'r'))
data['chain'] = s
data['chain_id'] = i
tokens.append(data)
return tokens
def print_tokens(tokens, python=False):
for t in sorted(tokens, key=lambda x: x['chain'] + x['symbol'].upper()):
address, name, symbol, decimal, chain, chain_id = t['address'], t['name'], t['symbol'], int(t['decimals']), t['chain'], t['chain_id']
address = '\\x'.join([address[i:i + 2] for i in range(0, len(address), 2)])[2:].lower()
if python:
print(" (%d, b'%s', '%s', %d), # %s / %s" % (chain_id, address, symbol, decimal, chain, name))
else:
print('\t{%2d, "%s", " %s", %d}, // %s / %s' % (chain_id, address, symbol, decimal, chain, name))
return len(tokens)
# disabled are networks with no tokens defined in ethereum-lists/tokens repo
networks = [
('eth', 1),
('exp', 2),
# ('rop', 3),
# ('rin', 4),
('ubq', 8),
# ('rsk', 30),
# ('kov', 42),
('etc', 61),
]
def generate_c(tokens):
count = print_tokens(tokens)
print('-' * 32)
print('#define TOKENS_COUNT %d' % count)
def generate_python(tokens):
print('tokens = [')
print_tokens(tokens, python=True)
print(']')
if __name__ == '__main__':
tokens = get_tokens()
if len(sys.argv) > 1 and sys.argv[1] == '--python':
generate_python(tokens)
else:
generate_c(tokens)