Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit42900d1

Browse files
committed
feat: started working on multihop swaps for v3
1 parente17818c commit42900d1

File tree

2 files changed

+80
-5
lines changed

2 files changed

+80
-5
lines changed

‎uniswap/uniswap.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,14 @@ def _get_token_token_input_price(
286286
ifself.version==2:
287287
price:int=self.router.functions.getAmountsOut(qty,route).call()[-1]
288288
elifself.version==3:
289+
# FIXME: How to calculate this properly? See https://docs.uniswap.org/reference/libraries/SqrtPriceMath
290+
sqrtPriceLimitX96=0
291+
289292
ifroute:
290293
# NOTE: to support custom routes we need to support the Path data encoding: https://github.com/Uniswap/uniswap-v3-periphery/blob/main/contracts/libraries/Path.sol
291294
# result: tuple = self.quoter.functions.quoteExactInput(route, qty).call()
292295
raiseException("custom route not yet supported for v3")
293296

294-
# FIXME: How to calculate this properly? See https://docs.uniswap.org/reference/libraries/SqrtPriceMath
295-
sqrtPriceLimitX96=0
296297
price=self.quoter.functions.quoteExactInputSingle(
297298
token0,token1,fee,qty,sqrtPriceLimitX96
298299
).call()

‎uniswap/util.py

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
importos
22
importjson
33
importfunctools
4-
fromtypingimportUnion,List,Tuple
4+
fromtypingimportUnion,List,Tuple,Any,Dict
5+
fromdataclassesimportdataclass
56

67
fromweb3importWeb3
78
fromweb3.exceptionsimportNameNotFound
9+
frometh_abiimportencode_abi
810

911
from .typesimportAddressLike,Address,Contract
1012

@@ -57,10 +59,82 @@ def _load_contract_erc20(w3: Web3, address: AddressLike) -> Contract:
5759
return_load_contract(w3,"erc20",address)
5860

5961

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:
6188
"""
6289
Needed for multi-hop swaps in V3.
6390
6491
https://github.com/Uniswap/uniswap-v3-sdk/blob/1a74d5f0a31040fec4aeb1f83bba01d7c03f4870/src/utils/encodeRouteToPath.ts
6592
"""
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)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp