Rehabilitate cli tools

This commit is contained in:
lash 2023-01-11 13:06:09 +00:00
parent e49493def1
commit 697d2bf227
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 83 additions and 98 deletions

View File

@ -16,17 +16,7 @@ import math
# external imports # external imports
import confini import confini
from funga.eth.signer import EIP155Signer import chainlib.eth.cli
from funga.eth.keystore.dict import DictKeystore
from chainlib.chain import ChainSpec
from chainlib.eth.nonce import (
RPCNonceOracle,
OverrideNonceOracle,
)
from chainlib.eth.gas import (
RPCGasOracle,
OverrideGasOracle,
)
from chainlib.eth.block import ( from chainlib.eth.block import (
block_latest, block_latest,
block_by_number, block_by_number,
@ -35,8 +25,16 @@ from chainlib.eth.block import (
from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt from chainlib.eth.tx import receipt
from chainlib.eth.constant import ZERO_ADDRESS from chainlib.eth.constant import ZERO_ADDRESS
import chainlib.eth.cli
from hexathon import to_int as hex_to_int from hexathon import to_int as hex_to_int
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
# local imports # local imports
import erc20_demurrage_token import erc20_demurrage_token
@ -45,32 +43,35 @@ from erc20_demurrage_token import (
DemurrageTokenSettings, DemurrageTokenSettings,
) )
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
script_dir = os.path.dirname(__file__) def process_config_local(config, arg, args, flags):
data_dir = os.path.join(script_dir, '..', 'data') config.add(args.steps, '_STEPS', False)
config_dir = os.path.join(data_dir, 'config')
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC arg_flags = ArgFlag()
argparser = chainlib.eth.cli.ArgumentParser(arg_flags) 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('--steps', type=int, default=0, help='Max demurrage steps to apply per round') argparser.add_argument('--steps', type=int, default=0, help='Max demurrage steps to apply per round')
args = argparser.parse_args() args = argparser.parse_args()
config = chainlib.eth.cli.Config.from_args(args, arg_flags, default_fee_limit=DemurrageToken.gas(), base_config_dir=config_dir)
config.add(args.steps, '_STEPS', False) logg = process_log(args, logg)
config = Config()
config = process_config(config, arg, args, flags)
config = process_config_local(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config)) logg.debug('config loaded:\n{}'.format(config))
wallet = chainlib.eth.cli.Wallet() settings = ChainSettings()
wallet.from_config(config) settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
def main(): def main():
chain_spec = settings.get('CHAIN_SPEC')
conn = settings.get('CONN')
o = block_latest() o = block_latest()
r = conn.do(o) r = conn.do(o)
@ -87,9 +88,9 @@ def main():
block_start_timestamp = block_start.timestamp block_start_timestamp = block_start.timestamp
block_start_datetime = datetime.datetime.fromtimestamp(block_start_timestamp) block_start_datetime = datetime.datetime.fromtimestamp(block_start_timestamp)
gas_oracle = rpc.get_gas_oracle() gas_oracle = settings.get('FEE_ORACLE')
c = DemurrageToken(chain_spec, gas_oracle=gas_oracle) c = DemurrageToken(chain_spec, gas_oracle=gas_oracle)
o = c.demurrage_timestamp(config.get('_EXEC_ADDRESS')) o = c.demurrage_timestamp(settings.get('EXEC'))
r = conn.do(o) r = conn.do(o)
demurrage_timestamp = None demurrage_timestamp = None
@ -120,17 +121,17 @@ def main():
last_tx_hash = None last_tx_hash = None
for i in range(rounds): for i in range(rounds):
signer = rpc.get_signer() signer = settings.get('SIGNER')
signer_address = rpc.get_sender_address() signer_address = settings.get('SENDER_ADDRESS')
nonce_oracle = rpc.get_nonce_oracle() nonce_oracle = settings.get('NONCE_ORACLE')
c = DemurrageToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) c = DemurrageToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.apply_demurrage(config.get('_EXEC_ADDRESS'), signer_address, limit=config.get('_STEPS')) (tx_hash_hex, o) = c.apply_demurrage(config.get('_EXEC_ADDRESS'), signer_address, limit=config.get('_STEPS'))
if config.get('_RPC_SEND'): if settings.get('RPC_SEND'):
print(tx_hash_hex) print(tx_hash_hex)
conn.do(o) conn.do(o)
if config.get('_WAIT_ALL') or (i == rounds - 1 and config.get('_WAIT')): if config.true('_WAIT_ALL') or (i == rounds - 1 and config.true('_WAIT')):
r = conn.wait(tx_hash_hex) r = conn.wait(tx_hash_hex)
if r['status'] == 0: if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you') sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')

View File

@ -14,21 +14,19 @@ import logging
# external imports # external imports
import confini import confini
from funga.eth.signer import EIP155Signer
from funga.eth.keystore.dict import DictKeystore
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.connection import EthHTTPConnection
from chainlib.eth.tx import receipt from chainlib.eth.tx import receipt
from chainlib.eth.constant import ZERO_ADDRESS from chainlib.eth.constant import ZERO_ADDRESS
import chainlib.eth.cli import chainlib.eth.cli
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
# local imports # local imports
import erc20_demurrage_token import erc20_demurrage_token
@ -37,84 +35,70 @@ from erc20_demurrage_token import (
DemurrageTokenSettings, DemurrageTokenSettings,
) )
logging.basicConfig(level=logging.WARNING) def process_config_local(config, arg, args, flags):
logg = logging.getLogger() config.add(args.name, 'TOKEN_NAME', False)
config.add(args.symbol, 'TOKEN_SYMBOL', False)
config.add(args.decimals, 'TOKEN_DECIMALS', False)
config.add(args.sink_address, 'TOKEN_SINK_ADDRESS', False)
config.add(args.redistribution_period, 'TOKEN_REDISTRIBUTION_PERIOD', False)
config.add(args.demurrage_level, 'TOKEN_DEMURRAGE_LEVEL', False)
#config.add(args.supply_limit, 'TOKEN_DECIMALS', False)
script_dir = os.path.dirname(__file__)
data_dir = os.path.join(script_dir, '..', 'data')
config_dir = os.path.join(data_dir, 'config') arg_flags = ArgFlag()
arg = Arg(arg_flags)
flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.WALLET
arg_flags = chainlib.eth.cli.argflag_std_write argparser = chainlib.eth.cli.ArgumentParser()
argparser = chainlib.eth.cli.ArgumentParser(arg_flags) argparser = process_args(argparser, arg, flags)
argparser.add_argument('--name', dest='token_name', type=str, help='Token name') argparser.add_argument('--name', dest='token_name', type=str, help='Token name')
argparser.add_argument('--symbol', dest='token_symbol', required=True, type=str, help='Token symbol') argparser.add_argument('--symbol', dest='token_symbol', required=True, type=str, help='Token symbol')
argparser.add_argument('--decimals', dest='token_decimals', type=int, help='Token decimals') argparser.add_argument('--decimals', dest='token_decimals', type=int, help='Token decimals')
argparser.add_argument('--sink-address', dest='sink_address', type=str, help='demurrage level,ppm per minute') argparser.add_argument('--sink-address', dest='sink_address', type=str, help='demurrage level,ppm per minute')
argparser.add_argument('--supply-limit', dest='supply_limit', type=int, help='token supply limit (0 = no limit)') #argparser.add_argument('--supply-limit', dest='supply_limit', type=int, help='token supply limit (0 = no limit)')
argparser.add_argument('--redistribution-period', type=int, help='redistribution period, minutes (0 = deactivate)') # default 10080 = week argparser.add_argument('--redistribution-period', dest='redistribution_period', type=int, help='redistribution period, minutes (0 = deactivate)') # default 10080 = week
argparser.add_argument('--multi', action='store_true', help='automatic redistribution') #argparser.add_argument('--multi', action='store_true', help='automatic redistribution')
argparser.add_argument('--demurrage-level', dest='demurrage_level', type=int, help='demurrage level, ppm per minute') argparser.add_argument('--demurrage-level', dest='demurrage_level', type=int, help='demurrage level, ppm per minute')
args = argparser.parse_args() args = argparser.parse_args()
arg_flags = chainlib.eth.cli.argflag_std_write logg = process_log(args, logg)
extra_args = { config = Config()
'redistribution_period': 'TOKEN_REDISTRIBUTION_PERIOD', config = process_config(config, arg, args, flags)
'demurrage_level': 'TOKEN_DEMURRAGE_LEVEL', config = process_config_local(config, arg, args, flags)
'supply_limit': 'TOKEN_SUPPLY_LIMIT',
'token_name': 'TOKEN_NAME',
'token_symbol': 'TOKEN_SYMBOL',
'token_decimals': 'TOKEN_DECIMALS',
'sink_address': 'TOKEN_SINK_ADDRESS',
'multi': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=DemurrageToken.gas(), base_config_dir=config_dir)
if not bool(config.get('TOKEN_NAME')):
logg.info('token name not set, using symbol {} as name'.format(config.get('TOKEN_SYMBOL')))
config.add(config.get('TOKEN_SYMBOL'), 'TOKEN_NAME', True)
if config.get('TOKEN_SUPPLY_LIMIT') == None:
config.add(0, 'TOKEN_SUPPLY_LIMIT', True)
if config.get('TOKEN_REDISTRIBUTION_PERIOD') == None:
config.add(10800, 'TOKEN_REDISTRIBUTION_PERIOD', True)
logg.debug('config loaded:\n{}'.format(config)) logg.debug('config loaded:\n{}'.format(config))
wallet = chainlib.eth.cli.Wallet() settings = ChainSettings()
wallet.from_config(config) settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
def main(): def main():
signer = rpc.get_signer() conn = settings.get('CONN')
signer_address = rpc.get_sender_address() signer = settings.get('SIGNER')
signer_address = settings.get('SENDER_ADDRESS')
gas_oracle = rpc.get_gas_oracle() gas_oracle = settings.get('FEE_ORACLE')
nonce_oracle = rpc.get_nonce_oracle() nonce_oracle = settings.get('NONCE_ORACLE')
c = DemurrageToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) c = DemurrageToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
settings = DemurrageTokenSettings() token_settings = DemurrageTokenSettings()
settings.name = config.get('TOKEN_NAME') token_settings.name = config.get('TOKEN_NAME')
settings.symbol = config.get('TOKEN_SYMBOL') token_settings.symbol = config.get('TOKEN_SYMBOL')
settings.decimals = int(config.get('TOKEN_DECIMALS')) token_settings.decimals = int(config.get('TOKEN_DECIMALS'))
settings.demurrage_level = int(config.get('TOKEN_DEMURRAGE_LEVEL')) token_settings.demurrage_level = int(config.get('TOKEN_DEMURRAGE_LEVEL'))
settings.period_minutes = int(config.get('TOKEN_REDISTRIBUTION_PERIOD')) token_settings.period_minutes = int(config.get('TOKEN_REDISTRIBUTION_PERIOD'))
settings.sink_address = config.get('TOKEN_SINK_ADDRESS') token_settings.sink_address = config.get('TOKEN_SINK_ADDRESS')
(tx_hash_hex, o) = c.constructor( (tx_hash_hex, o) = c.constructor(
signer_address, signer_address,
settings, token_settings,
redistribute=config.true('_MULTI'), redistribute=config.true('_MULTI'),
cap=int(config.get('TOKEN_SUPPLY_LIMIT')), cap=int(config.get('TOKEN_SUPPLY_LIMIT')),
) )
if config.get('_RPC_SEND'): if settings.get('RPC_SEND'):
conn.do(o) conn.do(o)
if config.get('_WAIT'): if config.true('_WAIT'):
r = conn.wait(tx_hash_hex) r = conn.wait(tx_hash_hex)
if r['status'] == 0: if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you') sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')