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

Positions start counting from 0 #97

Merged
merged 1 commit into from
Jul 11, 2024
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
10 changes: 5 additions & 5 deletions contracts/collections/positions.ral
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ 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 })
if (index != ownerListLength) {
expect(ownerAtIndexPositionAfter).toStrictEqual(ownerLastPositionBefore)
}
verifyPositionList(invariant, owner.address, ownerListLength - 1n)
verifyPositionList(invariant, recipient, recipientListLength + 1n)
verifyPositionList(invariant, recipient, recipientListLength)
}
4 changes: 2 additions & 2 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/claim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('invariant tests', () => {
await ClaimFee.execute(positionOwner, {
initialFields: {
invariant: invariant.contractId,
index: 1n
index: 0n
},
attoAlphAmount: DUST_AMOUNT
})
Expand All @@ -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
Expand All @@ -80,7 +80,7 @@ describe('invariant tests', () => {
ClaimFee.execute(notOwner, {
initialFields: {
invariant: invariant.contractId,
index: 1n
index: 0n
},
attoAlphAmount: DUST_AMOUNT
}),
Expand Down
8 changes: 4 additions & 4 deletions test/interaction_with_pool_on_removed_fee_tier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/liquidity_gap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions test/position.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 23 additions & 23 deletions test/position_list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand All @@ -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,
Expand All @@ -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 () => {
Expand All @@ -364,8 +364,8 @@ describe('position list tests', () => {
await transferAndVerifyPosition(
invariant,
positionsOwner,
4n,
1n,
3n,
0n,
positionsRecipient.address,
0n
)
Expand All @@ -374,8 +374,8 @@ describe('position list tests', () => {
await transferAndVerifyPosition(
invariant,
positionsOwner,
3n,
2n,
1n,
positionsRecipient.address,
1n
)
Expand All @@ -384,8 +384,8 @@ describe('position list tests', () => {
await transferAndVerifyPosition(
invariant,
positionsOwner,
2n,
2n,
1n,
0n,
positionsRecipient.address,
2n
)
Expand All @@ -394,8 +394,8 @@ describe('position list tests', () => {
await transferAndVerifyPosition(
invariant,
positionsOwner,
1n,
1n,
0n,
0n,
positionsRecipient.address,
3n
)
Expand All @@ -404,8 +404,8 @@ describe('position list tests', () => {
await transferAndVerifyPosition(
invariant,
positionsRecipient,
4n,
1n,
3n,
0n,
positionsOwner.address,
0n
)
Expand Down
Loading