commit 18abda7d2b746ef7538c81a653b3ae6d378a5a1e
parent b50b2dd6fc6f99300872e558a5cc44652d84d5e0
Author: nolash <dev@holbrook.no>
Date: Tue, 15 Jun 2021 12:29:18 +0200
Rehabilitate deploy script
Diffstat:
5 files changed, 121 insertions(+), 88 deletions(-)
diff --git a/python/erc20_faucet/faucet.py b/python/erc20_faucet/faucet.py
@@ -67,6 +67,11 @@ class SingleShotFaucet(Faucet):
return SingleShotFaucet.__bytecode
+ @staticmethod
+ def gas(code=None):
+ return 2000000
+
+
def store_constructor(self, sender_address):
code = SingleShotFaucet.bytecode(part='storage')
tx = self.template(sender_address, None, use_nonce=True)
diff --git a/python/erc20_faucet/interface.py b/python/erc20_faucet/interface.py
@@ -9,12 +9,14 @@ from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.contract import (
abi_decode_single,
ABIContractEncoder,
+ ABIContractDecoder,
ABIContractType,
)
from chainlib.eth.tx import (
TxFormat,
)
from chainlib.jsonrpc import jsonrpc_template
+from chainlib.eth.error import RequestMismatchException
from hexathon import (
add_0x,
strip_0x,
@@ -79,6 +81,28 @@ class Faucet(TxFactory):
return tx
+ @classmethod
+ def parse_give_to_request(self, v):
+ v = strip_0x(v)
+ cursor = 0
+ enc = ABIContractEncoder()
+ enc.method('giveTo')
+ enc.typ(ABIContractType.ADDRESS)
+ r = enc.get()
+ l = len(r)
+ m = v[:l]
+ if m != r:
+ logg.error('method mismatch, expected {}, got {}'.format(r, m))
+ raise RequestMismatchException(v)
+ cursor += l
+
+ dec = ABIContractDecoder()
+ dec.typ(ABIContractType.ADDRESS)
+ dec.val(v[cursor:cursor+64])
+ r = dec.decode()
+ return r
+
+
def set_amount(self, contract_address, sender_address, amount, tx_format=TxFormat.JSONRPC):
enc = ABIContractEncoder()
enc.method('setAmount')
diff --git a/python/erc20_faucet/runnable/deploy.py b/python/erc20_faucet/runnable/deploy.py
@@ -1,4 +1,4 @@
-"""Deploys erc20 single shot faucet
+"""Deploys sarafu faucet contract
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
@@ -6,40 +6,58 @@
"""
# standard imports
+import sys
import os
import json
import argparse
import logging
# external imports
-import web3
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore.dict import DictKeystore
-from crypto_dev_signer.eth.helper import EthTxExecutor
+from chainlib.chain import ChainSpec
+from chainlib.eth.nonce import (
+ RPCNonceOracle,
+ OverrideNonceOracle,
+ )
+from chainlib.eth.gas import (
+ RPCGasOracle,
+ OverrideGasOracle,
+ )
+from chainlib.eth.connection import EthHTTPConnection
+from chainlib.eth.tx import (
+ receipt,
+ TxFactory,
+ )
+from chainlib.eth.constant import ZERO_ADDRESS
+from erc20_faucet.faucet import SingleShotFaucet
+
+# local imports
+from erc20_faucet.faucet import SingleShotFaucet
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')
+default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
+
argparser = argparse.ArgumentParser()
-argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
+argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='RPC provider url (http only)')
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('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string')
-argparser.add_argument('--editor', action='append', type=str, help='Amount editor account to add')
-argparser.add_argument('-a', '--signer-address', dest='a', type=str, help='Owner account (provider must have private key)')
+argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
argparser.add_argument('-y', '--key-file', dest='y', type=str, help='Ethereum keystore file to use for signing')
-argparser.add_argument('--token-address', dest='token_address', required=True, type=str, help='Token to add faucet for')
-argparser.add_argument('--set-amount', dest='set_amount', default=0, type=int, help='Initial amount to set. Will be 0 if not set!')
-argparser.add_argument('--accounts-index-address', dest='accounts_index_address', required=False, type=str, help='Accounts index to verify requesting address against (if not specified, any address may use the faucet')
-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')
argparser.add_argument('-vv', action='store_true', help='Be more verbose')
+argparser.add_argument('--gas-price', type=int, dest='gas_price', help='Override gas price')
+argparser.add_argument('-d', action='store_true', help='Dump RPC calls to terminal and do not send')
+argparser.add_argument('--nonce', type=int, help='Override transaction nonce')
+argparser.add_argument('--overrider-address', type=str, dest='overrider_address', help='Overrider address')
+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('--account-index-address', type=str, dest='account_index_address', help='Account index contract address')
+argparser.add_argument('token_address', type=str, help='Mintable token address')
args = argparser.parse_args()
if args.vv:
@@ -50,91 +68,78 @@ elif args.v:
block_last = args.w
block_all = args.ww
-w3 = web3.Web3(web3.Web3.HTTPProvider(args.p))
+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=''
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)
+ signer_address = keystore.import_keystore_file(args.y, password=passphrase)
logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore)
+chain_spec = ChainSpec.from_chain_str(args.i)
+
+rpc = EthHTTPConnection(args.p)
+nonce_oracle = None
+if args.nonce != None:
+ nonce_oracle = OverrideNonceOracle(signer_address, args.nonce)
+else:
+ nonce_oracle = RPCNonceOracle(signer_address, rpc)
+
+gas_oracle = None
+if args.gas_price !=None:
+ gas_oracle = OverrideGasOracle(price=args.gas_price, conn=rpc, code_callback=SingleShotFaucet.gas)
+else:
+ gas_oracle = RPCGasOracle(rpc, code_callback=SingleShotFaucet.gas)
-chain_pair = args.i.split(':')
-chain_id = int(chain_pair[1])
+dummy = args.d
-helper = EthTxExecutor(
- w3,
- signer_address,
- signer,
- chain_id,
- block=args.ww,
- )
+token_address = args.token_address
+overrider_address = signer_address
+if args.overrider_address != None:
+ overrider_address = args.overrider_address
+account_index_address = args.account_index_address
+if account_index_address == None:
+ account_index_address = ZERO_ADDRESS
def main():
- token_address = args.token_address
-
- f = open(os.path.join(args.abi_dir, 'ERC20SingleShotFaucetStorage.json'), 'r')
- abi = json.load(f)
- f.close()
-
- f = open(os.path.join(args.abi_dir, 'ERC20SingleShotFaucetStorage.bin'), 'r')
- bytecode = f.read()
- f.close()
-
- c = w3.eth.contract(abi=abi, bytecode=bytecode)
- (tx_hash, rcpt) = helper.sign_and_send(
- [
- c.constructor().buildTransaction
- ],
- force_wait=True,
- )
- store_address = rcpt.contractAddress
-
- f = open(os.path.join(args.abi_dir, 'ERC20SingleShotFaucet.json'), 'r')
- abi = json.load(f)
- f.close()
-
- f = open(os.path.join(args.abi_dir, 'ERC20SingleShotFaucet.bin'), 'r')
- bytecode = f.read()
- f.close()
-
- c = w3.eth.contract(abi=abi, bytecode=bytecode)
-
- editors = [signer_address]
- if args.editor != None:
- for a in args.editor:
- editors.append(a)
- logg.info('add approver {}'.format(a))
-
- accounts_index_address = '0x0000000000000000000000000000000000000000'
- if args.accounts_index_address != None:
- accounts_index_address = args.accounts_index_address
-
- (tx_hash, rcpt) = helper.sign_and_send(
- [
- c.constructor(editors, token_address, store_address, accounts_index_address).buildTransaction,
- ],
- force_wait=True,
- )
- address = rcpt.contractAddress
-
- if args.set_amount > 0:
- c = w3.eth.contract(abi=abi, address=address)
- (tx_hash, rcpt) = helper.sign_and_send(
- [
- c.functions.setAmount(args.set_amount).buildTransaction,
- ],
- force_wait=True,
- )
-
- logg.debug('setting initial ammount to {} tx_hash {}'.format(args.set_amount, tx_hash))
- amount = c.functions.amount().call()
- logg.info('set initial ammount tx_hash {}'.format(amount))
-
- print(address)
+ c = SingleShotFaucet(chain_spec, signer=signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
+ (tx_hash_hex, o) = c.store_constructor(signer_address)
+ if dummy:
+ print(tx_hash_hex)
+ print(o)
+ else:
+ rpc.do(o)
+ 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)
+ # TODO: pass through translator for keys (evm tester uses underscore instead of camelcase)
+ store_address = r['contractAddress']
+ logg.info('deployed faucet store on {}'.format(store_address))
+
+ c = SingleShotFaucet(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
+ (tx_hash_hex, o) = c.constructor(signer_address, token_address, store_address, account_index_address, [overrider_address])
+ rpc.do(o)
+ 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)
+ # TODO: pass through translator for keys (evm tester uses underscore instead of camelcase)
+ address = r['contractAddress']
+
+ if block_last:
+ rpc.wait(tx_hash_hex)
+
+ print(address)
if __name__ == '__main__':
diff --git a/python/erc20_faucet/runnable/list.py b/python/erc20_faucet/runnable/list.py
@@ -15,7 +15,6 @@ import logging
# external imports
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
from crypto_dev_signer.keystore.dict import DictKeystore
-from crypto_dev_signer.eth.helper import EthTxExecutor
from chainlib.chain import ChainSpec
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import RPCGasOracle
diff --git a/python/setup.cfg b/python/setup.cfg
@@ -1,6 +1,6 @@
[metadata]
name = erc20-faucet
-version = 0.2.1a3
+version = 0.2.1a5
description = ERC20 token faucet
author = Louis Holbrook
author_email = dev@holbrook.no