From e5158d9cc5016a635b554db9546f67be81df11b8 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Thu, 11 Jul 2024 13:02:19 +0200 Subject: [PATCH] Positions start counting from 0 --- contracts/collections/positions.ral | 10 ++-- src/snippets.ts | 4 +- src/testUtils.ts | 4 +- test/claim.test.ts | 6 +-- ...tion_with_pool_on_removed_fee_tier.test.ts | 8 ++-- test/liquidity_gap.test.ts | 4 +- test/position.test.ts | 14 +++--- test/position_list.test.ts | 46 +++++++++---------- 8 files changed, 48 insertions(+), 48 deletions(-) diff --git a/contracts/collections/positions.ral b/contracts/collections/positions.ral index 89c8bb6..a03840b 100644 --- a/contracts/collections/positions.ral +++ b/contracts/collections/positions.ral @@ -5,15 +5,15 @@ Abstract Contract Positions(clamm: CLAMM) extends PositionHelper(clamm) { payer: Address, position: Position ) -> () { - let length = positionCount(caller) + 1 + let length = positionCount(caller) let key = toByteVec!(caller) ++ toByteVec!(length) positions.insert!(payer, key, position) if (!positionsCounter.contains!(caller)) { - positionsCounter.insert!(payer, caller, length) + positionsCounter.insert!(payer, caller, 1) } else { - positionsCounter[caller] = length + positionsCounter[caller] = length + 1 } } @@ -56,10 +56,10 @@ Abstract Contract Positions(clamm: CLAMM) extends PositionHelper(clamm) { fn wrappedRemovePosition(caller: Address, index: U256) -> () { let length = positionCount(caller) let key = toByteVec!(caller) ++ toByteVec!(index) - if (length == index) { + if (length - 1 == index) { positions.remove!(caller, key) } else { - let lastKey = toByteVec!(caller) ++ toByteVec!(length) + let lastKey = toByteVec!(caller) ++ toByteVec!(length - 1) let lastPosition = positions[lastKey] positions[key] = lastPosition positions.remove!(caller, lastKey) diff --git a/src/snippets.ts b/src/snippets.ts index e0b51aa..fde6358 100644 --- a/src/snippets.ts +++ b/src/snippets.ts @@ -151,7 +151,7 @@ export const transferAndVerifyPosition = async ( const { owner: previousOwner, ...toTransfer } = await getPosition(invariant, owner.address, index) const ownerLastPositionBefore = await getPosition(invariant, owner.address, ownerListLength) await transferPosition(invariant, owner, index, recipient) - const transferredPosition = await getPosition(invariant, recipient, recipientListLength + 1n) + const transferredPosition = await getPosition(invariant, recipient, recipientListLength) const ownerAtIndexPositionAfter = await getPosition(invariant, owner.address, index) expect(transferredPosition).toStrictEqual({ owner: recipient, ...toTransfer }) @@ -159,5 +159,5 @@ export const transferAndVerifyPosition = async ( expect(ownerAtIndexPositionAfter).toStrictEqual(ownerLastPositionBefore) } verifyPositionList(invariant, owner.address, ownerListLength - 1n) - verifyPositionList(invariant, recipient, recipientListLength + 1n) + verifyPositionList(invariant, recipient, recipientListLength) } diff --git a/src/testUtils.ts b/src/testUtils.ts index 50058c2..ec24e7b 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -316,13 +316,13 @@ export const verifyPositionList = async ( length: bigint, isWhole = false ) => { - for (let n = 1n; n <= length; ++n) { + for (let n = 0n; n < length; ++n) { const { exists: positionExists } = await getPosition(invariant, owner, n) expect(positionExists).toBeTruthy() } if (isWhole) { - const { exists: positionExists } = await getPosition(invariant, owner, length + 1n) + const { exists: positionExists } = await getPosition(invariant, owner, length) expect(positionExists).toBeFalsy() } } diff --git a/test/claim.test.ts b/test/claim.test.ts index 2a4c76d..fe1effc 100644 --- a/test/claim.test.ts +++ b/test/claim.test.ts @@ -53,7 +53,7 @@ describe('invariant tests', () => { await ClaimFee.execute(positionOwner, { initialFields: { invariant: invariant.contractId, - index: 1n + index: 0n }, attoAlphAmount: DUST_AMOUNT }) @@ -66,7 +66,7 @@ describe('invariant tests', () => { expect(dexXAfter + expectedTokensClaimed).toBe(dexXBefore) const poolAfter = await getPool(invariant, poolKey) - const positionAfter = await getPosition(invariant, positionOwner.address, 1n) + const positionAfter = await getPosition(invariant, positionOwner.address, 0n) expect(positionAfter).toMatchObject({ feeGrowthInsideX: poolAfter.feeGrowthGlobalX, tokensOwedX: 0n @@ -80,7 +80,7 @@ describe('invariant tests', () => { ClaimFee.execute(notOwner, { initialFields: { invariant: invariant.contractId, - index: 1n + index: 0n }, attoAlphAmount: DUST_AMOUNT }), 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 dc6277c..b9634d7 100644 --- a/test/interaction_with_pool_on_removed_fee_tier.test.ts +++ b/test/interaction_with_pool_on_removed_fee_tier.test.ts @@ -146,7 +146,7 @@ describe('interaction with pool on removed fee tiers tests', () => { await ClaimFee.execute(positionOwner, { initialFields: { invariant: invariant.contractId, - index: 1n + index: 0n }, attoAlphAmount: DUST_AMOUNT }) @@ -185,7 +185,7 @@ describe('interaction with pool on removed fee tiers tests', () => { expect(yAfter - yBefore).toBe(0n) }) test('close position', async () => { - await removePosition(invariant, positionOwner, 1n) + await removePosition(invariant, positionOwner, 0n) }) test('get pool', async () => { await getPool(invariant, poolKey) @@ -221,13 +221,13 @@ describe('interaction with pool on removed fee tiers tests', () => { await TransferPosition.execute(positionOwner, { initialFields: { invariant: invariant.contractId, - index: 1n, + index: 0n, recipient: recipient.address }, attoAlphAmount: MAP_ENTRY_DEPOSIT * 2n + DUST_AMOUNT * 2n }) - const transferedPosition = await getPosition(invariant, recipient.address, 1n) + const transferedPosition = await getPosition(invariant, recipient.address, 0n) expect(transferedPosition.exists).toBe(true) expect(transferedPosition.liquidity).toBe(liquidityDelta) expect(transferedPosition.lowerTickIndex).toBe(lowerTickIndex) diff --git a/test/liquidity_gap.test.ts b/test/liquidity_gap.test.ts index 6ca7738..abace95 100644 --- a/test/liquidity_gap.test.ts +++ b/test/liquidity_gap.test.ts @@ -151,8 +151,8 @@ describe('liquidity gap tests', () => { await initSwap(invariant, swapper, poolKey, true, amount, true, targetSqrtPrice) const pool = await getPool(invariant, poolKey) - const firstPosition = await getPosition(invariant, positionOwner.address, 1n) - const secondPosition = await getPosition(invariant, positionOwner.address, 2n) + const firstPosition = await getPosition(invariant, positionOwner.address, 0n) + const secondPosition = await getPosition(invariant, positionOwner.address, 1n) const { exists: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, -50n) const { exists: currentInMap } = await getTick(invariant, poolKey, -60n) diff --git a/test/position.test.ts b/test/position.test.ts index 391d119..d1323da 100644 --- a/test/position.test.ts +++ b/test/position.test.ts @@ -69,7 +69,7 @@ describe('position tests', () => { MaxSqrtPrice ) - const position = await getPosition(invariant, positionOwner.address, 1n) + const position = await getPosition(invariant, positionOwner.address, 0n) const expectedPosition = { exists: true, liquidity: 10n, @@ -179,7 +179,7 @@ describe('position tests', () => { slippageLimitUpper ) - const position = await getPosition(invariant, positionOwner.address, 1n) + const position = await getPosition(invariant, positionOwner.address, 0n) const expectedPosition = { exists: true, liquidity: liquidityDelta, @@ -217,7 +217,7 @@ describe('position tests', () => { slippageLimitUpper ) - const position = await getPosition(invariant, positionOwner.address, 2n) + const position = await getPosition(invariant, positionOwner.address, 1n) const expectedPosition = { exists: true, liquidity: liquidityDelta, @@ -265,7 +265,7 @@ describe('position tests', () => { const { x: dexXBefore, y: dexYBefore } = await getReserveBalances(invariant, poolKey) - await removePosition(invariant, positionOwner, 1n) + await removePosition(invariant, positionOwner, 0n) const pool = await getPool(invariant, poolKey) const { exists: lowerInMap } = await getTick(invariant, poolKey, lowerTickIndex) @@ -332,7 +332,7 @@ describe('position tests', () => { slippageLimitUpper ) - const position = await getPosition(invariant, positionOwner.address, 1n) + const position = await getPosition(invariant, positionOwner.address, 0n) const pool = await getPool(invariant, poolKey) const { exists: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) const { exists: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) @@ -420,7 +420,7 @@ describe('position tests', () => { slippageLimitUpper ) - const position = await getPosition(invariant, positionOwner.address, 1n) + const position = await getPosition(invariant, positionOwner.address, 0n) const pool = await getPool(invariant, poolKey) const { exists: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) const { exists: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) @@ -512,7 +512,7 @@ describe('position tests', () => { slippageLimitUpper ) - const position = await getPosition(invariant, positionOwner.address, 1n) + const position = await getPosition(invariant, positionOwner.address, 0n) const pool = await getPool(invariant, poolKey) const { exists: lowerInMap, ...lowerTick } = await getTick(invariant, poolKey, lowerTickIndex) const { exists: upperInMap, ...upperTick } = await getTick(invariant, poolKey, upperTickIndex) diff --git a/test/position_list.test.ts b/test/position_list.test.ts index 2a87d2a..b0c8705 100644 --- a/test/position_list.test.ts +++ b/test/position_list.test.ts @@ -93,7 +93,7 @@ describe('position list tests', () => { // all 3 exact same ticks { const [lowerTickIndex, upperTickIndex] = [-10n, 10n] - for (let n = 1n; n <= 3n; ++n) { + for (let n = 0n; n < 3n; ++n) { await initPosition( invariant, positionsOwner, @@ -127,7 +127,7 @@ describe('position list tests', () => { sign: false }) - for (let n = 1n; n <= 3n; ++n) { + for (let n = 0n; n < 3n; ++n) { const position = await getPosition(invariant, positionsOwner.address, n) expect(position).toMatchObject({ poolKey, @@ -163,7 +163,7 @@ describe('position list tests', () => { const newPosition = await getPosition( invariant, positionsOwner.address, - 3n + BigInt(index + 1) + 2n + BigInt(index + 1) ) expect(newPosition).toMatchObject({ poolKey, @@ -286,9 +286,9 @@ describe('position list tests', () => { // remove middle position { - const lastPosition = await getPosition(invariant, positionsOwner.address, 4n) - await removePosition(invariant, positionsOwner, 2n) - const replacedPosition = await getPosition(invariant, positionsOwner.address, 2n) + const lastPosition = await getPosition(invariant, positionsOwner.address, 3n) + await removePosition(invariant, positionsOwner, 1n) + const replacedPosition = await getPosition(invariant, positionsOwner.address, 1n) expect(replacedPosition).toStrictEqual(lastPosition) verifyPositionList(invariant, positionsOwner.address, 3n, true) @@ -307,20 +307,20 @@ describe('position list tests', () => { slippageLimitLower, MaxSqrtPrice ) - const { exists: positionExists } = await getPosition(invariant, positionsOwner.address, 4n) + const { exists: positionExists } = await getPosition(invariant, positionsOwner.address, 3n) expect(positionExists).toBeTruthy() } // remove last position { - await removePosition(invariant, positionsOwner, 4n) - const { exists: positionExists } = await getPosition(invariant, positionsOwner.address, 4n) + await removePosition(invariant, positionsOwner, 3n) + const { exists: positionExists } = await getPosition(invariant, positionsOwner.address, 3n) expect(positionExists).toBeFalsy() } // remove all positions { - for (let n = 3n; n > 0; --n) { + for (let n = 2n; n >= 0; --n) { await removePosition(invariant, positionsOwner, n) - verifyPositionList(invariant, positionsOwner.address, n - 1n, true) + verifyPositionList(invariant, positionsOwner.address, n, true) } } @@ -338,7 +338,7 @@ describe('position list tests', () => { slippageLimitLower, MaxSqrtPrice ) - const position = await getPosition(invariant, positionsOwner.address, 1n) + const position = await getPosition(invariant, positionsOwner.address, 0n) expect(position).toMatchObject({ exists: true, poolKey, @@ -347,14 +347,14 @@ describe('position list tests', () => { liquidity: liquiditiyDelta, owner: positionsOwner.address }) - const { exists: secondExists } = await getPosition(invariant, positionsOwner.address, 2n) + const { exists: secondExists } = await getPosition(invariant, positionsOwner.address, 1n) expect(secondExists).toBeFalsy() } }) test('only owner can modify position list', async () => { const notOwner = await getSigner(ONE_ALPH * 1000n, 0) - expectError(InvariantError.PositionNotFound, removePosition(invariant, notOwner, 4n), invariant) + expectError(InvariantError.PositionNotFound, removePosition(invariant, notOwner, 3n), invariant) }) test('transfer position ownership', async () => { @@ -364,8 +364,8 @@ describe('position list tests', () => { await transferAndVerifyPosition( invariant, positionsOwner, - 4n, - 1n, + 3n, + 0n, positionsRecipient.address, 0n ) @@ -374,8 +374,8 @@ describe('position list tests', () => { await transferAndVerifyPosition( invariant, positionsOwner, - 3n, 2n, + 1n, positionsRecipient.address, 1n ) @@ -384,8 +384,8 @@ describe('position list tests', () => { await transferAndVerifyPosition( invariant, positionsOwner, - 2n, - 2n, + 1n, + 0n, positionsRecipient.address, 2n ) @@ -394,8 +394,8 @@ describe('position list tests', () => { await transferAndVerifyPosition( invariant, positionsOwner, - 1n, - 1n, + 0n, + 0n, positionsRecipient.address, 3n ) @@ -404,8 +404,8 @@ describe('position list tests', () => { await transferAndVerifyPosition( invariant, positionsRecipient, - 4n, - 1n, + 3n, + 0n, positionsOwner.address, 0n )