eth-erc20

ERC20 interface and example giftable token contract
Log | Files | Refs | LICENSE

commit aec45d7f094b2cc3af3e5299a91a00ee25585375
parent 48156eb7baf77977651d3b353c33aae1c54b20f4
Author: lash <dev@holbrook.no>
Date:   Fri, 13 May 2022 07:43:46 +0000

Implement CLI tools on chainlib 0.3.0 structure

Diffstat:
Mpython/eth_erc20/runnable/balance.py | 57++++++++++++++++++++++++++++++++++++++++-----------------
Mpython/eth_erc20/runnable/info.py | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Mpython/eth_erc20/runnable/transfer.py | 92+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Mpython/requirements.txt | 4++--
Mpython/setup.cfg | 2+-
5 files changed, 146 insertions(+), 84 deletions(-)

diff --git a/python/eth_erc20/runnable/balance.py b/python/eth_erc20/runnable/balance.py @@ -26,6 +26,15 @@ import sha3 # external imports import chainlib.eth.cli +from chainlib.eth.cli.arg import ( + Arg, + ArgFlag, + process_args, + ) +from chainlib.eth.cli.config import ( + Config, + process_config, + ) from chainlib.eth.address import to_checksum_address from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.gas import ( @@ -33,35 +42,49 @@ from chainlib.eth.gas import ( balance, ) from chainlib.chain import ChainSpec +from chainlib.eth.cli.log import process_log +from chainlib.eth.settings import process_settings +from chainlib.settings import ChainSettings # local imports from eth_erc20 import ERC20 -logging.basicConfig(level=logging.WARNING) + +def process_config_local(config, arg, args, flags): + config.add(args.address, '_RECIPIENT', False) + return config + + logg = logging.getLogger() -arg_flags = chainlib.eth.cli.argflag_std_read | chainlib.eth.cli.Flag.EXEC -argparser = chainlib.eth.cli.ArgumentParser(arg_flags) -argparser.add_positional('address', type=str, help='Ethereum address of recipient') -args = argparser.parse_args() -config = chainlib.eth.cli.Config.from_args(args, arg_flags) +arg_flags = ArgFlag() +arg = Arg(arg_flags) +flags = arg_flags.STD_READ | arg_flags.EXEC -wallet = chainlib.eth.cli.Wallet() -wallet.from_config(config) -holder_address = args.address -if wallet.get_signer_address() == None and holder_address != None: - holder_address = wallet.from_address(holder_address) +argparser = chainlib.eth.cli.ArgumentParser() +argparser = process_args(argparser, arg, flags) +argparser.add_argument('address', type=str, help='Ethereum address of recipient') +args = argparser.parse_args() -rpc = chainlib.eth.cli.Rpc() -conn = rpc.connect_by_config(config) +logg = process_log(args, logg) -chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) +config = Config() +config = process_config(config, arg, args, flags) +config = process_config_local(config, arg, args, flags) +logg.debug('config loaded:\n{}'.format(config)) -token_address = config.get('_EXEC_ADDRESS') +settings = ChainSettings() +settings = process_settings(settings, config) +logg.debug('settings loaded:\n{}'.format(settings)) def main(): - g = ERC20(chain_spec=chain_spec, gas_oracle=rpc.get_gas_oracle()) + token_address = settings.get('EXEC') + conn = settings.get('CONN') + g = ERC20( + chain_spec=settings.get('CHAIN_SPEC'), + gas_oracle=settings.get('GAS_ORACLE'), + ) # determine decimals decimals_o = g.decimals(token_address) @@ -80,7 +103,7 @@ def main(): logg.info('symbol {}'.format(token_symbol)) # get balance - balance_o = g.balance(token_address, holder_address) + balance_o = g.balance(token_address, settings.get('RECIPIENT')) r = conn.do(balance_o) hx = strip_0x(r) diff --git a/python/eth_erc20/runnable/info.py b/python/eth_erc20/runnable/info.py @@ -26,6 +26,15 @@ import sha3 # external imports import chainlib.eth.cli +from chainlib.eth.cli.arg import ( + Arg, + ArgFlag, + process_args, + ) +from chainlib.eth.cli.config import ( + Config, + process_config, + ) from chainlib.eth.address import to_checksum_address from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.gas import ( @@ -33,76 +42,98 @@ from chainlib.eth.gas import ( balance, ) from chainlib.chain import ChainSpec +from chainlib.eth.settings import process_settings +from chainlib.settings import ChainSettings +from chainlib.eth.cli.log import process_log # local imports from eth_erc20 import ERC20 -logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() -arg_flags = chainlib.eth.cli.argflag_std_read | chainlib.eth.cli.Flag.EXEC -argparser = chainlib.eth.cli.ArgumentParser(arg_flags) -argparser.add_positional('item', required=False) + +def process_config_local(config, arg, args, flags): + config.add(args.item, '_ITEM', False) + return config + + +arg_flags = ArgFlag() +arg = Arg(arg_flags) +flags = arg_flags.STD_READ | arg_flags.EXEC + +argparser = chainlib.eth.cli.ArgumentParser() +argparser = process_args(argparser, arg, flags) +argparser.add_argument('item', type=str, nargs='?', help='display only given data item') args = argparser.parse_args() -config = chainlib.eth.cli.Config.from_args(args, arg_flags) -rpc = chainlib.eth.cli.Rpc() -conn = rpc.connect_by_config(config) +logg = process_log(args, logg) -chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) +config = Config() +config = process_config(config, arg, args, flags) +config = process_config_local(config, arg, args, flags) +logg.debug('config loaded:\n{}'.format(config)) -token_address = config.get('_EXEC_ADDRESS') +settings = ChainSettings() +settings = process_settings(settings, config) +logg.debug('settings loaded:\n{}'.format(settings)) def main(): - g = ERC20(chain_spec=chain_spec, gas_oracle=rpc.get_gas_oracle()) + token_address = settings.get('EXEC') + item = config.get('_ITEM') + conn = settings.get('CONN') + g = ERC20( + chain_spec=settings.get('CHAIN_SPEC'), + gas_oracle=settings.get('GAS_ORACLE'), + ) + - if not args.item or args.item == 'name': + if not item or item == 'name': name_o = g.name(token_address) r = conn.do(name_o) token_name = g.parse_name(r) s = '' - if not args.item or not args.raw: + if not item or not args.raw: s = 'Name: ' s += token_name print(s) - if args.item == 'name': + if item == 'name': sys.exit(0) - if not args.item or args.item == 'symbol': + if not item or item == 'symbol': symbol_o = g.symbol(token_address) r = conn.do(symbol_o) token_symbol = g.parse_symbol(r) s = '' - if not args.item or not args.raw: + if not item or not args.raw: s = 'Symbol: ' s += token_symbol print(s) - if args.item == 'symbol': + if item == 'symbol': sys.exit(0) - if not args.item or args.item == 'decimals': + if not item or item == 'decimals': decimals_o = g.decimals(token_address) r = conn.do(decimals_o) decimals = int(strip_0x(r), 16) s = '' - if not args.item or not args.raw: + if not item or not args.raw: s = 'Decimals: ' s += str(decimals) print(s) - if args.item == 'decimals': + if item == 'decimals': sys.exit(0) - if not args.item or args.item == 'supply': + if not item or item == 'supply': supply_o = g.total_supply(token_address) r = conn.do(supply_o) supply = int(strip_0x(r), 16) s = '' - if not args.item or not args.raw: + if not item or not args.raw: s = 'Supply: ' s += str(supply) print(s) - if args.item == 'supply': + if item == 'supply': sys.exit(0) diff --git a/python/eth_erc20/runnable/transfer.py b/python/eth_erc20/runnable/transfer.py @@ -26,39 +26,52 @@ from chainlib.chain import ChainSpec from chainlib.eth.runnable.util import decode_for_puny_humans from chainlib.eth.address import to_checksum_address import chainlib.eth.cli +from chainlib.eth.cli.log import process_log +from chainlib.eth.settings import process_settings +from chainlib.settings import ChainSettings +from chainlib.eth.cli.arg import ( + Arg, + ArgFlag, + process_args, + ) +from chainlib.eth.cli.config import ( + Config, + process_config, + ) # local imports from eth_erc20 import ERC20 -logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() -arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC | chainlib.eth.cli.Flag.WALLET -argparser = chainlib.eth.cli.ArgumentParser(arg_flags) -argparser.add_positional('amount', type=int, help='Token amount to send') -args = argparser.parse_args() -extra_args = { - 'amount': None, - } -config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=100000) -block_all = args.ww -block_last = args.w or block_all +def process_config_local(config, arg, args, flags): + config.add(args.amount, '_VALUE', False) + return config -wallet = chainlib.eth.cli.Wallet() -wallet.from_config(config) -rpc = chainlib.eth.cli.Rpc(wallet=wallet) -conn = rpc.connect_by_config(config) +arg_flags = ArgFlag() +arg = Arg(arg_flags) +flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.WALLET + +argparser = chainlib.eth.cli.ArgumentParser() +argparser = process_args(argparser, arg, flags) +argparser.add_argument('amount', type=str, help='Token amount to send') +args = argparser.parse_args() -chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) +logg = process_log(args, logg) -value = config.get('_AMOUNT') +config = Config() +config = process_config(config, arg, args, flags) +config = process_config_local(config, arg, args, flags) +logg.debug('config loaded:\n{}'.format(config)) -send = config.true('_RPC_SEND') +settings = ChainSettings() +settings = process_settings(settings, config) +logg.debug('settings loaded:\n{}'.format(settings)) -def balance(generator, token_address, address, id_generator=None): +def balance(conn, generator, token_address, address, id_generator=None): o = generator.balance(token_address, address, id_generator=id_generator) r = conn.do(o) token_balance = generator.parse_balance(r) @@ -66,37 +79,32 @@ def balance(generator, token_address, address, id_generator=None): def main(): - signer = rpc.get_signer() - signer_address = rpc.get_sender_address() - - gas_oracle = rpc.get_gas_oracle() - nonce_oracle = rpc.get_nonce_oracle() - - g = ERC20(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) - - recipient = to_checksum_address(config.get('_RECIPIENT')) - if not config.true('_UNSAFE') and recipient != add_0x(config.get('_RECIPIENT')): - raise ValueError('invalid checksum address for recipient') - - token_address = to_checksum_address(config.get('_EXEC_ADDRESS')) - if not config.true('_UNSAFE') and token_address != add_0x(config.get('_EXEC_ADDRESS')): - raise ValueError('invalid checksum address for contract') - + token_address = settings.get('EXEC') + signer_address = settings.get('SENDER_ADDRESS') + recipient = settings.get('RECIPIENT') + value = settings.get('VALUE') + conn = settings.get('CONN') + g = ERC20( + settings.get('CHAIN_SPEC'), + signer=settings.get('SIGNER'), + gas_oracle=settings.get('GAS_ORACLE'), + nonce_oracle=settings.get('NONCE_ORACLE'), + ) if logg.isEnabledFor(logging.DEBUG): - sender_balance = balance(g, token_address, signer_address, id_generator=rpc.id_generator) - recipient_balance = balance(g, token_address, recipient, id_generator=rpc.id_generator) + sender_balance = balance(conn, g, token_address, signer_address, id_generator=settings.get('RPC_ID_GENERATOR')) + recipient_balance = balance(conn, g, token_address, recipient, id_generator=settings.get('RPC_ID_GENERATOR')) logg.debug('sender {} balance before: {}'.format(signer_address, sender_balance)) logg.debug('recipient {} balance before: {}'.format(recipient, recipient_balance)) - (tx_hash_hex, o) = g.transfer(token_address, signer_address, recipient, value, id_generator=rpc.id_generator) + (tx_hash_hex, o) = g.transfer(token_address, signer_address, recipient, value, id_generator=settings.get('RPC_ID_GENERATOR')) - if send: + if settings.get('RPC_SEND'): conn.do(o) - if block_last: + if settings.get('WAIT'): r = conn.wait(tx_hash_hex) if logg.isEnabledFor(logging.DEBUG): - sender_balance = balance(g, token_address, signer_address, id_generator=rpc.id_generator) - recipient_balance = balance(g, token_address, recipient, id_generator=rpc.id_generator) + sender_balance = balance(conn, g, token_address, signer_address, id_generator=settings.get('RPC_ID_GENERATOR')) + recipient_balance = balance(conn, g, token_address, recipient, id_generator=settings.get('RPC_ID_GENERATOR')) logg.debug('sender {} balance after: {}'.format(signer_address, sender_balance)) logg.debug('recipient {} balance after: {}'.format(recipient, recipient_balance)) if r['status'] == 0: diff --git a/python/requirements.txt b/python/requirements.txt @@ -1,3 +1,3 @@ -confini>=0.5.7,<0.7.0 -chainlib-eth~=0.2.0 +confini~=0.6.1 +chainlib-eth~=0.3.0 potaahto~=0.1.1 diff --git a/python/setup.cfg b/python/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = eth-erc20 -version = 0.3.1 +version = 0.3.2 description = ERC20 interface and simple contract with deployment script that lets any address mint and gift itself tokens. author = Louis Holbrook author_email = dev@holbrook.no