From 49811e705c5c1e324c38758a002d835217c2e37a Mon Sep 17 00:00:00 2001 From: vedant dubey Date: Thu, 2 Jan 2025 16:26:35 +0530 Subject: [PATCH] updated codes --- web3/contract/base_contract.py | 51 ++++++++++++++++++++++++++++++++ web3/contract/contract.py | 53 ++-------------------------------- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/web3/contract/base_contract.py b/web3/contract/base_contract.py index 264764967e..7fb051090d 100644 --- a/web3/contract/base_contract.py +++ b/web3/contract/base_contract.py @@ -718,6 +718,57 @@ def factory( cls, class_name: str, **kwargs: Any ) -> Union["ContractFunction", "AsyncContractFunction"]: return PropertyCheckingFactory(class_name, (cls,), kwargs)() + def __call__( + self, + transaction: Optional[TxParams] = None, + block_identifier: BlockIdentifier = None, + ccip_read_enabled: Optional[bool] = None, + ) -> "ContractCaller": + if transaction is None: + transaction = {} + + return type(self)( + self.abi, + self.w3, + self.address, + transaction=transaction, + block_identifier=block_identifier, + ccip_read_enabled=ccip_read_enabled, + decode_tuples=self.decode_tuples, + ) + def __getattr__(self, function_name: str) -> "ContractFunction": + if super().__getattribute__("abi") is None: + raise NoABIFound( + "There is no ABI found for this contract.", + ) + elif "_functions" not in self.__dict__ or len(self._functions) == 0: + raise NoABIFunctionsFound( + "The abi for this contract contains no function definitions. ", + "Are you sure you provided the correct contract abi?", + ) + elif get_name_from_abi_element_identifier(function_name) not in [ + get_name_from_abi_element_identifier(function["name"]) + for function in self._functions + ]: + raise ABIFunctionNotFound( + f"The function '{function_name}' was not found in this ", + "contract's abi.", + ) + + if "(" not in function_name: + function_name = _get_any_abi_signature_with_name( + function_name, self._functions + ) + else: + function_name = f"_{function_name}" + + return super().__getattribute__( + function_name, + ) + + def __getitem__(self, function_name: str) -> "ContractFunction": + return getattr(self, function_name) + class BaseContractFunctions: diff --git a/web3/contract/contract.py b/web3/contract/contract.py index c27e188e6e..ab3a6646a9 100644 --- a/web3/contract/contract.py +++ b/web3/contract/contract.py @@ -572,39 +572,7 @@ def __iter__(self) -> Iterable["ContractFunction"]: for func in self._functions: yield self[abi_to_signature(func)] - def __getattr__(self, function_name: str) -> "ContractFunction": - if super().__getattribute__("abi") is None: - raise NoABIFound( - "There is no ABI found for this contract.", - ) - elif "_functions" not in self.__dict__ or len(self._functions) == 0: - raise NoABIFunctionsFound( - "The abi for this contract contains no function definitions. ", - "Are you sure you provided the correct contract abi?", - ) - elif get_name_from_abi_element_identifier(function_name) not in [ - get_name_from_abi_element_identifier(function["name"]) - for function in self._functions - ]: - raise ABIFunctionNotFound( - f"The function '{function_name}' was not found in this ", - "contract's abi.", - ) - - if "(" not in function_name: - function_name = _get_any_abi_signature_with_name( - function_name, self._functions - ) - else: - function_name = f"_{function_name}" - - return super().__getattribute__( - function_name, - ) - - def __getitem__(self, function_name: str) -> "ContractFunction": - return getattr(self, function_name) - + class Contract(BaseContract): # mypy types @@ -809,24 +777,7 @@ def __init__( ) setattr(self, str(fn.abi_element_identifier), caller_method) - def __call__( - self, - transaction: Optional[TxParams] = None, - block_identifier: BlockIdentifier = None, - ccip_read_enabled: Optional[bool] = None, - ) -> "ContractCaller": - if transaction is None: - transaction = {} - - return type(self)( - self.abi, - self.w3, - self.address, - transaction=transaction, - block_identifier=block_identifier, - ccip_read_enabled=ccip_read_enabled, - decode_tuples=self.decode_tuples, - ) + class ContractConstructor(BaseContractConstructor):