contract-registry

Ethereum Smart Contract key-value registry
Info | Log | Files | Refs

list.py (3575B)


      1 """Set identifier value on contract registry
      2 
      3 .. moduleauthor:: Louis Holbrook <dev@holbrook.no>
      4 .. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 
      5 
      6 """
      7 
      8 # standard imports
      9 import sys
     10 import os
     11 import logging
     12 
     13 # external imports
     14 import chainlib.eth.cli
     15 from chainlib.chain import ChainSpec
     16 from chainlib.eth.tx import receipt
     17 from chainlib.eth.constant import ZERO_CONTENT
     18 from chainlib.eth.constant import ZERO_ADDRESS
     19 from chainlib.error import JSONRPCException
     20 from chainlib.eth.address import to_checksum_address
     21 from hexathon import (
     22         add_0x,
     23         strip_0x,
     24         )
     25 from chainlib.eth.cli.arg import (
     26         Arg,
     27         ArgFlag,
     28         process_args,
     29         )
     30 from chainlib.eth.cli.config import (
     31         Config,
     32         process_config,
     33         )
     34 from chainlib.eth.cli.log import process_log
     35 from chainlib.eth.settings import process_settings
     36 from chainlib.settings import ChainSettings
     37 
     38 # local imports
     39 from eth_contract_registry.registry import ContractRegistry
     40 
     41 logging.basicConfig(level=logging.WARNING)
     42 logg = logging.getLogger()
     43 
     44 
     45 def process_config_local(config, arg, args, flags):
     46     config.add(config.get('_POSARG'), '_IDENTIFIER')
     47     return config
     48 
     49 
     50 arg_flags = ArgFlag()
     51 arg = Arg(arg_flags)
     52 flags = arg_flags.STD_READ | arg_flags.EXEC
     53 
     54 argparser = chainlib.eth.cli.ArgumentParser()
     55 argparser = process_args(argparser, arg, flags)
     56 argparser.add_argument('identifier', type=str, help='Contract identifier to look up')
     57 args = argparser.parse_args()
     58 
     59 logg = process_log(args, logg)
     60 
     61 config = Config()
     62 config = process_config(config, arg, args, flags, positional_name='identifier')
     63 config = process_config_local(config, arg, args, flags)
     64 logg.debug('config loaded:\n{}'.format(config))
     65 
     66 settings = ChainSettings()
     67 settings = process_settings(settings, config)
     68 logg.debug('settings loaded:\n{}'.format(settings))
     69 
     70 
     71 def out_element(e, w=sys.stdout):
     72     if config.get('_RAW'):
     73         w.write(e[1] + '\n')
     74     else:
     75         w.write(e[0] + '\t' + e[1] + '\n')
     76 
     77 
     78 def element(ifc, conn, registry_address, identifier, w=sys.stdout, sender_address=ZERO_ADDRESS):
     79     o = ifc.address_of(registry_address, identifier, sender_address=sender_address)
     80     r =  conn.do(o)
     81     address = ifc.parse_address_of(r)
     82     out_element((identifier, address), w)
     83 
     84 
     85 def ls(ifc, conn, registry_address, w=sys.stdout, sender_address=ZERO_ADDRESS):
     86     i = 0
     87     while True:
     88         o = ifc.identifier(registry_address, i, sender_address=sender_address)
     89         try:
     90             r =  conn.do(o)
     91             identifier = ifc.parse_identifier(r)
     92             element(ifc, conn, registry_address, identifier, w)
     93             i += 1
     94         except JSONRPCException:
     95             break
     96 
     97 
     98 def main():
     99     c = ContractRegistry(
    100             settings.get('CHAIN_SPEC')
    101             )
    102 
    103     identifier = config.get('_IDENTIFIER')
    104 
    105     registry_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
    106     if not config.true('_UNSAFE') and strip_0x(registry_address) != strip_0x(config.get('_EXEC_ADDRESS')):
    107         raise ValueError('invalid checksum address for contract')
    108 
    109     if identifier != None:
    110         element(
    111             c,
    112             settings.get('CONN'),
    113             settings.get('EXEC'),
    114             config.get('_IDENTIFIER'),
    115             w=sys.stdout,
    116             sender_address=settings.get('SENDER_ADDRESS'),
    117             )
    118     else:
    119         ls(
    120             c,
    121             settings.get('CONN'),
    122             settings.get('EXEC'),
    123             w=sys.stdout,
    124             sender_address=settings.get('SENDER_ADDRESS'),
    125             )
    126 
    127 
    128 if __name__ == '__main__':
    129     main()