Skip to content

Commit

Permalink
feat: Raise warning if value= is specified for anything but a `paya…
Browse files Browse the repository at this point in the history
…ble` function [APE-1283] (#1599)

* added option for setting the password to unlock accounts from env variables and docs updated

* code formmated for according to black & fixed type-check

* added option for setting the password to unlock accounts from env variables and docs updated

* Update docs/userguides/accounts.md

Co-authored-by: antazoey <admin@antazoey.me>

* Update docs/userguides/accounts.md

Co-authored-by: antazoey <admin@antazoey.me>

* Update docs/userguides/accounts.md

Co-authored-by: antazoey <admin@antazoey.me>

* updated passphrase arg type from Optional to str

* Update docs/userguides/accounts.md

Co-authored-by: antazoey <admin@antazoey.me>

* added tests for loading passphrase from env & sign

* feat: Raise warning if value= is specified for anything but a payable function [APE-874] #1396

* indentation fixed with black

* Update src/ape/contracts/base.py

Co-authored-by: El De-dog-lo <3859395+fubuloubu@users.noreply.github.com>

* made a separate exception class for the exception. code refactored

* indentation fixed with black & test added but it's broken. need help

* indentation fixed with black & test added but it's broken. need help

* proper exception added

* test_sending_funds_to_non_payable_constructor_by_contractContainerDeploy test added & working on test_sending_funds_to_non_payable_constructor_by_accountDeploy

* test_sending_funds_to_non_payable_constructor_by_contractContainerDeploy test added & test_sending_funds_to_non_payable_constructor_by_accountDeploy & hopefully final commit. file updated, ~ape/api/accounts.py, ~ape/contracts/base.py, ~ape/exceptions.py, tests/functional/test_contract_instance.py

* ran the fomatter script to pass the linting tests

* fixed the mess created by flake8. Opps :(

* SendingFundsToNonPayableConstructorError renamed to MethodNonPayableError & exception type changed from ApeException to ContractError

* SendingFundsToNonPayableConstructorError renamed to MethodNonPayableError & exception type changed from ApeException to ContractError. fixing the last blunder

* MethodNonPayableError exception type changed from ApeException to ContractError

* Update src/ape/exceptions.py

---------

Co-authored-by: antazoey <admin@antazoey.me>
Co-authored-by: antazoey <jules@apeworx.io>
Co-authored-by: El De-dog-lo <3859395+fubuloubu@users.noreply.github.com>
  • Loading branch information
4 people authored Aug 17, 2023
1 parent 9f8eace commit bd53369
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/ape/api/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

from ape.api.address import BaseAddress
from ape.api.transactions import ReceiptAPI, TransactionAPI
from ape.exceptions import AccountsError, AliasAlreadyInUseError, SignatureError, TransactionError
from ape.exceptions import (
AccountsError,
AliasAlreadyInUseError,
MethodNonPayableError,
SignatureError,
TransactionError,
)
from ape.logging import logger
from ape.types import AddressType, MessageSignature, SignableMessage
from ape.utils import BaseInterfaceModel, abstractmethod
Expand Down Expand Up @@ -215,6 +221,9 @@ def deploy(
:class:`~ape.contracts.ContractInstance`: An instance of the deployed contract.
"""
txn = contract(*args, **kwargs)
if kwargs.get("value") and not contract.contract_type.constructor.is_payable:
raise MethodNonPayableError("Sending funds to a non-payable constructor.")

txn.sender = self.address
receipt = contract._cache_wrap(lambda: self.call(txn, **kwargs))
if not (address := receipt.contract_address):
Expand Down
4 changes: 4 additions & 0 deletions src/ape/contracts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ContractError,
ContractLogicError,
CustomError,
MethodNonPayableError,
TransactionNotFoundError,
)
from ape.logging import logger
Expand Down Expand Up @@ -1301,6 +1302,9 @@ def deploy(self, *args, publish: bool = False, **kwargs) -> ContractInstance:
txn = self(*args, **kwargs)
private = kwargs.get("private", False)

if kwargs.get("value") and not self.contract_type.constructor.is_payable:
raise MethodNonPayableError("Sending funds to a non-payable constructor.")

if "sender" in kwargs and isinstance(kwargs["sender"], AccountAPI):
# Handle account-related preparation if needed, such as signing
receipt = self._cache_wrap(lambda: kwargs["sender"].call(txn, **kwargs))
Expand Down
6 changes: 6 additions & 0 deletions src/ape/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ def __init__(self, message: Optional[str] = None):
super().__init__(message)


class MethodNonPayableError(ContractError):
"""
Raises when sending funds to a non-payable method
"""


class TransactionError(ContractError):
"""
Raised when issues occur related to transactions.
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/test_contract_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ContractError,
ContractLogicError,
CustomError,
MethodNonPayableError,
)
from ape.types import AddressType
from ape_ethereum.transactions import TransactionStatusEnum
Expand Down Expand Up @@ -850,3 +851,23 @@ def test_contract_declared_from_blueprint(
# Ensure we can invoke a method on that contract.
receipt = instance.setAddress(sender, sender=sender)
assert not receipt.failed


def test_sending_funds_to_non_payable_constructor_by_contractContainerDeploy(
solidity_contract_container, owner
):
with pytest.raises(
MethodNonPayableError,
match="Sending funds to a non-payable constructor.",
):
solidity_contract_container.deploy(1, sender=owner, value="1 ether")


def test_sending_funds_to_non_payable_constructor_by_accountDeploy(
solidity_contract_container, owner
):
with pytest.raises(
MethodNonPayableError,
match="Sending funds to a non-payable constructor.",
):
owner.deploy(solidity_contract_container, 1, value="1 ether")

0 comments on commit bd53369

Please sign in to comment.