-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
451 lines (410 loc) · 19.3 KB
/
functions.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# Powered by Etherscan.io APIs
#
# Written by AtLeastSignificant
import json
import sys
import os
import datetime
from urllib.request import urlopen
from decimal import *
class bcolors:
HEADER = '\033[95m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def clearScreen():
try:
os.system('cls' if os.name == 'nt' else 'clear')
return
except:
print(sys.exc_info())
return
def loadTokenContracts():
try:
with open('token_contracts.json', 'r') as contracts:
data = json.load(contracts)
return data
except:
print(sys.exc_info())
return
def initFiles():
try:
l = ['app_info.json', 'wallet_addresses.json', 'token_contracts.json']
for file in l:
if not os.path.isfile(file):
f = open(file, 'w')
if file == 'app_info.json':
info = {'API_KEY':'YourApiKey'}
else:
info = {}
with open(file, 'w') as outfile:
json.dump(info, outfile)
return
except:
print(sys.exc_info())
return
def displayKnownTokens():
try:
contracts = loadTokenContracts()
print(bcolors.HEADER + "\nToken database contains " + bcolors.FAIL + str(len(contracts)) + bcolors.HEADER + " contracts:\n" + bcolors.ENDC)
for contract in contracts:
print(" " + bcolors.OKGREEN + contract + bcolors.ENDC)
print(" " + \
bcolors.FAIL + " Address: " + bcolors.ENDC + str(contracts[contract]['contract_address']) + \
bcolors.FAIL + " Symbol: " + bcolors.ENDC + str(contracts[contract]['symbol']) + \
bcolors.FAIL + " Decimals: " + bcolors.ENDC + str(contracts[contract]['decimals']))
return
except:
print(sys.exc_info())
return
def addTokenContract():
try:
print("\nTo find contract information about tokens, go to" + bcolors.WARNING + " https://etherscan.io/tokens" + bcolors.ENDC)
address = input("\nNew token contract address: ")
if address[:2] == "0x" and len(address) == 42:
contracts = loadTokenContracts()
for contract in contracts:
if address == contracts[contract]['contract_address']:
print("\n" + bcolors.OKGREEN + contract + bcolors.WARNING + " token already in database:" + bcolors.ENDC)
print(" " + str(contracts[contract]))
check = input("\nIs this correct? [Y/N]\n\n: ")
if check.lower() == 'n' or check.lower() == 'no':
name = input("\nNew token name: ")
symbol = input("New token symbol: ")
decimals = int(input("New token decimals: "))
newToken = {}
newToken[name] = {'symbol':symbol, 'contract_address':address, 'decimals':decimals}
print("\n", newToken)
check2 = input("\nIs this correct? [Y/N]\n\n: ")
if check2.lower() == 'y' or check2.lower() == 'yes':
print(bcolors.WARNING + "\nRemoving old token info..." + bcolors.ENDC)
del contracts[contract]
print(bcolors.WARNING + "Adding " + bcolors.OKGREEN + name + bcolors.WARNING + " to token database..." + bcolors.ENDC)
contracts[name] = {'symbol':symbol, 'contract_address':address, 'decimals':decimals}
with open('token_contracts.json', 'w') as outfile:
json.dump(contracts, outfile)
print(bcolors.OKGREEN + "Done." + bcolors.ENDC)
return
else:
print(bcolors.WARNING + "Leaving token database unchanged." + bcolors.ENDC)
return
else:
print(bcolors.WARNING + "Leaving token database unchanged." + bcolors.ENDC)
return
name = input("New token name: ")
symbol = input("New token symbol: ")
decimals = input("New token decimals: ")
newToken = {}
newToken[name] = {'symbol':symbol, 'contract_address':address, 'decimals':decimals}
print("\n", newToken)
check2 = input("\nIs this correct? [Y/N]\n\n: ")
if check2.lower() == 'y' or check2.lower() == 'yes':
print(bcolors.WARNING + "Adding " + bcolors.OKGREEN + name + bcolors.WARNING + " to token database..." + bcolors.ENDC)
contracts[name] = {'symbol':symbol, 'contract_address':address, 'decimals':decimals}
with open('token_contracts.json', 'w') as outfile:
json.dump(contracts, outfile)
print(bcolors.OKGREEN + "Done." + bcolors.ENDC)
return
else:
print(bcolors.WARNING + "Leaving token database unchanged." + bcolors.ENDC)
return
else:
print(bcolors.FAIL + "Incorrect address value.\nAction cancelled." + bcolors.ENDC)
return
return
except KeyboardInterrupt:
print("\n" + bcolors.WARNING + "Exiting..." + bcolors.ENDC)
exit()
except:
print(sys.exc_info())
return
def removeTokenContract():
try:
address = input("\nEnter token to remove (symbol/address): ")
contracts = loadTokenContracts()
for contract in contracts:
if (address[:2] == "0x" and len(address) == 42 and address == contracts[contract]['contract_address']) or \
address == contracts[contract]['symbol']:
print(bcolors.OKGREEN + "\nContract found:\n " + bcolors.ENDC + str(contracts[contract]))
check = input(bcolors.WARNING + "\nRemove " + bcolors.OKGREEN + contract + bcolors.WARNING + " from database? [Y/N]\n\n: " + bcolors.ENDC)
if check.lower() == 'y' or check.lower() == 'yes':
print(bcolors.WARNING + "\nRemoving old token info..." + bcolors.ENDC)
del contracts[contract]
with open('token_contracts.json', 'w') as outfile:
json.dump(contracts, outfile)
print(bcolors.OKGREEN + "Done." + bcolors.ENDC)
return
else:
print(bcolors.WARNING + "\nLeaving token database unchanged." + bcolors.ENDC)
return
print(bcolors.WARNING + "\nContract not found.\nLeaving token database unchanged." + bcolors.ENDC)
return
except KeyboardInterrupt:
print("\n" + bcolors.WARNING + "Exiting..." + bcolors.ENDC)
exit()
except:
print(sys.exc_info())
return
def getDecimalBalance(balance, dec):
try:
if len(balance) < dec:
diff = dec - len(balance)
for i in range(diff):
balance = "0" + balance
ret = balance[:len(balance)-dec] + "." + balance[len(balance)-dec:]
return Decimal(ret)
except:
print(sys.exc_info())
return
def queryTokenBalance(address, contract, decimals, api_key):
try:
query = "https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=" \
+ contract + "&address=" + address + "&tag=latest&apikey=" + api_key
request = urlopen(query)
response = request.read()
jsonObj = json.loads(response.decode('utf-8'))
request.close()
balance = getDecimalBalance(jsonObj['result'], decimals)
return balance
except:
print(sys.exc_info())
return
def querySingleAddressBalance(address):
try:
with open('app_info.json', 'r') as settings:
info = json.load(settings)
api_key = info['API_KEY']
query = "https://api.etherscan.io/api?module=account&action=balance&address=" \
+ address + "&tag=latest&apikey=" + api_key
request = urlopen(query)
response = request.read()
jsonObj = json.loads(response.decode('utf-8'))
request.close()
balance = getDecimalBalance(jsonObj['result'], 18)
return balance
except:
print(sys.exc_info())
return
def queryAllAddressBalances():
try:
with open('app_info.json', 'r') as settings:
info = json.load(settings)
api_key = info['API_KEY']
query = "https://api.etherscan.io/api?module=account&action=balancemulti&address="
with open('wallet_addresses.json', 'r') as wallet:
addresses = json.load(wallet)
for label, address in addresses.items():
query += (address + ",")
query = query[:-1]
query += "&tag=latest&apikey=" + api_key
request = urlopen(query)
response = request.read()
jsonObj = json.loads(response.decode('utf-8'))
request.close()
print(jsonObj)
return
except:
print(sys.exc_info())
return
def getWalletBalances():
try:
query = "https://api.coinmarketcap.com/v1/ticker/ethereum/"
request = urlopen(query)
response = request.read()
jsonObj = json.loads(response.decode('utf-8'))
request.close()
value = float(jsonObj[0]['price_usd'])
with open('wallet_addresses.json', 'r') as wallet:
addresses = json.load(wallet)
for label, address in addresses.items():
balance = float(querySingleAddressBalance(address))
print(" " + address + bcolors.FAIL + " :: " + bcolors.OKGREEN + label + bcolors.FAIL + " :: " \
+ bcolors.ENDC + str(balance) + bcolors.OKGREEN + " ETH" + bcolors.FAIL + " :: " \
+ bcolors.WARNING + "$" + str(round(value * balance, 2)) + bcolors.ENDC)
except:
print(sys.exc_info())
return
def getTokenBalance():
try:
with open('app_info.json', 'r') as settings:
info = json.load(settings)
api_key = info['API_KEY']
addressList1 = []
addressList2 = []
with open('wallet_addresses.json', 'r') as wallet:
addresses = json.load(wallet)
for label, address in addresses.items():
addressList1.append((address + bcolors.FAIL + " :: " + bcolors.OKGREEN + label + bcolors.ENDC))
addressList2.append(address)
print("")
count = 1
for address in addressList1:
print(" " + bcolors.OKGREEN + str(count) + bcolors.ENDC + ". " + address)
count += 1
selection = int(input("\nSelect address\n\n: "))
if selection <= int(len(addressList2)) and selection > 0:
address = addressList2[selection-1]
contracts = loadTokenContracts()
for contract in contracts:
balance = queryTokenBalance(address, contracts[contract]['contract_address'], int(contracts[contract]['decimals']), api_key)
print(" " + bcolors.OKGREEN + contract + bcolors.FAIL + " :: " + bcolors.ENDC + str(balance) + bcolors.OKGREEN \
+ " " + contracts[contract]['symbol'] + bcolors.ENDC)
else:
print(bcolors.WARNING + "\nInvalid selection." + bcolors.ENDC)
return
except KeyboardInterrupt:
print("\n" + bcolors.WARNING + "Exiting..." + bcolors.ENDC)
exit()
except:
print(sys.exc_info())
return
def snapshot():
try:
query = "https://api.coinmarketcap.com/v1/ticker/ethereum/"
request = urlopen(query)
response = request.read()
jsonObj = json.loads(response.decode('utf-8'))
request.close()
value = float(jsonObj[0]['price_usd'])
filename = datetime.datetime.now().strftime("%m-%d-%Y %I-%M-%S%p") + ".txt"
if not os.path.isdir("snapshots"):
os.mkdir("snapshots")
snapshot = open("snapshots/" + filename, "w")
snapshot.write("Balance snapshot for " + datetime.datetime.now().strftime("%m-%d-%Y %I:%M:%S%p") + "\n\n")
with open('wallet_addresses.json', 'r') as wallet:
addresses = json.load(wallet)
for label, address in addresses.items():
balance = float(querySingleAddressBalance(address))
snapshot.write(address + " :: " + label + " :: " + str(balance) + " ETH" + " :: $" + str(round(value * balance, 2)) + "\n")
snapshot.close()
print(bcolors.OKGREEN + "Done." + bcolors.ENDC)
except:
print(sys.exc_info())
return
def getAllWalletBalances():
try:
queryAllAddressBalances()
except:
print(sys.exc_info())
return
def getWalletAddresses():
try:
with open('wallet_addresses.json', 'r') as wallet:
addresses = json.load(wallet)
for label, address in addresses.items():
print(" " + address + bcolors.FAIL + " :: " + bcolors.OKGREEN + label + bcolors.ENDC)
return
except:
print(sys.exc_info())
return
def addNewWalletAddress():
try:
address = input("\nEnter public address (starts with \'0x\')\n\n: ")
if address[:2] == "0x" and len(address) == 42:
label = input("\nEnter a name for this address\n\n: ")
addToWallet = input("\nAdd \"" + label + "\": " + address + " to wallet? [Y/N]\n\n: ")
if addToWallet.lower() == 'y' or addToWallet.lower() == 'yes':
with open('wallet_addresses.json', 'r') as wallet:
addresses = json.load(wallet)
addresses[label] = address
with open('wallet_addresses.json', 'w') as newWallet:
json.dump(addresses, newWallet)
print(bcolors.OKGREEN + "Done." + bcolors.ENDC)
else:
print(bcolors.FAIL + "Action cancelled." + bcolors.ENDC)
else:
print(bcolors.FAIL + "Incorrect address value.\nAction cancelled." + bcolors.ENDC)
return
except KeyboardInterrupt:
print("\n" + bcolors.WARNING + "Exiting..." + bcolors.ENDC)
exit()
except:
print(sys.exc_info())
return
def removeWalletAddress():
try:
with open('wallet_addresses.json', 'r') as wallet:
addresses = json.load(wallet)
notDone = True
while notDone:
name = input("\nEnter name of address to remove\n\n: ")
if name in addresses.keys():
check = input("\nDelete \'" + name + "\' from wallet? [Y/N]\n\n: ")
if check.lower() == 'y' or check.lower() == 'yes':
del addresses[name]
with open('wallet_addresses.json', 'w') as newWallet:
json.dump(addresses, newWallet)
print(bcolors.OKGREEN + "Done." + bcolors.ENDC)
notDone = False
else:
print(bcolors.FAIL + "Action cancelled." + bcolors.ENDC)
notDone = False
else:
print(bcolors.WARNING + "\n\'" + name + "\' not found." + bcolors.ENDC)
tryAgain = input("Try another? [Y/N]\n\n: ")
if tryAgain.lower() == 'n' or tryAgain.lower() == 'no':
notDone = False
clearScreen()
printMenu()
else:
notDone = True
return
except KeyboardInterrupt:
print("\n" + bcolors.WARNING + "Exiting..." + bcolors.ENDC)
exit()
except:
print(sys.exc_info())
return
def changeApiKey():
try:
print(bcolors.HEADER + "\nAPI-Key info:\n" + bcolors.ENDC)
key = json.load(open('app_info.json', 'r'))['API_KEY']
print(" Current API key: \'" + bcolors.OKGREEN + key + bcolors.ENDC + "\'")
print("\n Visit https://etherscan.io/myapikey to generate a new one.\n")
command = input(bcolors.WARNING + "1 - Change API key\t\t" + bcolors.OKGREEN + \
"Enter" + bcolors.WARNING + " - Return to menu" + bcolors.ENDC + "\n\n: ")
if command.lower() == '1':
newKey = input("\nEnter new API key\n\n: ")
print("\nNew API key: \'" + bcolors.WARNING + newKey + bcolors.ENDC + "\'")
check = input("Is this correct? [Y/N]\n\n: ")
if check.lower() == 'y' or check.lower() == 'yes':
info['API_KEY'] = newKey
json.dump(info, open('app_info.json', 'w'))
print(bcolors.OKGREEN + "Done." + bcolors.ENDC)
else:
print(bcolors.FAIL + "Action cancelled." + bcolors.ENDC)
else:
clearScreen()
printMenu()
return
except KeyboardInterrupt:
print("\n" + bcolors.WARNING + "Exiting..." + bcolors.ENDC)
exit()
except:
print(sys.exc_info())
return
def printMenu():
menu = bcolors.HEADER + "\nCommands:" + bcolors.ENDC + "\n\n\
1 - " + bcolors.OKGREEN + "Check balances\n" + bcolors.ENDC + "\
2 - " + bcolors.OKGREEN + "View wallet info\n" + bcolors.ENDC + "\
3 - " + bcolors.OKGREEN + "View tracked ERC20 tokens\n" + bcolors.ENDC + "\
4 - " + bcolors.OKGREEN + "Change API key" + bcolors.FAIL + "\n\
Exit" + bcolors.ENDC + " - close the application"
print(menu)
return
def printHelp():
print(bcolors.OKGREEN + "\nThank you for using " + bcolors.FAIL + "AlertEth" + bcolors.OKGREEN + " by " + bcolors.HEADER + \
"/u/AtLeastSignificant" + bcolors.OKGREEN + " | " + bcolors.HEADER + "@Tomshwom" + bcolors.OKGREEN + " on Steemit" + \
".\n\nPlease send feedback and any bug reports to me via Reddit private message." + \
"\n\nIf you want to contribute to the development of this app or my other work, consider donating to " + \
bcolors.HEADER + "AtLeastSignificant.eth" + bcolors.OKGREEN + "." + \
"\n\nThis app is powered by " + bcolors.WARNING + "Etherscan.io" + bcolors.OKGREEN + " APIs and " + bcolors.WARNING + "Coinmarketcap.com" + bcolors.OKGREEN + "." + bcolors.ENDC)
help = bcolors.HEADER + "\nCommands info:" + bcolors.ENDC + "\n\n\
1 - " + bcolors.OKGREEN + "Check balances" + bcolors.FAIL + " :: " + bcolors.ENDC + "view current Ether and token balances on all addresses in your wallet\n\
2 - " + bcolors.OKGREEN + "View wallet info" + bcolors.FAIL + " :: " + bcolors.ENDC + "view/add/remove addresses from your wallet\n\
3 - " + bcolors.OKGREEN + "View tracked ERC20 tokens" + bcolors.FAIL + " :: " + bcolors.ENDC + "view/add/remove token contracts that are used when finding wallet balances\n\
4 - " + bcolors.OKGREEN + "Change API key" + bcolors.FAIL + " :: " + bcolors.ENDC + "view/change Etherscan API key" + bcolors.FAIL + "\n\
Exit" + bcolors.ENDC + " - close the application"
print(help)
return