|
1 | 1 | importos
|
2 | 2 | importjson
|
3 | 3 | importfunctools
|
4 |
| -fromtypingimportUnion,List,Tuple |
| 4 | +fromtypingimportUnion,List,Tuple,Any,Dict |
| 5 | +fromdataclassesimportdataclass |
5 | 6 |
|
6 | 7 | fromweb3importWeb3
|
7 | 8 | fromweb3.exceptionsimportNameNotFound
|
| 9 | +frometh_abiimportencode_abi |
8 | 10 |
|
9 | 11 | from .typesimportAddressLike,Address,Contract
|
10 | 12 |
|
@@ -57,10 +59,82 @@ def _load_contract_erc20(w3: Web3, address: AddressLike) -> Contract:
|
57 | 59 | return_load_contract(w3,"erc20",address)
|
58 | 60 |
|
59 | 61 |
|
60 |
| -def_encode_path(token_in:AddressLike,route:List[Tuple[int,AddressLike]])->bytes: |
| 62 | +@dataclass |
| 63 | +classPool(dict): |
| 64 | +token0:AddressLike |
| 65 | +token1:AddressLike |
| 66 | +fee:int |
| 67 | + |
| 68 | + |
| 69 | +@dataclass |
| 70 | +classRoute: |
| 71 | +pools:List[Pool] |
| 72 | + |
| 73 | + |
| 74 | +def_token_seq_to_route(tokens:List[AddressLike],fee:int=3000)->Route: |
| 75 | +returnRoute( |
| 76 | +pools=[ |
| 77 | +Pool(token0,token1,fee)fortoken0,token1inzip(tokens[:-1],tokens[1:]) |
| 78 | + ] |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def_encode_path( |
| 83 | +token_in:AddressLike, |
| 84 | +route:List[Tuple[int,AddressLike]], |
| 85 | +# route: Route, |
| 86 | +exactOutput:bool, |
| 87 | +)->bytes: |
61 | 88 | """
|
62 | 89 | Needed for multi-hop swaps in V3.
|
63 | 90 |
|
64 | 91 | https://github.com/Uniswap/uniswap-v3-sdk/blob/1a74d5f0a31040fec4aeb1f83bba01d7c03f4870/src/utils/encodeRouteToPath.ts
|
65 | 92 | """
|
66 |
| -raiseNotImplementedError |
| 93 | +fromfunctoolsimportreduce |
| 94 | + |
| 95 | +_route=_token_seq_to_route([token_in]+ [tokenforfee,tokeninroute]) |
| 96 | + |
| 97 | +defmerge(acc:Dict[str,Any],pool:Pool)->Dict[str,Any]: |
| 98 | +"""Returns a dict with the keys: inputToken, path, types""" |
| 99 | +index=0ifnotacc["types"]elseNone |
| 100 | +inputToken=acc["inputToken"] |
| 101 | +outputToken=pool.token1ifpool.token0==inputTokenelsepool.token0 |
| 102 | +ifindex==0: |
| 103 | +return { |
| 104 | +"inputToken":outputToken, |
| 105 | +"types": ["address","uint24","address"], |
| 106 | +"path": [inputToken,pool.fee,outputToken], |
| 107 | + } |
| 108 | +else: |
| 109 | +return { |
| 110 | +"inputToken":outputToken, |
| 111 | +"types": [*acc["types"],"uint24","address"], |
| 112 | +"path": [*path,pool.fee,outputToken], |
| 113 | + } |
| 114 | + |
| 115 | +params=reduce( |
| 116 | +merge, |
| 117 | +_route.pools, |
| 118 | + {"inputToken":_addr_to_str(token_in),"path": [],"types": []}, |
| 119 | + ) |
| 120 | +types=params["types"] |
| 121 | +path=params["path"] |
| 122 | + |
| 123 | +ifexactOutput: |
| 124 | +encoded:bytes=encode_abi(list(reversed(types)),list(reversed(path))) |
| 125 | +else: |
| 126 | +encoded=encode_abi(types,path) |
| 127 | + |
| 128 | +returnencoded |
| 129 | + |
| 130 | + |
| 131 | +deftest_encode_path()->None: |
| 132 | +"""Take tests from: https://github.com/Uniswap/uniswap-v3-sdk/blob/1a74d5f0a31040fec4aeb1f83bba01d7c03f4870/src/utils/encodeRouteToPath.test.ts""" |
| 133 | +fromuniswap.tokensimporttokens |
| 134 | + |
| 135 | +# TODO: Actually assert testcases |
| 136 | +path=_encode_path(tokens["WETH"], [(3000,tokens["DAI"])],exactOutput=True) |
| 137 | +print(path) |
| 138 | + |
| 139 | +path=_encode_path(tokens["WETH"], [(3000,tokens["DAI"])],exactOutput=False) |
| 140 | +print(path) |