Skip to content

updated codes #3567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions web3/contract/base_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
53 changes: 2 additions & 51 deletions web3/contract/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down