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

107 lines
3.4 KiB
Python
Raw Normal View History

2020-12-29 20:05:15 +01:00
"""Adds a new token to the token symbol index
2020-12-21 23:00:45 +01:00
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
import os
import json
import argparse
import logging
2020-12-29 20:05:15 +01:00
import hashlib
2020-12-21 23:00:45 +01:00
# third-party imports
import web3
2021-01-10 22:03:26 +01:00
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore import DictKeystore
from crypto_dev_signer.eth.helper import EthTxExecutor
2020-12-21 23:00:45 +01:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
logging.getLogger('web3').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
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-01-10 22:03:26 +01:00
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
2020-12-29 20:05:15 +01:00
argparser.add_argument('-r', '--contract-address', dest='r', type=str, help='Token endorsement contract address')
2021-01-10 22:03:26 +01:00
argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Accounts declarator owner')
2020-12-21 23:00:45 +01:00
argparser.add_argument('--abi-dir', dest='abi_dir', type=str, default=data_dir, help='Directory containing bytecode and abi (default: {})'.format(data_dir))
argparser.add_argument('-v', action='store_true', help='Be verbose')
2021-01-10 22:03:26 +01:00
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
2020-12-29 20:05:15 +01:00
argparser.add_argument('address', type=str, help='Ethereum address to add declaration to')
2020-12-21 23:00:45 +01:00
args = argparser.parse_args()
2021-01-10 22:03:26 +01:00
if args.vv:
2020-12-21 23:00:45 +01:00
logg.setLevel(logging.DEBUG)
2021-01-10 22:03:26 +01:00
elif args.v:
logg.setLevel(logging.INFO)
2020-12-21 23:00:45 +01:00
2021-01-10 22:03:26 +01:00
block_all = args.ww
2021-02-09 19:28:50 +01:00
block_last = args.w or block_all
2021-01-10 22:03:26 +01:00
w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
signer_address = None
keystore = DictKeystore()
if args.y != None:
logg.debug('loading keystore file {}'.format(args.y))
signer_address = keystore.import_keystore_file(args.y)
logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore)
chain_pair = args.i.split(':')
chain_id = int(chain_pair[1])
2020-12-21 23:00:45 +01:00
2021-01-10 22:03:26 +01:00
helper = EthTxExecutor(
w3,
signer_address,
signer,
chain_id,
block=args.ww,
)
def main():
2020-12-29 20:05:15 +01:00
f = open(os.path.join(args.abi_dir, 'TokenUniqueSymbolIndex.json'), 'r')
2020-12-21 23:00:45 +01:00
abi = json.load(f)
f.close()
2020-12-29 20:05:15 +01:00
f = open(os.path.join(args.abi_dir, 'ERC20.json'), 'r')
erc20_abi = json.load(f)
2020-12-21 23:00:45 +01:00
f.close()
2020-12-29 20:05:15 +01:00
t = w3.eth.contract(abi=erc20_abi, address=args.address)
token_symbol = t.functions.symbol().call()
c = w3.eth.contract(abi=abi, address=args.r)
2020-12-21 23:00:45 +01:00
2020-12-29 20:05:15 +01:00
h = hashlib.new('sha256')
h.update(token_symbol.encode('utf-8'))
z = h.digest()
logg.info('token symbol {} => {}'.format(token_symbol, z.hex()))
2020-12-21 23:00:45 +01:00
2021-01-10 22:03:26 +01:00
(tx_hash, rcpt) = helper.sign_and_send(
[
c.functions.register('0x' + z.hex(), args.address).buildTransaction,
],
)
2021-02-09 19:28:50 +01:00
if block_last:
helper.wait_for()
2021-01-10 22:03:26 +01:00
print(tx_hash)
2020-12-21 23:00:45 +01:00
if __name__ == '__main__':
main()