Skip to content

Commit

Permalink
refactor: typing and clarity improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Apr 29, 2024
1 parent 8e2707d commit 3a2c487
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions uniswap_sdk/universal_router.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import Any, Callable, ClassVar, Dict, List, Optional, Type
from typing import Any, Callable, ClassVar, Dict, Iterable, List, Optional, Type

from ape.api import ReceiptAPI, TransactionAPI
from ape.contracts import ContractInstance
from ape.exceptions import DecodingError
from ape.managers import ManagerAccessMixin, ProjectManager
from ape.utils import StructParser, cached_property
from ape_ethereum.ecosystem import parse_type
from eth_abi import decode, encode
from eth_abi import decode as abi_decode
from eth_abi import encode as abi_encode
from eth_abi.exceptions import InsufficientDataBytes
from ethpm_types.abi import ABIType, MethodABI
from hexbytes import HexBytes
Expand Down Expand Up @@ -61,26 +62,26 @@ def __repr__(self) -> str:
def command_byte(self) -> int:
return (Constants._ALLOW_REVERT_FLAG if self.allow_revert else 0x0) | self.type

# TODO: Complete
def encode_inputs(self) -> HexBytes:
parser = StructParser(MethodABI(name=self.__class__.__name__, inputs=self.inputs))
parser = StructParser(
MethodABI(name=self.__class__.__name__, inputs=self.__class__.definition)
)
arguments = parser.encode_input(self.inputs)
input_types = [i.canonical_type for i in self.definition]
python_types = tuple(
self.provider.network.ecosystem._python_type_for_abi_type(i) for i in self.definition
)
converted_args = self.conversion_manager.convert(arguments, python_types)
encoded_calldata = encode(input_types, converted_args)
encoded_calldata = abi_encode(input_types, converted_args)
return HexBytes(encoded_calldata)

@classmethod
# TODO: Complete
def _decode_inputs(cls, calldata: HexBytes) -> List[Any]:
raw_input_types = [i.canonical_type for i in cls.definition]
input_types = [parse_type(i.model_dump(mode="json")) for i in cls.definition]

try:
raw_input_values = decode(raw_input_types, calldata, strict=False)
raw_input_values = abi_decode(raw_input_types, calldata, strict=False)
except InsufficientDataBytes as err:
raise DecodingError(str(err)) from err

Expand Down Expand Up @@ -358,7 +359,7 @@ class Plan(BaseModel):
commands: List[Command] = []

@classmethod
def decode(cls, encoded_commands: HexBytes, encoded_inputs: List[HexBytes]) -> "Plan":
def decode(cls, encoded_commands: HexBytes, encoded_inputs: Iterable[HexBytes]) -> "Plan":
return cls(
commands=[
Command.decode(command_byte, encoded_args)
Expand Down

0 comments on commit 3a2c487

Please sign in to comment.