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

85 lines
2.2 KiB
Python
Raw 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
# 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()
2021-07-30 07:27:36 +02:00
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
argparser.add_positional('address', required=False, type=str, help='Check only whether given address is in registry')
2021-04-12 16:25:22 +02:00
args = argparser.parse_args()
2021-07-30 07:27:36 +02:00
extra_args = {
'address': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=AccountRegistry.gas())
2021-04-12 16:25:22 +02:00
2021-07-30 07:27:36 +02:00
wallet = chainlib.eth.cli.Wallet()
wallet.from_config(config)
2021-04-12 16:25:22 +02:00
2021-07-30 07:27:36 +02:00
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
2021-04-12 16:25:22 +02:00
2021-07-30 07:27:36 +02:00
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
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():
2021-07-30 07:27:36 +02:00
address = config.get('_ADDRESS')
contract_address = config.get('_EXEC_ADDRESS')
2021-04-30 13:14:48 +02:00
c = AccountsIndex(chain_spec)
2021-04-12 16:25:22 +02:00
if address != None:
2021-07-30 07:27:36 +02:00
element(c, conn, contract_address, address, w=sys.stdout)
2021-04-12 16:25:22 +02:00
else:
2021-07-30 07:27:36 +02:00
ls(c, conn, contract_address, w=sys.stdout)
2021-04-12 16:25:22 +02:00
if __name__ == '__main__':
main()