-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_tokens.py
More file actions
183 lines (161 loc) · 7.66 KB
/
update_tokens.py
File metadata and controls
183 lines (161 loc) · 7.66 KB
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
from pycoingecko import CoinGeckoAPI
from web3 import Web3
import json
import requests
import os
import time
from PIL import Image
from io import BytesIO
from pathlib import Path
cg = CoinGeckoAPI()
rpc_urls = {
'arbitrum-one': 'https://patient-distinguished-pallet.arbitrum-mainnet.quiknode.pro/4cbe7cbdb55ec4b33fdc1a4239e1169b167ae351/',
'scroll': 'https://scroll-mainnet.chainstacklabs.com',
'mode': 'https://mainnet.mode.network/'
}
w3 = Web3(Web3.HTTPProvider(rpc_urls['mode']))
print(w3.is_connected())
erc20_file = open('abis/ERC20.json')
erc20_abi = json.load(erc20_file)
supported_chains = ['mode']
branch_name_for_images = 'master'
new_tokens_only = False
search_tokens_blacklist = [
'1minBET',
'RealT Token',
'RealToken',
'All.me',
'AltEstate Token',
'CloutContracts',
'Cointorox',
'Cryptocurrency Top 10 Index',
'Curio Governance',
'DigixDAO',
'Energoncoin',
'Ethereum Gold',
'EXMR FDN',
'Freeliquid',
'Future Of Finance Fund',
'Giga Watt Token',
'HedgeTrade',
'High Performance Blockchain',
'LNX Protocol',
'Paypex',
'Prime DAI',
'Qobit'
'Rapidz',
'Sai',
'SENSO',
'Silent Notary',
'Spaghetti',
'SpideyFloki',
'Taxa Token',
'TOP Network',
'Union Fair Coin',
'WM PROFESSIONAL',
'Wrapped IoTex',
'Wrapped XMR by BTSE'
]
coins = cg.get_coins_list(include_platform=True)
i = 0
tokens = {}
print(time.time())
for chain in supported_chains:
tokens[chain] = []
for coin in coins:
if chain in coin['platforms'].keys() and coin['platforms'][chain] != '':
skip_token = False
new_token = False
for term in search_tokens_blacklist:
if coin['name'].startswith(term):
skip_token = True
#print('skipped token')
if skip_token == False:
contract = w3.eth.contract(address=Web3.to_checksum_address(coin['platforms'][chain]), abi=erc20_abi)
path = 'blockchains/' + chain + '/assets/' + contract.address
token = {}
if not os.path.exists(path):
os.makedirs(path)
new_token = True
#############################
###### CONTRACT CALLS #######
#############################
if not os.path.exists(path + '/info.json'):
try:
token['name'] = contract.functions.name().call()
print('name:', token['name'])
except:
print('name() failed for ' + json.dumps(coin) + '..using coingecko name value..')
token['name'] = coin['name']
if token['name'] == '':
print('name() failed for ' + json.dumps(coin) + '..using coingecko name value..')
token['name'] = coin['name']
try:
token['symbol'] = contract.functions.symbol().call()
except:
print('symbol() failed for ' + json.dumps(coin) + '..using coingecko symbol value..')
token['symbol'] = coin['symbol'].upper()
token['id'] = contract.address
try:
token['decimals'] = contract.functions.decimals().call()
except:
print('decimals() failed for' + json.dumps(coin) + '..continuing on..')
continue
##########################################
###### HANDLE TOKEN ALREADY EXISTS #######
##########################################
else:
print('token info.json already exists')
with open(path + '/info.json','r+') as token_info:
token = json.load(token_info)
print('found file: ' + json.dumps(coin))
if new_tokens_only and not new_token:
print(token)
tokens[chain].append(token)
continue
#############################
###### COINGECKO INFO #######
#############################
too_many_requests = True
while too_many_requests:
try:
time.sleep(1.3)
coin_info = cg.get_coin_info_from_contract_address_by_id(chain, coin['platforms'][chain])
too_many_requests = False
except:
print('429 - Too Many Requests: waiting 5 seconds')
time.sleep(5)
try:
img_data = requests.get(coin_info['image']['large']).content
except:
print('bad image URL...continuing')
continue
img = Image.open(BytesIO(img_data))
img.save(path + '/logo.png')
token['coingecko_url'] = 'https://www.coingecko.com/en/coins/' + coin['id']
if 'usd' in coin_info['market_data']['market_cap'].keys():
token['market_cap_usd'] = coin_info['market_data']['market_cap']['usd']
else:
token['market_cap_usd'] = 0.0
token['market_cap_rank'] = coin_info['market_data']['market_cap_rank']
if coin_info['market_data']['total_volume'] != None and coin_info['market_data']['total_volume'] != '{}' and 'usd' in coin_info['market_data']['total_volume'].keys():
#print(coin_info['market_data']['total_volume'])
token['24_hr_volume_usd'] =coin_info['market_data']['total_volume']['usd']
if coin_info['public_notice'] != None:
token['public_notice'] = coin_info['public_notice']
token['logoURI'] = 'https://raw.githubusercontent.com/poolsharks-protocol/token-metadata/' + branch_name_for_images + '/' + path + '/logo.png'
with open(path + '/info.json','w+') as token_info:
token_info.write(json.dumps(token, indent=4))
tokens[chain].append(token)
else:
print('skipped token')
if os.path.exists('blockchains/' + chain + '/tokenlist.json'):
f = open('blockchains/' + chain + '/tokenlist.json','r')
print(time.time())
tokenlist = json.load(f)
tokenlist['search_tokens'] = tokens[chain] + tokenlist['search_tokens']
else:
tokenlist = {}
tokenlist['search_tokens'] = tokens[chain]
fw = open('blockchains' + '/' + chain + '/tokenlist.json','w+')
fw.write(json.dumps(tokenlist, indent=4))