Repository navigation
Expand file tree
/
Copy pathutil.py
More file actions
66 lines (46 loc) 路 1.84 KB
/
Copy pathutil.py
File metadata and controls
66 lines (46 loc) 路 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import json
import functools
from typing import Union, List, Tuple
from web3 import Web3
from .types import AddressLike, Address, Contract
from .exceptions import InvalidToken
def _str_to_addr(s: Union[AddressLike, str]) -> Address:
"""Idempotent"""
if isinstance(s, str):
if s.startswith("0x"):
return Address(bytes.fromhex(s[2:]))
else:
raise Exception(f"Couldn't convert string '{s}' to AddressLike")
else:
return s
def _addr_to_str(a: AddressLike) -> str:
if isinstance(a, bytes):
# Address or ChecksumAddress
addr: str = Web3.toChecksumAddress("0x" + bytes(a).hex())
return addr
elif isinstance(a, str) and a.startswith("0x"):
addr = Web3.toChecksumAddress(a)
return addr
raise InvalidToken(a)
def is_same_address(a1: Union[AddressLike, str], a2: Union[AddressLike, str]) -> bool:
return _str_to_addr(a1) == _str_to_addr(a2)
def _validate_address(a: AddressLike) -> None:
assert _addr_to_str(a)
def _load_abi(name: str) -> str:
path = f"{os.path.dirname(os.path.abspath(__file__))}/assets/"
with open(os.path.abspath(path + f"{name}.abi")) as f:
abi: str = json.load(f)
return abi
@functools.lru_cache()
def _load_contract(w3: Web3, abi_name: str, address: AddressLike) -> Contract:
address = Web3.toChecksumAddress(address)
return w3.eth.contract(address=address, abi=_load_abi(abi_name))
def _load_contract_erc20(w3: Web3, address: AddressLike) -> Contract:
return _load_contract(w3, "erc20", address)
def _encode_path(token_in: AddressLike, route: List[Tuple[int, AddressLike]]) -> bytes:
"""
Needed for multi-hop swaps in V3.
https://github.com/Uniswap/uniswap-v3-sdk/blob/1a74d5f0a31040fec4aeb1f83bba01d7c03f4870/src/utils/encodeRouteToPath.ts
"""
raise NotImplementedError