Skip to content

Instantly share code, notes, and snippets.

@luzihang123
Created October 9, 2018 03:53
Show Gist options
  • Save luzihang123/d7bde99f96cdf8366209fa046839b6f7 to your computer and use it in GitHub Desktop.
Save luzihang123/d7bde99f96cdf8366209fa046839b6f7 to your computer and use it in GitHub Desktop.
requests 从infura API获取地址余额 demo
# curl -G https://api.infura.io/v1/jsonrpc/mainnet/eth_getBalance --data-urlencode 'params=["0xc94770007dda54cF92009BFF0dE90c06F603a09f","latest"]'
import requests
import urllib
params_str = '["{}","latest"]'.format('0xc94770007dda54cF92009BFF0dE90c06F603a09f')
params_encode = urllib.parse.quote(params_str)
r = requests.get(
url="https://api.infura.io/v1/jsonrpc/mainnet/eth_getBalance?params={}".format(params_encode),
)
print(r.json())
balance = int(r.json()["result"],16)
print("{} Wei".format(balance))
print("{} Ether".format(balance*10**-18))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment