1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-29 19:08:12 +00:00
trezor-firmware/ethereum_tokens-gen.py

69 lines
1.8 KiB
Python
Raw Normal View History

2017-12-18 17:34:11 +00:00
#!/usr/bin/env python3
import sys
import requests
def get_tokens(ipfs_hash, chain):
URL = 'https://gateway.ipfs.io/ipfs/%s/%s.json' % (ipfs_hash, chain)
2017-12-18 17:34:11 +00:00
r = requests.get(URL)
return r.json()
def print_tokens(ipfs_hash, chain, chain_id, python=False):
tokens = get_tokens(ipfs_hash, chain)
2017-12-18 17:34:11 +00:00
if len(tokens) > 0:
if python:
print(' # %s' % chain)
else:
print('\t// %s' % chain)
for t in sorted(tokens, key=lambda x: x['symbol'].upper()):
address, name, symbol, decimal = t['address'], t['name'], t['symbol'], int(t['decimals'])
2017-12-18 17:34:11 +00:00
address = '\\x'.join([address[i:i + 2] for i in range(0, len(address), 2)])[2:].lower()
if python:
2018-02-28 21:47:59 +00:00
print(" (%d, b'%s', '%s', %d), # %s" % (chain_id, address, symbol, decimal, name))
2017-12-18 17:34:11 +00:00
else:
2018-02-28 21:47:59 +00:00
print('\t{%2d, "%s", " %s", %d}, // %s' % (chain_id, address, symbol, decimal, name))
2017-12-18 17:34:11 +00:00
return len(tokens)
# disabled are networks with no tokens defined in ethereum-lists/tokens repo
2017-12-18 17:34:11 +00:00
networks = [
('eth', 1),
# ('exp', 2),
# ('rop', 3),
('rin', 4),
2017-12-18 17:34:11 +00:00
('ubq', 8),
# ('rsk', 30),
('kov', 42),
2017-12-18 17:34:11 +00:00
('etc', 61),
]
def generate_c(ipfs_hash):
2017-12-18 17:34:11 +00:00
count = 0
for s, i in networks:
count += print_tokens(ipfs_hash, s, i)
2017-12-18 17:34:11 +00:00
print('-' * 32)
print('#define TOKENS_COUNT %d' % count)
def generate_python(ipfs_hash):
print('ipfs_hash = "%s"' % ipfs_hash)
2017-12-18 17:34:11 +00:00
print('tokens = [')
for s, i in networks:
print_tokens(ipfs_hash, s, i, python=True)
2017-12-18 17:34:11 +00:00
print(']')
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: ethereum_tokens-gen.py ipfs_hash [--python]')
sys.exit(1)
ipfs_hash = sys.argv[1]
if len(sys.argv) > 2 and sys.argv[2] == '--python':
generate_python(ipfs_hash)
2017-12-18 17:34:11 +00:00
else:
generate_c(ipfs_hash)