You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey I've noticed that the prices are always displayed for direct routing, in many cases this returns very bad prices with high slippages.
Is there already a method to get the most efficient routing or could you implement it? Thanks!
Thanks for the quick feedback guys!
I'd think it would be open source since all the clones & forks also have the routing implemented (Quickswap and Sushiswap for example).
To my regret I can't make pr myself because my implementation differs too much but here is sort of the summary.
Let's say we need the optimal route for swap (token0,token1). Please consider that any of them might be eth/weth.
First, uniswap engine calls useAllCommonPairs(...) to get the list of possible routes. It doesn't contain EVERY possible route, just common ones. Key tokens are listed in BASES_TO_CHECK_TRADES_AGAINST. So, after that call we have list of the routes looking like [token0, basetoken0,token1], [token0, basetoken1,token1] etc. Please note that increasing hops count lead to dramatic growth of that list.
Second, we pass the list to bestTradeExactIn/Out(...) accordingly that analizes routes one by one via recursive calls and returns list of N routes sorted from the best to to worst.
Sure, this is not too easy to implement but also not THAT hard. Also remember that BASES_TO_CHECK_TRADES_AGAINST list varies from one network to another if you need alternate networks support like BSC/Heco/whatever.
To my regret I can't make pr myself because my implementation differs too much but here is sort of the summary.
Let's say we need the optimal route for swap (token0,token1). Please consider that any of them might be eth/weth.
First, uniswap engine calls useAllCommonPairs(...) to get the list of possible routes. It doesn't contain EVERY possible route, just common ones. Key tokens are listed in BASES_TO_CHECK_TRADES_AGAINST. So, after that call we have list of the routes looking like [token0, basetoken0,token1], [token0, basetoken1,token1] etc. Please note that increasing hops count lead to dramatic growth of that list.
Second, we pass the list to bestTradeExactIn/Out(...) accordingly that analizes routes one by one via recursive calls and returns list of N routes sorted from the best to to worst.
Sure, this is not too easy to implement but also not THAT hard. Also remember that BASES_TO_CHECK_TRADES_AGAINST list varies from one network to another if you need alternate networks support like BSC/Heco/whatever.
Activity
ErikBjare commentedon Mar 7, 2021
You're right that it's not supported (and that it should be). Would require modifications to:
https://github.com/shanefontaine/uniswap-python/blob/e58f3fb91dfca181630fb2732910a65a406b40db/uniswap/uniswap.py#L552
https://github.com/shanefontaine/uniswap-python/blob/e58f3fb91dfca181630fb2732910a65a406b40db/uniswap/uniswap.py#L669
https://github.com/shanefontaine/uniswap-python/blob/e58f3fb91dfca181630fb2732910a65a406b40db/uniswap/uniswap.py#L316
https://github.com/shanefontaine/uniswap-python/blob/e58f3fb91dfca181630fb2732910a65a406b40db/uniswap/uniswap.py#L357
Specifically, the lines looking like this:
https://github.com/shanefontaine/uniswap-python/blob/e58f3fb91dfca181630fb2732910a65a406b40db/uniswap/uniswap.py#L703
Should be pretty easy to fix by adding an optional
routekwarg (with the desired route) or similar for all the mentioned functions.Not sure how you'd get the most efficient routing though (without brute force search). @shanefontaine do you know?
shanefontaine commentedon Mar 8, 2021
I believe the Uniswap router logic (off-chain) that chooses the path is closed source, unfortunately.
With that said, I cannot find any data that confirms or denies this. I will try to figure out if it is open source today.
MoneroCrusher commentedon Mar 9, 2021
Thanks for the quick feedback guys!
I'd think it would be open source since all the clones & forks also have the routing implemented (Quickswap and Sushiswap for example).
liquid-8 commentedon Mar 11, 2021
I suppose that multihop feature is implemented here
public static bestTradeExactIn
adn next one method bestTradeExactOut
MoneroCrusher commentedon Mar 19, 2021
@ErikBjare @shanefontaine Are you guys there?
ErikBjare commentedon Mar 20, 2021
Yup, waiting for someone who wants this to make a PR!
liquid-8 commentedon Mar 22, 2021
To my regret I can't make pr myself because my implementation differs too much but here is sort of the summary.
Let's say we need the optimal route for swap (token0,token1). Please consider that any of them might be eth/weth.
First, uniswap engine calls useAllCommonPairs(...) to get the list of possible routes. It doesn't contain EVERY possible route, just common ones. Key tokens are listed in BASES_TO_CHECK_TRADES_AGAINST. So, after that call we have list of the routes looking like [token0, basetoken0,token1], [token0, basetoken1,token1] etc. Please note that increasing hops count lead to dramatic growth of that list.
Second, we pass the list to bestTradeExactIn/Out(...) accordingly that analizes routes one by one via recursive calls and returns list of N routes sorted from the best to to worst.
Sure, this is not too easy to implement but also not THAT hard. Also remember that BASES_TO_CHECK_TRADES_AGAINST list varies from one network to another if you need alternate networks support like BSC/Heco/whatever.
Hope that helps, regards.
dustiwe commentedon Mar 23, 2021
thanks, I will think
ErikBjare commentedon May 4, 2021
I added initial (largely untested) support for this in #58.
Closing this issue, opened #93 for finding the optimal trade route.
qraxiss commentedon Mar 11, 2023
My solution is here. +- 1 in 1000 margin of error.
If a PR is requested, I can write a more professional one. But for me this is enough.