chainlib/chainlib/eth/runnable/balance.py

92 lines
2.5 KiB
Python
Raw Normal View History

2021-02-08 10:39:42 +01:00
#!python3
"""Token balance query script
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
import os
import json
import argparse
import logging
# third-party imports
from hexathon import (
add_0x,
strip_0x,
even,
)
import sha3
from eth_abi import encode_single
# local imports
2021-02-11 08:52:59 +01:00
from chainlib.eth.address import to_checksum
2021-04-04 14:55:27 +02:00
from chainlib.jsonrpc import (
2021-02-08 10:39:42 +01:00
jsonrpc_template,
jsonrpc_result,
)
2021-04-04 14:55:27 +02:00
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.gas import (
OverrideGasOracle,
balance,
)
2021-04-08 17:39:32 +02:00
from chainlib.chain import ChainSpec
2021-02-08 10:39:42 +01:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
default_abi_dir = os.environ.get('ETH_ABI_DIR', '/usr/share/local/cic/solidity/abi')
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
argparser = argparse.ArgumentParser()
argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='Web3 provider url (http only)')
2021-04-04 14:55:27 +02:00
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
2021-02-08 10:39:42 +01:00
argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress')
argparser.add_argument('-v', action='store_true', help='Be verbose')
2021-04-04 14:55:27 +02:00
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('address', type=str, help='Account address')
2021-02-08 10:39:42 +01:00
args = argparser.parse_args()
2021-04-04 14:55:27 +02:00
if args.vv:
2021-02-08 10:39:42 +01:00
logg.setLevel(logging.DEBUG)
2021-04-04 14:55:27 +02:00
elif args.v:
logg.setLevel(logging.INFO)
2021-02-08 10:39:42 +01:00
2021-04-04 14:55:27 +02:00
conn = EthHTTPConnection(args.p)
gas_oracle = OverrideGasOracle(conn)
2021-02-09 12:12:37 +01:00
2021-04-04 14:55:27 +02:00
address = to_checksum(args.address)
if not args.u and address != add_0x(args.address):
raise ValueError('invalid checksum address')
2021-02-08 10:39:42 +01:00
2021-04-04 14:55:27 +02:00
chain_spec = ChainSpec.from_chain_str(args.i)
2021-02-09 12:12:37 +01:00
2021-04-04 14:55:27 +02:00
def main():
2021-02-08 10:39:42 +01:00
r = None
decimals = 18
2021-05-02 14:49:34 +02:00
o = balance(address)
r = conn.do(o)
2021-02-08 10:39:42 +01:00
hx = strip_0x(r)
2021-04-04 14:55:27 +02:00
balance_value = int(hx, 16)
logg.debug('balance {} = {} decimals {}'.format(even(hx), balance_value, decimals))
2021-02-08 10:39:42 +01:00
2021-04-04 14:55:27 +02:00
balance_str = str(balance_value)
2021-02-08 10:39:42 +01:00
balance_len = len(balance_str)
if balance_len < decimals + 1:
2021-02-08 10:39:42 +01:00
print('0.{}'.format(balance_str.zfill(decimals)))
else:
offset = balance_len-decimals
print('{}.{}'.format(balance_str[:offset],balance_str[offset:]))
if __name__ == '__main__':
main()