chainlib-eth

Ethereum implementation of the chainlib interface
Info | Log | Files | Refs | README | LICENSE

balance.py (2815B)


      1 # SPDX-License-Identifier: GPL-3.0-or-later
      2 
      3 # standard imports
      4 import os
      5 import logging
      6 
      7 # external imports
      8 from hexathon import (
      9         add_0x,
     10         strip_0x,
     11         even,
     12         )
     13 from chainlib.settings import ChainSettings
     14 from chainlib.chain import ChainSpec
     15 from funga.eth.signer import EIP155Signer
     16 from chainlib.jsonrpc import (
     17         jsonrpc_result,
     18         IntSequenceGenerator,
     19         )
     20 
     21 # local imports
     22 import chainlib.eth.cli
     23 from chainlib.eth.cli.arg import (
     24         Arg,
     25         ArgFlag,
     26         process_args,
     27         stdin_arg,
     28         )
     29 from chainlib.eth.cli.config import (
     30         Config,
     31         process_config,
     32         )
     33 from chainlib.eth.cli.log import process_log
     34 from chainlib.eth.address import AddressChecksum
     35 from chainlib.eth.connection import EthHTTPConnection
     36 from chainlib.eth.gas import (
     37         OverrideGasOracle,
     38         balance,
     39         )
     40 from chainlib.eth.settings import process_settings
     41 from chainlib.eth.jsonrpc import to_blockheight_param
     42 
     43 
     44 logg = logging.getLogger()
     45 
     46 script_dir = os.path.dirname(os.path.realpath(__file__)) 
     47 
     48 
     49 def process_config_local(config, arg, args, flags):
     50     recipient = None
     51     address = config.get('_POSARG')
     52     if address:
     53         recipient = add_0x(address)
     54     else:
     55         recipient = stdin_arg()
     56     config.add(recipient, '_RECIPIENT', False)
     57     return config
     58 
     59 
     60 arg_flags = ArgFlag()
     61 arg = Arg(arg_flags)
     62 flags = arg_flags.STD_READ
     63 
     64 argparser = chainlib.eth.cli.ArgumentParser()
     65 argparser = process_args(argparser, arg, flags)
     66 argparser.add_argument('address', type=str, help='Ethereum address of recipient')
     67 
     68 args = argparser.parse_args()
     69 
     70 logg = process_log(args, logg)
     71 logg.debug('flags {} {} {}'.format(flags, arg_flags.SEQ, flags & arg_flags.SEQ))
     72 
     73 config = Config()
     74 config = process_config(config, arg, args, flags, positional_name='address')
     75 config = process_config_local(config, arg, args, flags)
     76 logg.debug('config loaded:\n{}'.format(config))
     77 
     78 settings = ChainSettings()
     79 settings = process_settings(settings, config)
     80 
     81 
     82 def main():
     83     r = None
     84     decimals = 18
     85 
     86     height = to_blockheight_param(config.get('_HEIGHT'))
     87     o = balance(settings.get('RECIPIENT'), id_generator=settings.get('RPC_ID_GENERATOR'), height=height)
     88     r = settings.get('CONN').do(o)
     89    
     90     hx = strip_0x(r)
     91     balance_value = int(hx, 16)
     92     logg.debug('balance {} = {} decimals {}'.format(even(hx), balance_value, decimals))
     93 
     94     balance_str = str(balance_value)
     95     balance_len = len(balance_str)
     96     if config.get('_RAW'):
     97         print(balance_str)
     98     else:
     99         if balance_len < decimals + 1:
    100             print('0.{}'.format(balance_str.zfill(decimals)))
    101         else:
    102             offset = balance_len-decimals
    103             print('{}.{}'.format(balance_str[:offset],balance_str[offset:]))
    104 
    105 
    106 if __name__ == '__main__':
    107     main()