eth-erc20

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

balance.py (3344B)


      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         )
     34 from chainlib.eth.cli.config import (
     35         Config,
     36         process_config,
     37         )
     38 from chainlib.eth.address import to_checksum_address
     39 from chainlib.eth.connection import EthHTTPConnection
     40 from chainlib.eth.gas import (
     41         OverrideGasOracle,
     42         balance,
     43         )
     44 from chainlib.chain import ChainSpec
     45 from chainlib.eth.cli.log import process_log
     46 from chainlib.eth.settings import process_settings
     47 from chainlib.settings import ChainSettings
     48 
     49 # local imports
     50 from eth_erc20 import ERC20
     51 
     52 
     53 def process_config_local(config, arg, args, flags):
     54     recipient = None
     55     address = config.get('_POSARG')
     56     if address:
     57         recipient = add_0x(address)
     58     else:
     59         recipient = stdin_arg()
     60     config.add(recipient, '_RECIPIENT', False)
     61     return config
     62 
     63 
     64 logg = logging.getLogger()
     65 
     66 arg_flags = ArgFlag()
     67 arg = Arg(arg_flags)
     68 flags = arg_flags.STD_READ | arg_flags.EXEC | arg_flags.SENDER
     69 
     70 argparser = chainlib.eth.cli.ArgumentParser()
     71 argparser = process_args(argparser, arg, flags)
     72 argparser.add_argument('address', type=str, help='Ethereum address of recipient')
     73 args = argparser.parse_args()
     74 
     75 logg = process_log(args, logg)
     76 
     77 config = Config()
     78 config = process_config(config, arg, args, flags, positional_name='address')
     79 config = process_config_local(config, arg, args, flags)
     80 logg.debug('config loaded:\n{}'.format(config))
     81 
     82 settings = ChainSettings()
     83 settings = process_settings(settings, config)
     84 logg.debug('settings loaded:\n{}'.format(settings))
     85 
     86 
     87 def main():
     88     token_address = settings.get('EXEC')
     89     conn = settings.get('CONN')
     90     sender_address = settings.get('SENDER_ADDRESS')
     91     g = ERC20(
     92             chain_spec=settings.get('CHAIN_SPEC'),
     93             gas_oracle=settings.get('GAS_ORACLE'),
     94             )
     95 
     96     # determine decimals
     97     if not config.get('_RAW'):
     98         decimals_o = g.decimals(token_address, sender_address=sender_address)
     99         r = conn.do(decimals_o)
    100         decimals = int(strip_0x(r), 16)
    101         logg.info('decimals {}'.format(decimals))
    102 
    103     # get balance
    104     balance_o = g.balance(token_address, settings.get('RECIPIENT'), sender_address=sender_address)
    105     r = conn.do(balance_o)
    106    
    107     hx = strip_0x(r)
    108     balance_value = int(hx, 16)
    109     if config.get('_RAW'):
    110         logg.debug('balance {} = {}'.format(even(hx), balance_value))
    111     else:
    112         logg.debug('balance {} = {} decimals {}'.format(even(hx), balance_value, decimals))
    113 
    114     balance_str = str(balance_value)
    115     balance_len = len(balance_str)
    116     if config.get('_RAW'):
    117         print(balance_str)
    118     else:
    119         if balance_len < decimals + 1:
    120             print('0.{}'.format(balance_str.zfill(decimals)))
    121         else:
    122             offset = balance_len-decimals
    123             print('{}.{}'.format(balance_str[:offset],balance_str[offset:]))
    124 
    125 
    126 if __name__ == '__main__':
    127     main()