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

100 lines
3.0 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
# third-party imports
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
2021-03-22 17:11:20 +01:00
from crypto_dev_signer.keystore.dict import DictKeystore
2021-03-06 22:57:58 +01:00
from chainlib.chain import ChainSpec
2021-03-22 17:11:20 +01:00
from chainlib.eth.connection import EthHTTPConnection
2021-05-02 17:59:37 +02:00
from eth_erc20 import ERC20
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')
argparser = argparse.ArgumentParser()
argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
2021-04-04 14:40:12 +02:00
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
2021-03-22 17:11:20 +01:00
argparser.add_argument('-a', '--contract-address', dest='a', type=str, required=True, help='Token endorsement contract address')
2021-03-25 09:27:17 +01:00
argparser.add_argument('-f', '--format', dest='f', type=str, default=default_format, help='Output format [human, brief]')
2021-03-06 22:57:58 +01:00
argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
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()
if args.vv:
logg.setLevel(logging.DEBUG)
elif args.v:
logg.setLevel(logging.INFO)
2021-03-22 17:11:20 +01:00
rpc = EthHTTPConnection(args.p)
contract_address = args.a
2021-03-25 09:27:17 +01:00
token_symbol = args.token_symbol
fmt = args.f
2021-03-06 22:57:58 +01:00
2021-04-04 14:40:12 +02:00
chain_spec = ChainSpec.from_chain_str(args.i)
2021-03-25 09:27:17 +01:00
def out_element(e, fmt=default_format, w=sys.stdout):
if fmt == 'brief':
w.write(e[1] + '\n')
else:
w.write('{} {}\n'.format(e[0], e[1]))
def element(ifc, contract_address, token_symbol, fmt=fmt, w=sys.stdout):
o = ifc.address_of(contract_address, token_symbol)
r = rpc.do(o)
a = ifc.parse_address_of(r)
out_element((token_symbol, a), fmt, w)
def ls(ifc, contract_address, token_ifc, fmt=fmt, w=sys.stdout):
o = ifc.entry_count(contract_address)
2021-03-22 17:11:20 +01:00
r = rpc.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)
2021-03-22 17:11:20 +01:00
r = rpc.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)
2021-03-22 17:11:20 +01:00
r = rpc.do(o)
2021-03-25 09:27:17 +01:00
token_symbol = token_ifc.parse_symbol(r)
element(ifc, contract_address, token_symbol, fmt, 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)
2021-03-25 09:27:17 +01:00
if token_symbol != None:
2021-03-25 19:18:37 +01:00
element(ifc, contract_address, token_symbol, fmt, sys.stdout)
2021-03-25 09:27:17 +01:00
else:
ls(ifc, contract_address, token_ifc, fmt, sys.stdout)
2021-03-06 22:57:58 +01:00
if __name__ == '__main__':
main()