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

fix(router-sdk): special case eth, weth fake pool #282

Merged
merged 8 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdks/router-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@uniswap/swap-router-contracts": "^1.3.0",
"@uniswap/v2-sdk": "^4.13.0",
"@uniswap/v3-sdk": "^3.24.0",
"@uniswap/v4-sdk": "^1.18.0"
"@uniswap/v4-sdk": "^1.18.1"
},
"devDependencies": {
"@types/jest": "^24.0.25",
Expand Down
41 changes: 41 additions & 0 deletions sdks/router-sdk/src/utils/encodeMixedRouteToPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ describe('#encodeMixedRouteToPath', () => {
0,
[]
)
const fake_v4_eth_weth_pool = new V4Pool(
weth,
ETHER,
FeeAmount.MEDIUM,
0,
ADDRESS_ZERO,
encodeSqrtRatioX96(1, 1),
0,
0
)

const pair_0_1 = new Pair(CurrencyAmount.fromRawAmount(token0, '100'), CurrencyAmount.fromRawAmount(token1, '200'))
const pair_1_2 = new Pair(CurrencyAmount.fromRawAmount(token1, '150'), CurrencyAmount.fromRawAmount(token2, '150'))
Expand Down Expand Up @@ -61,6 +71,12 @@ describe('#encodeMixedRouteToPath', () => {
const route_eth_V4_0_V3_1 = new MixedRouteSDK([pool_V4_0_eth, pool_V3_0_1_medium], ETHER, token1)
const route_eth_V3_0_V4_1 = new MixedRouteSDK([pool_V3_0_weth, pool_V4_0_1], ETHER, token1)

const route_1_v2_weth_v0_eth_v4_token0 = new MixedRouteSDK(
[pair_1_weth, fake_v4_eth_weth_pool, pool_V4_0_eth],
token1,
token0
)

describe('pure V3', () => {
it('packs them for exact input single hop', () => {
expect(encodeMixedRouteToPath(route_0_V3_1)).toEqual(
Expand Down Expand Up @@ -181,5 +197,30 @@ describe('#encodeMixedRouteToPath', () => {
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2300bb80000000000000000000000000000000000000001400bb800001e00000000000000000000000000000000000000000000000000000000000000000000000000000002'
)
})

it('encodes the mixed route with an unwrap, token1 v2 -> v4 token0 through an unwrap', () => {
expect(encodeMixedRouteToPath(route_1_v2_weth_v0_eth_v4_token0)).toEqual(
'0x000000000000000000000000000000000000000220c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000400bb800001e00000000000000000000000000000000000000000000000000000000000000000000000000000001'
)
// comments left for future reference, to show special cased eth-weth v4 (version0) encoding in the mixed route quoter
// // first path address - token1
// 0x0000000000000000000000000000000000000002
// // first path fee - v2 "version"
// 0x20
// // first path second address - weth
// 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
// // second path - fake v4 pool, "version"
// 0x00
// // second path - fake v4 pool, second address - eth
// 0x0000000000000000000000000000000000000000
// // last path - v4 pool, with Fee.MEDIUM
// 0x400bb8
// // last path - v4, tick spacing of 30
// 0x00001e
// // last path - v4, hook address
// 0x0000000000000000000000000000000000000000
// // last path address - v4 pool, token0
// 0x0000000000000000000000000000000000000001
})
})
})
24 changes: 16 additions & 8 deletions sdks/router-sdk/src/utils/encodeMixedRouteToPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ export function encodeMixedRouteToPath(route: MixedRouteSDK<Currency, Currency>)
const currencyOut = currencyIn.equals(pool.token0) ? pool.token1 : pool.token0

if (pool instanceof V4Pool) {
const v4Fee = pool.fee + MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER
path.push(
v4Fee,
pool.tickSpacing,
pool.hooks,
currencyOut.isNative ? ADDRESS_ZERO : currencyOut.wrapped.address
)
types.push('uint24', 'uint24', 'address', 'address')
// a tickSpacing of 0 indicates a "fake" v4 pool where the quote actually requires a wrap or unwrap
snreynolds marked this conversation as resolved.
Show resolved Hide resolved
// the fake v4 pool will always have native as token0 and wrapped native as token1
if (pool.tickSpacing === 0) {
const wrapOrUnwrapEncoding = 0
path.push(wrapOrUnwrapEncoding, currencyOut.isNative ? ADDRESS_ZERO : currencyOut.wrapped.address)
types.push('uint8', 'address')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the uint8 of 0 for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use that to denote protocol version 0, which just signals a wrap/unwrap in the mixed route quoter

} else {
const v4Fee = pool.fee + MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER
path.push(
v4Fee,
pool.tickSpacing,
pool.hooks,
currencyOut.isNative ? ADDRESS_ZERO : currencyOut.wrapped.address
)
types.push('uint24', 'uint24', 'address', 'address')
}
} else if (pool instanceof V3Pool) {
const v3Fee = pool.fee + MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER
path.push(v3Fee, currencyOut.wrapped.address)
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4533,7 +4533,7 @@ __metadata:
"@uniswap/swap-router-contracts": ^1.3.0
"@uniswap/v2-sdk": ^4.13.0
"@uniswap/v3-sdk": ^3.24.0
"@uniswap/v4-sdk": ^1.18.0
"@uniswap/v4-sdk": ^1.18.1
prettier: ^2.4.1
tsdx: ^0.14.1
languageName: unknown
Expand Down Expand Up @@ -4791,16 +4791,16 @@ __metadata:
languageName: node
linkType: hard

"@uniswap/v4-sdk@npm:^1.18.0":
version: 1.18.0
resolution: "@uniswap/v4-sdk@npm:1.18.0"
"@uniswap/v4-sdk@npm:^1.18.0, @uniswap/v4-sdk@npm:^1.18.1":
version: 1.18.1
resolution: "@uniswap/v4-sdk@npm:1.18.1"
dependencies:
"@ethersproject/solidity": ^5.0.9
"@uniswap/sdk-core": ^7.5.0
"@uniswap/v3-sdk": 3.24.0
tiny-invariant: ^1.1.0
tiny-warning: ^1.0.3
checksum: 37d2ebd3781eb9d5f5e0e875d7788d2369720fd30a53dba9abb7bb8df97eb89ddad2baab99c99aab566b350e1efbfe4e0d26765a9a49da38aaab767be05eafb8
checksum: b107e7afec5f9422472bb0752a117b17f98b2671a6824a216d557e8d179ce2af9eacd21c8591b7f946f0f4cf933e2adda6d4dfd3747067576fb16435714dde0c
languageName: node
linkType: hard

Expand Down
Loading