Skip to content
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

Merged
merged 36 commits into from
Sep 12, 2023

Conversation

jsy1218
Copy link
Member

@jsy1218 jsy1218 commented Sep 7, 2023

  • 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:

  • BLAST => BULLET exact in, both are FOT tokens
    ./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:
Screenshot 2023-09-10 at 10 20 05 PM

Amount close enough to prod. because interface uses total tax off the output quote.

local sor quote amount:
Best Route:

[V2] 100.00% = BLAST -- [0x6aDB403912608FffFd8Fe623a7DB35f59e4b4F43] --> WETH -- [0x0D0A1767da735F725f41c4315E072c63Dbc6ab3D] --> BULLET
	Raw Quote Exact In:
		312.28
	Gas Adjusted Quote In:
		138.66

Gas Used Quote Token: 173.619938
Gas Used USD: 2.443752
Calldata: undefined
Value: undefined

  blockNumber: "18110981"
  estimatedGasUsed: "185000"
  gasPriceWei: "8212630637"
Total ticks crossed: 1
  • BLAST => BULLET exact out, both are FOT tokens
    ./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:
Screenshot 2023-09-10 at 11 39 30 PM
Screenshot 2023-09-10 at 11 39 19 PM

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:

Best Route:
[V2] 100.00% = BLAST -- [0x6aDB403912608FffFd8Fe623a7DB35f59e4b4F43] --> WETH -- [0x0D0A1767da735F725f41c4315E072c63Dbc6ab3D] --> BULLET
	Raw Quote Exact In:
		3280561.73
	Gas Adjusted Quote In:
		3334261.55

Gas Used Quote Token: 53699.822780
Gas Used USD: 2.587821
Calldata: undefined
Value: undefined

  blockNumber: "18111320"
  estimatedGasUsed: "185000"
  gasPriceWei: "8666681201"
Total ticks crossed: 1

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.

  • BLASTER => stETH exact in, BLASTER is FOT tokens, stETH is rebase token with 0 FOT tax.
    ./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.
Screenshot 2023-09-11 at 9 31 19 AM

local sor quote amount:
Best Route:

best quote gas adjusted -0.001740487281211106
Best Route:
[V2] 100.00% = BLAST -- [0x6aDB403912608FffFd8Fe623a7DB35f59e4b4F43] --> WETH -- [0x4028DAAC072e492d34a3Afdbef0ba7e35D8b55C4] --> stETH
	Raw Quote Exact In:
		0.00
	Gas Adjusted Quote In:
		0.00

Gas Used Quote Token: 0.004631
Gas Used USD: 7.200170
Calldata: undefined
Value: undefined

  blockNumber: "18114287"
  estimatedGasUsed: "185000"
  gasPriceWei: "24912518964"
Total ticks crossed: 1
  • BLASTER => stETH exact out, BLASTER is FOT tokens, stETH is rebase token with 0 FOT tax.
    ./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:
Screenshot 2023-09-11 at 9 43 51 AM

local sor quote amount:
Best Route:

Best Route:
[V2] 100.00% = BLAST -- [0x6aDB403912608FffFd8Fe623a7DB35f59e4b4F43] --> WETH -- [0x4028DAAC072e492d34a3Afdbef0ba7e35D8b55C4] --> stETH
	Raw Quote Exact In:
		45656156.89
	Gas Adjusted Quote In:
		45834833.75

Gas Used Quote Token: 178676.855023
Gas Used USD: 8.335765
Calldata: undefined
Value: undefined

  blockNumber: "18114315"
  estimatedGasUsed: "185000"
  gasPriceWei: "28836879906"
Total ticks crossed: 1

@jsy1218 jsy1218 self-assigned this Sep 7, 2023
src/providers/v2/pool-provider.ts Outdated Show resolved Hide resolved
src/providers/v2/pool-provider.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@cgkol cgkol left a 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

@jsy1218 jsy1218 marked this pull request as ready for review September 8, 2023 02:19
@jsy1218 jsy1218 requested review from cgkol and mikeki September 8, 2023 02:19
src/routers/alpha-router/alpha-router.ts Show resolved Hide resolved
src/providers/v2/pool-provider.ts Outdated Show resolved Hide resolved
src/providers/provider.ts Show resolved Hide resolved
@jsy1218 jsy1218 requested a review from cgkol September 8, 2023 15:45
Copy link
Contributor

@cgkol cgkol left a 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

@jsy1218
Copy link
Member Author

jsy1218 commented Sep 11, 2023

@andysmith415 offline commented on adding unit test for critical paths. Will do as part of this PR.

Copy link
Contributor

@cgkol cgkol left a 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

src/providers/v2/pool-provider.ts Show resolved Hide resolved
src/routers/alpha-router/alpha-router.ts Show resolved Hide resolved
src/util/pools.ts Outdated Show resolved Hide resolved
@jsy1218 jsy1218 merged commit 725907d into main Sep 12, 2023
@jsy1218 jsy1218 deleted the jsy1218/sor-quote-with-fot-fees branch September 12, 2023 19:55
@jsy1218 jsy1218 mentioned this pull request Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants