eth-contract-registry/python/eth_contract_registry/runnable/list.py

94 lines
2.5 KiB
Python
Raw Normal View History

2021-03-25 09:27:31 +01:00
"""Set identifier value on contract registry
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
import sys
import os
import json
import argparse
import logging
2021-07-30 08:13:47 +02:00
# external imports
import chainlib.eth.cli
2021-03-25 09:27:31 +01: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
2021-07-30 08:13:47 +02:00
from chainlib.eth.address import to_checksum_address
from hexathon import (
add_0x,
strip_0x,
)
2021-03-25 09:27:31 +01:00
# local imports
2021-04-30 22:03:48 +02:00
from eth_contract_registry.registry import ContractRegistry
2021-03-25 09:27:31 +01:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
2021-07-30 08:13:47 +02:00
arg_flags = chainlib.eth.cli.argflag_std_read | chainlib.eth.cli.Flag.EXEC
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
2021-03-25 09:27:31 +01:00
argparser.add_argument('identifier', type=str, nargs='?', help='Token symbol to return address for')
args = argparser.parse_args()
2021-07-30 08:13:47 +02:00
extra_args = {
'identifier': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=ContractRegistry.gas())
2021-03-25 09:27:31 +01:00
2021-07-30 08:13:47 +02:00
wallet = chainlib.eth.cli.Wallet()
wallet.from_config(config)
2021-03-25 09:27:31 +01:00
2021-07-30 08:13:47 +02:00
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
conn = rpc.connect_by_config(config)
2021-03-25 09:27:31 +01:00
2021-07-30 08:13:47 +02:00
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
2021-03-25 09:27:31 +01:00
2021-07-30 08:13:47 +02:00
def out_element(e, w=sys.stdout):
w.write(e[0] + '\t' + e[1] + '\n')
2021-03-25 09:27:31 +01:00
2021-07-30 08:13:47 +02:00
def element(ifc, conn, registry_address, identifier, w=sys.stdout):
2021-03-25 09:27:31 +01:00
o = ifc.address_of(registry_address, identifier)
2021-07-30 08:13:47 +02:00
r = conn.do(o)
2021-03-25 09:27:31 +01:00
address = ifc.parse_address_of(r)
2021-07-30 08:13:47 +02:00
out_element((identifier, address), w)
2021-03-25 09:27:31 +01:00
2021-07-30 08:13:47 +02:00
def ls(ifc, conn, registry_address, w=sys.stdout):
2021-03-25 09:27:31 +01:00
i = 0
while True:
o = ifc.identifier(registry_address, i)
try:
2021-07-30 08:13:47 +02:00
r = conn.do(o)
2021-03-25 09:27:31 +01:00
identifier = ifc.parse_identifier(r)
2021-07-30 08:13:47 +02:00
element(ifc, conn, registry_address, identifier, w)
2021-03-25 09:27:31 +01:00
i += 1
except JSONRPCException:
break
def main():
2021-04-30 22:03:48 +02:00
c = ContractRegistry(chain_spec)
2021-07-30 08:13:47 +02:00
identifier = config.get('_IDENTIFIER')
registry_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
if not config.true('_UNSAFE') and registry_address != add_0x(config.get('_EXEC_ADDRESS')):
raise ValueError('invalid checksum address for contract')
2021-03-25 09:27:31 +01:00
if identifier != None:
2021-07-30 08:13:47 +02:00
element(c, conn, identifier, registry_address, w=sys.stdout)
2021-03-25 09:27:31 +01:00
else:
2021-07-30 08:13:47 +02:00
ls(c, conn, registry_address, w=sys.stdout)
2021-03-25 09:27:31 +01:00
if __name__ == '__main__':
main()