Skip to content

Commit

Permalink
lint fixes and regen
Browse files Browse the repository at this point in the history
  • Loading branch information
sentilesdal committed Feb 1, 2024
1 parent 8bfbcb9 commit 314bab4
Show file tree
Hide file tree
Showing 22 changed files with 258 additions and 11 deletions.
53 changes: 44 additions & 9 deletions example/types/ExampleContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, Iterable, NamedTuple, Sequence, Type, cast

from eth_abi.codec import ABICodec
from eth_abi.registry import registry as default_registry
from eth_account.signers.local import LocalAccount
from eth_typing import ChecksumAddress, HexStr
from hexbytes import HexBytes
Expand All @@ -39,10 +45,10 @@
ContractFunctions,
)
from web3.exceptions import FallbackNotFound
from web3.types import ABI, BlockIdentifier, CallOverride, EventData, TxParams
from web3.types import ABI, ABIFunction, BlockIdentifier, CallOverride, EventData, TxParams

from .ExampleTypes import InnerStruct, NestedStruct, SimpleStruct
from .utilities import dataclass_to_tuple, rename_returned_types
from .utilities import dataclass_to_tuple, get_abi_input_types, rename_returned_types

structs = {
"SimpleStruct": SimpleStruct,
Expand Down Expand Up @@ -622,17 +628,32 @@ def __init__(
def decode_error_data( # type: ignore
self: "ExampleWrongChoiceContractError",
data: HexBytes,
# TODO: get the return type here
) -> str:
# do the decoding
return "data goes here."
# TODO: instead of returning a tuple, return a dataclass with the input names and types just like we do for functions
) -> tuple[Any, ...]:
"""Decodes error data returns from a smart contract."""
error_abi = cast(
ABIFunction,
[item for item in example_abi if item.get("name") == "WrongChoice" and item.get("type") == "error"][0],
)
types = get_abi_input_types(error_abi)
abi_codec = ABICodec(default_registry)
decoded = abi_codec.decode(types, data)
return decoded

@classmethod
def decode_error_data( # type: ignore
cls: Type["ExampleWrongChoiceContractError"],
data: HexBytes,
) -> str:
return "data goes here."
) -> tuple[Any, ...]:
"""Decodes error data returns from a smart contract."""
error_abi = cast(
ABIFunction,
[item for item in example_abi if item.get("name") == "WrongChoice" and item.get("type") == "error"][0],
)
types = get_abi_input_types(error_abi)
abi_codec = ABICodec(default_registry)
decoded = abi_codec.decode(types, data)
return decoded


class ExampleContractErrors:
Expand All @@ -645,6 +666,19 @@ def __init__(
) -> None:
self.WrongChoice = ExampleWrongChoiceContractError()

self._all = [
self.WrongChoice,
]

def decode_custom_error(self, data: str) -> tuple[Any, ...]:
"""Decodes a custom contract error."""
selector = data[:10]
for err in self._all:
if err.selector == selector:
return err.decode_error_data(HexBytes(data[10:]))

raise ValueError(f"Example does not have a selector matching {selector}")


example_abi: ABI = cast(
ABI,
Expand Down Expand Up @@ -929,7 +963,7 @@ def __init__(self, address: ChecksumAddress | None = None) -> None:

events: ExampleContractEvents

errors: ExampleContractErrors
errors: ExampleContractErrors = ExampleContractErrors()

functions: ExampleContractFunctions

Expand Down Expand Up @@ -1019,5 +1053,6 @@ def factory(cls, w3: Web3, class_name: str | None = None, **kwargs: Any) -> Type
"""
contract = super().factory(w3, class_name, **kwargs)
contract.functions = ExampleContractFunctions(example_abi, w3, None)
contract.errors = ExampleContractErrors()

return contract
27 changes: 27 additions & 0 deletions example/types/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from dataclasses import fields, is_dataclass
from typing import Any, Tuple, TypeVar, cast

from eth_utils.abi import collapse_if_tuple
from web3.types import ABIFunction

T = TypeVar("T")


Expand Down Expand Up @@ -108,3 +111,27 @@ def rename_returned_types(
# cover case of single return value
converted_value = tuple_to_dataclass(return_types, structs, raw_values)
return converted_value


def get_abi_input_types(abi: ABIFunction) -> list[str]:
"""Gets all the solidity input types for a function or error.
Cribbed from web3._utils.abi.py file.
Parameters
----------
abi: ABIFunction
The ABIFunction or ABIError that we want to get input types for.
Returns
-------
list[str]
A list of solidity input types.
"""

if "inputs" not in abi and (abi.get("type") == "fallback" or abi.get("type") == "receive"):
return []
else:
return [collapse_if_tuple(cast(dict[str, Any], arg)) for arg in abi.get("inputs", [])]
2 changes: 1 addition & 1 deletion pypechain/render/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ def render_contract_file(contract_info: ContractInfo) -> str | None:
str
A serialized python file.
"""
# pylint: disable=too-many-locals
# if the abi is empty, then we are dealing with an interface or library so we don't want to
# create a contract file for it.
if contract_info.abi == []:
return None
# TODO: break this function up or bundle arguments to save on variables
# pylint: disable=too-many-locals

env = get_jinja_env()
templates = get_templates_for_contract_file(env)
Expand Down
4 changes: 4 additions & 0 deletions pypechain/templates/contract.py/base.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ https://github.com/delvtech/pypechain"""
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from dataclasses import fields, is_dataclass
Expand Down
2 changes: 2 additions & 0 deletions pypechain/templates/contract.py/contract.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ class {{contract_name}}Contract(Contract):
"""
contract = super().factory(w3, class_name, **kwargs)
contract.functions = {{contract_name}}ContractFunctions({{contract_name | lower}}_abi, w3, None)
{% if has_errors -%}
contract.errors = {{contract_name}}ContractErrors()
{%- endif %}

return contract

4 changes: 4 additions & 0 deletions pypechain/test/deployment/types/ConstructorNoArgsContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, Type, cast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, NamedTuple, Type, cast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, NamedTuple, Type, cast
Expand Down
4 changes: 4 additions & 0 deletions pypechain/test/deployment/types/NoConstructorContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, Type, cast
Expand Down
27 changes: 27 additions & 0 deletions pypechain/test/deployment/types/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from dataclasses import fields, is_dataclass
from typing import Any, Tuple, TypeVar, cast

from eth_utils.abi import collapse_if_tuple
from web3.types import ABIFunction

T = TypeVar("T")


Expand Down Expand Up @@ -108,3 +111,27 @@ def rename_returned_types(
# cover case of single return value
converted_value = tuple_to_dataclass(return_types, structs, raw_values)
return converted_value


def get_abi_input_types(abi: ABIFunction) -> list[str]:
"""Gets all the solidity input types for a function or error.
Cribbed from web3._utils.abi.py file.
Parameters
----------
abi: ABIFunction
The ABIFunction or ABIError that we want to get input types for.
Returns
-------
list[str]
A list of solidity input types.
"""

if "inputs" not in abi and (abi.get("type") == "fallback" or abi.get("type") == "receive"):
return []
else:
return [collapse_if_tuple(cast(dict[str, Any], arg)) for arg in abi.get("inputs", [])]
2 changes: 1 addition & 1 deletion pypechain/test/errors/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_error_two(self, w3):
abi_codec = ABICodec(default_registry)
decoded = abi_codec.decode(types, HexBytes(data))
assert decoded == (
"I will not pledge allegiance to Bart. I will not pledge allegiance to Bart. I will not pledge allegiance to Bart.",
"I will not pledge allegiance to Bart. I will not pledge allegiance to Bart. I will not pledge allegiance to Bart.", # pylint: disable=line-too-long
"0x0000000000000000000000000000000000000000",
255,
)
Expand Down
4 changes: 4 additions & 0 deletions pypechain/test/errors/types/ErrorsContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, Type, cast
Expand Down
4 changes: 4 additions & 0 deletions pypechain/test/events/types/EventsContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, Iterable, Sequence, Type, cast
Expand Down
27 changes: 27 additions & 0 deletions pypechain/test/events/types/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from dataclasses import fields, is_dataclass
from typing import Any, Tuple, TypeVar, cast

from eth_utils.abi import collapse_if_tuple
from web3.types import ABIFunction

T = TypeVar("T")


Expand Down Expand Up @@ -108,3 +111,27 @@ def rename_returned_types(
# cover case of single return value
converted_value = tuple_to_dataclass(return_types, structs, raw_values)
return converted_value


def get_abi_input_types(abi: ABIFunction) -> list[str]:
"""Gets all the solidity input types for a function or error.
Cribbed from web3._utils.abi.py file.
Parameters
----------
abi: ABIFunction
The ABIFunction or ABIError that we want to get input types for.
Returns
-------
list[str]
A list of solidity input types.
"""

if "inputs" not in abi and (abi.get("type") == "fallback" or abi.get("type") == "receive"):
return []
else:
return [collapse_if_tuple(cast(dict[str, Any], arg)) for arg in abi.get("inputs", [])]
4 changes: 4 additions & 0 deletions pypechain/test/overloading/types/OverloadedMethodsContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, NamedTuple, Type, cast, overload
Expand Down
27 changes: 27 additions & 0 deletions pypechain/test/overloading/types/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from dataclasses import fields, is_dataclass
from typing import Any, Tuple, TypeVar, cast

from eth_utils.abi import collapse_if_tuple
from web3.types import ABIFunction

T = TypeVar("T")


Expand Down Expand Up @@ -108,3 +111,27 @@ def rename_returned_types(
# cover case of single return value
converted_value = tuple_to_dataclass(return_types, structs, raw_values)
return converted_value


def get_abi_input_types(abi: ABIFunction) -> list[str]:
"""Gets all the solidity input types for a function or error.
Cribbed from web3._utils.abi.py file.
Parameters
----------
abi: ABIFunction
The ABIFunction or ABIError that we want to get input types for.
Returns
-------
list[str]
A list of solidity input types.
"""

if "inputs" not in abi and (abi.get("type") == "fallback" or abi.get("type") == "receive"):
return []
else:
return [collapse_if_tuple(cast(dict[str, Any], arg)) for arg in abi.get("inputs", [])]
4 changes: 4 additions & 0 deletions pypechain/test/return_types/types/ReturnTypesContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# methods are overriden with specific arguments instead of generic *args, **kwargs
# pylint: disable=arguments-differ

# consumers have too many opinions on line length
# pylint: disable=line-too-long


from __future__ import annotations

from typing import Any, NamedTuple, Type, cast
Expand Down
Loading

0 comments on commit 314bab4

Please sign in to comment.