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

refactor get all pool keys and get all positions #280

Merged
merged 12 commits into from
Jul 8, 2024
14 changes: 6 additions & 8 deletions sdk/contracts/invariant/invariant.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0x86f442ed070586bbb8157cda6fdc259b84f953e62727d779806988f81937cd1f",
"hash": "0xba8d2adeacedf76483f1b5e50843cdf9e09f979a0be15def9c09711cd6821c4e",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down Expand Up @@ -1400,9 +1400,9 @@
"label": "size",
"type": {
"displayName": [
"u8"
"u16"
],
"type": 4
"type": 10
}
},
{
Expand All @@ -1417,7 +1417,7 @@
],
"default": false,
"docs": [],
"label": "InvariantTrait::get_pools",
"label": "InvariantTrait::get_pool_keys",
"mutates": false,
"payable": false,
"returnType": {
Expand All @@ -1427,7 +1427,7 @@
],
"type": 92
},
"selector": "0x6dd2d776"
"selector": "0x57d47dcb"
},
{
"args": [],
Expand Down Expand Up @@ -4649,9 +4649,7 @@
"def": {
"tuple": [
15,
25,
36,
36
25
]
}
}
Expand Down
Binary file modified sdk/contracts/invariant/invariant.wasm
Binary file not shown.
5 changes: 3 additions & 2 deletions sdk/package.json
Original file line number Diff line number Diff line change
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-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:query-on-pair && npm run test:get-all && 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 All @@ -54,7 +54,8 @@
"test:crosses": "mocha ./tests/crosses-testnet.test.ts -g testnet-crosses-limitations",
"test:tx": "mocha ./tests/tx.test.ts -g tx",
"test:simulate-invariant-swap": "mocha ./tests/simulate-invariant-swap.test.ts -g simulateInvariantSwap",
"test:query-on-pair": "mocha ./tests/query-on-pair.test.ts -g query-on-pair"
"test:query-on-pair": "mocha ./tests/query-on-pair.test.ts -g query-on-pair",
"test:get-all": "mocha ./tests/get-all.test.ts -g get-all"
},
"dependencies": {
"@polkadot/api": "^10.12.4",
Expand Down
14 changes: 6 additions & 8 deletions sdk/src/abis/invariant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const abi = `
{
"source": {
"hash": "0x86f442ed070586bbb8157cda6fdc259b84f953e62727d779806988f81937cd1f",
"hash": "0xba8d2adeacedf76483f1b5e50843cdf9e09f979a0be15def9c09711cd6821c4e",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down Expand Up @@ -1401,9 +1401,9 @@ export const abi = `
"label": "size",
"type": {
"displayName": [
"u8"
"u16"
],
"type": 4
"type": 10
}
},
{
Expand All @@ -1418,7 +1418,7 @@ export const abi = `
],
"default": false,
"docs": [],
"label": "InvariantTrait::get_pools",
"label": "InvariantTrait::get_pool_keys",
"mutates": false,
"payable": false,
"returnType": {
Expand All @@ -1428,7 +1428,7 @@ export const abi = `
],
"type": 92
},
"selector": "0x6dd2d776"
"selector": "0x57d47dcb"
},
{
"args": [],
Expand Down Expand Up @@ -4650,9 +4650,7 @@ export const abi = `
"def": {
"tuple": [
15,
25,
36,
36
25
]
}
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getMaxTickmapQuerySize,
getPercentageDenominator,
getPercentageScale,
getPositionsEntriesLimit,
getPriceDenominator,
getPriceScale,
getSecondsPerLiquidityDenominator,
Expand Down Expand Up @@ -67,3 +68,4 @@ export const MAX_TICK_CROSS = getMaxTickCross()
export const LIQUIDITY_TICKS_LIMIT = getLiquidityTicksLimit()
export const MAX_POOL_KEYS_RETURNED = getMaxPoolKeysReturned()
export const MAX_POOL_PAIRS_RETURNED = getMaxPoolPairsReturned()
export const POSITIONS_ENTRIES_LIMIT = getPositionsEntriesLimit()
91 changes: 88 additions & 3 deletions sdk/src/invariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import {
DEFAULT_PROOF_SIZE,
DEFAULT_REF_TIME,
LIQUIDITY_TICKS_LIMIT,
MAX_TICKMAP_QUERY_SIZE
MAX_POOL_KEYS_RETURNED,
MAX_TICKMAP_QUERY_SIZE,
POSITIONS_ENTRIES_LIMIT
} from './consts.js'
import { Network } from './network.js'
import {
Expand Down Expand Up @@ -61,6 +63,9 @@ import {
getMinTick
} from './utils.js'
import { SubmittableExtrinsic } from '@polkadot/api/types/submittable'

type Page = { index: number; entries: [Position, Pool][] }

export class Invariant {
contract: ContractPromise
api: ApiPromise
Expand Down Expand Up @@ -502,7 +507,7 @@ export class Invariant {
refTime: this.gasLimit.refTime.toNumber(),
proofSize: this.gasLimit.proofSize.toNumber()
}
): Promise<[[Position, Pool, Tick, Tick][], bigint]> {
): Promise<[[Position, Pool][], bigint]> {
const result = await sendQuery(
this.contract,
this.api.registry.createType('WeightV2', {
Expand All @@ -522,6 +527,66 @@ export class Invariant {
}

async getAllPositions(
owner: string,
positionsCount?: bigint,
skipPages?: number[],
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

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

pages.push({ index: 1, entries: positionEntries })
zielvna marked this conversation as resolved.
Show resolved Hide resolved
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));
i++
) {
if (skipPages?.includes(i + 1)) {
continue
}

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

const positionsEntriesList = await Promise.all(promises)
pages = [
...pages,
...positionsEntriesList.map(([positionsEntries], index) => {
return { index: pageIndexes[index], entries: positionsEntries }
})
]

return pages
}

async _getAllPositions(
owner: string,
options: ContractOptions = {
storageDepositLimit: this.storageDepositLimit,
Expand Down Expand Up @@ -855,7 +920,7 @@ export class Invariant {
proofSize: options.proofSize
}) as WeightV2,
options.storageDepositLimit,
InvariantQuery.GetPools,
InvariantQuery.GetPoolKeys,
[size, offset]
)
if (result.ok) {
Expand All @@ -865,6 +930,26 @@ export class Invariant {
}
}

async getAllPoolKeys(
options: ContractOptions = {
storageDepositLimit: this.storageDepositLimit,
refTime: this.gasLimit.refTime.toNumber(),
proofSize: this.gasLimit.proofSize.toNumber()
}
): Promise<PoolKey[]> {
const [poolKeys, poolKeysCount] = await this.getPoolKeys(MAX_POOL_KEYS_RETURNED, 0n, options)

const promises: Promise<[PoolKey[], bigint]>[] = []
for (let i = 1; i < Math.ceil(Number(poolKeysCount) / Number(MAX_POOL_KEYS_RETURNED)); i++) {
promises.push(
this.getPoolKeys(MAX_POOL_KEYS_RETURNED, BigInt(i) * MAX_POOL_KEYS_RETURNED, options)
)
}

const poolKeysEntries = await Promise.all(promises)
return [...poolKeys, ...poolKeysEntries.map(([poolKeys]) => poolKeys).flat(1)]
}

createPoolTx(
poolKey: PoolKey,
initSqrtPrice: SqrtPrice,
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum InvariantQuery {
GetFeeTiers = `${invariantActionPrefix}getFeeTiers`,
FeeTierExist = `${invariantActionPrefix}feeTierExist`,
GetPool = `${invariantActionPrefix}getPool`,
GetPools = `${invariantActionPrefix}getPools`,
GetPoolKeys = `${invariantActionPrefix}getPoolKeys`,
GetTick = `${invariantActionPrefix}getTick`,
IsTickInitialized = `${invariantActionPrefix}isTickInitialized`,
GetPosition = `${invariantActionPrefix}getPosition`,
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/wasm/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ pub const POOL_KEY_SIZE: usize = ACCOUNT_ID_SIZE + ACCOUNT_ID_SIZE + (64 + 8);
pub const POSITION_SIZE: usize = POOL_KEY_SIZE + 128 + 32 + 32 + 128 + 128 + 64 + 128 + 128;
pub const POOL_SIZE: usize = 128 + 128 + 32 + 128 + 128 + 128 + 128 + 64 + 64 + ACCOUNT_ID_SIZE;
pub const TICK_SIZE: usize = 32 + 8 + 128 + 128 + 128 + 128 + 128 + 64;
pub const POSITIONS_ENTRIES_LIMIT: usize =
(MAX_RESULT_SIZE - 32) / (POSITION_SIZE + POOL_SIZE + TICK_SIZE + TICK_SIZE);
pub const POSITIONS_ENTRIES_LIMIT: usize = (MAX_RESULT_SIZE - 32) / (POSITION_SIZE + POOL_SIZE);

#[wasm_wrapper]
pub fn get_global_max_sqrt_price() -> u128 {
Expand Down
Loading
Loading