eth-erc20

ERC20 interface and example giftable token contract
Info | Log | Files | Refs | LICENSE

info.py (3804B)


      1 #!python3
      2 
      3 """Token balance query script
      4 
      5 .. moduleauthor:: Louis Holbrook <dev@holbrook.no>
      6 .. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 
      7 
      8 """
      9 
     10 # SPDX-License-Identifier: GPL-3.0-or-later
     11 
     12 # standard imports
     13 import sys
     14 import os
     15 import json
     16 import argparse
     17 import logging
     18 
     19 # external imports
     20 from hexathon import (
     21         add_0x,
     22         strip_0x,
     23         even,
     24         )
     25 import sha3
     26 
     27 # external imports
     28 import chainlib.eth.cli
     29 from chainlib.eth.cli.arg import (
     30         Arg,
     31         ArgFlag,
     32         process_args,
     33         stdin_arg,
     34         )
     35 from chainlib.eth.cli.config import (
     36         Config,
     37         process_config,
     38         )
     39 from chainlib.eth.address import to_checksum_address
     40 from chainlib.eth.connection import EthHTTPConnection
     41 from chainlib.eth.gas import (
     42         OverrideGasOracle,
     43         balance,
     44         )
     45 from chainlib.chain import ChainSpec
     46 from chainlib.eth.settings import process_settings
     47 from chainlib.settings import ChainSettings
     48 from chainlib.eth.cli.log import process_log
     49 
     50 # local imports
     51 from eth_erc20 import ERC20
     52 
     53 logg = logging.getLogger()
     54 
     55 
     56 
     57 def process_config_local(config, arg, args, flags):
     58     contract = None
     59     try:
     60         contract = config.get('_EXEC_ADDRESS')
     61     except KeyError:
     62         pass
     63 
     64     if contract == None:
     65         address = config.get('_POSARG')
     66         if address:
     67             contract = add_0x(address)
     68         else:
     69             contract = stdin_arg()
     70 
     71     config.add(contract, '_CONTRACT', False)
     72     return config
     73 
     74 
     75 arg_flags = ArgFlag()
     76 arg = Arg(arg_flags)
     77 flags = arg_flags.STD_READ | arg_flags.EXEC | arg_flags.TAB | arg_flags.SENDER 
     78 
     79 argparser = chainlib.eth.cli.ArgumentParser()
     80 argparser = process_args(argparser, arg, flags)
     81 argparser.add_argument('contract_address', type=str, help='Token contract address (may also be specified by -e)')
     82 args = argparser.parse_args()
     83 
     84 logg = process_log(args, logg)
     85 
     86 config = Config()
     87 config = process_config(config, arg, args, flags, positional_name='contract_address')
     88 config = process_config_local(config, arg, args, flags)
     89 logg.debug('config loaded:\n{}'.format(config))
     90 
     91 settings = ChainSettings()
     92 settings = process_settings(settings, config)
     93 logg.debug('settings loaded:\n{}'.format(settings))
     94 
     95 
     96 def main():
     97     token_address = config.get('_CONTRACT')
     98     conn = settings.get('CONN')
     99     sender_address = settings.get('SENDER_ADDRESS')
    100     g = ERC20(
    101             chain_spec=settings.get('CHAIN_SPEC'),
    102             gas_oracle=settings.get('GAS_ORACLE'),
    103             )
    104 
    105     outkeys = config.get('_OUTARG')
    106 
    107     if not outkeys or 'address' in outkeys:
    108         name_o = g.name(token_address, sender_address=sender_address)
    109         r = conn.do(name_o)
    110         token_name = g.parse_name(r)
    111         s = ''
    112         if not config.true('_RAW'):
    113             s = 'Name: '
    114         s += token_name
    115         print(s)
    116 
    117     if not outkeys or 'symbol' in outkeys:
    118         symbol_o = g.symbol(token_address, sender_address=sender_address)
    119         r = conn.do(symbol_o)
    120         token_symbol = g.parse_symbol(r)
    121         s = ''
    122         if not config.true('_RAW'):
    123             s = 'Symbol: '
    124         s += token_symbol
    125         print(s)
    126 
    127     if not outkeys or 'decimals' in outkeys:
    128         decimals_o = g.decimals(token_address, sender_address=sender_address)
    129         r = conn.do(decimals_o)
    130         decimals = int(strip_0x(r), 16)
    131         s = ''
    132         if not config.true('_RAW'):
    133             s = 'Decimals: '
    134         s += str(decimals)
    135         print(s)
    136 
    137     if not outkeys or 'supply' in outkeys:
    138         supply_o = g.total_supply(token_address, sender_address=sender_address)
    139         r = conn.do(supply_o)
    140         supply = int(strip_0x(r), 16)
    141         s = ''
    142         if not config.true('_RAW'):
    143             s = 'Supply: '
    144         s += str(supply)
    145         print(s)
    146 
    147 
    148 if __name__ == '__main__':
    149     main()