Implement chainlib cli util for transfer, view, tag

This commit is contained in:
nolash 2021-07-24 15:07:21 +02:00
parent f77e2fd4dc
commit 127a16ac07
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 40 additions and 92 deletions

View File

@ -7,7 +7,6 @@ import json
import argparse import argparse
# external imports # external imports
import confini
import redis import redis
from xdg.BaseDirectory import xdg_config_home from xdg.BaseDirectory import xdg_config_home
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
@ -18,8 +17,6 @@ from cic_eth.api import Api
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
logging.getLogger('confini').setLevel(logging.WARNING)
logging.getLogger('gnupg').setLevel(logging.WARNING)
arg_flags = cic_eth.cli.argflag_std_base arg_flags = cic_eth.cli.argflag_std_base
local_arg_flags = cic_eth.cli.argflag_local_taskcallback local_arg_flags = cic_eth.cli.argflag_local_taskcallback

View File

@ -18,8 +18,6 @@ from cic_eth.db.enum import LockEnum
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
logging.getLogger('confini').setLevel(logging.WARNING)
logging.getLogger('gnupg').setLevel(logging.WARNING)
arg_flags = cic_eth.cli.argflag_std_read arg_flags = cic_eth.cli.argflag_std_read

View File

@ -18,8 +18,6 @@ from cic_eth.api.admin import AdminApi
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
logging.getLogger('confini').setLevel(logging.WARNING)
logging.getLogger('gnupg').setLevel(logging.WARNING)
arg_flags = cic_eth.cli.argflag_std_base arg_flags = cic_eth.cli.argflag_std_base
local_arg_flags = cic_eth.cli.argflag_local_taskcallback local_arg_flags = cic_eth.cli.argflag_local_taskcallback
@ -34,6 +32,7 @@ celery_app = cic_eth.cli.CeleryApp.from_config(config)
api = Api(config.get('CHAIN_SPEC'), queue=config.get('CELERY_QUEUE')) api = Api(config.get('CHAIN_SPEC'), queue=config.get('CELERY_QUEUE'))
admin_api = AdminApi(None) admin_api = AdminApi(None)
def main(): def main():
t = admin_api.registry() t = admin_api.registry()
registry_address = t.get() registry_address = t.get()

View File

@ -6,8 +6,7 @@ import argparse
import re import re
# external imports # external imports
import celery import cic_eth.cli
import confini
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
from xdg.BaseDirectory import xdg_config_home from xdg.BaseDirectory import xdg_config_home
@ -19,43 +18,28 @@ from cic_eth.db.models.base import SessionBase
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
default_config_dir = os.environ.get('CONFINI_DIR', '/usr/local/etc/cic') arg_flags = cic_eth.cli.argflag_std_base
local_arg_flags = cic_eth.cli.argflag_local_taskcallback
argparser = cic_eth.cli.ArgumentParser(arg_flags)
argparser.add_positional('tag', type=str, help='address tag')
argparser.add_positional('address', type=str, help='address')
argparser.process_local_flags(local_arg_flags)
args = argparser.parse_args()
config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags)
argparser = argparse.ArgumentParser(description='daemon that monitors transactions in new blocks') celery_app = cic_eth.cli.CeleryApp.from_config(config)
argparser.add_argument('-p', '--provider', dest='p', type=str, help='Web3 provider url (http only)')
argparser.add_argument('-c', type=str, default=default_config_dir, help='config root to use')
argparser.add_argument('-v', help='be verbose', action='store_true')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, help='chain spec')
argparser.add_argument('-vv', help='be more verbose', action='store_true')
argparser.add_argument('tag', type=str, help='address tag')
argparser.add_argument('address', type=str, help='address')
args = argparser.parse_args(sys.argv[1:])
if args.v == True: admin_api = AdminApi(None)
logging.getLogger().setLevel(logging.INFO)
elif args.vv == True:
logging.getLogger().setLevel(logging.DEBUG)
config = confini.Config(args.c) chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
config.process()
args_override = {
'ETH_PROVIDER': getattr(args, 'p'),
'CIC_CHAIN_SPEC': getattr(args, 'i'),
}
config.dict_override(args_override, 'cli flag')
config.censor('PASSWORD', 'DATABASE')
config.censor('PASSWORD', 'SSL')
logg.debug('config loaded from {}\n{}'.format(args.c, config))
chain_spec = ChainSpec.from_chain_str(args.i) celery_app = cic_eth.cli.CeleryApp.from_config(config)
api = AdminApi(None)
celery_app = celery.Celery(broker=config.get('CELERY_BROKER_URL'), backend=config.get('CELERY_RESULT_URL'))
def main(): def main():
api = AdminApi(None) admin_api.tag_account(args.tag, args.address, chain_spec)
api.tag_account(args.tag, args.address, chain_spec)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -7,8 +7,7 @@ import json
import argparse import argparse
# external imports # external imports
import celery import cic_eth.cli
import confini
import redis import redis
from xdg.BaseDirectory import xdg_config_home from xdg.BaseDirectory import xdg_config_home
from chainlib.eth.address import to_checksum_address from chainlib.eth.address import to_checksum_address
@ -18,77 +17,48 @@ from cic_eth.api import Api
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger('create_account_script') logg = logging.getLogger('create_account_script')
logging.getLogger('confini').setLevel(logging.WARNING)
logging.getLogger('gnupg').setLevel(logging.WARNING)
default_config_dir = os.environ.get('CONFINI_DIR', '/usr/local/etc/cic') arg_flags = cic_eth.cli.argflag_std_base
local_arg_flags = cic_eth.cli.argflag_local_taskcallback
argparser = argparse.ArgumentParser() argparser = cic_eth.cli.ArgumentParser(arg_flags)
argparser.add_argument('--no-register', dest='no_register', action='store_true', help='Do not register new account in on-chain accounts index') argparser.add_argument('--token-symbol', dest='token_symbol', type=str, help='Token symbol')
argparser.add_argument('-c', type=str, default=default_config_dir, help='config file') argparser.add_positional('sender', type=str, help='Token transaction sender')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, help='chain spec') argparser.add_positional('recipient', type=str, help='Token transaction recipient')
argparser.add_argument('--token-symbol', dest='token_symbol', type=str, help='Symbol of token to transfer') argparser.add_positional('value', type=int, help='Token transaction value')
argparser.add_argument('--redis-host', dest='redis_host', type=str, help='redis host to use for task submission') argparser.process_local_flags(local_arg_flags)
argparser.add_argument('--redis-port', dest='redis_port', type=int, help='redis host to use for task submission')
argparser.add_argument('--redis-db', dest='redis_db', type=int, help='redis db to use for task submission and callback')
argparser.add_argument('--redis-host-callback', dest='redis_host_callback', default='localhost', type=str, help='redis host to use for callback')
argparser.add_argument('--redis-port-callback', dest='redis_port_callback', default=6379, type=int, help='redis port to use for callback')
argparser.add_argument('--timeout', default=20.0, type=float, help='Callback timeout')
argparser.add_argument('-q', type=str, default='cic-eth', help='Task queue')
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('sender', type=str, help='Transaction sender')
argparser.add_argument('recipient', type=str, help='Transaction recipient')
argparser.add_argument('value', type=int, help='Transaction value with decimals')
args = argparser.parse_args() args = argparser.parse_args()
if args.vv: extra_args = {
logg.setLevel(logging.DEBUG) 'token_symbol': None,
if args.v: 'sender': None,
logg.setLevel(logging.INFO) 'recipient': None,
'value': None,
config_dir = args.c
config = confini.Config(config_dir, os.environ.get('CONFINI_ENV_PREFIX'))
config.process()
args_override = {
'CIC_CHAIN_SPEC': getattr(args, 'i'),
'REDIS_HOST': getattr(args, 'redis_host'),
'REDIS_PORT': getattr(args, 'redis_port'),
'REDIS_DB': getattr(args, 'redis_db'),
} }
config.dict_override(args_override, 'cli') config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags, extra_args=extra_args)
config.add(to_checksum_address(args.sender), '_SENDER', True)
config.add(to_checksum_address(args.recipient), '_RECIPIENT', True) celery_app = cic_eth.cli.CeleryApp.from_config(config)
config.add(args.value, '_VALUE', True)
config.add(args.token_symbol, '_SYMBOL', True)
if config.get('_SYMBOL') == None:
raise ValueError('gas transfers not yet supported; token symbol required')
celery_app = celery.Celery(broker=config.get('CELERY_BROKER_URL'), backend=config.get('CELERY_RESULT_URL'))
def main(): def main():
redis_host = config.get('REDIS_HOST')
redis_port = config.get('REDIS_PORT')
redis_db = config.get('REDIS_DB')
redis_channel = str(uuid.uuid4()) redis_channel = str(uuid.uuid4())
r = redis.Redis(redis_host, redis_port, redis_db) r = redis.Redis(config.get('REDIS_HOST'), config.get('REDIS_PORT'), config.get('REDIS_DB'))
ps = r.pubsub() ps = r.pubsub()
ps.subscribe(redis_channel) ps.subscribe(redis_channel)
ps.get_message() ps.get_message()
api = Api( api = Api(
config.get('CIC_CHAIN_SPEC'), config.get('CHAIN_SPEC'),
queue=args.q, queue=config.get('CELERY_QUEUE'),
callback_param='{}:{}:{}:{}'.format(args.redis_host_callback, args.redis_port_callback, redis_db, redis_channel), callback_param='{}:{}:{}:{}'.format(config.get('_REDIS_HOST_CALLBACK'), config.get('_REDIS_PORT_CALLBACK'), config.get('REDIS_DB'), redis_channel),
callback_task='cic_eth.callbacks.redis.redis', callback_task='cic_eth.callbacks.redis.redis',
callback_queue=args.q, callback_queue=config.get('CELERY_QUEUE')
) )
t = api.transfer(config.get('_SENDER'), config.get('_RECIPIENT'), config.get('_VALUE'), config.get('_SYMBOL')) t = api.transfer(config.get('_SENDER'), config.get('_RECIPIENT'), config.get('_VALUE'), config.get('_TOKEN_SYMBOL'))
ps.get_message() ps.get_message()
o = ps.get_message(timeout=args.timeout) o = ps.get_message(timeout=config.get('REDIS_TIMEOUT'))
m = json.loads(o['data']) m = json.loads(o['data'])
print(m['result']) print(m['result'])