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

103 lines
3.7 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
2021-03-22 17:11:20 +01:00
import sys
2020-12-21 23:00:45 +01:00
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
2021-01-10 22:03:26 +01:00
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-02-19 13:50:13 +01:00
from chainlib.chain import ChainSpec
2021-03-22 17:11:20 +01:00
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import RPCGasOracle
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
from chainlib.eth.erc20 import ERC20
# local imports
from eth_token_index import TokenUniqueSymbolIndex
2020-12-21 23:00:45 +01:00
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
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')
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', required=True, type=str, help='Token index contract address')
2020-12-21 23:00:45 +01:00
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')
2021-03-22 17:11:20 +01:00
argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration')
argparser.add_argument('token_address', type=str, help='Token address to add to index')
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
2021-03-22 17:11:20 +01:00
passphrase_env = 'ETH_PASSPHRASE'
if args.env_prefix != None:
passphrase_env = args.env_prefix + '_' + passphrase_env
passphrase = os.environ.get(passphrase_env)
if passphrase == None:
logg.warning('no passphrase given')
passphrase=''
2021-01-10 22:03:26 +01:00
signer_address = None
keystore = DictKeystore()
if args.y != None:
logg.debug('loading keystore file {}'.format(args.y))
2021-03-22 17:11:20 +01:00
signer_address = keystore.import_keystore_file(args.y, password=passphrase)
2021-01-10 22:03:26 +01:00
logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore)
2021-02-19 13:50:13 +01:00
chain_spec = ChainSpec.from_chain_str(args.i)
2020-12-21 23:00:45 +01:00
2021-03-22 17:11:20 +01:00
rpc = EthHTTPConnection(args.p)
nonce_oracle = RPCNonceOracle(signer_address, rpc)
gas_oracle = RPCGasOracle(rpc, code_callback=TokenUniqueSymbolIndex.gas)
contract_address = args.a
token_address = args.token_address
2021-01-10 22:03:26 +01:00
def main():
2021-04-04 14:40:12 +02:00
c = TokenUniqueSymbolIndex(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
2021-03-22 17:11:20 +01:00
(tx_hash_hex, o) = c.register(contract_address, signer_address, token_address)
rpc.do(o)
2021-03-22 17:59:36 +01:00
if block_last:
r = rpc.wait(tx_hash_hex)
if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1)
2021-03-22 17:11:20 +01:00
2021-04-04 14:40:12 +02:00
c = ERC20(chain_spec)
2021-03-22 17:11:20 +01:00
o = c.symbol(token_address)
r = rpc.do(o)
token_symbol = ERC20.parse_symbol(r)
2021-01-10 22:03:26 +01:00
2021-03-22 17:11:20 +01:00
logg.info('added token {} at {} to token index {}'.format(token_symbol, token_address, contract_address))
print(tx_hash_hex)
2020-12-21 23:00:45 +01:00
if __name__ == '__main__':
main()