ape.contracts

class ape.contracts.base.ContractInstance(address: ChecksumAddress, contract_type: ethpm_types.contract_type.ContractType)

Bases: ape.api.address.BaseAddress

An interactive instance of a smart contract. After you deploy a contract using the deploy method, you get back a contract instance.

Usage example:

from ape import accounts, project

a = accounts.load("alias")  # Load an account by alias
contract = a.deploy(project.MyContract)  # The result of 'deploy()' is a ContractInstance
__dir__() List[str]

Display methods to IPython on c.[TAB] tab completion.

Returns

List[str]

__getattr__(attr_name: str) Any

Access a method, property, or event on the contract using . access.

Usage example:

result = contract.vote()  # Implies a method named "vote" exists on the contract.
Parameters

attr_name (str) – The name of the method or property to access.

Returns

The return value from the contract call, or a transaction receipt.

Return type

Any

property address: ChecksumAddress

The address of the contract.

Returns

AddressType

get_event_by_signature(signature: str) ape.contracts.base.ContractEvent

Get an event by its signature. Most often, you can use the __getattr__() method on this class to access events. However, in the case when you have more than one event with the same name, such as the case where one event is coming from a base contract, you can use this method to access the respective events.

Parameters

signature (str) – The signature of the event.

Returns

ContractEvent

class ape.contracts.base.ContractContainer(contract_type: ethpm_types.contract_type.ContractType)

Bases: ape.utils.basemodel.ManagerAccessMixin

A wrapper around the contract type that has access to the provider. When you import your contracts from the ape.managers.project.ProjectManager, you are using this class.

Usage example:

from ape import project

contract_container = project.MyContract  # Assuming there is a contract named "MyContract"
at(address: str) ape.contracts.base.ContractInstance

Get a contract at the given address.

Usage example:

from ape import project

my_contract = project.MyContract.at("0xAbC1230001112223334445566611855443322111")
Parameters

address (str) – The address to initialize a contract. NOTE: Things will not work as expected if the contract is not actually deployed to this address or if the contract at the given address has a different ABI than contract_type.

Returns

ContractInstance

class ape.contracts.base.ContractEvent(contract: ape.contracts.base.ContractInstance, abi: ethpm_types.abi.EventABI, cached_logs: Optional[List[ape.types.ContractLog]] = None)

Bases: ape.utils.basemodel.ManagerAccessMixin

The types of events on a ContractInstance. Use the event types via . access on the contract instances.

Usage example:

# 'my_contract' refers to a ContractInstance in this case.
my_event_type = my_contract.MyEvent
__iter__() Iterator[ape.types.ContractLog]

Get all logs that have occurred for this event.

from_receipt(receipt: ape.api.transactions.ReceiptAPI) Iterator[ape.types.ContractLog]

Get all the events from the given receipt.

Parameters

receipt (ReceiptAPI) – The receipt containing the logs.

Returns

Iterator[ContractLog]

property name: str

The name of the contract event, as defined in the contract.

poll_logs(start_block: Optional[int] = None, stop_block: Optional[int] = None, required_confirmations: Optional[int] = None) Iterator[ape.types.ContractLog]

Poll new blocks. Optionally set a start block to include historical blocks. NOTE: This is a daemon method; it does not terminate unless an exception occurrs.

Usage example:

from ape import chain

for new_block in chain.blocks.poll_blocks():
    print(f"New block found: number={new_block.number}")
Parameters
  • start_block (Optional[int]) – The block number to start with. Defaults to the pending block number.

  • stop_block (Optional[int]) – Optionally set a future block number to stop at. Defaults to never-ending.

  • required_confirmations (Optional[int]) – The amount of confirmations to wait before yielding the block. The more confirmations, the less likely a reorg will occur. Defaults to the network’s configured required confirmations.

Returns

Iterator[ContractLog]

range(start_or_stop: int, stop: Optional[int] = None, block_page_size: Optional[int] = None, event_parameters: Optional[Dict] = None, extra_addresses: Optional[List] = None) Iterator[ape.types.ContractLog]

Search through the logs for this event using the given filter parameters.

Parameters
  • start_or_stop (int) – When also given stop, this is the the earliest block number in the desired log set. Otherwise, it is the total amount of blocks to get starting from 0.

  • stop (Optional[int]) – The latest block number in the desired log set. Defaults to delegating to provider.

  • block_page_size (Optional[int]) – The amount of block to request on each page.

  • event_parameters (Optional[Dict]) – Arguments on the event that you can search for.

  • extra_addresses (Optional[List[AddressType]]) – Additional contract addresses containing the same event type. Defaults to only looking at the contract instance where this event is defined.

Returns

Iterator[ContractLog]