Skip to content

Commit

Permalink
fix: TransactionAPI abstract methods bug during receipt decoding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored May 10, 2024
1 parent c7c352a commit 69e04c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,22 @@ def __ape_extra_attributes__(self) -> Iterator[ExtraModelAttributes]:

@field_validator("transaction", mode="before")
@classmethod
def confirm_transaction(cls, value):
if isinstance(value, dict):
value = TransactionAPI.model_validate(value)
def _validate_transaction(cls, value):
if not isinstance(value, dict):
# Already a `TransactionAPI`.
return value

return value
# Attempt to create a transaction model for the data.
if provider := cls.network_manager.active_provider:
ecosystem = provider.network.ecosystem
else:
logger.warning(
"Given raw-transaction data when not connected to any provider. "
"Network is unknown. Assuming EVM-like transaction model."
)
ecosystem = cls.network_manager.ethereum

return ecosystem.create_transaction(**value)

@field_validator("txn_hash", mode="before")
@classmethod
Expand Down
14 changes: 13 additions & 1 deletion tests/functional/test_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ape.api import ReceiptAPI
from ape.exceptions import APINotImplementedError, ContractLogicError, OutOfGasError
from ape.utils import ManagerAccessMixin
from ape_ethereum.transactions import Receipt, TransactionStatusEnum
from ape_ethereum.transactions import DynamicFeeTransaction, Receipt, TransactionStatusEnum


@pytest.fixture
Expand Down Expand Up @@ -198,3 +198,15 @@ def test_track_coverage(deploy_receipt, mocker):
def test_access_from_tx(deploy_receipt):
actual = deploy_receipt.transaction.receipt
assert actual == deploy_receipt


def test_transaction_validated_from_dict(ethereum, owner, deploy_receipt):
tx = ethereum.create_transaction(sender=owner.address, value=123, data=b"hello")
tx_data = tx.model_dump()
receipt_data = deploy_receipt.model_dump(by_alias=True)
receipt_data["transaction"] = tx_data
receipt = Receipt.model_validate(receipt_data)
assert isinstance(receipt.transaction, DynamicFeeTransaction)
assert receipt.transaction.sender == owner.address
assert receipt.transaction.value == 123
assert receipt.transaction.data == b"hello"

0 comments on commit 69e04c2

Please sign in to comment.