Skip to content

Commit

Permalink
feat: allow setting sign=False on transactions (good for titanoboa) (
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jan 31, 2025
1 parent adc0458 commit 8bf2e2d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/ape/api/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def call(
txn: TransactionAPI,
send_everything: bool = False,
private: bool = False,
sign: bool = True,
**signer_options,
) -> ReceiptAPI:
"""
Expand All @@ -143,6 +144,8 @@ def call(
Defaults to ``False``.
private (bool): ``True`` will use the
:meth:`~ape.api.providers.ProviderAPI.send_private_transaction` method.
sign (bool): ``False`` to not sign the transaction (useful for providers like Titanoboa
which still use a sender but don't need to sign).
**signer_options: Additional kwargs given to the signer to modify the signing operation.
Returns:
Expand Down Expand Up @@ -177,17 +180,21 @@ def call(
else:
txn.value = amount_to_send

signed_txn = self.sign_transaction(txn, **signer_options)
if not signed_txn:
raise SignatureError("The transaction was not signed.")
if sign:
prepared_txn = self.sign_transaction(txn, **signer_options)
if not prepared_txn:
raise SignatureError("The transaction was not signed.")

if not txn.sender:
txn.sender = self.address
else:
prepared_txn = txn

if not prepared_txn.sender:
prepared_txn.sender = self.address

return (
self.provider.send_private_transaction(signed_txn)
self.provider.send_private_transaction(prepared_txn)
if private
else self.provider.send_transaction(signed_txn)
else self.provider.send_transaction(prepared_txn)
)

def transfer(
Expand Down Expand Up @@ -683,7 +690,12 @@ def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[Tr
return txn

def call(
self, txn: TransactionAPI, send_everything: bool = False, private: bool = False, **kwargs
self,
txn: TransactionAPI,
send_everything: bool = False,
private: bool = False,
sign: bool = True,
**kwargs,
) -> ReceiptAPI:
txn = self.prepare_transaction(txn)
txn.sender = txn.sender or self.raw_address
Expand Down
17 changes: 17 additions & 0 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ def test_transfer_mixed_up_sender_and_value(sender, receiver):
sender.transfer("123 wei", receiver)


def test_transfer_sign_is_false(sender, receiver):
with pytest.raises(SignatureError):
sender.transfer(receiver, "1 gwei", sign=False)


def test_deploy(owner, contract_container, clean_contract_caches):
contract = owner.deploy(contract_container, 0)
assert contract.address
Expand Down Expand Up @@ -978,3 +983,15 @@ def test_repr(account_manager):
"""
actual = repr(account_manager)
assert actual == "<AccountManager>"


def test_call(owner, vyper_contract_instance):
tx = vyper_contract_instance.setNumber.as_transaction(5991)
receipt = owner.call(tx)
assert not receipt.failed


def test_call_sign_false(owner, vyper_contract_instance):
tx = vyper_contract_instance.setNumber.as_transaction(5991)
with pytest.raises(SignatureError):
owner.call(tx, sign=False)
6 changes: 6 additions & 0 deletions tests/functional/test_contract_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ContractLogicError,
CustomError,
MethodNonPayableError,
SignatureError,
)
from ape.types.address import AddressType
from ape_ethereum.transactions import TransactionStatusEnum, TransactionType
Expand Down Expand Up @@ -74,6 +75,11 @@ def test_contract_transactions(owner, contract_instance):
assert contract_instance.myNumber() == 2


def test_contract_transaction_when_sign_false(owner, contract_instance):
with pytest.raises(SignatureError):
contract_instance.setNumber(2, sender=owner, sign=False)


def test_wrong_number_of_arguments(owner, contract_instance):
if "sol" in contract_instance.contract_type.source_id.lower():
second = r"\n\t.*setNumber\(uint256 num, address _address\).*"
Expand Down

0 comments on commit 8bf2e2d

Please sign in to comment.