eth-accounts-index/python/eth_accounts_index/runnable/list.py

122 lines
3.0 KiB
Python
Raw Permalink Normal View History

2021-04-12 16:25:22 +02:00
"""Query account index state
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
import sys
import os
import json
import argparse
import logging
2021-07-30 07:27:36 +02:00
# external imports
import chainlib.eth.cli
2021-04-12 16:25:22 +02:00
from chainlib.chain import ChainSpec
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
from chainlib.eth.constant import ZERO_CONTENT
from chainlib.error import JSONRPCException
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
from chainlib.eth.cli.log import process_log
from chainlib.eth.settings import process_settings
from chainlib.settings import ChainSettings
from chainlib.eth.address import to_checksum_address
2021-04-12 16:25:22 +02:00
# local imports
2021-04-30 13:14:48 +02:00
from eth_accounts_index import AccountsIndex
2021-07-30 07:27:36 +02:00
from eth_accounts_index.registry import AccountRegistry
2021-04-12 16:25:22 +02:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
def process_config_local(config, arg, args, flags):
address = config.get('_POSARG')
if address != None:
address = to_checksum_address(address)
config.add(address, '_ADDRESS')
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('address', type=str, help='Address to add to registry')
args = argparser.parse_args()
2021-04-12 16:25:22 +02:00
logg = process_log(args, logg)
2021-04-12 16:25:22 +02:00
config = Config()
config = process_config(config, arg, args, flags, positional_name='address')
config = process_config_local(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config))
2021-04-12 16:25:22 +02:00
settings = ChainSettings()
settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
2021-04-12 16:25:22 +02:00
2021-07-30 07:27:36 +02:00
def out_element(e, w=sys.stdout):
w.write(str(e[1]) + '\n')
2021-04-12 16:25:22 +02:00
2021-07-30 07:27:36 +02:00
def element(ifc, conn, contract_address, address, w=sys.stdout):
o = ifc.have(contract_address, address)
r = conn.do(o)
2021-04-12 16:25:22 +02:00
have = ifc.parse_have(r)
2021-07-30 07:27:36 +02:00
out_element((0, address), w)
2021-04-12 16:25:22 +02:00
2021-07-30 07:27:36 +02:00
def ls(ifc, conn, contract_address, w=sys.stdout):
2021-05-14 16:35:23 +02:00
i = 0
2021-04-12 16:25:22 +02:00
while True:
2021-07-30 07:27:36 +02:00
o = ifc.entry(contract_address, i)
2021-04-12 16:25:22 +02:00
try:
2021-07-30 07:27:36 +02:00
r = conn.do(o)
2021-04-12 16:25:22 +02:00
account = ifc.parse_account(r)
2021-07-30 07:27:36 +02:00
out_element((i, account), w)
2021-04-12 16:25:22 +02:00
i += 1
except JSONRPCException as e:
break
def main():
conn = settings.get('CONN')
2021-07-30 07:27:36 +02:00
address = config.get('_ADDRESS')
c = AccountsIndex(
settings.get('CHAIN_SPEC')
)
2021-04-12 16:25:22 +02:00
if address != None:
element(
c,
conn,
settings.get('EXEC'),
address,
w=sys.stdout,
)
2021-04-12 16:25:22 +02:00
else:
ls(
c,
conn,
settings.get('EXEC'),
w=sys.stdout,
)
2021-04-12 16:25:22 +02:00
if __name__ == '__main__':
main()