Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow private transaction receipts to bail out before transaction acceptance #2280

32 changes: 24 additions & 8 deletions src/ape_ethereum/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,34 @@ def get_receipt(
)
hex_hash = HexBytes(txn_hash)

txn = {}
if transaction := kwargs.get("transaction"):
# perf: If called `send_transaction()`, we should already have the data!
txn = (
transaction
if isinstance(transaction, dict)
else transaction.model_dump(by_alias=True, mode="json")
)

private = kwargs.get("private")

try:
receipt_data = dict(
self.web3.eth.wait_for_transaction_receipt(hex_hash, timeout=timeout)
)
except TimeExhausted as err:
# We don't auto-wait for acceptance or confirmations for private transactions.
bitwise-constructs marked this conversation as resolved.
Show resolved Hide resolved
# TODO: Update comment with instructions for how to manually wait
# once the feature is ready in core Ape.
if private:
# Return with a partial receipt
data = {
"block_number": -1,
"required_confirmations": required_confirmations,
**txn,
}
receipt = self._create_receipt(**data)
return receipt
msg_str = str(err)
if f"HexBytes('{txn_hash}')" in msg_str:
msg_str = msg_str.replace(f"HexBytes('{txn_hash}')", f"'{txn_hash}'")
Expand All @@ -641,18 +664,11 @@ def get_receipt(
network_config: dict = ecosystem_config.get(self.network.name, {})
max_retries = network_config.get("max_get_transaction_retries", DEFAULT_MAX_RETRIES_TX)

if transaction := kwargs.get("transaction"):
# perf: If called `send_transaction()`, we should already have the data!
txn = (
transaction
if isinstance(transaction, dict)
else transaction.model_dump(by_alias=True, mode="json")
)
if transaction:
if "effectiveGasPrice" in receipt_data:
receipt_data["gasPrice"] = receipt_data["effectiveGasPrice"]

else:
txn = {}
for attempt in range(max_retries):
try:
txn = dict(self.web3.eth.get_transaction(HexStr(txn_hash)))
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ def test_get_receipt_exists_with_timeout(eth_tester_provider, vyper_contract_ins
assert receipt_from_provider.receiver == vyper_contract_instance.address


def test_get_receipt_for_private_bypasses_confirmations(
antazoey marked this conversation as resolved.
Show resolved Hide resolved
eth_tester_provider, vyper_contract_instance, owner
):
receipt_from_invoke = vyper_contract_instance.setNumber(888, sender=owner)
receipt_from_provider = eth_tester_provider.get_receipt(
receipt_from_invoke.txn_hash, timeout=0, private=True
)
assert receipt_from_provider.txn_hash == receipt_from_invoke.txn_hash
assert receipt_from_provider.receiver == vyper_contract_instance.address
assert not receipt_from_provider.confirmed


def test_get_contracts_logs_all_logs(chain, contract_instance, owner, eth_tester_provider):
start_block = chain.blocks.height
stop_block = start_block + 100
Expand Down
Loading