Add docstrings
This commit is contained in:
@@ -17,6 +17,7 @@ address = add_0x(address_bytes.hex())
|
||||
rpc_provider = os.environ.get('RPC_PROVIDER', 'http://localhost:8545')
|
||||
rpc = EthHTTPConnection(rpc_provider)
|
||||
o = balance(address)
|
||||
print(o)
|
||||
r = rpc.do(o)
|
||||
|
||||
clean_address = strip_0x(address)
|
||||
|
||||
@@ -3,22 +3,23 @@ import os
|
||||
import sys
|
||||
|
||||
# local imports
|
||||
from chainlib.jsonrpc import jsonrpc_template
|
||||
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')
|
||||
rpc = EthHTTPConnection(rpc_provider)
|
||||
conn = EthHTTPConnection(rpc_provider)
|
||||
|
||||
# check the connection
|
||||
if not rpc.check():
|
||||
if not conn.check():
|
||||
sys.stderr.write('node {} not usable\n'.format(rpc_provider))
|
||||
sys.exit(1)
|
||||
|
||||
# build and send rpc call
|
||||
o = jsonrpc_template()
|
||||
g = JSONRPCRequest()
|
||||
o = g.template()
|
||||
o['method'] = 'eth_blockNumber'
|
||||
r = rpc.do(o)
|
||||
r = conn.do(o)
|
||||
|
||||
# interpret result for humans
|
||||
try:
|
||||
|
||||
41
example/jsonrpc_factory.py
Normal file
41
example/jsonrpc_factory.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# standard imports
|
||||
import os
|
||||
import sys
|
||||
|
||||
# local imports
|
||||
from chainlib.jsonrpc import JSONRPCRequest
|
||||
from chainlib.chain import ChainSpec
|
||||
from chainlib.connection import (
|
||||
JSONRPCHTTPConnection,
|
||||
RPCConnection,
|
||||
ConnType,
|
||||
)
|
||||
|
||||
|
||||
# set up node connection and execute rpc call
|
||||
rpc_provider = os.environ.get('RPC_PROVIDER', 'http://localhost:8545')
|
||||
RPCConnection.register_constructor(ConnType.HTTP, JSONRPCHTTPConnection)
|
||||
|
||||
tag = 'baz'
|
||||
chain_spec = ChainSpec('foo', 'bar', 42, tag=tag)
|
||||
RPCConnection.register_location(rpc_provider, chain_spec, tag='default')
|
||||
conn = RPCConnection.connect(chain_spec, 'default')
|
||||
|
||||
# check the connection
|
||||
if not conn.check():
|
||||
sys.stderr.write('node {} not usable\n'.format(rpc_provider))
|
||||
sys.exit(1)
|
||||
|
||||
# build and send rpc call
|
||||
g = JSONRPCRequest()
|
||||
o = g.template()
|
||||
o['method'] = 'eth_blockNumber'
|
||||
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))
|
||||
@@ -21,13 +21,21 @@ from chainlib.eth.tx import (
|
||||
)
|
||||
from chainlib.error import JSONRPCException
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
|
||||
# eth transactions need an explicit chain parameter as part of their signature
|
||||
chain_spec = ChainSpec.from_chain_str('evm:ethereum:1')
|
||||
|
||||
# create keystore and signer
|
||||
keystore = DictKeystore()
|
||||
signer = EIP155Signer(keystore)
|
||||
sender_address = keystore.new()
|
||||
|
||||
# import private key for sender
|
||||
sender_keystore_file = os.path.join(script_dir, '..', 'tests', 'testdata', 'keystore', 'UTC--2021-01-08T17-18-44.521011372Z--eb3907ecad74a0013c259d5874ae7f22dcbcc95c')
|
||||
sender_address = keystore.import_keystore_file(sender_keystore_file)
|
||||
|
||||
# create a new address to use as recipient
|
||||
recipient_address = keystore.new()
|
||||
|
||||
# set up node connection
|
||||
|
||||
Reference in New Issue
Block a user