From 69b528cf68a8f8c6f2cd630ea395894a5927c466 Mon Sep 17 00:00:00 2001 From: DevRozaDev <158298065+DevRozaDev@users.noreply.github.com> Date: Fri, 5 Jul 2024 16:40:19 +0200 Subject: [PATCH 1/7] `Swap` TxScript supports the `byAmountIn` parameter --- contracts/scripts/invariant_tx.ral | 12 ++++++++---- src/testUtils.ts | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/contracts/scripts/invariant_tx.ral b/contracts/scripts/invariant_tx.ral index 9cce27c..547899b 100644 --- a/contracts/scripts/invariant_tx.ral +++ b/contracts/scripts/invariant_tx.ral @@ -50,11 +50,15 @@ TxScript ClaimFee(invariant: Invariant, index: U256) { invariant.claimFee(index) } -TxScript Swap(invariant: Invariant, poolKey: PoolKey, xToY: Bool, amount: U256, byAmountIn: Bool, sqrtPriceLimit: U256) { - if(xToY) { - let _ = invariant.swap{callerAddress!() -> poolKey.tokenX: amount}(poolKey, xToY, amount, byAmountIn, sqrtPriceLimit) +TxScript Swap(invariant: Invariant, poolKey: PoolKey, xToY: Bool, amount: U256, byAmountIn: Bool, sqrtPriceLimit: U256, approvedAmount: U256) { + let mut tokensApproved = amount + if (!byAmountIn) { + tokensApproved = approvedAmount + } + if (xToY) { + let _ = invariant.swap{callerAddress!() -> poolKey.tokenX: tokensApproved}(poolKey, xToY, amount, byAmountIn, sqrtPriceLimit) } else { - let _ = invariant.swap{callerAddress!() -> poolKey.tokenY: amount}(poolKey, xToY, amount, byAmountIn, sqrtPriceLimit) + let _ = invariant.swap{callerAddress!() -> poolKey.tokenY: tokensApproved}(poolKey, xToY, amount, byAmountIn, sqrtPriceLimit) } } diff --git a/src/testUtils.ts b/src/testUtils.ts index 737a760..f4a0d57 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -320,6 +320,7 @@ export const verifyPositionList = async ( } } +/** When not using `byAmountIn` set approvedTokens manually via the last parameter. */ export async function initSwap( invariant: InvariantInstance, signer: SignerProvider, @@ -327,7 +328,8 @@ export async function initSwap( xToY: boolean, amount: bigint, byAmountIn: boolean, - sqrtPriceLimit: bigint + sqrtPriceLimit: bigint, + approvedAmount = amount ) { const id = xToY ? poolKey.tokenX : poolKey.tokenY return await Swap.execute(signer, { @@ -337,9 +339,10 @@ export async function initSwap( xToY, amount, byAmountIn, - sqrtPriceLimit + sqrtPriceLimit, + approvedAmount }, - tokens: [{ id, amount }] + tokens: [{ id, amount: approvedAmount }] }) } From e57ce194bab42719960a7c43f6a01288e913ee0d Mon Sep 17 00:00:00 2001 From: DevRozaDev <158298065+DevRozaDev@users.noreply.github.com> Date: Fri, 5 Jul 2024 16:58:26 +0200 Subject: [PATCH 2/7] add `liquidity` util function for applying the scale --- src/snippets.ts | 11 ++++-- src/testUtils.ts | 6 ++- test/cross.test.ts | 7 ++-- ...tion_with_pool_on_removed_fee_tier.test.ts | 7 ++-- test/liquidity_gap.test.ts | 9 +++-- test/position.test.ts | 21 ++++------ test/position_list.test.ts | 5 ++- test/position_slippage.test.ts | 7 ++-- test/swap.test.ts | 39 ++++++++----------- 9 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/snippets.ts b/src/snippets.ts index 9d918cb..0d78136 100644 --- a/src/snippets.ts +++ b/src/snippets.ts @@ -1,6 +1,6 @@ import { Address, SignerProvider } from '@alephium/web3' import { InvariantInstance, TokenFaucetInstance } from '../artifacts/ts' -import { LiquidityScale, MinSqrtPrice, PercentageScale } from './consts' +import { MinSqrtPrice, PercentageScale } from './consts' import { getPool, getPosition, @@ -8,12 +8,14 @@ import { initPosition, initSwap, initTokensXY, + liquidity, transferPosition, verifyPositionList, withdrawTokens } from './testUtils' import { balanceOf, deployInvariant, newFeeTier, newPoolKey } from './utils' import { PrivateKeyWallet } from '@alephium/web3-wallet' +import { calculateSqrtPrice } from './math' type TokenInstance = TokenFaucetInstance @@ -43,9 +45,10 @@ export const initBasicPool = async ( tokenX: TokenInstance, tokenY: TokenInstance ) => { - const initSqrtPrice = 10n ** 24n + const initTick = 0n + const initSqrtPrice = await calculateSqrtPrice(initTick) const feeTier = await newFeeTier(fee, tickSpacing) - await initPool(invariant, admin, tokenX, tokenY, feeTier, initSqrtPrice, 0n) + await initPool(invariant, admin, tokenX, tokenY, feeTier, initSqrtPrice, initTick) const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) const pool = await getPool(invariant, poolKey) expect(pool).toMatchObject({ poolKey, sqrtPrice: initSqrtPrice, exist: true }) @@ -65,7 +68,7 @@ export const initBasicPosition = async ( const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) const poolBefore = await getPool(invariant, poolKey) - const liquidityDelta = 1000000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1000000n) const slippageLimit = poolBefore.sqrtPrice const [lowerTick, upperTick] = [-20n, 10n] await initPosition( diff --git a/src/testUtils.ts b/src/testUtils.ts index f4a0d57..9f20d26 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -25,7 +25,7 @@ import { } from './utils' import { expectAssertionError } from '@alephium/web3-test' import { PrivateKeyWallet } from '@alephium/web3-wallet' -import { VMError } from './consts' +import { LiquidityScale, VMError } from './consts' import { FeeTier, PoolKey } from '../artifacts/ts/types' type TokenInstance = TokenFaucetInstance @@ -373,3 +373,7 @@ export const isTickInitialized = async ( }) ).returns } + +export const liquidity = (value: bigint) => { + return value * 10n ** LiquidityScale +} diff --git a/test/cross.test.ts b/test/cross.test.ts index 818559a..a4f19af 100644 --- a/test/cross.test.ts +++ b/test/cross.test.ts @@ -8,13 +8,14 @@ import { initDexAndTokens } from '../src/snippets' import { balanceOf, newFeeTier, newPoolKey } from '../src/utils' -import { LiquidityScale, MinSqrtPrice } from '../src/consts' +import { MinSqrtPrice } from '../src/consts' import { getPool, getTick, initFeeTier, initPosition, initSwap, + liquidity, withdrawTokens } from '../src/testUtils' @@ -36,7 +37,7 @@ describe('cross tests', () => { await initBasicPosition(invariant, positionsOwner, tokenX, tokenY) // cross position - const liquidityDelta = 1_000_000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1_000_000n) { // definitely enough for the given liquidity/ticks const approvedAmount = liquidityDelta @@ -102,7 +103,7 @@ describe('cross tests', () => { } expect(swapperBalance).toMatchObject({ tokenX: 0n, tokenY: 990n }) - const liquidityChange = 1000000n * 10n ** LiquidityScale + const liquidityChange = liquidity(1000000n) const lowerTick = await getTick(invariant, poolKey, -20n) const middleTick = await getTick(invariant, poolKey, -10n) const upperTick = await getTick(invariant, poolKey, 10n) diff --git a/test/interaction_with_pool_on_removed_fee_tier.test.ts b/test/interaction_with_pool_on_removed_fee_tier.test.ts index e0f6f27..df5d68e 100644 --- a/test/interaction_with_pool_on_removed_fee_tier.test.ts +++ b/test/interaction_with_pool_on_removed_fee_tier.test.ts @@ -2,7 +2,7 @@ import { DUST_AMOUNT, ONE_ALPH, web3 } from '@alephium/web3' import { getSigner } from '@alephium/web3-test' import { PrivateKeyWallet } from '@alephium/web3-wallet' import { MAP_ENTRY_DEPOSIT, balanceOf, deployInvariant, newFeeTier, newPoolKey } from '../src/utils' -import { InvariantError, LiquidityScale, MinSqrtPrice, PercentageScale } from '../src/consts' +import { InvariantError, MinSqrtPrice, PercentageScale } from '../src/consts' import { getPool, initPool, @@ -16,7 +16,8 @@ import { removePosition, getPools, getPosition, - expectError + expectError, + liquidity } from '../src/testUtils' import { ChangeFeeReceiver, @@ -49,7 +50,7 @@ describe('interaction with pool on removed fee tiers tests', () => { const lowerTickIndex = -20n const upperTickIndex = 10n const mint = 10n ** 10n - const liquidityDelta = 1000000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1000000n) let feeTier: FeeTier let poolKey: PoolKey diff --git a/test/liquidity_gap.test.ts b/test/liquidity_gap.test.ts index 1c3b62c..ad0881a 100644 --- a/test/liquidity_gap.test.ts +++ b/test/liquidity_gap.test.ts @@ -3,7 +3,7 @@ import { getSigner } from '@alephium/web3-test' import { PrivateKeyWallet } from '@alephium/web3-wallet' import { InvariantInstance, TokenFaucetInstance } from '../artifacts/ts' import { balanceOf, deployInvariant, newFeeTier, newPoolKey } from '../src/utils' -import { LiquidityScale, MinSqrtPrice, PercentageScale } from '../src/consts' +import { MinSqrtPrice, PercentageScale } from '../src/consts' import { getPool, getPosition, @@ -13,6 +13,7 @@ import { initPosition, initSwap, initTokensXY, + liquidity, quote, withdrawTokens } from '../src/testUtils' @@ -52,7 +53,7 @@ describe('liquidity gap tests', () => { const amount = 10n ** 6n const lowerTick = -10n const upperTick = 10n - const liquidityDelta = 20006000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(20006000n) await withdrawTokens(positionOwner, [tokenX, amount], [tokenY, amount]) const poolBefore = await getPool(invariant, poolKey) @@ -92,7 +93,7 @@ describe('liquidity gap tests', () => { await invariant.methods.calculateSqrtPrice({ args: { tickIndex: -10n } }) ).returns const expectedYAmountOut = 9999n - const liquidityDelta = 20006000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(20006000n) const lowerTick = -10n expect(pool.sqrtPrice).toEqual(expectedSqrtPrice) @@ -119,7 +120,7 @@ describe('liquidity gap tests', () => { test('Open second position non-adjacent to the previous one, consequently creating a gap in liquidity', async () => { const lowerTick = -90n const upperTick = -50n - const liquidityDelta = 20008000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(20008000n) const amount = 10n ** 6n await withdrawTokens(positionOwner, [tokenX, amount], [tokenY, amount]) diff --git a/test/position.test.ts b/test/position.test.ts index dcc9575..a6d8901 100644 --- a/test/position.test.ts +++ b/test/position.test.ts @@ -14,16 +14,11 @@ import { initSwap, initTokensXY, isTickInitialized, + liquidity, removePosition, withdrawTokens } from '../src/testUtils' -import { - InvariantError, - LiquidityScale, - MaxSqrtPrice, - MinSqrtPrice, - PercentageScale -} from '../src/consts' +import { InvariantError, MaxSqrtPrice, MinSqrtPrice, PercentageScale } from '../src/consts' web3.setCurrentNodeProvider('http://127.0.0.1:22973') @@ -164,7 +159,7 @@ describe('position tests', () => { const incorrectUpperTickIndex = upperTickIndex + 50n { - const liquidityDelta = 1000000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1000000n) const poolBefore = await getPool(invariant, poolKey) const ownerX = await balanceOf(tokenX.contractId, positionOwner.address) const ownerY = await balanceOf(tokenY.contractId, positionOwner.address) @@ -200,7 +195,7 @@ describe('position tests', () => { expect(position).toMatchObject(expectedPosition) } { - const liquidityDelta = 1000000n * (1000000n * 10n ** LiquidityScale) + const liquidityDelta = liquidity(1000000n * 1000000n) const poolBefore = await getPool(invariant, poolKey) await withdrawTokens(positionOwner, [tokenX, mint], [tokenY, mint]) const ownerX = await balanceOf(tokenX.contractId, positionOwner.address) @@ -234,7 +229,7 @@ describe('position tests', () => { owner: positionOwner.address } const pool = await getPool(invariant, poolKey) - expect(pool.liquidity).toBe(liquidityDelta + 1000000n * 10n ** LiquidityScale) + expect(pool.liquidity).toBe(liquidityDelta + liquidity(1000000n)) expect(position).toMatchObject(expectedPosition) } @@ -321,7 +316,7 @@ describe('position tests', () => { const lowerTickIndex = minTick + 10n const upperTickIndex = maxTick - 10n - const liquidityDelta = 100n * 10n ** LiquidityScale + const liquidityDelta = liquidity(100n) const poolBefore = await getPool(invariant, poolKey) const [slippageLimitLower, slippageLimitUpper] = [poolBefore.sqrtPrice, MaxSqrtPrice] @@ -409,7 +404,7 @@ describe('position tests', () => { const lowerTickIndex = -46080n const upperTickIndex = -23040n - const liquidityDelta = 10000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(10000n) const poolBefore = await getPool(invariant, poolKey) const [slippageLimitLower, slippageLimitUpper] = [poolBefore.sqrtPrice, MaxSqrtPrice] @@ -502,7 +497,7 @@ describe('position tests', () => { const lowerTickIndex = -22980n const upperTickIndex = 0n - const liquidityDelta = 10000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(10000n) const poolBefore = await getPool(invariant, poolKey) const [slippageLimitLower, slippageLimitUpper] = [poolBefore.sqrtPrice, MaxSqrtPrice] diff --git a/test/position_list.test.ts b/test/position_list.test.ts index 2996237..c10ac6d 100644 --- a/test/position_list.test.ts +++ b/test/position_list.test.ts @@ -12,13 +12,14 @@ import { initPosition, initTokensXY, isTickInitialized, + liquidity, removePosition, transferPosition, verifyPositionList, withdrawTokens } from '../src/testUtils' import { calculateSqrtPrice } from '../src/math' -import { InvariantError, LiquidityScale, MaxSqrtPrice, PercentageScale } from '../src/consts' +import { InvariantError, MaxSqrtPrice, PercentageScale } from '../src/consts' import { deployInvariant, newFeeTier, newPoolKey } from '../src/utils' import { InvariantInstance, TokenFaucetInstance } from '../artifacts/ts' @@ -218,7 +219,7 @@ describe('position list tests', () => { describe('position list tests', () => { const tickIndexes = [-9780n, -42n, 0n, 9n, 276n] - const liquiditiyDelta = 10n * 10n ** LiquidityScale + const liquiditiyDelta = liquidity(10n) let invariant: InvariantInstance let tokenX: TokenFaucetInstance diff --git a/test/position_slippage.test.ts b/test/position_slippage.test.ts index e623058..e76421a 100644 --- a/test/position_slippage.test.ts +++ b/test/position_slippage.test.ts @@ -10,10 +10,11 @@ import { initPool, initPosition, initTokensXY, + liquidity, withdrawTokens } from '../src/testUtils' import { calculateSqrtPrice } from '../src/math' -import { InvariantError, LiquidityScale } from '../src/consts' +import { InvariantError } from '../src/consts' import { InvariantInstance, TokenFaucetInstance } from '../artifacts/ts' web3.setCurrentNodeProvider('http://127.0.0.1:22973') @@ -27,7 +28,7 @@ let tokenY: TokenFaucetInstance describe('position slippage tests', () => { const [fee, tickSpacing] = getBasicFeeTickSpacing() const approvedTokens = 10n ** 10n - const liquidityDelta = 1_000_000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1_000_000n) const [lowerTick, upperTick] = [-tickSpacing, tickSpacing] beforeAll(async () => { @@ -50,7 +51,7 @@ describe('position slippage tests', () => { const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) const [lowerTick, upperTick] = [-1000n, 1000n] - const liquidityDelta = 10_000_000_000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(10_000_000_000n) const { sqrtPrice: slippageLimit } = await getPool(invariant, poolKey) await initPosition( diff --git a/test/swap.test.ts b/test/swap.test.ts index 786516d..7c98ae9 100644 --- a/test/swap.test.ts +++ b/test/swap.test.ts @@ -18,18 +18,11 @@ import { initPosition, initSwap, initTokensXY, + liquidity, quote, withdrawTokens } from '../src/testUtils' -import { - InvariantError, - LiquidityScale, - MaxSqrtPrice, - MinSqrtPrice, - PercentageScale, - VMError -} from '../src/consts' -import { FeeTier, PoolKey } from '../artifacts/ts/types' +import { InvariantError, MaxSqrtPrice, MinSqrtPrice, PercentageScale, VMError } from '../src/consts' web3.setCurrentNodeProvider('http://127.0.0.1:22973') let admin: PrivateKeyWallet @@ -75,7 +68,7 @@ describe('swap tests', () => { const poolBefore = await getPool(invariant, poolKey) const slippageLimit = poolBefore.sqrtPrice - const liquidity = 1000000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1000000n) const positionAmount = positionsAmount / 2n @@ -87,7 +80,7 @@ describe('swap tests', () => { positionAmount, lowerTickIndex, upperTickIndex, - liquidity, + liquidityDelta, slippageLimit, slippageLimit ) @@ -100,7 +93,7 @@ describe('swap tests', () => { positionAmount, lowerTickIndex - 20n, middleTickIndex, - liquidity, + liquidityDelta, slippageLimit, slippageLimit ) @@ -137,7 +130,7 @@ describe('swap tests', () => { // check pool const poolAfter = await getPool(invariant, poolKey) const poolExpected = { - liquidity: 2n * 1000000n * 10n ** LiquidityScale, + liquidity: liquidity(2n * 1000000n), currentTickIndex: -20n, feeGrowthGlobalX: 4n * 10n ** 22n, feeGrowthGlobalY: 0n, @@ -184,7 +177,7 @@ describe('swap tests', () => { const poolBefore = await getPool(invariant, poolKey) const slippageLimit = poolBefore.sqrtPrice - const liquidity = 1000000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1000000n) const positionAmount = positionsAmount / 2n @@ -196,7 +189,7 @@ describe('swap tests', () => { positionAmount, lowerTickIndex, upperTickIndex, - liquidity, + liquidityDelta, slippageLimit, slippageLimit ) @@ -209,7 +202,7 @@ describe('swap tests', () => { positionAmount, middleTickIndex, upperTickIndex + 20n, - liquidity, + liquidityDelta, slippageLimit, slippageLimit ) @@ -254,7 +247,7 @@ describe('swap tests', () => { // check pool const poolAfter = await getPool(invariant, poolKey) const poolExpected = { - liquidity: 2n * 1000000n * 10n ** LiquidityScale, + liquidity: liquidity(2n * 1000000n), currentTickIndex: 10n, feeGrowthGlobalX: 0n, feeGrowthGlobalY: 4n * 10n ** 22n, @@ -300,7 +293,7 @@ describe('swap tests', () => { const poolBefore = await getPool(invariant, poolKey) const slippageLimit = poolBefore.sqrtPrice - const liquidity = 1000000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1000000n) const positionAmount = positionsAmount / 2n @@ -312,7 +305,7 @@ describe('swap tests', () => { positionAmount, lowerTickIndex, upperTickIndex, - liquidity, + liquidityDelta, slippageLimit, slippageLimit ) @@ -325,7 +318,7 @@ describe('swap tests', () => { positionAmount, middleTickIndex, upperTickIndex + 20n, - liquidity, + liquidityDelta, slippageLimit, slippageLimit ) @@ -373,7 +366,7 @@ describe('swap tests', () => { const poolBefore = await getPool(invariant, poolKey) const slippageLimit = poolBefore.sqrtPrice - const liquidity = 1000000n * 10n ** LiquidityScale + const liquidityDelta = liquidity(1000000n) const positionAmount = positionsAmount / 2n @@ -385,7 +378,7 @@ describe('swap tests', () => { positionAmount, lowerTickIndex, upperTickIndex, - liquidity, + liquidityDelta, slippageLimit, slippageLimit ) @@ -398,7 +391,7 @@ describe('swap tests', () => { positionAmount, lowerTickIndex - 20n, middleTickIndex, - liquidity, + liquidityDelta, slippageLimit, slippageLimit ) From f190a49271b36be8d8746e8d61b8ac43e35f73be Mon Sep 17 00:00:00 2001 From: DevRozaDev <158298065+DevRozaDev@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:00:56 +0200 Subject: [PATCH 3/7] add `cross_both_side` e2e tests like other protocol versions --- test/cross_both_side.test.ts | 224 +++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 test/cross_both_side.test.ts diff --git a/test/cross_both_side.test.ts b/test/cross_both_side.test.ts new file mode 100644 index 0000000..3b22455 --- /dev/null +++ b/test/cross_both_side.test.ts @@ -0,0 +1,224 @@ +import { ONE_ALPH, web3 } from '@alephium/web3' +import { getSigner } from '@alephium/web3-test' +import { PrivateKeyWallet } from '@alephium/web3-wallet' +import { getBasicFeeTickSpacing, initBasicPool, initDexAndTokens } from '../src/snippets' +import { newFeeTier, newPoolKey } from '../src/utils' +import { + expectError, + getPool, + getTick, + initFeeTier, + initPosition, + initSwap, + liquidity, + withdrawTokens +} from '../src/testUtils' +import { InvariantError, MaxSqrtPrice, MinSqrtPrice } from '../src/consts' +import { calculateSqrtPrice } from '../src/math' +import { InvariantInstance, TokenFaucetInstance } from '../artifacts/ts' + +web3.setCurrentNodeProvider('http://127.0.0.1:22973') + +let admin: PrivateKeyWallet +let invariant: InvariantInstance +let tokenX: TokenFaucetInstance +let tokenY: TokenFaucetInstance +let positionsOwner: PrivateKeyWallet +let swapper: PrivateKeyWallet + +describe('cross tests', () => { + const [fee, tickSpacing] = getBasicFeeTickSpacing() + beforeAll(async () => { + admin = await getSigner(ONE_ALPH * 100000n, 0) + }) + beforeEach(async () => { + ;[invariant, tokenX, tokenY] = await initDexAndTokens(admin, 10n ** 24n) + const feeTier = await newFeeTier(fee, tickSpacing) + await initFeeTier(invariant, admin, feeTier) + const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) + + positionsOwner = await getSigner(ONE_ALPH * 1000n, 0) + await initBasicPool(invariant, positionsOwner, tokenX, tokenY) + // init positions + { + const mintAmount = 10n ** 5n + const positionAmount = mintAmount / 2n + await withdrawTokens(positionsOwner, [tokenX, mintAmount], [tokenY, mintAmount]) + const [lowerTick, middleTick, upperTick] = [-20n, -10n, 10n] + const liquidityDelta = liquidity(20006000n) + + const { sqrtPrice: slippageLimit } = await getPool(invariant, poolKey) + + await initPosition( + invariant, + positionsOwner, + poolKey, + positionAmount, + positionAmount, + lowerTick, + middleTick, + liquidityDelta, + slippageLimit, + slippageLimit + ) + await initPosition( + invariant, + positionsOwner, + poolKey, + positionAmount, + positionAmount, + middleTick, + upperTick, + liquidityDelta, + slippageLimit, + slippageLimit + ) + + expect(await getPool(invariant, poolKey)).toMatchObject({ liquidity: liquidityDelta }) + } + + swapper = await getSigner(ONE_ALPH * 1000n, 0) + const limitWithoutCrossTickAmount = 10_068n + + await withdrawTokens( + swapper, + [tokenX, limitWithoutCrossTickAmount], + [tokenY, limitWithoutCrossTickAmount] + ) + + // no tick crossing + const { liquidity: beforeLiquidity } = await getPool(invariant, poolKey) + + await initSwap( + invariant, + swapper, + poolKey, + true, + limitWithoutCrossTickAmount, + true, + MinSqrtPrice + ) + + expect(await getPool(invariant, poolKey)).toMatchObject({ + currentTickIndex: -10n, + sqrtPrice: calculateSqrtPrice(-10n), + liquidity: beforeLiquidity + }) + }) + + test('cross both sides', async () => { + const feeTier = await newFeeTier(fee, tickSpacing) + const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) + + const minAmountToCrossFromTickPrice = 3n + await withdrawTokens( + swapper, + [tokenX, minAmountToCrossFromTickPrice], + [tokenY, minAmountToCrossFromTickPrice] + ) + + // cross between different positions' ticks minimally + { + await initSwap( + invariant, + swapper, + poolKey, + true, + minAmountToCrossFromTickPrice, + true, + MinSqrtPrice + ) + + await initSwap( + invariant, + swapper, + poolKey, + false, + minAmountToCrossFromTickPrice, + true, + MaxSqrtPrice + ) + } + // a new position with massive liquidity + { + const mintAmount = 10n ** 19n + await withdrawTokens(positionsOwner, [tokenX, mintAmount], [tokenY, mintAmount]) + + const [lowerTick, upperTick] = [-20n, 0n] + const liquidityDelta = liquidity(19996000399699881985603n) + await initPosition( + invariant, + positionsOwner, + poolKey, + mintAmount, + mintAmount, + lowerTick, + upperTick, + liquidityDelta, + MinSqrtPrice, + MaxSqrtPrice + ) + } + { + const predictedRequiredX = 3n + const xOut = 1n + const yIn = 2n + await withdrawTokens(swapper, [tokenX, predictedRequiredX]) + + await initSwap( + invariant, + swapper, + poolKey, + true, + xOut, + false, + MinSqrtPrice, + predictedRequiredX + ) + await initSwap(invariant, swapper, poolKey, false, yIn, true, MaxSqrtPrice) + + const expectedLiquidity = liquidity(19996000399699901991603n) + expect(await getPool(invariant, poolKey)).toMatchObject({ + currentTickIndex: -20n, + feeGrowthGlobalX: 29991002699190242927121n, + feeGrowthGlobalY: 0n, + feeProtocolTokenX: 4n, + feeProtocolTokenY: 2n, + liquidity: expectedLiquidity, + sqrtPrice: 999500149964999999999999n + }) + + expect(await getTick(invariant, poolKey, -20n)).toMatchObject({ + liquidityChange: liquidity(19996000399699901991603n), + feeGrowthOutsideX: 0n, + feeGrowthOutsideY: 0n + }) + + expect(await getTick(invariant, poolKey, -10n)).toMatchObject({ + liquidityChange: 0n, + feeGrowthOutsideX: 29991002699190242927121n, + feeGrowthOutsideY: 0n + }) + + expect(await getTick(invariant, poolKey, 10n)).toMatchObject({ + liquidityChange: liquidity(20006000n), + feeGrowthOutsideX: 0n, + feeGrowthOutsideY: 0n + }) + } + }) + + test('cross both sides not cross', async () => { + const feeTier = await newFeeTier(fee, tickSpacing) + const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) + + const notCrossAmount = 1n + await withdrawTokens(swapper, [tokenX, notCrossAmount]) + + await expectError( + InvariantError.NoGainSwap, + initSwap(invariant, swapper, poolKey, true, notCrossAmount, true, MinSqrtPrice), + invariant + ) + }) +}) From 57bd35047d2a581ff5a290f4a427d2cc8e12f2dd Mon Sep 17 00:00:00 2001 From: DevRozaDev <158298065+DevRozaDev@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:32:49 +0200 Subject: [PATCH 4/7] ignore local ralph-lsp files --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 32b4eab..e4ac53d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules coverage artifacts -.project.json \ No newline at end of file +.project.json +.ralph-lsp \ No newline at end of file From 4fdbbdfad9a414e1bd2072687f4dfc88dfc8b4c8 Mon Sep 17 00:00:00 2001 From: DevRozaDev <158298065+DevRozaDev@users.noreply.github.com> Date: Mon, 8 Jul 2024 17:09:22 +0200 Subject: [PATCH 5/7] fix `CLAMM.isEnoughAmountToChangePrice` --- contracts/math/clamm.ral | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/math/clamm.ral b/contracts/math/clamm.ral index b6b739a..221ea9d 100644 --- a/contracts/math/clamm.ral +++ b/contracts/math/clamm.ral @@ -276,8 +276,8 @@ Contract CLAMM() extends Uints(), Log(){ } let mut nextSqrtPrice = 0 - if (byAmountIn == true) { - let amountAfterFee = toU256(bigMulDiv256(amount, (one(PercentageScale) - fee), PercentageScale)) + if (byAmountIn) { + let amountAfterFee = toU256(bigMulDiv256(amount, (one(PercentageScale) - fee), one(PercentageScale))) nextSqrtPrice = getNextSqrtPriceFromInput(startingSqrtPrice, liquidity, amountAfterFee, xToY) } else { nextSqrtPrice = getNextSqrtPriceFromOutput(startingSqrtPrice, liquidity, amount, xToY) From 0000399e04165d1ea761bc1a41d196dc633c9bf9 Mon Sep 17 00:00:00 2001 From: DevRozaDev <158298065+DevRozaDev@users.noreply.github.com> Date: Mon, 8 Jul 2024 18:23:38 +0200 Subject: [PATCH 6/7] throw error when accessing uninitialized Pool/Tick/Position field + minor test fixes --- src/snippets.ts | 2 +- src/testUtils.ts | 4 +-- src/utils.ts | 29 ++++++++++-------- test/create_pool.test.ts | 2 +- test/cross_both_side.test.ts | 2 +- ...tion_with_pool_on_removed_fee_tier.test.ts | 2 +- test/liquidity_gap.test.ts | 12 ++++---- test/position.test.ts | 30 +++++++++---------- test/position_list.test.ts | 9 +++--- test/swap.test.ts | 16 +++++----- 10 files changed, 56 insertions(+), 52 deletions(-) diff --git a/src/snippets.ts b/src/snippets.ts index 0d78136..8213eea 100644 --- a/src/snippets.ts +++ b/src/snippets.ts @@ -51,7 +51,7 @@ export const initBasicPool = async ( await initPool(invariant, admin, tokenX, tokenY, feeTier, initSqrtPrice, initTick) const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) const pool = await getPool(invariant, poolKey) - expect(pool).toMatchObject({ poolKey, sqrtPrice: initSqrtPrice, exist: true }) + expect(pool).toMatchObject({ poolKey, sqrtPrice: initSqrtPrice, exists: true }) } /** Requires TokenX and TokenY faucets to have at least 1000 in supply. */ diff --git a/src/testUtils.ts b/src/testUtils.ts index 9f20d26..d0e184f 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -310,12 +310,12 @@ export const verifyPositionList = async ( isWhole = false ) => { for (let n = 1n; n <= length; ++n) { - const { exist: positionExists } = await getPosition(invariant, owner, n) + const { exists: positionExists } = await getPosition(invariant, owner, n) expect(positionExists).toBeTruthy() } if (isWhole) { - const { exist: positionExists } = await getPosition(invariant, owner, length + 1n) + const { exists: positionExists } = await getPosition(invariant, owner, length + 1n) expect(positionExists).toBeFalsy() } } diff --git a/src/utils.ts b/src/utils.ts index f7fba07..b6c6da8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -121,25 +121,30 @@ export function decodePools(string: string) { return pools } +function createEntityProxy(entity: T, exists: boolean) { + return new Proxy( + { ...entity, exists }, + { + get(target, prop, receiver) { + if (!exists && prop !== 'exists' && prop in target) { + throw new Error(`Entity does not exist, cannot access property "${String(prop)}"`) + } + return Reflect.get(target, prop, receiver) + } + } + ) +} + export function decodePool(array: [boolean, Pool]) { - return { - exist: array[0], - ...array[1] - } + return createEntityProxy(array[1], array[0]) } export function decodeTick(array: [boolean, Tick]) { - return { - exist: array[0], - ...array[1] - } + return createEntityProxy(array[1], array[0]) } export function decodePosition(array: [boolean, Position]) { - return { - exist: array[0], - ...array[1] - } + return createEntityProxy(array[1], array[0]) } export function hexToBytes(hex: string): Uint8Array { diff --git a/test/create_pool.test.ts b/test/create_pool.test.ts index 8dfc5bd..f38ce49 100644 --- a/test/create_pool.test.ts +++ b/test/create_pool.test.ts @@ -44,7 +44,7 @@ describe('invariant tests', () => { liquidity: 0n, poolKey, feeReceiver: admin.address, - exist: true + exists: true } expect(pool).toMatchObject(expectedPool) }) diff --git a/test/cross_both_side.test.ts b/test/cross_both_side.test.ts index 3b22455..22beb47 100644 --- a/test/cross_both_side.test.ts +++ b/test/cross_both_side.test.ts @@ -101,7 +101,7 @@ describe('cross tests', () => { expect(await getPool(invariant, poolKey)).toMatchObject({ currentTickIndex: -10n, - sqrtPrice: calculateSqrtPrice(-10n), + sqrtPrice: await calculateSqrtPrice(-10n), liquidity: beforeLiquidity }) }) diff --git a/test/interaction_with_pool_on_removed_fee_tier.test.ts b/test/interaction_with_pool_on_removed_fee_tier.test.ts index df5d68e..d604a18 100644 --- a/test/interaction_with_pool_on_removed_fee_tier.test.ts +++ b/test/interaction_with_pool_on_removed_fee_tier.test.ts @@ -227,7 +227,7 @@ describe('interaction with pool on removed fee tiers tests', () => { }) const transferedPosition = await getPosition(invariant, recipient.address, 1n) - expect(transferedPosition.exist).toBe(true) + expect(transferedPosition.exists).toBe(true) expect(transferedPosition.liquidity).toBe(liquidityDelta) expect(transferedPosition.lowerTickIndex).toBe(lowerTickIndex) expect(transferedPosition.upperTickIndex).toBe(upperTickIndex) diff --git a/test/liquidity_gap.test.ts b/test/liquidity_gap.test.ts index ad0881a..2bae3a2 100644 --- a/test/liquidity_gap.test.ts +++ b/test/liquidity_gap.test.ts @@ -155,12 +155,12 @@ describe('liquidity gap tests', () => { const firstPosition = await getPosition(invariant, positionOwner.address, 1n) const secondPosition = await getPosition(invariant, positionOwner.address, 2n) - const { exist: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, -50n) - const { exist: currentInMap } = await getTick(invariant, poolKey, -60n) - const { exist: upperInMap, ...upperTick } = await getTick(invariant, poolKey, -10n) + const { exists: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, -50n) + const { exists: currentInMap } = await getTick(invariant, poolKey, -60n) + const { exists: upperInMap, ...upperTick } = await getTick(invariant, poolKey, -10n) const expectedFirstPosition = { - exist: true, + exists: true, liquidity: 2000600000000n, lowerTickIndex: -10n, upperTickIndex: 10n, @@ -171,7 +171,7 @@ describe('liquidity gap tests', () => { owner: positionOwner.address } const expectedSecondPosition = { - exist: true, + exists: true, liquidity: 2000800000000n, lowerTickIndex: -90n, upperTickIndex: -50n, @@ -213,7 +213,7 @@ describe('liquidity gap tests', () => { expect(upperTick).toMatchObject(expectedUpperTick) const expectedPool = { - exist: true, + exists: true, poolKey, liquidity: secondPosition.liquidity, currentTickIndex: -60n, diff --git a/test/position.test.ts b/test/position.test.ts index a6d8901..272a1db 100644 --- a/test/position.test.ts +++ b/test/position.test.ts @@ -70,7 +70,7 @@ describe('position tests', () => { const position = await getPosition(invariant, positionOwner.address, 1n) const expectedPosition = { - exist: true, + exists: true, liquidity: 10n, lowerTickIndex: -10n, upperTickIndex: 10n, @@ -180,7 +180,7 @@ describe('position tests', () => { const position = await getPosition(invariant, positionOwner.address, 1n) const expectedPosition = { - exist: true, + exists: true, liquidity: liquidityDelta, lowerTickIndex, upperTickIndex, @@ -218,7 +218,7 @@ describe('position tests', () => { const position = await getPosition(invariant, positionOwner.address, 2n) const expectedPosition = { - exist: true, + exists: true, liquidity: liquidityDelta, lowerTickIndex: incorrectLowerTickIndex, upperTickIndex: incorrectUpperTickIndex, @@ -245,7 +245,7 @@ describe('position tests', () => { const poolAfter = await getPool(invariant, poolKey) const expectedPool = { - exist: true, + exists: true, poolKey, liquidity: poolBefore.liquidity, currentTickIndex: -10n, @@ -268,8 +268,8 @@ describe('position tests', () => { await removePosition(invariant, positionOwner, 1n) const pool = await getPool(invariant, poolKey) - const { exist: lowerInMap } = await getTick(invariant, poolKey, lowerTickIndex) - const { exist: upperInMap } = await getTick(invariant, poolKey, upperTickIndex) + const { exists: lowerInMap } = await getTick(invariant, poolKey, lowerTickIndex) + const { exists: upperInMap } = await getTick(invariant, poolKey, upperTickIndex) const lowerBit = await isTickInitialized(invariant, poolKey, lowerTickIndex) const upperBit = await isTickInitialized(invariant, poolKey, upperTickIndex) @@ -335,8 +335,8 @@ describe('position tests', () => { const position = await getPosition(invariant, positionOwner.address, 1n) const pool = await getPool(invariant, poolKey) - const { exist: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) - const { exist: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) + const { exists: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) + const { exists: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) const ownerX = await balanceOf(tokenX.contractId, positionOwner.address) const ownerY = await balanceOf(tokenY.contractId, positionOwner.address) const dexX = await balanceOf(tokenX.contractId, invariant.address) @@ -360,7 +360,7 @@ describe('position tests', () => { expect(pool.currentTickIndex).toBe(initTick) const expectedPosition = { - exist: true, + exists: true, liquidity: liquidityDelta, lowerTickIndex, upperTickIndex, @@ -424,8 +424,8 @@ describe('position tests', () => { const position = await getPosition(invariant, positionOwner.address, 1n) const pool = await getPool(invariant, poolKey) - const { exist: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) - const { exist: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) + const { exists: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) + const { exists: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) const lowerBit = await isTickInitialized(invariant, poolKey, lowerTickIndex) const upperBit = await isTickInitialized(invariant, poolKey, upperTickIndex) const ownerX = await balanceOf(tokenX.contractId, positionOwner.address) @@ -453,7 +453,7 @@ describe('position tests', () => { expect(pool.currentTickIndex).toBe(initTick) const expectedPosition = { - exist: true, + exists: true, liquidity: liquidityDelta, lowerTickIndex, upperTickIndex, @@ -517,8 +517,8 @@ describe('position tests', () => { const position = await getPosition(invariant, positionOwner.address, 1n) const pool = await getPool(invariant, poolKey) - const { exist: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) - const { exist: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) + const { exists: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) + const { exists: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) const ownerX = await balanceOf(tokenX.contractId, positionOwner.address) const ownerY = await balanceOf(tokenY.contractId, positionOwner.address) const dexX = await balanceOf(tokenX.contractId, invariant.address) @@ -542,7 +542,7 @@ describe('position tests', () => { expect(pool.currentTickIndex).toBe(initTick) const expectedPosition = { - exist: true, + exists: true, liquidity: liquidityDelta, lowerTickIndex, upperTickIndex, diff --git a/test/position_list.test.ts b/test/position_list.test.ts index c10ac6d..925f780 100644 --- a/test/position_list.test.ts +++ b/test/position_list.test.ts @@ -307,20 +307,19 @@ describe('position list tests', () => { slippageLimitLower, MaxSqrtPrice ) - const { exist: positionExists } = await getPosition(invariant, positionsOwner.address, 4n) + const { exists: positionExists } = await getPosition(invariant, positionsOwner.address, 4n) expect(positionExists).toBeTruthy() } // remove last position { await removePosition(invariant, positionsOwner, 4n) - const { exist: positionExists } = await getPosition(invariant, positionsOwner.address, 4n) + const { exists: positionExists } = await getPosition(invariant, positionsOwner.address, 4n) expect(positionExists).toBeFalsy() } // remove all positions { for (let n = 3n; n > 0; --n) { await removePosition(invariant, positionsOwner, n) - const { exist: positionExists } = await getPosition(invariant, positionsOwner.address, n) verifyPositionList(invariant, positionsOwner.address, n - 1n, true) } } @@ -341,14 +340,14 @@ describe('position list tests', () => { ) const position = await getPosition(invariant, positionsOwner.address, 1n) expect(position).toMatchObject({ - exist: true, + exists: true, poolKey, lowerTickIndex: tickIndexes[0], upperTickIndex: tickIndexes[1], liquidity: liquiditiyDelta, owner: positionsOwner.address }) - const { exist: secondExists } = await getPosition(invariant, positionsOwner.address, 2n) + const { exists: secondExists } = await getPosition(invariant, positionsOwner.address, 2n) expect(secondExists).toBeFalsy() } }) diff --git a/test/swap.test.ts b/test/swap.test.ts index 7c98ae9..8d7b8c4 100644 --- a/test/swap.test.ts +++ b/test/swap.test.ts @@ -142,13 +142,13 @@ describe('swap tests', () => { // check ticks const lowerTick = await getTick(invariant, poolKey, lowerTickIndex) - expect(lowerTick).toMatchObject({ exist: true, feeGrowthOutsideX: 0n }) + expect(lowerTick).toMatchObject({ exists: true, feeGrowthOutsideX: 0n }) const middleTick = await getTick(invariant, poolKey, middleTickIndex) - expect(middleTick).toMatchObject({ exist: true, feeGrowthOutsideX: 3n * 10n ** 22n }) + expect(middleTick).toMatchObject({ exists: true, feeGrowthOutsideX: 3n * 10n ** 22n }) const upperTick = await getTick(invariant, poolKey, upperTickIndex) - expect(upperTick).toMatchObject({ exist: true, feeGrowthOutsideX: 0n }) + expect(upperTick).toMatchObject({ exists: true, feeGrowthOutsideX: 0n }) } }) @@ -259,13 +259,13 @@ describe('swap tests', () => { // check ticks const lowerTick = await getTick(invariant, poolKey, lowerTickIndex) - expect(lowerTick).toMatchObject({ exist: true, feeGrowthOutsideY: 0n }) + expect(lowerTick).toMatchObject({ exists: true, feeGrowthOutsideY: 0n }) const middleTick = await getTick(invariant, poolKey, middleTickIndex) - expect(middleTick).toMatchObject({ exist: true, feeGrowthOutsideY: 3n * 10n ** 22n }) + expect(middleTick).toMatchObject({ exists: true, feeGrowthOutsideY: 3n * 10n ** 22n }) const upperTick = await getTick(invariant, poolKey, upperTickIndex) - expect(upperTick).toMatchObject({ exist: true, feeGrowthOutsideY: 0n }) + expect(upperTick).toMatchObject({ exists: true, feeGrowthOutsideY: 0n }) } }) test('swap not enough liquidity token x', async () => { @@ -335,7 +335,7 @@ describe('swap tests', () => { expect(dexBalance).toStrictEqual({ tokenX: 2499n, tokenY: 500n }) // we run out of gas before completing the calculation, might be related to the performance of `prevInitialized` // in the particularly unlikely in the real world scenario of only uninitialized chunks - expectVMError( + await expectVMError( VMError.OutOfGas, initSwap(invariant, swapper, poolKey, true, swapAmount, true, MinSqrtPrice) ) @@ -408,7 +408,7 @@ describe('swap tests', () => { } expect(dexBalance).toStrictEqual({ tokenX: 500n, tokenY: 2499n }) - expectError( + await expectError( InvariantError.TickLimitReached, initSwap(invariant, swapper, poolKey, false, swapAmount, true, MaxSqrtPrice), invariant From 2758951a45f55d9edb6d73bfdedee54c645ee020 Mon Sep 17 00:00:00 2001 From: DevRozaDev <158298065+DevRozaDev@users.noreply.github.com> Date: Tue, 9 Jul 2024 09:04:31 +0200 Subject: [PATCH 7/7] rename `liquidity` util function to `toLiquidity` --- src/snippets.ts | 4 ++-- src/testUtils.ts | 2 +- test/cross.test.ts | 6 +++--- test/cross_both_side.test.ts | 12 ++++++------ ...teraction_with_pool_on_removed_fee_tier.test.ts | 4 ++-- test/liquidity_gap.test.ts | 8 ++++---- test/position.test.ts | 14 +++++++------- test/position_list.test.ts | 4 ++-- test/position_slippage.test.ts | 6 +++--- test/swap.test.ts | 14 +++++++------- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/snippets.ts b/src/snippets.ts index 8213eea..34c7050 100644 --- a/src/snippets.ts +++ b/src/snippets.ts @@ -8,7 +8,7 @@ import { initPosition, initSwap, initTokensXY, - liquidity, + toLiquidity, transferPosition, verifyPositionList, withdrawTokens @@ -68,7 +68,7 @@ export const initBasicPosition = async ( const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) const poolBefore = await getPool(invariant, poolKey) - const liquidityDelta = liquidity(1000000n) + const liquidityDelta = toLiquidity(1000000n) const slippageLimit = poolBefore.sqrtPrice const [lowerTick, upperTick] = [-20n, 10n] await initPosition( diff --git a/src/testUtils.ts b/src/testUtils.ts index d0e184f..c38f6ce 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -374,6 +374,6 @@ export const isTickInitialized = async ( ).returns } -export const liquidity = (value: bigint) => { +export const toLiquidity = (value: bigint) => { return value * 10n ** LiquidityScale } diff --git a/test/cross.test.ts b/test/cross.test.ts index a4f19af..38f9878 100644 --- a/test/cross.test.ts +++ b/test/cross.test.ts @@ -15,7 +15,7 @@ import { initFeeTier, initPosition, initSwap, - liquidity, + toLiquidity, withdrawTokens } from '../src/testUtils' @@ -37,7 +37,7 @@ describe('cross tests', () => { await initBasicPosition(invariant, positionsOwner, tokenX, tokenY) // cross position - const liquidityDelta = liquidity(1_000_000n) + const liquidityDelta = toLiquidity(1_000_000n) { // definitely enough for the given liquidity/ticks const approvedAmount = liquidityDelta @@ -103,7 +103,7 @@ describe('cross tests', () => { } expect(swapperBalance).toMatchObject({ tokenX: 0n, tokenY: 990n }) - const liquidityChange = liquidity(1000000n) + const liquidityChange = toLiquidity(1000000n) const lowerTick = await getTick(invariant, poolKey, -20n) const middleTick = await getTick(invariant, poolKey, -10n) const upperTick = await getTick(invariant, poolKey, 10n) diff --git a/test/cross_both_side.test.ts b/test/cross_both_side.test.ts index 22beb47..2334b23 100644 --- a/test/cross_both_side.test.ts +++ b/test/cross_both_side.test.ts @@ -10,7 +10,7 @@ import { initFeeTier, initPosition, initSwap, - liquidity, + toLiquidity, withdrawTokens } from '../src/testUtils' import { InvariantError, MaxSqrtPrice, MinSqrtPrice } from '../src/consts' @@ -45,7 +45,7 @@ describe('cross tests', () => { const positionAmount = mintAmount / 2n await withdrawTokens(positionsOwner, [tokenX, mintAmount], [tokenY, mintAmount]) const [lowerTick, middleTick, upperTick] = [-20n, -10n, 10n] - const liquidityDelta = liquidity(20006000n) + const liquidityDelta = toLiquidity(20006000n) const { sqrtPrice: slippageLimit } = await getPool(invariant, poolKey) @@ -145,7 +145,7 @@ describe('cross tests', () => { await withdrawTokens(positionsOwner, [tokenX, mintAmount], [tokenY, mintAmount]) const [lowerTick, upperTick] = [-20n, 0n] - const liquidityDelta = liquidity(19996000399699881985603n) + const liquidityDelta = toLiquidity(19996000399699881985603n) await initPosition( invariant, positionsOwner, @@ -177,7 +177,7 @@ describe('cross tests', () => { ) await initSwap(invariant, swapper, poolKey, false, yIn, true, MaxSqrtPrice) - const expectedLiquidity = liquidity(19996000399699901991603n) + const expectedLiquidity = toLiquidity(19996000399699901991603n) expect(await getPool(invariant, poolKey)).toMatchObject({ currentTickIndex: -20n, feeGrowthGlobalX: 29991002699190242927121n, @@ -189,7 +189,7 @@ describe('cross tests', () => { }) expect(await getTick(invariant, poolKey, -20n)).toMatchObject({ - liquidityChange: liquidity(19996000399699901991603n), + liquidityChange: toLiquidity(19996000399699901991603n), feeGrowthOutsideX: 0n, feeGrowthOutsideY: 0n }) @@ -201,7 +201,7 @@ describe('cross tests', () => { }) expect(await getTick(invariant, poolKey, 10n)).toMatchObject({ - liquidityChange: liquidity(20006000n), + liquidityChange: toLiquidity(20006000n), feeGrowthOutsideX: 0n, feeGrowthOutsideY: 0n }) diff --git a/test/interaction_with_pool_on_removed_fee_tier.test.ts b/test/interaction_with_pool_on_removed_fee_tier.test.ts index d604a18..1fea19d 100644 --- a/test/interaction_with_pool_on_removed_fee_tier.test.ts +++ b/test/interaction_with_pool_on_removed_fee_tier.test.ts @@ -17,7 +17,7 @@ import { getPools, getPosition, expectError, - liquidity + toLiquidity } from '../src/testUtils' import { ChangeFeeReceiver, @@ -50,7 +50,7 @@ describe('interaction with pool on removed fee tiers tests', () => { const lowerTickIndex = -20n const upperTickIndex = 10n const mint = 10n ** 10n - const liquidityDelta = liquidity(1000000n) + const liquidityDelta = toLiquidity(1000000n) let feeTier: FeeTier let poolKey: PoolKey diff --git a/test/liquidity_gap.test.ts b/test/liquidity_gap.test.ts index 2bae3a2..352d68e 100644 --- a/test/liquidity_gap.test.ts +++ b/test/liquidity_gap.test.ts @@ -13,7 +13,7 @@ import { initPosition, initSwap, initTokensXY, - liquidity, + toLiquidity, quote, withdrawTokens } from '../src/testUtils' @@ -53,7 +53,7 @@ describe('liquidity gap tests', () => { const amount = 10n ** 6n const lowerTick = -10n const upperTick = 10n - const liquidityDelta = liquidity(20006000n) + const liquidityDelta = toLiquidity(20006000n) await withdrawTokens(positionOwner, [tokenX, amount], [tokenY, amount]) const poolBefore = await getPool(invariant, poolKey) @@ -93,7 +93,7 @@ describe('liquidity gap tests', () => { await invariant.methods.calculateSqrtPrice({ args: { tickIndex: -10n } }) ).returns const expectedYAmountOut = 9999n - const liquidityDelta = liquidity(20006000n) + const liquidityDelta = toLiquidity(20006000n) const lowerTick = -10n expect(pool.sqrtPrice).toEqual(expectedSqrtPrice) @@ -120,7 +120,7 @@ describe('liquidity gap tests', () => { test('Open second position non-adjacent to the previous one, consequently creating a gap in liquidity', async () => { const lowerTick = -90n const upperTick = -50n - const liquidityDelta = liquidity(20008000n) + const liquidityDelta = toLiquidity(20008000n) const amount = 10n ** 6n await withdrawTokens(positionOwner, [tokenX, amount], [tokenY, amount]) diff --git a/test/position.test.ts b/test/position.test.ts index 272a1db..eef0596 100644 --- a/test/position.test.ts +++ b/test/position.test.ts @@ -14,7 +14,7 @@ import { initSwap, initTokensXY, isTickInitialized, - liquidity, + toLiquidity, removePosition, withdrawTokens } from '../src/testUtils' @@ -159,7 +159,7 @@ describe('position tests', () => { const incorrectUpperTickIndex = upperTickIndex + 50n { - const liquidityDelta = liquidity(1000000n) + const liquidityDelta = toLiquidity(1000000n) const poolBefore = await getPool(invariant, poolKey) const ownerX = await balanceOf(tokenX.contractId, positionOwner.address) const ownerY = await balanceOf(tokenY.contractId, positionOwner.address) @@ -195,7 +195,7 @@ describe('position tests', () => { expect(position).toMatchObject(expectedPosition) } { - const liquidityDelta = liquidity(1000000n * 1000000n) + const liquidityDelta = toLiquidity(1000000n * 1000000n) const poolBefore = await getPool(invariant, poolKey) await withdrawTokens(positionOwner, [tokenX, mint], [tokenY, mint]) const ownerX = await balanceOf(tokenX.contractId, positionOwner.address) @@ -229,7 +229,7 @@ describe('position tests', () => { owner: positionOwner.address } const pool = await getPool(invariant, poolKey) - expect(pool.liquidity).toBe(liquidityDelta + liquidity(1000000n)) + expect(pool.liquidity).toBe(liquidityDelta + toLiquidity(1000000n)) expect(position).toMatchObject(expectedPosition) } @@ -316,7 +316,7 @@ describe('position tests', () => { const lowerTickIndex = minTick + 10n const upperTickIndex = maxTick - 10n - const liquidityDelta = liquidity(100n) + const liquidityDelta = toLiquidity(100n) const poolBefore = await getPool(invariant, poolKey) const [slippageLimitLower, slippageLimitUpper] = [poolBefore.sqrtPrice, MaxSqrtPrice] @@ -404,7 +404,7 @@ describe('position tests', () => { const lowerTickIndex = -46080n const upperTickIndex = -23040n - const liquidityDelta = liquidity(10000n) + const liquidityDelta = toLiquidity(10000n) const poolBefore = await getPool(invariant, poolKey) const [slippageLimitLower, slippageLimitUpper] = [poolBefore.sqrtPrice, MaxSqrtPrice] @@ -497,7 +497,7 @@ describe('position tests', () => { const lowerTickIndex = -22980n const upperTickIndex = 0n - const liquidityDelta = liquidity(10000n) + const liquidityDelta = toLiquidity(10000n) const poolBefore = await getPool(invariant, poolKey) const [slippageLimitLower, slippageLimitUpper] = [poolBefore.sqrtPrice, MaxSqrtPrice] diff --git a/test/position_list.test.ts b/test/position_list.test.ts index 925f780..2a87d2a 100644 --- a/test/position_list.test.ts +++ b/test/position_list.test.ts @@ -12,7 +12,7 @@ import { initPosition, initTokensXY, isTickInitialized, - liquidity, + toLiquidity, removePosition, transferPosition, verifyPositionList, @@ -219,7 +219,7 @@ describe('position list tests', () => { describe('position list tests', () => { const tickIndexes = [-9780n, -42n, 0n, 9n, 276n] - const liquiditiyDelta = liquidity(10n) + const liquiditiyDelta = toLiquidity(10n) let invariant: InvariantInstance let tokenX: TokenFaucetInstance diff --git a/test/position_slippage.test.ts b/test/position_slippage.test.ts index e76421a..d7da50e 100644 --- a/test/position_slippage.test.ts +++ b/test/position_slippage.test.ts @@ -10,7 +10,7 @@ import { initPool, initPosition, initTokensXY, - liquidity, + toLiquidity, withdrawTokens } from '../src/testUtils' import { calculateSqrtPrice } from '../src/math' @@ -28,7 +28,7 @@ let tokenY: TokenFaucetInstance describe('position slippage tests', () => { const [fee, tickSpacing] = getBasicFeeTickSpacing() const approvedTokens = 10n ** 10n - const liquidityDelta = liquidity(1_000_000n) + const liquidityDelta = toLiquidity(1_000_000n) const [lowerTick, upperTick] = [-tickSpacing, tickSpacing] beforeAll(async () => { @@ -51,7 +51,7 @@ describe('position slippage tests', () => { const poolKey = await newPoolKey(tokenX.contractId, tokenY.contractId, feeTier) const [lowerTick, upperTick] = [-1000n, 1000n] - const liquidityDelta = liquidity(10_000_000_000n) + const liquidityDelta = toLiquidity(10_000_000_000n) const { sqrtPrice: slippageLimit } = await getPool(invariant, poolKey) await initPosition( diff --git a/test/swap.test.ts b/test/swap.test.ts index 8d7b8c4..bc728db 100644 --- a/test/swap.test.ts +++ b/test/swap.test.ts @@ -18,7 +18,7 @@ import { initPosition, initSwap, initTokensXY, - liquidity, + toLiquidity, quote, withdrawTokens } from '../src/testUtils' @@ -68,7 +68,7 @@ describe('swap tests', () => { const poolBefore = await getPool(invariant, poolKey) const slippageLimit = poolBefore.sqrtPrice - const liquidityDelta = liquidity(1000000n) + const liquidityDelta = toLiquidity(1000000n) const positionAmount = positionsAmount / 2n @@ -130,7 +130,7 @@ describe('swap tests', () => { // check pool const poolAfter = await getPool(invariant, poolKey) const poolExpected = { - liquidity: liquidity(2n * 1000000n), + liquidity: toLiquidity(2n * 1000000n), currentTickIndex: -20n, feeGrowthGlobalX: 4n * 10n ** 22n, feeGrowthGlobalY: 0n, @@ -177,7 +177,7 @@ describe('swap tests', () => { const poolBefore = await getPool(invariant, poolKey) const slippageLimit = poolBefore.sqrtPrice - const liquidityDelta = liquidity(1000000n) + const liquidityDelta = toLiquidity(1000000n) const positionAmount = positionsAmount / 2n @@ -247,7 +247,7 @@ describe('swap tests', () => { // check pool const poolAfter = await getPool(invariant, poolKey) const poolExpected = { - liquidity: liquidity(2n * 1000000n), + liquidity: toLiquidity(2n * 1000000n), currentTickIndex: 10n, feeGrowthGlobalX: 0n, feeGrowthGlobalY: 4n * 10n ** 22n, @@ -293,7 +293,7 @@ describe('swap tests', () => { const poolBefore = await getPool(invariant, poolKey) const slippageLimit = poolBefore.sqrtPrice - const liquidityDelta = liquidity(1000000n) + const liquidityDelta = toLiquidity(1000000n) const positionAmount = positionsAmount / 2n @@ -366,7 +366,7 @@ describe('swap tests', () => { const poolBefore = await getPool(invariant, poolKey) const slippageLimit = poolBefore.sqrtPrice - const liquidityDelta = liquidity(1000000n) + const liquidityDelta = toLiquidity(1000000n) const positionAmount = positionsAmount / 2n