Skip to content

Commit

Permalink
fix: transaction was missing signature / couldn't get hash when came …
Browse files Browse the repository at this point in the history
…from receipt (#2446)
  • Loading branch information
antazoey authored Dec 23, 2024
1 parent 0686379 commit 587fc48
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/ape_ethereum/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,10 @@ def send_transaction(self, txn: TransactionAPI) -> ReceiptAPI:
else self.network.required_confirmations
)
txn_data = txn_data or txn.model_dump(by_alias=True, mode="json")

# Signature is excluded from the model fields, so we have to include it manually.
txn_data["signature"] = txn.signature

if vm_err:
receipt = self._create_receipt(
block_number=-1, # Not in a block.
Expand Down Expand Up @@ -1076,6 +1080,9 @@ def send_transaction(self, txn: TransactionAPI) -> ReceiptAPI:
# `nonce`, it's not needed anyway.
txn_data.pop("nonce", None)

# Signature causes issues when making call (instead of tx)
txn_data.pop("signature", None)

# NOTE: Using JSON mode since used as request data.
txn_params = cast(TxParams, txn_data)

Expand Down
2 changes: 2 additions & 0 deletions src/ape_ethereum/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def serialize_transaction(self) -> bytes:

return signed_txn

# TODO: In 0.9, either use hex-str or hex-bytes between both this
# and ReceiptAPI (make consistent).
@property
def txn_hash(self) -> HexBytes:
txn_bytes = self.serialize_transaction()
Expand Down
3 changes: 0 additions & 3 deletions src/ape_test/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,6 @@ def send_transaction(self, txn: "TransactionAPI") -> "ReceiptAPI":
self.chain_manager.history.append(receipt)

if receipt.failed:
# NOTE: Using JSON mode since used as request data.
txn_dict = txn_dict or txn.model_dump(mode="json")

txn_dict["nonce"] += 1
txn_params = cast(TxParams, txn_dict)
txn_dict.pop("signature", None)
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/geth/test_receipt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from eth_utils import to_hex

from ape.api import TraceAPI
from ape.utils import ManagerAccessMixin
from tests.conftest import geth_process_test
Expand Down Expand Up @@ -73,3 +75,11 @@ def test_await_confirmations_zero_confirmations(mocker, geth_account, geth_contr
tx.await_confirmations()
assert tx.confirmed
assert spy.call_count == 1


@geth_process_test
def test_transaction(geth_account, geth_contract):
receipt = geth_contract.setNumber(1998, sender=geth_account)
actual = receipt.transaction
assert actual.sender == geth_account.address
assert to_hex(actual.txn_hash) == receipt.txn_hash
8 changes: 8 additions & 0 deletions tests/functional/test_receipt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING

import pytest
from eth_utils import to_hex
from rich.table import Table
from rich.tree import Tree

Expand Down Expand Up @@ -270,6 +271,13 @@ def test_access_from_tx(deploy_receipt):
assert actual == deploy_receipt


def test_transaction(owner, vyper_contract_instance):
receipt = vyper_contract_instance.setNumber(1999, sender=owner)
actual = receipt.transaction
assert actual.sender == owner.address
assert to_hex(actual.txn_hash) == receipt.txn_hash


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()
Expand Down

0 comments on commit 587fc48

Please sign in to comment.