forked from elkfinance/tokens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_list.py
executable file
·145 lines (117 loc) · 4.16 KB
/
update_list.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
#!/usr/bin/python3
import argparse
import csv
from datetime import datetime
import itertools
import json
import pandas
import requests
import shutil
import os
import re
import sys
from web3 import Web3
CHAIN_IDS = {
'MUMBAI': 80001,
'MATIC': 137,
'FUJI': 43113,
'AVAX': 43114,
'FTMTEST': 4002,
'FTM': 250,
'HECOTEST': 256,
'HECO': 128,
'BSC': 56,
'XDAI': 100,
}
WETH = {
'MUMBAI': '0x5B67676a984807a212b1c59eBFc9B3568a474F0a',
'MATIC': '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
'FUJI': '0xd00ae08403B9bbb9124bB305C09058E32C39A48c',
'AVAX': '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7',
'FTMTEST': '0xf1277d1Ed8AD466beddF92ef448A132661956621',
'FTM': '0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83',
'HECOTEST': '0x7aF326B6351C8A9b8fb8CD205CBe11d4Ac5FA836',
'HECO': '0x5545153CCFcA01fbd7Dd11C0b23ba694D9509A6F',
'BSC': '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
'XDAI': '0xe91d153e0b41518a2ce8dd3d7944fa863463a97d',
}
LOGO_DIR = 'logos'
HOSTED_URL = 'https://raw.githubusercontent.com/elkfinance/tokens/main/'
def process_token(t, chain, fetch_logo):
chainId = CHAIN_IDS[chain.upper()]
if t['chainId'] == chainId:
address = Web3.toChecksumAddress(t['address'])
path = os.path.join(chain.lower(), Web3.toChecksumAddress(t['address']), 'logo.png')
logo_path = os.path.join(LOGO_DIR, path)
logo_target = os.path.join(HOSTED_URL, logo_path)
if fetch_logo:
download_logo(t['logoURI'], logo_path)
t['address'] = address
t['logoURI'] = logo_target
return [t]
else:
return []
def download_logo(url, path):
print(f'Downloading logo from {url} to {path}...', end='')
try:
os.makedirs(os.path.dirname(path))
except:
pass
r = requests.get(url, verify=False, allow_redirects=True)
with open(path, 'wb') as f:
f.write(r.content)
print(' Done!')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Manage Elk.Finance tokens')
parser.add_argument('--chain', '-c', required=True)
parser.add_argument('--output_list', '-o', required=True)
parser.add_argument('--url', '-u')
parser.add_argument('--logos', '-l', default=False, action='store_true')
parser.add_argument('--version')
args = parser.parse_args()
chain = args.chain.upper()
if chain not in CHAIN_IDS:
print(f'Unknown chain: {chain}!')
sys.exit(-1)
with open(args.output_list, 'r') as f:
old_tokenlist = json.load(f)
if args.url is not None:
new_tokenlist = requests.get(args.url).json()
else:
new_tokenlist = {}
tokenlist = {}
tokenlist['name'] = f'Elk {chain} Tokens'
tokenlist['timestamp'] = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S+00:00')
tokenlist['keywords'] = ['elk', 'defi', chain.lower()]
if args.version is not None:
m = re.match(r'(\d+)\.(\d+)\.(\d+)', args.version)
if m is None:
print(f'Invalid version: {args.version}!')
sys.exit(-1)
version = {'major': int(m.group(1)), 'minor': int(m.group(2)), 'patch': int(m.group(3))}
else:
try:
old_version = old_tokenlist['version']
version = old_version
version['minor'] += 1
version['patch'] = 0
except:
version = {'major': 1, 'minor': 0, 'patch': 0}
tokenlist['version'] = version
tokens = {}
if 'tokens' in old_tokenlist:
for token in old_tokenlist['tokens']:
tokens[token['address'].lower()] = token
if 'tokens' in new_tokenlist:
for token in new_tokenlist['tokens']:
tokens[token['address'].lower()] = token
tokenlist['tokens'] = list(itertools.chain(*[process_token(t, chain, args.logos) for t in tokens.values()]))
list_logo = None
for token in tokenlist['tokens']:
if token['address'] == WETH[chain]:
list_logo = token['logoURI']
break
tokenlist['logoURI'] = list_logo
with open(args.output_list, 'w') as f:
f.write(json.dumps(tokenlist, indent=4, sort_keys=True))
print(f'New list written to {args.output_list}')