2022-02-19 21:31:12 +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 sys
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
even,
|
|
|
|
)
|
|
|
|
import sha3
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
import chainlib.eth.cli
|
2022-05-13 09:43:46 +02:00
|
|
|
from chainlib.eth.cli.arg import (
|
|
|
|
Arg,
|
|
|
|
ArgFlag,
|
|
|
|
process_args,
|
2022-11-06 11:29:58 +01:00
|
|
|
stdin_arg,
|
2022-05-13 09:43:46 +02:00
|
|
|
)
|
|
|
|
from chainlib.eth.cli.config import (
|
|
|
|
Config,
|
|
|
|
process_config,
|
|
|
|
)
|
2022-02-19 21:31:12 +01:00
|
|
|
from chainlib.eth.address import to_checksum_address
|
|
|
|
from chainlib.eth.connection import EthHTTPConnection
|
|
|
|
from chainlib.eth.gas import (
|
|
|
|
OverrideGasOracle,
|
|
|
|
balance,
|
|
|
|
)
|
|
|
|
from chainlib.chain import ChainSpec
|
2022-05-13 09:43:46 +02:00
|
|
|
from chainlib.eth.settings import process_settings
|
|
|
|
from chainlib.settings import ChainSettings
|
|
|
|
from chainlib.eth.cli.log import process_log
|
2022-02-19 21:31:12 +01:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from eth_erc20 import ERC20
|
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2022-05-13 09:43:46 +02:00
|
|
|
|
2022-11-06 11:29:58 +01:00
|
|
|
|
2022-05-13 09:43:46 +02:00
|
|
|
def process_config_local(config, arg, args, flags):
|
2022-11-06 11:29:58 +01:00
|
|
|
contract = None
|
|
|
|
try:
|
|
|
|
contract = config.get('_EXEC_ADDRESS')
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if contract == None:
|
|
|
|
address = config.get('_POSARG')
|
|
|
|
if address:
|
|
|
|
contract = add_0x(address)
|
|
|
|
else:
|
|
|
|
contract = stdin_arg()
|
|
|
|
|
|
|
|
config.add(contract, '_CONTRACT', False)
|
2022-05-13 09:43:46 +02:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
arg_flags = ArgFlag()
|
|
|
|
arg = Arg(arg_flags)
|
2023-02-13 07:38:17 +01:00
|
|
|
flags = arg_flags.STD_READ | arg_flags.EXEC | arg_flags.TAB | arg_flags.SENDER
|
2022-05-13 09:43:46 +02:00
|
|
|
|
|
|
|
argparser = chainlib.eth.cli.ArgumentParser()
|
|
|
|
argparser = process_args(argparser, arg, flags)
|
2022-11-06 11:29:58 +01:00
|
|
|
argparser.add_argument('contract_address', type=str, help='Token contract address (may also be specified by -e)')
|
2022-02-19 21:31:12 +01:00
|
|
|
args = argparser.parse_args()
|
|
|
|
|
2022-05-13 09:43:46 +02:00
|
|
|
logg = process_log(args, logg)
|
2022-02-19 21:31:12 +01:00
|
|
|
|
2022-05-13 09:43:46 +02:00
|
|
|
config = Config()
|
2022-11-06 11:29:58 +01:00
|
|
|
config = process_config(config, arg, args, flags, positional_name='contract_address')
|
2022-05-13 09:43:46 +02:00
|
|
|
config = process_config_local(config, arg, args, flags)
|
|
|
|
logg.debug('config loaded:\n{}'.format(config))
|
2022-02-19 21:31:12 +01:00
|
|
|
|
2022-05-13 09:43:46 +02:00
|
|
|
settings = ChainSettings()
|
|
|
|
settings = process_settings(settings, config)
|
|
|
|
logg.debug('settings loaded:\n{}'.format(settings))
|
2022-02-19 21:31:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-11-06 11:29:58 +01:00
|
|
|
token_address = config.get('_CONTRACT')
|
2022-05-13 09:43:46 +02:00
|
|
|
conn = settings.get('CONN')
|
2023-02-13 07:38:17 +01:00
|
|
|
sender_address = settings.get('SENDER_ADDRESS')
|
2022-05-13 09:43:46 +02:00
|
|
|
g = ERC20(
|
|
|
|
chain_spec=settings.get('CHAIN_SPEC'),
|
|
|
|
gas_oracle=settings.get('GAS_ORACLE'),
|
|
|
|
)
|
|
|
|
|
2022-11-06 11:29:58 +01:00
|
|
|
outkeys = config.get('_OUTARG')
|
2022-02-19 21:31:12 +01:00
|
|
|
|
2022-11-06 11:29:58 +01:00
|
|
|
if not outkeys or 'address' in outkeys:
|
2023-02-13 07:38:17 +01:00
|
|
|
name_o = g.name(token_address, sender_address=sender_address)
|
2022-02-19 21:41:47 +01:00
|
|
|
r = conn.do(name_o)
|
|
|
|
token_name = g.parse_name(r)
|
|
|
|
s = ''
|
2022-11-06 11:29:58 +01:00
|
|
|
if not config.true('_RAW'):
|
2022-02-19 21:41:47 +01:00
|
|
|
s = 'Name: '
|
|
|
|
s += token_name
|
|
|
|
print(s)
|
|
|
|
|
2022-11-06 11:29:58 +01:00
|
|
|
if not outkeys or 'symbol' in outkeys:
|
2023-02-13 07:38:17 +01:00
|
|
|
symbol_o = g.symbol(token_address, sender_address=sender_address)
|
2022-02-19 21:41:47 +01:00
|
|
|
r = conn.do(symbol_o)
|
|
|
|
token_symbol = g.parse_symbol(r)
|
|
|
|
s = ''
|
2022-11-06 11:29:58 +01:00
|
|
|
if not config.true('_RAW'):
|
2022-02-19 21:41:47 +01:00
|
|
|
s = 'Symbol: '
|
|
|
|
s += token_symbol
|
|
|
|
print(s)
|
|
|
|
|
2022-11-06 11:29:58 +01:00
|
|
|
if not outkeys or 'decimals' in outkeys:
|
2023-02-13 07:38:17 +01:00
|
|
|
decimals_o = g.decimals(token_address, sender_address=sender_address)
|
2022-02-19 21:41:47 +01:00
|
|
|
r = conn.do(decimals_o)
|
|
|
|
decimals = int(strip_0x(r), 16)
|
|
|
|
s = ''
|
2022-11-06 11:29:58 +01:00
|
|
|
if not config.true('_RAW'):
|
2022-02-19 21:41:47 +01:00
|
|
|
s = 'Decimals: '
|
|
|
|
s += str(decimals)
|
|
|
|
print(s)
|
|
|
|
|
2022-11-06 11:29:58 +01:00
|
|
|
if not outkeys or 'supply' in outkeys:
|
2023-02-13 07:38:17 +01:00
|
|
|
supply_o = g.total_supply(token_address, sender_address=sender_address)
|
2022-02-19 21:41:47 +01:00
|
|
|
r = conn.do(supply_o)
|
|
|
|
supply = int(strip_0x(r), 16)
|
|
|
|
s = ''
|
2022-11-06 11:29:58 +01:00
|
|
|
if not config.true('_RAW'):
|
2022-02-19 21:41:47 +01:00
|
|
|
s = 'Supply: '
|
|
|
|
s += str(supply)
|
|
|
|
print(s)
|
2022-02-19 21:31:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|