Skip to content

Commit

Permalink
chore(v4-sdk): add important testcases for v4 routes (#79)
Browse files Browse the repository at this point in the history
Co-authored-by: marktoda <[email protected]>
  • Loading branch information
ewilz and marktoda authored Aug 29, 2024
1 parent be049f6 commit 9068d73
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
6 changes: 3 additions & 3 deletions sdks/v4-sdk/src/entities/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export class Pool {
ticks: TickDataProvider | (Tick | TickConstructorArgs)[] = NO_TICK_DATA_PROVIDER_DEFAULT
) {
invariant(isAddress(hooks), 'Invalid hook address')
invariant(Number.isInteger(fee) && (fee == DYNAMIC_FEE_FLAG || fee < 1_000_000), 'FEE')
if (fee == DYNAMIC_FEE_FLAG) {
invariant(Number.isInteger(fee) && (fee === DYNAMIC_FEE_FLAG || fee < 1_000_000), 'FEE')
if (fee === DYNAMIC_FEE_FLAG) {
invariant(Number(hooks) > 0, 'Dynamic fee pool requires a hook')
}
const tickCurrentSqrtRatioX96 = TickMath.getSqrtRatioAtTick(tickCurrent)
Expand Down Expand Up @@ -310,7 +310,7 @@ export class Pool {
sqrtPriceLimitX96
)
} else {
throw 'Error: Unsupported hook'
throw new Error('Unsupported hook')
}
}

Expand Down
41 changes: 39 additions & 2 deletions sdks/v4-sdk/src/entities/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ether, Token } from '@uniswap/sdk-core'
import { Ether, Token, WETH9 } from '@uniswap/sdk-core'
import { encodeSqrtRatioX96, TickMath } from '@uniswap/v3-sdk'
import { Pool } from './pool'
import { Route } from './route'
Expand All @@ -9,7 +9,7 @@ describe('Route', () => {
const currency0 = new Token(1, '0x0000000000000000000000000000000000000001', 18, 't0')
const currency1 = new Token(1, '0x0000000000000000000000000000000000000002', 18, 't1')
const currency2 = new Token(1, '0x0000000000000000000000000000000000000003', 18, 't2')
// const weth = WETH9[1]
const weth = WETH9[1]

const pool_0_1 = new Pool(
currency0,
Expand Down Expand Up @@ -44,6 +44,28 @@ describe('Route', () => {
0,
[]
)
const pool_0_weth = new Pool(
currency0,
weth,
FEE_AMOUNT_MEDIUM,
TICK_SPACING_TEN,
ADDRESS_ZERO,
encodeSqrtRatioX96(1, 1),
0,
0,
[]
)
const pool_eth_weth = new Pool(
eth,
weth,
FEE_AMOUNT_MEDIUM,
TICK_SPACING_TEN,
ADDRESS_ZERO,
encodeSqrtRatioX96(1, 1),
0,
0,
[]
)

describe('path', () => {
it('constructs a path from the currencies', () => {
Expand Down Expand Up @@ -83,6 +105,21 @@ describe('Route', () => {
expect(route.output).toEqual(eth)
})

it('does not support WETH -> ETH conversion without trading through an ETH->WETH pool', () => {
expect(() => new Route([pool_0_weth, pool_1_eth], currency0, currency1)).toThrow('PATH')
})

it('does not support ETH -> WETH conversion without trading through an ETH->WETH pool', () => {
expect(() => new Route([pool_1_eth, pool_0_weth], currency1, currency0)).toThrow('PATH')
})

it('supports trading through ETH/WETH pools', () => {
const route = new Route([pool_0_weth, pool_eth_weth, pool_1_eth], currency0, currency1)
expect(route.pools).toEqual([pool_0_weth, pool_eth_weth, pool_1_eth])
expect(route.input).toEqual(currency0)
expect(route.output).toEqual(currency1)
})

describe('#midPrice', () => {
const pool_0_1 = new Pool(
currency0,
Expand Down

0 comments on commit 9068d73

Please sign in to comment.