Skip to content

Commit

Permalink
fix: contract-container deploy type checking (#1806)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jan 2, 2024
1 parent eab084e commit 1801129
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/ape/api/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ def deploy(
Returns:
:class:`~ape.contracts.ContractInstance`: An instance of the deployed contract.
"""
from ape.contracts import ContractContainer

# NOTE: It is important to type check here to prevent cases where user
# may accidentally pass in a ContractInstance, which has a very
# different implementation for __call__ than ContractContainer.
if not isinstance(contract, ContractContainer):
raise TypeError(
"contract argument must be a ContractContainer type, "
"such as 'project.MyContract' where 'MyContract' is the name of "
"a contract in your project."
)

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.")
Expand Down
16 changes: 16 additions & 0 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,22 @@ def test_deploy_proxy(owner, vyper_contract_instance, proxy_contract_container,
assert implementation.contract_type == vyper_contract_instance.contract_type


def test_deploy_instance(owner, vyper_contract_instance, chain, clean_contracts_cache):
"""
Tests against a confusing scenario where you would get a SignatureError when
trying to deploy a ContractInstance because Ape would attempt to create a tx
by calling the contract's default handler.
"""

expected = (
r"contract argument must be a ContractContainer type, "
r"such as 'project\.MyContract' where 'MyContract' is the "
r"name of a contract in your project\."
)
with pytest.raises(TypeError, match=expected):
owner.deploy(vyper_contract_instance)


def test_send_transaction_with_bad_nonce(sender, receiver):
# Bump the nonce so we can set one that is too low.
sender.transfer(receiver, "1 gwei", type=0)
Expand Down

0 comments on commit 1801129

Please sign in to comment.