-
Notifications
You must be signed in to change notification settings - Fork 434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: V2 routes quote with fee-on-transfer fees off the quote amount #395
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine for right now, waiting for new changes! Ping me again as needed
This reverts commit 5587a4e.
…erFeeFetching as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks fine, I would wait for Miguel's or Andy's blessing too though
@andysmith415 offline commented on adding unit test for critical paths. Will do as part of this PR. |
…omCache, because writing into Cached Routes can have FOT Token instances
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor comments only nits
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature
What is the current behavior? (You can also link to an open issue here)
V2 quotes do not consider FOT fees, resulting in higher than usual fees
What is the new behavior (if this is a feature change)?
V2 quotes will consider FOT fees. When we get the eligible pools as part of v2 route discovery, we concurrently get the FOT fees, in addition to reserves.
Also since web already detect FOT tax and shave off quote of the tax rate, we need to expose a feature flag so that web doesn't double tax against the quote.
Other information:
amountOut = B * 997 * amountIn * (1 - sellFeeBps / 10000) / (1000 * A + 997 * amountIn) * (1 - sellFeeBps / 10000)
amountIn = A * amountOut / (1 - buyFeeBps / 10000) * 1000 / (B - amountOut / (1 - buyFeeBps / 10000)) / 997
tested in following cases:
./bin/cli quote --tokenIn 0xab98093c7232e98a47d7270ce0c1c2106f61c73b --tokenOut 0x8ef32a03784c8fd63bbf027251b9620865bd54b6 --amount 100000 --exactIn --protocols v2,v3,mixed
https://app.warp.dev/block/N6HaxYjmubtDZzr9fH6aTR
Route path:
BLAST => WETH => BULLET
First reserves pair quote:
BLAST => WETH
WETH amountOut = 4.113888521182481518 * 997 * 100000 * (1 - 0.03) / (1000 * 137784644.932841276+ 997 * 100000 * (1 - 0.03))
= 0.002885452119862558
Second reserves pair quote:
WETH => BULLET
BULLET amountOut = 4845053.62651238 * 997 * 0.002885452119862558 * (1 - 0.05) / (1000 * 42.398745781910187318 + 997 * 0.002885452119862558 * (1 - 0.05))
= 312.28329139
prod interface quote amount:
Amount close enough to prod. because interface uses total tax off the output quote.
local sor quote amount:
Best Route:
./bin/cli quote --tokenIn 0xab98093c7232e98a47d7270ce0c1c2106f61c73b --tokenOut 0x8ef32a03784c8fd63bbf027251b9620865bd54b6 --amount 10000 --exactOut --protocols v2,v3,mixed
https://app.warp.dev/block/SfX0udzX4l8MuX6uOJPm7j
Route path:
BLAST => WETH => BULLET
First reserves pair quote:
WETH => BULLET
WETH amountIn = 42.398745781910187318 * 10000 / (1 - 0.05) * 1000 / (4845053.62651238 - 10000 / (1 - 0.05)) / 997
= 0.09259344191
Second reserves pair quote:
BLAST => WETH
BLAST amountIn = 137784644.932841276 * 0.092593441918643854 / (1 - 0.03) * 1000 / (4.113888521182481518 - 0.092593441918643854 / (1 - 0.03)) / 997
= 3282899.607885666
prod interface quote amount:
amountOut tokenOut FOT not supported by prod interface, since it hardcode tax on exact in to total tax output token. Use 3282899.607885666 gives slightly lower than 10000 amount, i.e. 9976, which is expected. Actual output token amount will be higher than 9976, because of the round up needed for exact out. i.e., a higher tokenOut amount will be needed for this exact out BLAST => BULLET swap to get ~3282899.607885666 BLAST tokens.
local sor quote amount:
Best Route:
Note final quote is slightly different from the amountIn using the math formula (3282899.607885666). This is due to the rounding happened during the v2-sdk
Pair.getAmountIn
that happened when applying the FOT tax to both tokenIn and tokenOut at path BLAST => WETH. However this is to be expected, and should be small enough to not make the swap fail../bin/cli quote --tokenIn 0xab98093c7232e98a47d7270ce0c1c2106f61c73b --tokenOut 0xae7ab96520de3a18e5e111b5eaab095312d7fe84 --amount 100000 --exactIn --protocols v2,v3,mixed
https://app.warp.dev/block/iWNKeZXPxzy1DJAYcSgzrz
Route path:
BLASTER => WETH => stETH
First reserves pair quote:
BLASTER => WETH
WETH amountOut = 4.113888521182481518 * 997 * 100000 * (1 - 0.03) / (1000 * 137784644.932841276 + 997 * 100000 * (1 - 0.03))
= 0.002885452119862558
Second reserves pair quote:
WETH => stETH
stETH amountOut = 4049.2856428329465489529970.002885452119862558/(10004029.591656670802761841+9970.002885452119862558)
= 0.002890853579874669
prod interface quote amount:
Amount close enough to prod. because interface uses total tax off the output quote.
local sor quote amount:
Best Route:
./bin/cli quote --tokenIn 0xab98093c7232e98a47d7270ce0c1c2106f61c73b --tokenOut 0xae7ab96520de3a18e5e111b5eaab095312d7fe84 --amount 1 --exactOut --protocols v2,v3,mixed
https://app.warp.dev/block/XGbILfp1v1VVdvxhcoZxye
Route path:
BLASTER => WETH => stETH
First reserves pair quote:
WETH => stETH
WETH amountIn = 4029.591656670802761841 * 1 * 1000 / (4049.285642832946548952 - 1) / 997
= 0.998377378396582028
Second reserves pair quote:
BLASTER => WETH
BLASTER amountIn = 137784644.932841276 * 0.998377378396582028 / (1 - 300 / 10000) * 1000 / (4.113888521182481518 - 0.998377378396582028 / (1 - 300 / 10000)) / 997
= 46113181.9789
prod interface quote amount:
local sor quote amount:
Best Route: