Skip to content

Commit

Permalink
Merge pull request #282 from invariant-labs/refactor-get-all-positions
Browse files Browse the repository at this point in the history
Refactor get all positions
  • Loading branch information
zielvna authored Jul 10, 2024
2 parents 46c6cde + c6cffd3 commit 987083c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 57 deletions.
4 changes: 2 additions & 2 deletions sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invariant-labs/a0-sdk",
"version": "0.2.10",
"version": "0.2.11",
"collaborators": [
"Invariant Labs"
],
Expand Down Expand Up @@ -36,7 +36,7 @@
"docs:copy": "cp ../README.md README.md",
"build:copy-wasm": "cd target && mkdir wasm && cp -r ../src/wasm/pkg ./wasm/pkg",
"test:all": "ts-mocha",
"test:local": "npm run test:utils && npm run test:wazero && npm run test:psp22 && npm run test:protocol-fee && npm run test:math && npm run test:invariant && npm run test:example && npm run test:events && npm run test:tx && npm run test:position && npm run test:get-positions && npm run test:query-on-pair && npm run test:get-all && npm run test:get-liquidity-ticks",
"test:local": "npm run test:utils && npm run test:wazero && npm run test:psp22 && npm run test:protocol-fee && npm run test:math && npm run test:invariant && npm run test:example && npm run test:events && npm run test:tx && npm run test:position && npm run test:get-positions && npm run test:get-all && npm run test:query-on-pair && npm run test:get-liquidity-ticks",
"test:utils": "mocha ./tests/utils.test.ts -g utils",
"test:wazero": "mocha ./tests/wrapped-azero.test.ts -g wrapped-azero",
"test:psp22": "mocha ./tests/psp22.test.ts -g psp22",
Expand Down
25 changes: 11 additions & 14 deletions sdk/src/invariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,48 +530,45 @@ export class Invariant {
owner: string,
positionsCount?: bigint,
skipPages?: number[],
positionsPerPage?: bigint,
options: ContractOptions = {
storageDepositLimit: this.storageDepositLimit,
refTime: this.gasLimit.refTime.toNumber(),
proofSize: this.gasLimit.proofSize.toNumber()
}
): Promise<Page[]> {
const firstPageIndex = skipPages?.find(i => !skipPages.includes(i)) || 1
const firstPageIndex = skipPages?.find(i => !skipPages.includes(i)) || 0
const positionsPerPageLimit = positionsPerPage || POSITIONS_ENTRIES_LIMIT

let pages: Page[] = []
let actualPositionsCount = positionsCount
if (!positionsCount) {
const [positionEntries, positionsCount] = await this.getPositions(
owner,
POSITIONS_ENTRIES_LIMIT,
BigInt(firstPageIndex - 1) * POSITIONS_ENTRIES_LIMIT,
positionsPerPageLimit,
BigInt(firstPageIndex) * positionsPerPageLimit,
options
)

pages.push({ index: 1, entries: positionEntries })
pages.push({ index: 0, entries: positionEntries })
actualPositionsCount = positionsCount
}

const promises: Promise<[[Position, Pool][], bigint]>[] = []
const pageIndexes: number[] = []

for (
let i = positionsCount ? firstPageIndex - 1 : firstPageIndex;
i < Math.ceil(Number(actualPositionsCount) / Number(POSITIONS_ENTRIES_LIMIT));
let i = positionsCount ? firstPageIndex : firstPageIndex + 1;
i < Math.ceil(Number(actualPositionsCount) / Number(positionsPerPageLimit));
i++
) {
if (skipPages?.includes(i + 1)) {
if (skipPages?.includes(i)) {
continue
}

pageIndexes.push(i + 1)
pageIndexes.push(i)
promises.push(
this.getPositions(
owner,
POSITIONS_ENTRIES_LIMIT,
BigInt(i) * POSITIONS_ENTRIES_LIMIT,
options
)
this.getPositions(owner, positionsPerPageLimit, BigInt(i) * positionsPerPageLimit, options)
)
}

Expand Down
127 changes: 88 additions & 39 deletions sdk/tests/get-all.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('get-all', async () => {
for (const [positionIndex, [position, pool]] of entries.entries()) {
const expectedPosition = await invariant.getPosition(
account.address,
BigInt((index - 1) * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
BigInt(index * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
)
const expectedPool = await invariant.getPool(
expectedPosition.poolKey.tokenX,
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('get-all', async () => {
for (const [positionIndex, [position, pool]] of entries.entries()) {
const expectedPosition = await invariant.getPosition(
account.address,
BigInt((index - 1) * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
BigInt(index * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
)
const expectedPool = await invariant.getPool(
expectedPosition.poolKey.tokenX,
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('get-all', async () => {
for (const [positionIndex, [position, pool]] of entries.entries()) {
const expectedPosition = await invariant.getPosition(
account.address,
BigInt((index - 1) * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
BigInt(index * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
)
const expectedPool = await invariant.getPool(
expectedPosition.poolKey.tokenX,
Expand Down Expand Up @@ -236,14 +236,14 @@ describe('get-all', async () => {
)
}

const pages = await invariant.getAllPositions(account.address, undefined, [2, 4])
const pages = await invariant.getAllPositions(account.address, undefined, [1, 3])
assert.equal(pages.map(page => page.entries).flat(1).length, 102)

for (const { index, entries } of pages) {
for (const [positionIndex, [position, pool]] of entries.entries()) {
const expectedPosition = await invariant.getPosition(
account.address,
BigInt((index - 1) * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
BigInt(index * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
)
const expectedPool = await invariant.getPool(
expectedPosition.poolKey.tokenX,
Expand All @@ -254,47 +254,96 @@ describe('get-all', async () => {
assert.deepEqual(pool, expectedPool)
}
}
})

it('get all positions with positions count and skip pages', async function () {
this.timeout(300000)
it('get all positions with positions count and skip pages', async function () {
this.timeout(300000)

await invariant.addFeeTier(account, feeTier)
await invariant.createPool(
await invariant.addFeeTier(account, feeTier)
await invariant.createPool(
account,
newPoolKey(token0Address, token1Address, feeTier),
SQRT_PRICE_DENOMINATOR
)
for (let i = 0; i < 160; i++) {
await invariant.createPosition(
account,
newPoolKey(token0Address, token1Address, feeTier),
SQRT_PRICE_DENOMINATOR
poolKey,
-BigInt((i + 1) * 10),
BigInt((i + 1) * 10),
1000000n,
SQRT_PRICE_DENOMINATOR,
0n
)
for (let i = 0; i < 160n; i++) {
await invariant.createPosition(
account,
poolKey,
-BigInt((i + 1) * 10),
BigInt((i + 1) * 10),
1000000n,
SQRT_PRICE_DENOMINATOR,
0n
}

const pages = await invariant.getAllPositions(account.address, 140n, [0, 1])
assert.equal(pages.map(page => page.entries).flat(1).length, 51)

for (const { index, entries } of pages) {
for (const [positionIndex, [position, pool]] of entries.entries()) {
const expectedPosition = await invariant.getPosition(
account.address,
BigInt(index * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
)
const expectedPool = await invariant.getPool(
expectedPosition.poolKey.tokenX,
expectedPosition.poolKey.tokenY,
expectedPosition.poolKey.feeTier
)

assert.deepEqual(position, expectedPosition)
assert.deepEqual(pool, expectedPool)
}
}
})

it('get all positions with positions per page and skip pages', async function () {
this.timeout(30000)

const pages = await invariant.getAllPositions(account.address, 140n, [1, 2])
assert.equal(pages.map(page => page.entries).flat(1).length, 38)

for (const { index, entries } of pages) {
for (const [positionIndex, [position, pool]] of entries.entries()) {
const expectedPosition = await invariant.getPosition(
account.address,
BigInt((index - 1) * Number(POSITIONS_ENTRIES_LIMIT) + positionIndex)
)
const expectedPool = await invariant.getPool(
expectedPosition.poolKey.tokenX,
expectedPosition.poolKey.tokenY,
expectedPosition.poolKey.feeTier
)

assert.deepEqual(position, expectedPosition)
assert.deepEqual(pool, expectedPool)
}
await invariant.addFeeTier(account, feeTier)
await invariant.createPool(
account,
newPoolKey(token0Address, token1Address, feeTier),
SQRT_PRICE_DENOMINATOR
)
for (let i = 0; i < 50; i++) {
await invariant.createPosition(
account,
poolKey,
-BigInt((i + 1) * 10),
BigInt((i + 1) * 10),
1000000n,
SQRT_PRICE_DENOMINATOR,
0n
)
}

const positionsPerPage = 10n
const pages = await invariant.getAllPositions(
account.address,
undefined,
[1, 3],
positionsPerPage
)
assert.equal(pages.length, 3)
assert.equal(pages.map(page => page.entries).flat(1).length, 30)

for (const { index, entries } of pages) {
for (const [positionIndex, [position, pool]] of entries.entries()) {
const expectedPosition = await invariant.getPosition(
account.address,
BigInt(index * Number(positionsPerPage) + positionIndex)
)
const expectedPool = await invariant.getPool(
expectedPosition.poolKey.tokenX,
expectedPosition.poolKey.tokenY,
expectedPosition.poolKey.feeTier
)

assert.deepEqual(position, expectedPosition)
assert.deepEqual(pool, expectedPool)
}
})
}
})
})

0 comments on commit 987083c

Please sign in to comment.