eth-token-index

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

writer.py (2705B)


      1 """Adds/removes writers to accounts index
      2 
      3 .. moduleauthor:: Louis Holbrook <dev@holbrook.no>
      4 .. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 
      5 
      6 """
      7 
      8 # standard imports
      9 import os
     10 import json
     11 import argparse
     12 import logging
     13 import sys
     14 
     15 # external imports
     16 import chainlib.eth.cli
     17 from chainlib.chain import ChainSpec
     18 from chainlib.eth.connection import EthHTTPConnection
     19 from chainlib.eth.tx import receipt
     20 from chainlib.eth.address import to_checksum_address
     21 from hexathon import add_0x
     22 
     23 # local imports
     24 from eth_token_index.index import TokenUniqueSymbolIndex
     25 
     26 logging.basicConfig(level=logging.WARNING)
     27 logg = logging.getLogger()
     28 
     29 arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
     30 argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
     31 argparser.add_argument('--rm', action='store_true', help='Delete entry')
     32 argparser.add_positional('address', type=str, help='Update writer array with this address')
     33 args = argparser.parse_args()
     34 
     35 extra_args = {
     36     'rm': None,
     37     'address': None,
     38         }
     39 config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=TokenUniqueSymbolIndex.gas())
     40 
     41 wallet = chainlib.eth.cli.Wallet()
     42 wallet.from_config(config)
     43 
     44 rpc = chainlib.eth.cli.Rpc(wallet=wallet)
     45 conn = rpc.connect_by_config(config)
     46 
     47 chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
     48 
     49 
     50 def main():
     51     signer = rpc.get_signer()
     52     signer_address = rpc.get_sender_address()
     53 
     54     gas_oracle = rpc.get_gas_oracle()
     55     nonce_oracle = rpc.get_nonce_oracle()
     56 
     57     c = TokenUniqueSymbolIndex(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
     58 
     59     subject_address = to_checksum_address(config.get('_ADDRESS'))
     60     if not config.true('_UNSAFE') and subject_address != add_0x(config.get('_ADDRESS')):
     61         raise ValueError('invalid checksum address for subject_address')
     62 
     63 
     64     contract_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
     65     if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')):
     66         raise ValueError('invalid checksum address for contract')
     67 
     68     if config.get('_RM'):
     69         (tx_hash_hex, o) = c.delete_writer(contract_address, signer_address, subject_address)
     70     else:
     71         (tx_hash_hex, o) = c.add_writer(contract_address, signer_address, subject_address)
     72     if config.get('_RPC_SEND'):
     73         conn.do(o)
     74         if config.get('_WAIT'):
     75             r = conn.wait(tx_hash_hex)
     76             if r['status'] == 0:
     77                 sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
     78                 sys.exit(1)
     79 
     80         print(tx_hash_hex)
     81     else:
     82         print(o)
     83 
     84 
     85 if __name__ == '__main__':
     86     main()