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: Adjust SignatureError with helpful tip on including sender= tx kwarg #1826

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ape_ethereum/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ class AccessList(BaseModel):
class BaseTransaction(TransactionAPI):
def serialize_transaction(self) -> bytes:
if not self.signature:
raise SignatureError("The transaction is not signed.")
message = "The transaction is not signed."
if not self.sender:
message = (
f"{message} "
"Did you forget to add the sender argument to the transaction function call?"
)

raise SignatureError(message)

txn_data = self.model_dump(by_alias=True, exclude={"sender", "type"})

Expand Down
29 changes: 29 additions & 0 deletions tests/functional/test_transaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from eth_pydantic_types import HexBytes

from ape.exceptions import SignatureError
from ape_ethereum.transactions import DynamicFeeTransaction, StaticFeeTransaction, TransactionType


Expand Down Expand Up @@ -91,3 +92,31 @@ def test_txn_str_when_data_is_bytes(ethereum):
def test_transaction_with_none_receipt(ethereum):
txn = ethereum.create_transaction(data=HexBytes("0x123"))
assert txn.receipt is None


def test_serialize_transaction(owner, ethereum):
txn = ethereum.create_transaction(
data=HexBytes("0x123"), max_fee=0, max_priority_fee=0, nonce=0
)
txn = owner.sign_transaction(txn)
assert txn is not None

actual = txn.serialize_transaction()
assert isinstance(actual, bytes)


def test_serialize_transaction_missing_signature(ethereum, owner):
expected = r"The transaction is not signed."
txn = ethereum.create_transaction(data=HexBytes("0x123"), sender=owner.address)
with pytest.raises(SignatureError, match=expected):
txn.serialize_transaction()


def test_serialize_transaction_missing_signature_and_sender(ethereum):
expected = (
r"The transaction is not signed. "
r"Did you forget to add the sender argument to the transaction function call?"
)
txn = ethereum.create_transaction(data=HexBytes("0x123"))
with pytest.raises(SignatureError, match=expected):
txn.serialize_transaction()
Loading