Skip to content

Commit

Permalink
improve ux of constructing ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Apr 9, 2021
1 parent 514a639 commit fc97d1d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
9 changes: 4 additions & 5 deletions src/entities/pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { FeeAmount, TICK_SPACINGS } from '../constants'
import { nearestUsableTick } from '../utils/nearestUsableTick'
import { TickMath } from '../utils/tickMath'
import { Pool } from './pool'
import { Tick } from './tick'
import { encodeSqrtRatioX96 } from '../utils/encodeSqrtRatioX96'
import JSBI from 'jsbi'
import { NEGATIVE_ONE } from '../internalConstants'
Expand Down Expand Up @@ -180,16 +179,16 @@ describe('Pool', () => {

beforeEach(() => {
pool = new Pool(USDC, DAI, FeeAmount.LOW, encodeSqrtRatioX96(1, 1), ONE_ETHER, 0, [
new Tick({
{
index: nearestUsableTick(TickMath.MIN_TICK, TICK_SPACINGS[FeeAmount.LOW]),
liquidityNet: ONE_ETHER,
liquidityGross: ONE_ETHER
}),
new Tick({
},
{
index: nearestUsableTick(TickMath.MAX_TICK, TICK_SPACINGS[FeeAmount.LOW]),
liquidityNet: JSBI.multiply(ONE_ETHER, NEGATIVE_ONE),
liquidityGross: ONE_ETHER
})
}
])
})

Expand Down
10 changes: 6 additions & 4 deletions src/entities/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { computePoolAddress } from '../utils/computePoolAddress'
import { LiquidityMath } from '../utils/liquidityMath'
import { SwapMath } from '../utils/swapMath'
import { TickMath } from '../utils/tickMath'
import { Tick } from './tick'
import { Tick, TickConstructorArgs } from './tick'
import { TickList } from '../utils/tickList'

interface StepComputations {
Expand Down Expand Up @@ -53,7 +53,7 @@ export class Pool {
sqrtRatioX96: BigintIsh,
liquidity: BigintIsh,
tickCurrent: number,
ticks: Tick[]
ticks: (Tick | TickConstructorArgs)[]
) {
invariant(Number.isInteger(fee) && fee < 1_000_000, 'FEE')

Expand All @@ -64,13 +64,15 @@ export class Pool {
JSBI.lessThanOrEqual(JSBI.BigInt(sqrtRatioX96), nextTickSqrtRatioX96),
'PRICE_BOUNDS'
)
TickList.validate(ticks, TICK_SPACINGS[fee])
// always create a copy of the list since we want the pool's tick list to be immutable
const ticksMapped: Tick[] = ticks.map(t => (t instanceof Tick ? t : new Tick(t)))
TickList.validate(ticksMapped, TICK_SPACINGS[fee])
;[this.token0, this.token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA]
this.fee = fee
this.sqrtRatioX96 = JSBI.BigInt(sqrtRatioX96)
this.liquidity = JSBI.BigInt(liquidity)
this.tickCurrent = tickCurrent
this.ticks = ticks.slice() // create a copy since we want the pool's list to be immutable
this.ticks = ticksMapped
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/entities/tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import invariant from 'tiny-invariant'
import { BigintIsh } from '@uniswap/sdk-core'
import { TickMath } from '../utils'

interface TickConstructorArgs {
export interface TickConstructorArgs {
index: number
liquidityGross: BigintIsh
liquidityNet: BigintIsh
Expand Down
9 changes: 4 additions & 5 deletions src/entities/trade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { nearestUsableTick } from '../utils/nearestUsableTick'
import { TickMath } from '../utils/tickMath'
import { Pool } from './pool'
import { Route } from './route'
import { Tick } from './tick'
import { Trade } from './trade'

describe('Trade', () => {
Expand All @@ -26,16 +25,16 @@ describe('Trade', () => {
liquidity,
TickMath.getTickAtSqrtRatio(sqrtRatioX96),
[
new Tick({
{
index: nearestUsableTick(TickMath.MIN_TICK, TICK_SPACINGS[feeAmount]),
liquidityNet: liquidity,
liquidityGross: liquidity
}),
new Tick({
},
{
index: nearestUsableTick(TickMath.MAX_TICK, TICK_SPACINGS[feeAmount]),
liquidityNet: JSBI.multiply(liquidity, JSBI.BigInt(-1)),
liquidityGross: liquidity
})
}
]
)
}
Expand Down

0 comments on commit fc97d1d

Please sign in to comment.