erc20-transfer-authorization

Simple approval escrow for ERC20 spending
Info | Log | Files | Refs

publish.py (4186B)


      1 """Deploys erc20 transfer authorization contract
      2 
      3 .. moduleauthor:: Louis Holbrook <dev@holbrook.no>
      4 .. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 
      5 
      6 """
      7 
      8 # standard imports
      9 import sys
     10 import os
     11 import json
     12 import argparse
     13 import logging
     14 
     15 # third-party imports
     16 from funga.eth.signer import EIP155Signer
     17 from funga.eth.keystore.dict import DictKeystore
     18 from chainlib.chain import ChainSpec
     19 from chainlib.eth.nonce import (
     20         RPCNonceOracle,
     21         OverrideNonceOracle,
     22         )
     23 from chainlib.eth.gas import (
     24         RPCGasOracle,
     25         OverrideGasOracle,
     26         )
     27 from chainlib.eth.connection import EthHTTPConnection
     28 from chainlib.eth.tx import receipt
     29 
     30 # local imports
     31 from erc20_transfer_authorization import TransferAuthorization
     32 
     33 logging.basicConfig(level=logging.WARNING)
     34 logg = logging.getLogger()
     35 
     36 script_dir = os.path.dirname(__file__)
     37 data_dir = os.path.join(script_dir, '..', 'data')
     38 
     39 argparser = argparse.ArgumentParser()
     40 argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
     41 argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
     42 argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')
     43 argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
     44 argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Signer address')
     45 argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
     46 argparser.add_argument('-v', action='store_true', help='Be verbose')
     47 argparser.add_argument('-vv', action='store_true', help='Be more verbose')
     48 argparser.add_argument('-d', action='store_true', help='Dump RPC calls to terminal and do not send')
     49 argparser.add_argument('--gas-price', type=int, dest='gas_price', help='Override gas price')
     50 argparser.add_argument('--nonce', type=int, help='Override transaction nonce')
     51 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')
     52 args = argparser.parse_args()
     53 
     54 if args.vv:
     55     logg.setLevel(logging.DEBUG)
     56 elif args.v:
     57     logg.setLevel(logging.INFO)
     58 
     59 block_last = args.w
     60 block_all = args.ww
     61 
     62 passphrase_env = 'ETH_PASSPHRASE'
     63 if args.env_prefix != None:
     64     passphrase_env = args.env_prefix + '_' + passphrase_env
     65 passphrase = os.environ.get(passphrase_env)
     66 if passphrase == None:
     67     logg.warning('no passphrase given')
     68     passphrase=''
     69 
     70 signer_address = None
     71 keystore = DictKeystore()
     72 if args.y != None:
     73     logg.debug('loading keystore file {}'.format(args.y))
     74     signer_address = keystore.import_keystore_file(args.y, password=passphrase)
     75     logg.debug('now have key for signer address {}'.format(signer_address))
     76 signer = EIP155Signer(keystore)
     77 
     78 chain_spec = ChainSpec.from_chain_str(args.i)
     79 
     80 rpc = EthHTTPConnection(args.p)
     81 nonce_oracle = None
     82 if args.nonce != None:
     83     nonce_oracle = OverrideNonceOracle(signer_address, args.nonce)
     84 else:
     85     nonce_oracle = RPCNonceOracle(signer_address, rpc)
     86 
     87 gas_oracle = None
     88 if args.gas_price !=None:
     89     gas_oracle = OverrideGasOracle(price=args.gas_price, conn=rpc, code_callback=TransferAuthorization.gas)
     90 else:
     91     gas_oracle = RPCGasOracle(rpc, code_callback=TransferAuthorization.gas)
     92 
     93 dummy = args.d
     94 
     95 
     96 def main():
     97     c = TransferAuthorization(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
     98     (tx_hash_hex, o) = c.constructor(signer_address)
     99     if dummy:
    100         print(tx_hash_hex)
    101         print(o)
    102     else:
    103         rpc.do(o)
    104         if block_last:
    105             r = rpc.wait(tx_hash_hex)
    106             if r['status'] == 0:
    107                 sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
    108                 sys.exit(1)
    109             # TODO: pass through translator for keys (evm tester uses underscore instead of camelcase)
    110             address = r['contractAddress']
    111 
    112             print(address)
    113         else:
    114             print(tx_hash_hex)
    115 
    116 
    117 if __name__ == '__main__':
    118     main()