Skip to content

Commit 13bdb6f

Browse files
committed
Switch to use etherscan API
1 parent 6925b14 commit 13bdb6f

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

logic/scripts/update_token_insight.py

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -346,48 +346,71 @@ def fetch_ogn_token_info():
346346

347347
return token_info
348348

349+
def fetch_wallet_token_balance(wallet, token_addr, decimals = 18):
350+
etherscan_url = (
351+
"http://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=%s&tag=latest&address=%s&apikey=%s"
352+
% (token_addr, wallet, constants.ETHERSCAN_KEY)
353+
)
354+
355+
resp = call_etherscan(etherscan_url)
356+
if resp["status"] != "1":
357+
print("Error while fetching balance")
358+
print(resp)
359+
raise ValueError(resp["message"] or ("Failed to fetch %s balance" % token_addr))
360+
if resp["result"]:
361+
return float(resp["result"]) / math.pow(10, decimals)
362+
return 0
363+
364+
def fetch_wallet_eth_balance(wallet):
365+
etherscan_url = (
366+
"http://api.etherscan.io/api?module=account&action=balance&tag=latest&address=%s&apikey=%s"
367+
% (wallet, constants.ETHERSCAN_KEY)
368+
)
369+
370+
resp = call_etherscan(etherscan_url)
371+
372+
if resp["status"] != "1":
373+
print("Error while fetching balance")
374+
print(resp)
375+
raise ValueError(resp["message"] or "Failed to fetch ETH balance")
376+
if resp["result"]:
377+
return float(resp["result"]) / math.pow(10, 18)
378+
return 0
349379

350380
# Fetches wallet balance from API and stores that to DB
351381
def fetch_wallet_balance(wallet):
352382
print("Checking the balance of wallet {}".format(
353383
wallet,
354384
))
355385

386+
eth_balance = fetch_wallet_eth_balance(wallet)
387+
ogn_balance = fetch_wallet_token_balance(wallet, token_stats.ogn_contract)
388+
ogv_balance = fetch_wallet_token_balance(wallet, token_stats.ogv_contract)
389+
dai_balance = fetch_wallet_token_balance(wallet, token_stats.dai_contract)
390+
356391
url = "https://api.ethplorer.io/getAddressInfo/%s" % (wallet)
357392
results = call_ethplorer(url)
358393

359394
contact = db_common.get_or_create(
360395
db.session, db_models.EthContact, address=wallet
361396
)
362397

363-
if "error" in results:
364-
print("Error while fetching balance")
365-
print(results["error"]["message"])
366-
raise ValueError(results["error"]["message"])
398+
contact.eth_balance = eth_balance
399+
contact.ogn_balance = ogn_balance
400+
contact.ogv_balance = ogv_balance
401+
contact.dai_balance = dai_balance
402+
403+
print("ETH balance of {} is {}".format(wallet, eth_balance))
404+
print("OGN balance of {} is {}".format(wallet, ogn_balance))
405+
print("OGV balance of {} is {}".format(wallet, ogv_balance))
406+
print("DAI balance of %s is %s".format(wallet, dai_balance))
407+
408+
# Note: We have these columns and have been populating it in the past
409+
# But I don't see it being used anywhere, so I'm commenting this out
410+
# since it'd require more API calls
411+
# # contact.transaction_count = results["countTxs"]
412+
# # contact.token_count = len(results["tokens"])
367413

368-
contact.eth_balance = results["ETH"]["balance"]
369-
contact.transaction_count = results["countTxs"]
370-
371-
print("ETH balance of {} is {}".format(wallet, results["ETH"]["balance"]))
372-
if "tokens" in results:
373-
contact.tokens = results["tokens"]
374-
# update the OGN & DAI balance
375-
for token in results["tokens"]:
376-
if token["tokenInfo"]["address"] == token_stats.ogn_contract:
377-
contact.ogn_balance = float(token["balance"]) / math.pow(10, 18)
378-
print("OGN balance of {} is {}".format(wallet, contact.ogn_balance))
379-
elif token["tokenInfo"]["address"] == token_stats.ogv_contract:
380-
contact.ogv_balance = float(token["balance"]) / math.pow(10, 18)
381-
print("OGV balance of {} is {}".format(wallet, contact.ogv_balance))
382-
elif token["tokenInfo"]["address"] == token_stats.dai_contract:
383-
contact.dai_balance = float(token["balance"]) / math.pow(10, 18)
384-
print("DAI balance of %s is %s".format(wallet, contact.dai_balance))
385-
contact.token_count = len(results["tokens"])
386-
else:
387-
print("OGN balance of {} is {}".format(wallet, 0))
388-
contact.ogn_balance = 0
389-
contact.ogv_balance = 0
390-
contact.dai_balance = 0
391414
contact.last_updated = datetime.utcnow()
392415

393416
db.session.add(contact)

0 commit comments

Comments
 (0)