Implement chainlib cli util for token index

This commit is contained in:
nolash 2021-07-30 20:54:48 +02:00
parent 20d7e03af5
commit 9cbd168524
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 92 additions and 182 deletions

View File

@ -13,21 +13,12 @@ import argparse
import logging
import hashlib
# third-party imports
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore.dict import DictKeystore
# external imports
import chainlib.eth.cli
from chainlib.chain import ChainSpec
from chainlib.eth.nonce import (
RPCNonceOracle,
OverrideNonceOracle,
)
from chainlib.eth.gas import (
RPCGasOracle,
OverrideGasOracle,
)
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
from eth_erc20 import ERC20
from chainlib.eth.address import to_checksum_address
# local imports
from eth_token_index import TokenUniqueSymbolIndex
@ -35,92 +26,62 @@ from eth_token_index import TokenUniqueSymbolIndex
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
script_dir = os.path.dirname(__file__)
data_dir = os.path.join(script_dir, '..', 'data')
argparser = argparse.ArgumentParser()
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
argparser.add_argument('-a', '--contract-address', dest='a', required=True, type=str, help='Token index contract address')
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('-d', action='store_true', help='Dump RPC calls to terminal and do not send')
argparser.add_argument('--gas-price', type=int, dest='gas_price', help='Override gas price')
argparser.add_argument('--nonce', type=int, help='Override transaction nonce')
argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration')
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
argparser.add_argument('token_address', type=str, help='Token address to add to index')
args = argparser.parse_args()
if args.vv:
logg.setLevel(logging.DEBUG)
elif args.v:
logg.setLevel(logging.INFO)
extra_args = {
'token_address': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=TokenUniqueSymbolIndex.gas())
block_all = args.ww
block_last = args.w or block_all
wallet = chainlib.eth.cli.Wallet()
wallet.from_config(config)
passphrase_env = 'ETH_PASSPHRASE'
if args.env_prefix != None:
passphrase_env = args.env_prefix + '_' + passphrase_env
passphrase = os.environ.get(passphrase_env)
if passphrase == None:
logg.warning('no passphrase given')
passphrase=''
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
signer_address = None
keystore = DictKeystore()
if args.y != None:
logg.debug('loading keystore file {}'.format(args.y))
signer_address = keystore.import_keystore_file(args.y, password=passphrase)
logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore)
chain_spec = ChainSpec.from_chain_str(args.i)
rpc = EthHTTPConnection(args.p)
nonce_oracle = None
if args.nonce != None:
nonce_oracle = OverrideNonceOracle(signer_address, args.nonce)
else:
nonce_oracle = RPCNonceOracle(signer_address, rpc)
gas_oracle = None
if args.gas_price !=None:
gas_oracle = OverrideGasOracle(price=args.gas_price, conn=rpc, code_callback=TokenUniqueSymbolIndex.gas)
else:
gas_oracle = RPCGasOracle(rpc, code_callback=TokenUniqueSymbolIndex.gas)
dummy = args.d
contract_address = args.a
token_address = args.token_address
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
def main():
signer = rpc.get_signer()
signer_address = rpc.get_sender_address()
gas_oracle = rpc.get_gas_oracle()
nonce_oracle = rpc.get_nonce_oracle()
c = TokenUniqueSymbolIndex(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
token_address = to_checksum_address(config.get('_TOKEN_ADDRESS'))
if not config.true('_UNSAFE') and token_address != add_0x(config.get('_TOKEN_ADDRESS')):
raise ValueError('invalid checksum address for token_address')
contract_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')):
raise ValueError('invalid checksum address for contract')
(tx_hash_hex, o) = c.register(contract_address, signer_address, token_address)
if dummy:
print(tx_hash_hex)
print(o)
else:
rpc.do(o)
if block_last:
r = rpc.wait(tx_hash_hex)
if config.get('_RPC_SEND'):
conn.do(o)
if config.get('_WAIT'):
r = conn.wait(tx_hash_hex)
if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1)
c = ERC20(chain_spec)
o = c.symbol(token_address)
r = rpc.do(o)
r = conn.do(o)
token_symbol = ERC20.parse_symbol(r)
logg.info('added token {} at {} to token index {}'.format(token_symbol, token_address, contract_address))
print(tx_hash_hex)
else:
print(o)
if __name__ == '__main__':

View File

@ -12,19 +12,9 @@ import json
import argparse
import logging
# third-party imports
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore.dict import DictKeystore
# external imports
import chainlib.eth.cli
from chainlib.chain import ChainSpec
from chainlib.eth.nonce import (
RPCNonceOracle,
OverrideNonceOracle,
)
from chainlib.eth.gas import (
RPCGasOracle,
OverrideGasOracle,
)
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
# local imports
@ -33,75 +23,35 @@ from eth_token_index import TokenUniqueSymbolIndex
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
script_dir = os.path.dirname(__file__)
data_dir = os.path.join(script_dir, '..', 'data')
argparser = argparse.ArgumentParser()
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('-d', action='store_true', help='Dump RPC calls to terminal and do not send')
argparser.add_argument('--gas-price', type=int, dest='gas_price', help='Override gas price')
argparser.add_argument('--nonce', type=int, help='Override transaction nonce')
argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration')
arg_flags = chainlib.eth.cli.argflag_std_write
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
args = argparser.parse_args()
if args.vv:
logg.setLevel(logging.DEBUG)
elif args.v:
logg.setLevel(logging.INFO)
config = chainlib.eth.cli.Config.from_args(args, arg_flags, default_fee_limit=TokenUniqueSymbolIndex.gas())
block_all = args.ww
block_last = args.w or block_all
wallet = chainlib.eth.cli.Wallet()
wallet.from_config(config)
passphrase_env = 'ETH_PASSPHRASE'
if args.env_prefix != None:
passphrase_env = args.env_prefix + '_' + passphrase_env
passphrase = os.environ.get(passphrase_env)
if passphrase == None:
logg.warning('no passphrase given')
passphrase=''
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
signer_address = None
keystore = DictKeystore()
if args.y != None:
logg.debug('loading keystore file {}'.format(args.y))
signer_address = keystore.import_keystore_file(args.y, password=passphrase)
logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore)
chain_spec = ChainSpec.from_chain_str(args.i)
rpc = EthHTTPConnection(args.p)
nonce_oracle = None
if args.nonce != None:
nonce_oracle = OverrideNonceOracle(signer_address, args.nonce)
else:
nonce_oracle = RPCNonceOracle(signer_address, rpc)
gas_oracle = None
if args.gas_price !=None:
gas_oracle = OverrideGasOracle(price=args.gas_price, conn=rpc, code_callback=TokenUniqueSymbolIndex.gas)
else:
gas_oracle = RPCGasOracle(rpc, code_callback=TokenUniqueSymbolIndex.gas)
dummy = args.d
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
def main():
signer = rpc.get_signer()
signer_address = rpc.get_sender_address()
gas_oracle = rpc.get_gas_oracle()
nonce_oracle = rpc.get_nonce_oracle()
c = TokenUniqueSymbolIndex(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.constructor(signer_address)
if dummy:
print(tx_hash_hex)
print(o)
else:
rpc.do(o)
if block_last:
r = rpc.wait(tx_hash_hex)
if config.get('_RPC_SEND'):
conn.do(o)
if config.get('_WAIT'):
r = conn.wait(tx_hash_hex)
if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1)
@ -111,6 +61,8 @@ def main():
print(address)
else:
print(tx_hash_hex)
else:
print(o)
if __name__ == '__main__':

View File

@ -13,12 +13,11 @@ import argparse
import logging
import hashlib
# third-party imports
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore.dict import DictKeystore
# external imports
import chainlib.eth.cli
from chainlib.chain import ChainSpec
from chainlib.eth.connection import EthHTTPConnection
from eth_erc20 import ERC20
from chainlib.eth.address import to_checksum_address
# local imports
from eth_token_index import TokenUniqueSymbolIndex
@ -30,69 +29,67 @@ default_format = 'terminal'
script_dir = os.path.dirname(__file__)
data_dir = os.path.join(script_dir, '..', 'data')
argparser = argparse.ArgumentParser()
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
argparser.add_argument('-a', '--contract-address', dest='a', type=str, required=True, help='Token endorsement contract address')
argparser.add_argument('-f', '--format', dest='f', type=str, default=default_format, help='Output format [human, brief]')
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
argparser.add_argument('token_symbol', type=str, nargs='?', help='Token symbol to return address for')
args = argparser.parse_args()
if args.vv:
logg.setLevel(logging.DEBUG)
elif args.v:
logg.setLevel(logging.INFO)
extra_args = {
'token_symbol': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=TokenUniqueSymbolIndex.gas())
rpc = EthHTTPConnection(args.p)
contract_address = args.a
#wallet = chainlib.eth.cli.Wallet()
#wallet.from_config(config)
token_symbol = args.token_symbol
fmt = args.f
rpc = chainlib.eth.cli.Rpc()
conn = rpc.connect_by_config(config)
chain_spec = ChainSpec.from_chain_str(args.i)
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
def out_element(e, fmt=default_format, w=sys.stdout):
if fmt == 'brief':
w.write(e[1] + '\n')
else:
w.write('{} {}\n'.format(e[0], e[1]))
def out_element(e, w=sys.stdout):
w.write(e[1] + '\t' + e[0] + '\n')
def element(ifc, contract_address, token_symbol, fmt=fmt, w=sys.stdout):
def element(ifc, conn, contract_address, token_symbol, w=sys.stdout):
o = ifc.address_of(contract_address, token_symbol)
r = rpc.do(o)
r = conn.do(o)
a = ifc.parse_address_of(r)
out_element((token_symbol, a), fmt, w)
out_element((token_symbol, a), w)
def ls(ifc, contract_address, token_ifc, fmt=fmt, w=sys.stdout):
def ls(ifc, conn, contract_address, token_ifc, w=sys.stdout):
o = ifc.entry_count(contract_address)
r = rpc.do(o)
r = conn.do(o)
count = ifc.parse_entry_count(r)
logg.debug('count {}'.format(count))
for i in range(count):
o = ifc.entry(contract_address, i)
r = rpc.do(o)
r = conn.do(o)
token_address = ifc.parse_entry(r)
o = token_ifc.symbol(token_address)
r = rpc.do(o)
r = conn.do(o)
token_symbol = token_ifc.parse_symbol(r)
element(ifc, contract_address, token_symbol, fmt, w)
element(ifc, conn, contract_address, token_symbol, w)
def main():
token_ifc = ERC20(chain_spec)
ifc = TokenUniqueSymbolIndex(chain_spec)
contract_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')):
raise ValueError('invalid checksum address for contract')
token_symbol = config.get('_TOKEN_SYMBOL')
if token_symbol != None:
element(ifc, contract_address, token_symbol, fmt, sys.stdout)
element(ifc, conn, contract_address, token_symbol, sys.stdout)
else:
ls(ifc, contract_address, token_ifc, fmt, sys.stdout)
ls(ifc, conn, contract_address, token_ifc, sys.stdout)
if __name__ == '__main__':