eth-address-index/python/eth_token_index/runnable/list.py

100 lines
2.7 KiB
Python
Raw Normal View History

2021-03-06 22:57:58 +01:00
"""Adds a new token to the token symbol index
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
2021-03-25 09:27:17 +01:00
import sys
2021-03-06 22:57:58 +01:00
import os
import json
import argparse
import logging
import hashlib
# external imports
import chainlib.eth.cli
2021-03-06 22:57:58 +01:00
from chainlib.chain import ChainSpec
2021-05-02 17:59:37 +02:00
from eth_erc20 import ERC20
from chainlib.eth.address import to_checksum_address
2021-03-22 17:11:20 +01:00
# local imports
from eth_token_index import TokenUniqueSymbolIndex
2021-03-06 22:57:58 +01:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
2021-03-25 09:27:17 +01:00
default_format = 'terminal'
2021-03-06 22:57:58 +01:00
script_dir = os.path.dirname(__file__)
data_dir = os.path.join(script_dir, '..', 'data')
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
2021-03-25 09:27:17 +01:00
argparser.add_argument('token_symbol', type=str, nargs='?', help='Token symbol to return address for')
2021-03-06 22:57:58 +01:00
args = argparser.parse_args()
extra_args = {
'token_symbol': None,
}
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=TokenUniqueSymbolIndex.gas())
2021-03-06 22:57:58 +01:00
#wallet = chainlib.eth.cli.Wallet()
#wallet.from_config(config)
2021-03-22 17:11:20 +01:00
rpc = chainlib.eth.cli.Rpc()
conn = rpc.connect_by_config(config)
2021-03-06 22:57:58 +01:00
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
2021-04-04 14:40:12 +02:00
2021-03-25 09:27:17 +01:00
def out_element(e, w=sys.stdout):
2021-08-24 21:43:06 +02:00
if config.get('_RAW'):
w.write(e[1] + '\n')
else:
w.write(e[1] + '\t' + e[0] + '\n')
2021-03-25 09:27:17 +01:00
def element(ifc, conn, contract_address, token_symbol, w=sys.stdout):
2021-03-25 09:27:17 +01:00
o = ifc.address_of(contract_address, token_symbol)
r = conn.do(o)
2021-03-25 09:27:17 +01:00
a = ifc.parse_address_of(r)
out_element((token_symbol, a), w)
2021-03-25 09:27:17 +01:00
def ls(ifc, conn, contract_address, token_ifc, w=sys.stdout):
2021-03-25 09:27:17 +01:00
o = ifc.entry_count(contract_address)
r = conn.do(o)
2021-03-25 09:27:17 +01:00
count = ifc.parse_entry_count(r)
logg.debug('count {}'.format(count))
2021-03-22 17:11:20 +01:00
for i in range(count):
2021-03-25 09:27:17 +01:00
o = ifc.entry(contract_address, i)
r = conn.do(o)
2021-03-25 09:27:17 +01:00
token_address = ifc.parse_entry(r)
2021-03-22 17:11:20 +01:00
2021-03-25 09:27:17 +01:00
o = token_ifc.symbol(token_address)
r = conn.do(o)
2021-03-25 09:27:17 +01:00
token_symbol = token_ifc.parse_symbol(r)
element(ifc, conn, contract_address, token_symbol, w)
2021-03-22 17:11:20 +01:00
2021-03-25 09:27:17 +01:00
def main():
2021-04-04 14:40:12 +02:00
token_ifc = ERC20(chain_spec)
ifc = TokenUniqueSymbolIndex(chain_spec)
contract_address = to_checksum_address(config.get('_EXEC_ADDRESS'))
if not config.true('_UNSAFE') and contract_address != add_0x(config.get('_EXEC_ADDRESS')):
raise ValueError('invalid checksum address for contract')
token_symbol = config.get('_TOKEN_SYMBOL')
2021-03-25 09:27:17 +01:00
if token_symbol != None:
element(ifc, conn, contract_address, token_symbol, sys.stdout)
2021-03-25 09:27:17 +01:00
else:
ls(ifc, conn, contract_address, token_ifc, sys.stdout)
2021-03-06 22:57:58 +01:00
if __name__ == '__main__':
main()