Skip to content

Commit 0e04f4f

Browse files
authored
#127 - Replace deprecated typing features
- typing.Dict → dict - typing.List → list - typing.Tuple → tuple - typing.Sequence → collections.abc.Sequence
1 parent d8c4213 commit 0e04f4f

File tree

5 files changed

+32
-44
lines changed

5 files changed

+32
-44
lines changed

uniswap_universal_router_decoder/_abi_builder.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
"""
88
from __future__ import annotations
99

10+
from collections.abc import Sequence
1011
from io import BytesIO
1112
from typing import (
1213
Any,
1314
cast,
14-
Dict,
15-
List,
1615
Optional,
17-
Sequence,
1816
Union,
1917
)
2018

@@ -30,7 +28,7 @@
3028
)
3129

3230

33-
def _get_types_from_list(type_list: List[Any]) -> List[str]:
31+
def _get_types_from_list(type_list: list[Any]) -> list[str]:
3432
types = []
3533
for item in type_list:
3634
if item["type"][:5] == "tuple":
@@ -41,28 +39,28 @@ def _get_types_from_list(type_list: List[Any]) -> List[str]:
4139
return types
4240

4341

44-
def build_abi_type_list(abi_dict: Dict[str, Any]) -> List[str]:
42+
def build_abi_type_list(abi_dict: dict[str, Any]) -> list[str]:
4543
return _get_types_from_list(abi_dict["inputs"])
4644

4745

4846
class FunctionABI:
49-
def __init__(self, inputs: List[Any], name: str, _type: str) -> None:
47+
def __init__(self, inputs: list[Any], name: str, _type: str) -> None:
5048
self.inputs = inputs
5149
self.name = name
5250
self.type = _type
5351

54-
def get_abi(self) -> Dict[str, Any]:
52+
def get_abi(self) -> dict[str, Any]:
5553
return {"inputs": self.inputs, "name": self.name, "type": self.type}
5654

57-
def get_struct_abi(self) -> Dict[str, Any]:
55+
def get_struct_abi(self) -> dict[str, Any]:
5856
result = self.get_abi()
5957
result["components"] = result.pop("inputs")
6058
return result
6159

62-
def get_full_abi(self) -> List[Dict[str, Any]]:
60+
def get_full_abi(self) -> list[dict[str, Any]]:
6361
return [self.get_abi()]
6462

65-
def get_abi_types(self) -> List[str]:
63+
def get_abi_types(self) -> list[str]:
6664
return build_abi_type_list(self.get_abi())
6765

6866
def get_signature(self) -> str:
@@ -75,7 +73,7 @@ def encode(self, args: Sequence[Any]) -> bytes:
7573
return encode(self.get_abi_types(), args)
7674

7775

78-
ABIMap = Dict[Union[MiscFunctions, RouterFunction, V4Actions], FunctionABI]
76+
ABIMap = dict[Union[MiscFunctions, RouterFunction, V4Actions], FunctionABI]
7977

8078

8179
class FunctionABIBuilder:
@@ -438,12 +436,12 @@ def _build_v4_swap_exact_in() -> FunctionABI:
438436
builder = FunctionABIBuilder(V4Actions.SWAP_EXACT_IN.name)
439437
return builder.add_v4_exact_input_params().build()
440438

441-
def decode_v4_exact_input_params(self, stream: BytesIO) -> Dict[str, Any]:
439+
def decode_v4_exact_input_params(self, stream: BytesIO) -> dict[str, Any]:
442440
fct_abi = self.abi_map[MiscFunctions.STRICT_V4_SWAP_EXACT_IN]
443441
raw_data = stream.read()
444442
sub_contract = self.w3.eth.contract(abi=fct_abi.get_full_abi())
445443
fct_name, decoded_params = sub_contract.decode_function_input(fct_abi.get_selector() + raw_data[32:])
446-
return cast(Dict[str, Any], decoded_params)
444+
return cast(dict[str, Any], decoded_params)
447445

448446
def encode_v4_exact_input_params(self, args: Sequence[Any]) -> bytes:
449447
fct_abi = self.abi_map[MiscFunctions.STRICT_V4_SWAP_EXACT_IN]
@@ -455,12 +453,12 @@ def _build_v4_swap_exact_out() -> FunctionABI:
455453
builder = FunctionABIBuilder(V4Actions.SWAP_EXACT_OUT.name)
456454
return builder.add_v4_exact_output_params().build()
457455

458-
def decode_v4_exact_output_params(self, stream: BytesIO) -> Dict[str, Any]:
456+
def decode_v4_exact_output_params(self, stream: BytesIO) -> dict[str, Any]:
459457
fct_abi = self.abi_map[MiscFunctions.STRICT_V4_SWAP_EXACT_OUT]
460458
raw_data = stream.read()
461459
sub_contract = self.w3.eth.contract(abi=fct_abi.get_full_abi())
462460
fct_name, decoded_params = sub_contract.decode_function_input(fct_abi.get_selector() + raw_data[32:])
463-
return cast(Dict[str, Any], decoded_params)
461+
return cast(dict[str, Any], decoded_params)
464462

465463
def encode_v4_exact_output_params(self, args: Sequence[Any]) -> bytes:
466464
fct_abi = self.abi_map[MiscFunctions.STRICT_V4_SWAP_EXACT_OUT]

uniswap_universal_router_decoder/_decoder.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
* License: MIT.
66
* Doc: https://github.com/Elnaril/uniswap-universal-router-decoder
77
"""
8+
from collections.abc import Sequence
89
from itertools import chain
910
import json
1011
from typing import (
1112
Any,
12-
Dict,
13-
List,
14-
Sequence,
15-
Tuple,
1613
Union,
1714
)
1815

@@ -54,7 +51,7 @@ def __init__(self, w3: Web3, abi_map: ABIMap) -> None:
5451
def _decode_v4_actions(
5552
self,
5653
actions: bytes,
57-
params: List[bytes]) -> List[Tuple[BaseContractFunction, Dict[str, Any]]]:
54+
params: list[bytes]) -> list[tuple[BaseContractFunction, dict[str, Any]]]:
5855
if len(actions) != len(params):
5956
raise ValueError(f"Number of actions {len(actions)} is different from number of params: {len(params)}")
6057

@@ -69,10 +66,10 @@ def _decode_v4_actions(
6966
decoded_params.append(params[i].hex())
7067
return decoded_params
7168

72-
def decode_v4_swap(self, actions: bytes, params: List[bytes]) -> List[Tuple[BaseContractFunction, Dict[str, Any]]]:
69+
def decode_v4_swap(self, actions: bytes, params: list[bytes]) -> list[tuple[BaseContractFunction, dict[str, Any]]]:
7370
return self._decode_v4_actions(actions, params)
7471

75-
def decode_v4_pm_call(self, encoded_input: bytes) -> Dict[str, Any]:
72+
def decode_v4_pm_call(self, encoded_input: bytes) -> dict[str, Any]:
7673
actions, params = decode(["bytes", "bytes[]"], encoded_input)
7774
return {"actions": actions, "params": self._decode_v4_actions(actions, params)}
7875

@@ -84,7 +81,7 @@ def __init__(self, w3: Web3, abi_map: ABIMap) -> None:
8481
self._abi_map = abi_map
8582
self._v4_decoder = _V4Decoder(w3, abi_map)
8683

87-
def function_input(self, input_data: Union[HexStr, HexBytes]) -> Tuple[BaseContractFunction, Dict[str, Any]]:
84+
def function_input(self, input_data: Union[HexStr, HexBytes]) -> tuple[BaseContractFunction, dict[str, Any]]:
8885
"""
8986
Decode the data sent to an UR function
9087
@@ -147,7 +144,7 @@ def function_input(self, input_data: Union[HexStr, HexBytes]) -> Tuple[BaseContr
147144
decoded_input["inputs"] = decoded_command_input
148145
return fct_name, decoded_input
149146

150-
def transaction(self, trx_hash: Union[HexBytes, HexStr]) -> Dict[str, Any]:
147+
def transaction(self, trx_hash: Union[HexBytes, HexStr]) -> dict[str, Any]:
151148
"""
152149
Get transaction details and decode the data used to call a UR function.
153150
@@ -166,7 +163,7 @@ def _get_transaction(self, trx_hash: Union[HexBytes, HexStr]) -> TxData:
166163
return self._w3.eth.get_transaction(trx_hash)
167164

168165
@staticmethod
169-
def v3_path(v3_fn_name: str, path: Union[bytes, str]) -> Tuple[Union[int, ChecksumAddress], ...]:
166+
def v3_path(v3_fn_name: str, path: Union[bytes, str]) -> tuple[Union[int, ChecksumAddress], ...]:
170167
"""
171168
Decode a V3 router path
172169
@@ -180,8 +177,8 @@ def v3_path(v3_fn_name: str, path: Union[bytes, str]) -> Tuple[Union[int, Checks
180177
raise ValueError(f"v3_fn_name must be in {valid_fn_names}")
181178
path_str = path.hex() if isinstance(path, bytes) else str(path)
182179
path_str = path_str[2:] if path_str.startswith("0x") else path_str
183-
path_list: List[Union[int, ChecksumAddress]] = [Web3.to_checksum_address(path_str[0:40]), ]
184-
parsed_remaining_path: List[List[Union[int, ChecksumAddress]]] = [
180+
path_list: list[Union[int, ChecksumAddress]] = [Web3.to_checksum_address(path_str[0:40]), ]
181+
parsed_remaining_path: list[list[Union[int, ChecksumAddress]]] = [
185182
[
186183
int(path_str[40:][i:i + 6], 16),
187184
Web3.to_checksum_address(path_str[40:][i + 6:i + 46]),
@@ -199,7 +196,7 @@ def contract_error(
199196
self,
200197
contract_error: Union[str, HexStr],
201198
abis: Sequence[str] = (_permit2_abi, _pool_manager_abi, _position_manager_abi, _router_abi),
202-
) -> Tuple[str, Dict[str, Any]]:
199+
) -> tuple[str, dict[str, Any]]:
203200
"""
204201
Decode contract custom errors.
205202

uniswap_universal_router_decoder/_encoder.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
from __future__ import annotations
99

1010
from abc import ABC
11+
from collections.abc import Sequence
1112
from typing import (
1213
Any,
1314
cast,
14-
Dict,
15-
List,
1615
Optional,
17-
Sequence,
1816
TypedDict,
1917
TypeVar,
2018
Union,
@@ -182,7 +180,7 @@ def __init__(self, builder: _ChainedFunctionBuilder, w3: Web3, abi_map: ABIMap):
182180
self._w3 = w3
183181
self._abi_map = abi_map
184182
self.actions: bytearray = bytearray()
185-
self.arguments: List[bytes] = []
183+
self.arguments: list[bytes] = []
186184

187185
def _add_action(self, action: V4Actions, args: Sequence[Any]) -> None:
188186
abi = self._abi_map[action]
@@ -523,7 +521,7 @@ def __init__(self, w3: Web3, abi_map: ABIMap):
523521
self._router_contract = self._w3.eth.contract(abi=_router_abi)
524522
self._abi_map = abi_map
525523
self.commands: bytearray = bytearray()
526-
self.arguments: List[bytes] = []
524+
self.arguments: list[bytes] = []
527525

528526
def _add_command(self, command: RouterFunction, args: Sequence[Any], add_selector: bool = False) -> None:
529527
abi = self._abi_map[command]
@@ -758,7 +756,7 @@ def v3_swap_exact_out(
758756

759757
def permit2_permit(
760758
self,
761-
permit_single: Dict[str, Any],
759+
permit_single: dict[str, Any],
762760
signed_permit_single: SignedMessage) -> _ChainedFunctionBuilder:
763761
"""
764762
Encode the call to the function PERMIT2_PERMIT, which gives token allowances to the Permit2 contract.

uniswap_universal_router_decoder/router_codec.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
from datetime import datetime
99
from typing import (
1010
Any,
11-
Dict,
1211
Optional,
13-
Tuple,
1412
)
1513

1614
from eth_account.messages import (
@@ -84,7 +82,7 @@ def create_permit2_signable_message(
8482
spender: ChecksumAddress,
8583
deadline: int,
8684
chain_id: int = 1,
87-
verifying_contract: ChecksumAddress = _permit2_address) -> Tuple[Dict[str, Any], SignableMessage]:
85+
verifying_contract: ChecksumAddress = _permit2_address) -> tuple[dict[str, Any], SignableMessage]:
8886
"""
8987
Create a eth_account.messages.SignableMessage that will be sent to the UR/Permit2 contracts
9088
to set token permissions through signature validation.
@@ -136,7 +134,7 @@ def fetch_permit2_allowance(
136134
spender: ChecksumAddress = _ur_address,
137135
permit2: ChecksumAddress = _permit2_address,
138136
permit2_abi: str = _permit2_abi,
139-
block_identifier: BlockIdentifier = "latest") -> Tuple[Wei, int, Nonce]:
137+
block_identifier: BlockIdentifier = "latest") -> tuple[Wei, int, Nonce]:
140138
"""
141139
Request the permit2 allowance function to know if the UR has enough valid allowance,
142140
and to get the current permit2 nonce for a given wallet and token.

uniswap_universal_router_decoder/utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
from collections.abc import Sequence
12
from statistics import quantiles
2-
from typing import (
3-
cast,
4-
Sequence,
5-
Tuple,
6-
)
3+
from typing import cast
74

85
from web3 import Web3
96
from web3.types import (
@@ -26,7 +23,7 @@
2623
def compute_gas_fees(
2724
w3: Web3,
2825
trx_speed: TransactionSpeed = TransactionSpeed.FAST,
29-
block_identifier: BlockIdentifier = "latest") -> Tuple[Wei, Wei]:
26+
block_identifier: BlockIdentifier = "latest") -> tuple[Wei, Wei]:
3027
"""
3128
Compute the priority_fee (maxPriorityFeePerGas) and max_fee_per_gas (maxFeePerGas) according to the given
3229
transaction 'speed'. All speeds will compute gas fees in order to try to place the transaction in the next block

0 commit comments

Comments
 (0)