Skip to content

Commit

Permalink
feat: Adjust SignatureError with helpful tip on including sender=
Browse files Browse the repository at this point in the history
… tx kwarg (#1826)

Co-authored-by: Juliya Smith <jules@apeworx.io>
  • Loading branch information
miohtama and antazoey authored Jan 8, 2024
1 parent 2323228 commit a0354ca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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=` kwarg 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=` kwarg to the transaction function call?"
)
txn = ethereum.create_transaction(data=HexBytes("0x123"))
with pytest.raises(SignatureError, match=expected):
txn.serialize_transaction()

0 comments on commit a0354ca

Please sign in to comment.