Skip to content

Commit 732a93f

Browse files
authored
Merge pull request #654 from OriginProtocol/shah/fix-ogn-dashboard
Switch to use etherscan API
2 parents 6925b14 + ccf7397 commit 732a93f

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

logic/scripts/update_token_insight.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -346,48 +346,70 @@ 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

356-
url = "https://api.ethplorer.io/getAddressInfo/%s" % (wallet)
357-
results = call_ethplorer(url)
386+
eth_balance = fetch_wallet_eth_balance(wallet)
387+
print("ETH balance of {} is {}".format(wallet, eth_balance))
388+
389+
ogn_balance = fetch_wallet_token_balance(wallet, token_stats.ogn_contract)
390+
print("OGN balance of {} is {}".format(wallet, ogn_balance))
391+
392+
ogv_balance = fetch_wallet_token_balance(wallet, token_stats.ogv_contract)
393+
print("OGV balance of {} is {}".format(wallet, ogv_balance))
394+
395+
dai_balance = fetch_wallet_token_balance(wallet, token_stats.dai_contract)
396+
print("DAI balance of %s is %s".format(wallet, dai_balance))
358397

359398
contact = db_common.get_or_create(
360399
db.session, db_models.EthContact, address=wallet
361400
)
362401

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

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
391413
contact.last_updated = datetime.utcnow()
392414

393415
db.session.add(contact)

0 commit comments

Comments
 (0)