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: Raise warning if value= is specified for anything but a payable function [APE-1283] #1599

Merged
merged 37 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2ec0095
added option for setting the password to unlock accounts from env var…
Aviksaikat May 31, 2023
395e82a
Merge branch 'ApeWorX:main' into feat/accounts-env-passphrase
Aviksaikat May 31, 2023
5171da9
code formmated for according to black & fixed type-check
Aviksaikat May 31, 2023
ec46049
Merge branch 'feat/accounts-env-passphrase' of github.com:Aviksaikat/…
Aviksaikat May 31, 2023
2869a41
added option for setting the password to unlock accounts from env var…
Aviksaikat May 31, 2023
5fbf133
Merge branch 'main' into feat/accounts-env-passphrase
Aviksaikat Jun 1, 2023
67a4cdd
Update docs/userguides/accounts.md
Aviksaikat Jun 1, 2023
ed45f8d
Update docs/userguides/accounts.md
Aviksaikat Jun 1, 2023
4de26f4
Update docs/userguides/accounts.md
Aviksaikat Jun 1, 2023
fcf719d
updated passphrase arg type from Optional to str
Aviksaikat Jun 1, 2023
c7d553d
Merge branch 'main' into feat/accounts-env-passphrase
Aviksaikat Jun 2, 2023
56f5943
Update docs/userguides/accounts.md
Aviksaikat Jun 2, 2023
64e68e1
added tests for loading passphrase from env & sign
Aviksaikat Jun 3, 2023
f5ee6e8
Merge branch 'main' into feat/accounts-env-passphrase
antazoey Jun 5, 2023
248a7ce
Merge branch 'ApeWorX:main' into feat/accounts-env-passphrase
Aviksaikat Jun 9, 2023
3add012
Merge branch 'ApeWorX:main' into feat/accounts-env-passphrase
Aviksaikat Jun 17, 2023
8fa0c75
Merge branch 'ApeWorX:main' into feat/accounts-env-passphrase
Aviksaikat Jun 29, 2023
8f65b3f
Merge branch 'ApeWorX:main' into feat/accounts-env-passphrase
Aviksaikat Aug 1, 2023
13baf41
Merge branch 'ApeWorX:main' into feat/accounts-env-passphrase
Aviksaikat Aug 8, 2023
3554877
Merge branch 'ApeWorX:main' into feat/Raise-warning-if-value
Aviksaikat Aug 10, 2023
f9e4fa0
feat: Raise warning if value= is specified for anything but a payable…
Aviksaikat Aug 10, 2023
3380bab
indentation fixed with black
Aviksaikat Aug 10, 2023
daa44e1
Update src/ape/contracts/base.py
Aviksaikat Aug 11, 2023
ccf6ed7
made a separate exception class for the exception. code refactored
Aviksaikat Aug 11, 2023
c47e037
indentation fixed with black & test added but it's broken. need help
Aviksaikat Aug 11, 2023
9950ed1
indentation fixed with black & test added but it's broken. need help
Aviksaikat Aug 11, 2023
898b1c3
proper exception added
Aviksaikat Aug 11, 2023
7dbb8cb
test_sending_funds_to_non_payable_constructor_by_contractContainerDep…
Aviksaikat Aug 12, 2023
a686816
test_sending_funds_to_non_payable_constructor_by_contractContainerDep…
Aviksaikat Aug 12, 2023
1688a82
ran the fomatter script to pass the linting tests
Aviksaikat Aug 12, 2023
f153b7a
fixed the mess created by flake8. Opps :(
Aviksaikat Aug 12, 2023
9a8cc54
Merge branch 'main' into feat/Raise-warning-if-value
Aviksaikat Aug 12, 2023
9371a02
SendingFundsToNonPayableConstructorError renamed to MethodNonPayableE…
Aviksaikat Aug 13, 2023
23f66ec
SendingFundsToNonPayableConstructorError renamed to MethodNonPayableE…
Aviksaikat Aug 13, 2023
7fc28a2
MethodNonPayableError exception type changed from ApeException to Con…
Aviksaikat Aug 13, 2023
d2bf5c1
Update src/ape/exceptions.py
fubuloubu Aug 16, 2023
26f027c
Merge branch 'main' into feat/Raise-warning-if-value
fubuloubu Aug 17, 2023
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
13 changes: 12 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,
SendingFundsToNonPayableConstructorError,
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,11 @@ def deploy(
:class:`~ape.contracts.ContractInstance`: An instance of the deployed contract.
"""
txn = contract(*args, **kwargs)
Aviksaikat marked this conversation as resolved.
Show resolved Hide resolved
if kwargs.get("value") and not contract.contract_type.constructor.is_payable:
raise SendingFundsToNonPayableConstructorError(
"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
6 changes: 6 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,
SendingFundsToNonPayableConstructorError,
TransactionNotFoundError,
)
from ape.logging import logger
Expand Down Expand Up @@ -1301,6 +1302,11 @@ 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 SendingFundsToNonPayableConstructorError(
"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 @@ -832,3 +832,9 @@ def _get_custom_python_traceback(
tb_next = tb

return frames[-1]


class SendingFundsToNonPayableConstructorError(ApeException):
Aviksaikat marked this conversation as resolved.
Show resolved Hide resolved
"""
Raises when sending funds to a non-payable constructor
"""
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,
SendingFundsToNonPayableConstructorError,
)
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(
SendingFundsToNonPayableConstructorError,
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(
SendingFundsToNonPayableConstructorError,
match="Sending funds to a non-payable constructor.",
):
owner.deploy(solidity_contract_container, 1, value="1 ether")