def get_balance(address):
"""
Retrieves the balance from etherscan.io.
The balance is returned in ETH rounded to the second decimal.
"""
address = PyWalib.address_hex(address)
url = 'https://api.etherscan.io/api'
url += '?module=account&action=balance'
url += '&address=%s' % address
url += '&tag=latest'
if ETHERSCAN_API_KEY:
'&apikey=%' % ETHERSCAN_API_KEY
# TODO: handle 504 timeout, 403 and other errors from etherscan
response = requests.get(url)
response_json = response.json()
PyWalib.handle_etherscan_error(response_json)
balance_wei = int(response_json["result"])
balance_eth = balance_wei / float(pow(10, 18))
balance_eth = round(balance_eth, ROUND_DIGITS)
return balance_eth
评论列表
文章目录