|
| 1 | +""" |
| 2 | +this script deploys the contract used by eip4788. It has been presigned and the contract uses a deterministic deployment. |
| 3 | +
|
| 4 | +""" |
| 5 | + |
| 6 | +from web3 import Web3 |
| 7 | +from web3.middleware import construct_sign_and_send_raw_middleware |
| 8 | +import os |
| 9 | +import time |
| 10 | +import logging |
| 11 | +from decimal import Decimal |
| 12 | + |
| 13 | +VALUE_TO_SEND = 0x9184 |
| 14 | + |
| 15 | +logging.basicConfig(filename="/tmp/sender.log", |
| 16 | + filemode='a', |
| 17 | + format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', |
| 18 | + datefmt='%H:%M:%S', |
| 19 | + level=logging.INFO) |
| 20 | + |
| 21 | + |
| 22 | +def eip4788_deployment(): |
| 23 | + # this is the 5th prefunded address |
| 24 | + sender = os.getenv("SENDER_PRIVATE_KEY", "7da08f856b5956d40a72968f93396f6acff17193f013e8053f6fbb6c08c194d6") |
| 25 | + # this is the 4788 presigned contract deployer |
| 26 | + receiver = "0x0B799C86a49DEeb90402691F1041aa3AF2d3C875" |
| 27 | + signed_4788_deployment_tx = os.getenv("SIGNED_4788_DEPLOYMENT_TX", "f8838085e8d4a510008303d0908080b86a60618060095f395ff33373fffffffffffffffffffffffffffffffffffffffe14604d57602036146024575f5ffd5b5f35801560495762001fff810690815414603c575f5ffd5b62001fff01545f5260205ff35b5f5ffd5b62001fff42064281555f359062001fff0155001b820539851b9b6eb1f0") |
| 28 | + el_uri = os.getenv("EL_RPC_URI", 'http://0.0.0.0:53913') |
| 29 | + logging.info(f"Using sender {sender} receiver {receiver} and el_uri {el_uri}") |
| 30 | + |
| 31 | + w3 = Web3(Web3.HTTPProvider(el_uri)) |
| 32 | + # sleep for 10s before checking again |
| 33 | + time.sleep(10) |
| 34 | + |
| 35 | + # Check if the chain has started before submitting transactions |
| 36 | + block = w3.eth.get_block('latest') |
| 37 | + |
| 38 | + logging.info(f"Latest block number: {block.number}") |
| 39 | + if block.number >1: |
| 40 | + logging.info("Chain has started, proceeding with Funding") |
| 41 | + # Import sender account |
| 42 | + sender_account = w3.eth.account.from_key(sender) |
| 43 | + # Prepare to Construct and sign transaction |
| 44 | + w3.middleware_onion.add(construct_sign_and_send_raw_middleware(sender_account)) |
| 45 | + |
| 46 | + # Prepare funding transaction |
| 47 | + logging.info("Preparing funding tx") |
| 48 | + transaction = { |
| 49 | + "from": sender_account.address, |
| 50 | + "to": receiver, |
| 51 | + "value": w3.to_wei(Decimal('1000.0'), 'ether'), # Sending 1000 Ether |
| 52 | + "gasPrice": w3.eth.gas_price, |
| 53 | + 'nonce': w3.eth.get_transaction_count(sender_account.address) |
| 54 | + } |
| 55 | + |
| 56 | + # Estimate gas |
| 57 | + logging.info("Estimating gas") |
| 58 | + estimated_gas = w3.eth.estimate_gas(transaction) |
| 59 | + |
| 60 | + # Set gas value |
| 61 | + transaction["gas"] = estimated_gas |
| 62 | + |
| 63 | + # Send transaction |
| 64 | + logging.debug(f"Sending deployment tx: {transaction}") |
| 65 | + tx_hash = w3.eth.send_transaction(transaction) |
| 66 | + |
| 67 | + time.sleep(10) |
| 68 | + # Wait for the transaction to be mined |
| 69 | + funding_tx = w3.eth.get_transaction(tx_hash) |
| 70 | + logging.debug(f"Funding Txhash: {tx_hash.hex()}") |
| 71 | + logging.info(f"Genesis funder Balance: {w3.eth.get_balance(sender_account.address)}") |
| 72 | + logging.info(f"4788 deployer Balance: {w3.eth.get_balance(receiver)}") |
| 73 | + |
| 74 | + if funding_tx["from"] == sender_account.address: |
| 75 | + logging.info("Funding tx mined successfully") |
| 76 | + logging.info("Deploying signed tx") |
| 77 | + # Prepare deployment transaction |
| 78 | + deployment_tx_hash = w3.eth.send_raw_transaction(signed_4788_deployment_tx) |
| 79 | + |
| 80 | + # Sleep before checking |
| 81 | + time.sleep(10) |
| 82 | + deployment_tx = w3.eth.get_transaction(deployment_tx_hash) |
| 83 | + logging.debug(f"Deployment Txhash: {deployment_tx.hash.hex()}") |
| 84 | + |
| 85 | + # Sleep before checking |
| 86 | + time.sleep(10) |
| 87 | + |
| 88 | + logging.info(f"4788 deployer Balance: {w3.eth.get_balance(receiver)}") |
| 89 | + assert deployment_tx["from"] == receiver |
| 90 | + |
| 91 | + # Check if contract has been deployed |
| 92 | + eip4788_code = w3.eth.get_code('0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02') |
| 93 | + if eip4788_code != "": |
| 94 | + logging.info(f"Contract deployed: {eip4788_code.hex()}") |
| 95 | + logging.info("Deployment tx mined successfully") |
| 96 | + |
| 97 | + # Exit script |
| 98 | + return True |
| 99 | + else: |
| 100 | + logging.info("Deployment failed, restarting script") |
| 101 | + return False |
| 102 | + else: |
| 103 | + logging.info("Funding failed, restarting script") |
| 104 | + return False |
| 105 | + else: |
| 106 | + logging.info("Chain has not started, restarting script") |
| 107 | + return False |
| 108 | + |
| 109 | +def run_till_deployed(): |
| 110 | + deployment_status = False |
| 111 | + while deployment_status is False: |
| 112 | + try: |
| 113 | + deployment_status = eip4788_deployment() |
| 114 | + except Exception as e: |
| 115 | + logging.error(e) |
| 116 | + logging.error("restarting deployment as previous one failed") |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + run_till_deployed() |
| 122 | + logging.info("Deployment complete, exiting script") |
0 commit comments