Skip to content

Commit

Permalink
fix: raise TransactionNotFoundError when transaction is not found (#95
Browse files Browse the repository at this point in the history
)
  • Loading branch information
antazoey authored Jan 3, 2025
1 parent 8de9fdb commit 548b653
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
15 changes: 12 additions & 3 deletions ape_alchemy/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from typing import TYPE_CHECKING, Any, Optional

from ape.api import ReceiptAPI, TraceAPI, TransactionAPI, UpstreamProvider
from ape.exceptions import APINotImplementedError, ContractLogicError, VirtualMachineError
from ape.exceptions import (
APINotImplementedError,
ContractLogicError,
TransactionNotFoundError,
VirtualMachineError,
)
from ape.logging import logger
from ape.utils import request_with_retry
from ape_ethereum.provider import Web3Provider
Expand All @@ -12,7 +17,7 @@
from requests import HTTPError
from web3 import HTTPProvider, Web3
from web3.exceptions import ContractLogicError as Web3ContractLogicError
from web3.exceptions import ExtraDataLengthError
from web3.exceptions import ExtraDataLengthError, TransactionNotFound
from web3.gas_strategies.rpc import rpc_gas_price_strategy

try:
Expand Down Expand Up @@ -365,7 +370,11 @@ def get_receipt(
) -> ReceiptAPI:
if not required_confirmations and not timeout:
# Allows `get_receipt` to work better when not sending.
data = self.web3.eth.get_transaction_receipt(HexStr(txn_hash))
try:
data = self.web3.eth.get_transaction_receipt(HexStr(txn_hash))
except TransactionNotFound as err:
raise TransactionNotFoundError(txn_hash) from err

txn = dict(self.web3.eth.get_transaction(HexStr(txn_hash)))
return self.network.ecosystem.decode_receipt(
{
Expand Down
13 changes: 12 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import websocket # type: ignore
from ape import accounts, networks
from ape.exceptions import APINotImplementedError
from ape.exceptions import APINotImplementedError, TransactionNotFoundError
from ape.utils import ZERO_ADDRESS

from ape_alchemy._utils import NETWORKS
Expand Down Expand Up @@ -68,3 +68,14 @@ def test_make_request_handles_result():
with networks.polygon_zkevm.cardona.use_provider("alchemy") as provider:
result = provider.make_request("eth_call", [tx, "latest"])
assert not isinstance(result, dict)


def test_get_receipt():
with networks.ethereum.sepolia.use_provider("alchemy") as alchemy:
txn_hash = "0x68605140856c13038d325048c411aed98cc1eecc189f628a38edb597f6b9679e"
existing_tx = alchemy.get_receipt(txn_hash)
assert existing_tx.txn_hash == txn_hash

txn_hash = "0x66600044856c13038d325048c411aed98cc1eecc189f628a38eeeeeeeeeeeeee"
with pytest.raises(TransactionNotFoundError):
_ = alchemy.get_receipt(txn_hash)

0 comments on commit 548b653

Please sign in to comment.