Skip to content

Commit

Permalink
refactor: contract instance receipt
Browse files Browse the repository at this point in the history
- remove .receipt as it's available as .creation.receipt
- precompute .creation when creating .from_receipt
  • Loading branch information
banteg committed Apr 12, 2024
1 parent f50871a commit 0a90376
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
9 changes: 9 additions & 0 deletions src/ape/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ class ContractCreation(BaseModel, BaseInterface):
def receipt(self):
return self.chain_manager.get_receipt(self.txn_hash)

@classmethod
def from_receipt(cls, receipt: ReceiptAPI):
return cls(
txn_hash=receipt.txn_hash,
block=receipt.block_number,
deployer=receipt.sender,
# factory is not detected since this is meant for eoa deployments
)


class ContractEventQuery(_BaseBlockQuery):
"""
Expand Down
31 changes: 2 additions & 29 deletions src/ape/contracts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
CustomError,
MethodNonPayableError,
MissingDeploymentBytecodeError,
TransactionNotFoundError,
)
from ape.logging import logger
from ape.types import AddressType, ContractLog, LogFilter, MockContractLog
Expand Down Expand Up @@ -837,7 +836,6 @@ def __init__(
self._address = address
self.contract_type = contract_type
self.txn_hash = txn_hash
self._cached_receipt: Optional[ReceiptAPI] = None

def __call__(self, *args, **kwargs) -> ReceiptAPI:
has_value = kwargs.get("value")
Expand Down Expand Up @@ -876,38 +874,13 @@ def from_receipt(cls, receipt: ReceiptAPI, contract_type: ContractType) -> "Cont
contract_type=contract_type,
txn_hash=receipt.txn_hash,
)
instance._cached_receipt = receipt
instance.creation = ContractCreation.from_receipt(receipt)
return instance

@property
def receipt(self) -> ReceiptAPI:
"""
The receipt associated with deploying the contract instance,
if it is known and exists.
"""

if self._cached_receipt:
return self._cached_receipt

if self.txn_hash:
# Hash is known. Use that to get the receipt.
try:
receipt = self.chain_manager.get_receipt(self.txn_hash)
except (TransactionNotFoundError, ValueError, ChainError):
pass
else:
self._cached_receipt = receipt
return receipt

# query the deployment to find the receipt
receipt = self.chain_manager.get_receipt(self.creation.txn_hash)
self._cached_receipt = receipt
return receipt

@cached_property
def creation(self) -> ContractCreation:
"""
Contract creation details: txn_hash, block, deployer, factory.
Contract creation details: txn_hash, block, deployer, factory, receipt.
"""
data = self.chain_manager.contracts.get_creation_metadata(self.address)
if data is not None:
Expand Down

0 comments on commit 0a90376

Please sign in to comment.