chainlib-eth/example/jsonrpc.py

31 lines
713 B
Python
Raw Permalink Normal View History

# standard imports
import os
import sys
# local imports
2021-08-21 09:27:40 +02:00
from chainlib.jsonrpc import JSONRPCRequest
from chainlib.eth.connection import EthHTTPConnection
# set up node connection and execute rpc call
rpc_provider = os.environ.get('RPC_PROVIDER', 'http://localhost:8545')
2021-08-21 09:27:40 +02:00
conn = EthHTTPConnection(rpc_provider)
# check the connection
2021-08-21 09:27:40 +02:00
if not conn.check():
sys.stderr.write('node {} not usable\n'.format(rpc_provider))
sys.exit(1)
# build and send rpc call
2021-08-21 09:27:40 +02:00
g = JSONRPCRequest()
o = g.template()
o['method'] = 'eth_blockNumber'
2021-08-21 09:27:40 +02:00
r = conn.do(o)
# interpret result for humans
try:
block_number = int(r, 10)
except ValueError:
block_number = int(r, 16)
print('block number {}'.format(block_number))