Skip to content

Commit

Permalink
feat: allow deploying contract types from account (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jun 11, 2024
1 parent 824ec2e commit 6d56b8a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ape/api/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from eth_account import Account
from eth_account.messages import encode_defunct
from eth_pydantic_types import HexBytes
from ethpm_types import ContractType

from ape.api.address import BaseAddress
from ape.api.transactions import ReceiptAPI, TransactionAPI
Expand Down Expand Up @@ -249,10 +250,15 @@ def deploy(
"""
from ape.contracts import ContractContainer

if isinstance(contract, ContractType):
# Hack to allow deploying ContractTypes w/o being
# wrapped in a container first.
contract = ContractContainer(contract)

# 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):
elif not isinstance(contract, ContractContainer):
raise TypeError(
"contract argument must be a ContractContainer type, "
"such as 'project.MyContract' where 'MyContract' is the name of "
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ def test_deploy_no_deployment_bytecode(owner, bytecode):
owner.deploy(contract)


def test_deploy_contract_type(owner, vyper_contract_type, chain, clean_contracts_cache):
contract = owner.deploy(vyper_contract_type, 0)
assert contract.address
assert contract.txn_hash


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
11 changes: 11 additions & 0 deletions tests/functional/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,17 @@ def test_project_api_foundry_and_ape_config_found(foundry_toml):
assert not isinstance(actual, FoundryProject)


def test_get_contract(project_with_contracts):
actual = project_with_contracts.get_contract("Other")
assert isinstance(actual, ContractContainer)
assert actual.contract_type.name == "Other"


def test_get_contract_not_exists(project):
actual = project.get_contract("this is not a contract")
assert actual is None


class TestProject:
"""
All tests related to ``ape.Project``.
Expand Down

0 comments on commit 6d56b8a

Please sign in to comment.