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
{{ message }}
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
I create pair with ratio 1200/400 and try to simulate swap.
Input amount is 3 USDC. I add fee for input and expect to get output amount, which should be 1 WETH, but get different value
The calculation done by the SDK is correct. If you look at the code of a swap in a V2 Pair, in line 180-181 you can see that the fee is subtracted before calculating that K is still correct after the transaction.
Looking at your transaction, the way the constant product formula works is by calculating a product of the reserves k which needs to stay the same after a trade.
In your example that means k before the calculation: 1200 * 400 = 480.000
And the output is: 400 - wethReservesAfter = 0.99750...
If we compare the calculation provided by the SDK and on chain results of swaps, it is also correct. Just keep in mind that the result is rounded to the minimum unit of the currency on-chain and cut off by the formatting function you use in the SDK. The fractional result by the SDK is 100% correct but any representation might be a bit off.
royalaid
pushed a commit
to royalaid/qidao-sdk
that referenced
this issue
Apr 12, 2024
I create pair with ratio 1200/400 and try to simulate swap.
Input amount is 3 USDC. I add fee for input and expect to get output amount, which should be 1 WETH, but get different value
outputAmount
should equal to 1, but I get 0.99750It seems like fee isn't counted correctly
The text was updated successfully, but these errors were encountered: