eth-token-index

Token symbol to address unique index
Info | Log | Files | Refs

list.py (4394B)


      1 """Adds a new token to the token symbol index
      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 json
     12 import argparse
     13 import logging
     14 import hashlib
     15 
     16 # external imports
     17 import chainlib.eth.cli
     18 from chainlib.chain import ChainSpec
     19 from eth_erc20 import ERC20
     20 from chainlib.eth.address import to_checksum_address
     21 from hexathon import add_0x
     22 from chainlib.eth.cli.arg import (
     23         Arg,
     24         ArgFlag,
     25         process_args,
     26         )
     27 from chainlib.eth.cli.config import (
     28         Config,
     29         process_config,
     30         )
     31 from chainlib.eth.cli.log import process_log
     32 from chainlib.eth.settings import process_settings
     33 from chainlib.settings import ChainSettings
     34 from chainlib.eth.address import is_same_address
     35 from chainlib.eth.constant import ZERO_ADDRESS
     36 
     37 # local imports
     38 from eth_token_index import TokenUniqueSymbolIndex
     39 
     40 logging.basicConfig(level=logging.WARNING)
     41 logg = logging.getLogger()
     42 default_format = 'terminal'
     43 
     44 script_dir = os.path.dirname(__file__)
     45 data_dir = os.path.join(script_dir, '..', 'data')
     46 
     47 arg_flags = ArgFlag()
     48 arg = Arg(arg_flags)
     49 flags = arg_flags.STD_READ | arg_flags.EXEC
     50 
     51 argparser = chainlib.eth.cli.ArgumentParser()
     52 argparser.add_argument('token_symbol', type=str, nargs='?', help='Token symbol to return address for')
     53 argparser = process_args(argparser, arg, flags)
     54 args = argparser.parse_args()
     55 
     56 logg = process_log(args, logg)
     57 
     58 extra_args = {
     59     'token_symbol': None,
     60         }
     61 #config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=TokenUniqueSymbolIndex.gas())
     62 config = Config()
     63 config = process_config(config, arg, args, flags, positional_name='token_symbol')
     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[1] + '\t' + e[0] + '\n')
     76 
     77 
     78 def element(ifc, conn, contract_address, token_symbol, w=sys.stdout):
     79     o = ifc.address_of(contract_address, token_symbol)
     80     r = conn.do(o)
     81     a = ifc.parse_address_of(r)
     82     out_element((token_symbol, a), w)
     83 
     84 
     85 def ls(ifc, conn, contract_address, token_ifc, w=sys.stdout):
     86     o = ifc.entry_count(contract_address)
     87     r = conn.do(o)
     88     count = ifc.parse_entry_count(r)
     89     logg.debug('count {}'.format(count))
     90 
     91     for i in range(count):
     92         o = ifc.entry(contract_address, i)
     93         r = conn.do(o)
     94         token_address = ifc.parse_entry(r)
     95 
     96         o = token_ifc.symbol(token_address)
     97         r = conn.do(o)
     98         token_symbol = token_ifc.parse_symbol(r)
     99 
    100         o = ifc.address_of(contract_address, token_symbol)
    101         r = conn.do(o)
    102         reverse_token_address = ifc.parse_entry(r)
    103         logg.debug('checking token idx {} symbol {} address {} reverse address {}'.format(i, token_symbol, token_address, reverse_token_address))
    104         if is_same_address(token_address, ZERO_ADDRESS):
    105             logg.warning('token idx {} {} was registered with zero-address'.format(i, token_symbol))
    106             continue
    107         if is_same_address(reverse_token_address, ZERO_ADDRESS):
    108             raise ValueError('token idx {} {} has entry but zero-address reverse lookup. Are you using the correct reverse lookup keys?'.format(i, token_symbol))
    109         if not is_same_address(token_address, reverse_token_address):
    110             logg.info('token idx {} {} address {} does not match reverse address {}, skipping'.format(i, token_symbol, token_address, reverse_token_address))
    111             continue
    112 
    113         element(ifc, conn, contract_address, token_symbol, w)
    114 
    115 
    116 def main():
    117     conn = settings.get('CONN')
    118     token_ifc = ERC20(settings.get('CHAIN_SPEC'))
    119     ifc = TokenUniqueSymbolIndex(settings.get('CHAIN_SPEC'))
    120 
    121     contract_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
    122     if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')):
    123         raise ValueError('invalid checksum address for contract')
    124 
    125     token_symbol = config.get('_POSARG')
    126     if token_symbol != None:
    127         element(ifc, conn, contract_address, token_symbol, sys.stdout)
    128     else:
    129         ls(ifc, conn, contract_address, token_ifc, sys.stdout)
    130 
    131 
    132 if __name__ == '__main__':
    133     main()