Reputation: 59
I'm writing a decentralized buy/sell program and I've stumbled on an issue that I find hard to figure out.
I'm using swapExactTokensForTokens() to swap this token for WMATIC on QuickSwap. The ratio is all wrong (121 tokens = 121$ for 0.58 WMATIC = 1.3$).
If I'm using the QS interface I can swap 121 tokens for 43 WMATIC (passing through ETH, so not a direct swap).
My questions are:
Upvotes: 1
Views: 460
Reputation: 2187
why is the big discrepancy when using a middle pair? how can I prevent this from happening?
Because a different route is being followed. When there is a middle pair you swap token_a -> token_b then token_b -> token_c. The swap from b -> c gives you a better ratio than a -> c because there are differing amounts in each pool. QS automatically searches all pairs and gives you the best swap rate and constructs the optimal multi-hop swap route. (121:43 MATIC vs 121:0.58 MATIC).
how can I find using the web3 api if my pair is a direct pair or not?
There is no way. All pairs are inherently "direct" (there are only 2 tokens in a pool). It's up to you to construct a multi-hop route.
Upvotes: 1