Skip to content

Commit

Permalink
Merge pull request #12 from ChorusOne/add_retry_contract_deployment
Browse files Browse the repository at this point in the history
Add retry for `deploy_compiled_contract`
  • Loading branch information
enriquefynn authored Mar 12, 2024
2 parents fb232b0 + 7a5e97f commit 26189f3
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions eth_possim/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@
import re
import time
from typing import List, Tuple
from web3.types import TxReceipt, HexStr


logger = logging.getLogger(__name__)

def wait_for_transaction_receipt(w3: web3.Web3, signed_txn_hash: HexStr) -> TxReceipt:
for _ in range(1, 100):
try:
tx_receipt = w3.eth.wait_for_transaction_receipt(signed_txn_hash)
return tx_receipt
except ValueError as exc:
if exc.args[0]["message"] == "transaction indexing is in progress":
logger.info(
"Failed to get transaction receipt due to indexing, will retry"
)
time.sleep(5)
continue
else:
raise

def deploy_compiled_contract(
cfg: dict,
Expand Down Expand Up @@ -54,12 +69,9 @@ def deploy_compiled_contract(
private_key=cfg["el"]["funder"]["private_key"],
)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(signed_txn.hash)

logger.info(
f"Contract from '{foundry_json_path}' was published at address '{tx_receipt['contractAddress']}' [block: {tx_receipt['blockNumber']}]"
)

tx_receipt = wait_for_transaction_receipt(w3, signed_txn.hash)
logger.info(f"Contract from '{foundry_json_path}' was published at address '{tx_receipt['contractAddress']}' [block: {tx_receipt['blockNumber']}]")
return tx_receipt["contractAddress"]


Expand Down Expand Up @@ -105,21 +117,10 @@ def deploy_contract_onchain(
private_key=cfg["el"]["funder"]["private_key"],
)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)
for _ in range(1, 10):
try:
tx_receipt = w3.eth.wait_for_transaction_receipt(signed_txn.hash)
except ValueError as exc:
if exc.args[0]["message"] == "transaction indexing is in progress":
logger.info(
"Failed to get transaction receipt due to indexing, will retry"
)
time.sleep(5)
continue
else:
raise
tx_receipt = wait_for_transaction_receipt(w3, signed_txn.hash)

logger.info(
f"Contract from '{path}' was published at address '{tx_receipt['contractAddress']}' [block: {tx_receipt['blockNumber']}]"
)
logger.info(
f"Contract from '{path}' was published at address '{tx_receipt['contractAddress']}' [block: {tx_receipt['blockNumber']}]"
)

return tx_receipt["contractAddress"]
return tx_receipt["contractAddress"]

0 comments on commit 26189f3

Please sign in to comment.