Skip to content

Commit

Permalink
add support for choosing whether to wait for receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
YanhuiJessica committed Mar 5, 2024
1 parent 7284ca9 commit 971d22a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cheb3/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def call(self, to: HexStr, data: HexStr = "0x", **kwargs) -> HexBytes:
state_override=kwargs.get("state_override", None),
)

def send_transaction(self, to: Union[HexStr, None], value: int = 0, data: HexStr = "0x", **kwargs) -> TxReceipt:
def send_transaction(
self, to: Union[HexStr, None], value: int = 0, data: HexStr = "0x", **kwargs
) -> Union[TxReceipt, HexStr]:
"""Transfer ETH or interact with a smart contract.
:param to: The address of the receiver.
Expand All @@ -89,8 +91,12 @@ def send_transaction(self, to: Union[HexStr, None], value: int = 0, data: HexStr
gas_limit (int): Specify the maximum gas the transaction can use.
access_list (List[Dict]): Specify a list of addresses and storage
keys that the transaction plans to access (EIP-2930).
wait_for_receipt (bool): Wait for the transaction receipt,
defaults to :const:`True`.
:rtype: TxReceipt
:returns: The transaction receipt or the transaction hash if
`wait_for_receipt` is :const:`False`.
:rtype: Union[TxReceipt, HexStr]
"""

if to:
Expand All @@ -115,6 +121,8 @@ def send_transaction(self, to: Union[HexStr, None], value: int = 0, data: HexStr
tx = self.eth_acct.sign_transaction(tx).rawTransaction
tx_hash = self.w3.eth.send_raw_transaction(tx).hex()
logger.info(f"Transaction to {to}: {tx_hash}")
if not kwargs.get("wait_for_receipt", True):
return tx_hash
receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash)
if not receipt.status:
raise Exception(f"Transact to {to} failed.")
Expand Down
2 changes: 2 additions & 0 deletions cheb3/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ def send_transaction(self, **kwargs) -> TxReceipt:
tx_hash = self.w3.eth.send_raw_transaction(tx).hex()
func_name = self.function_identifier if isinstance(self.function_identifier, str) else self.function_identifier.__name__
logger.info(f"({self.address}).{func_name} transaction hash: {tx_hash}")
if not kwargs.get("wait_for_receipt", True):
return tx_hash
receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash)
if not receipt.status:
raise Exception(f"Transact to ({self.address}).{func_name} errored.")
Expand Down

0 comments on commit 971d22a

Please sign in to comment.