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

Add created_at timestamp field to Position #286

Merged
merged 2 commits into from
Jul 29, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ To begin, navigate to the directory dedicated to the [SDK](https://github.com/in

- Run e2e tests
```bash
./test.sh
./tests.sh
```
2 changes: 1 addition & 1 deletion sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ To begin, navigate to the directory dedicated to the [SDK](https://github.com/in

- Run e2e tests
```bash
./test.sh
./tests.sh
```
16 changes: 15 additions & 1 deletion sdk/contracts/invariant/invariant.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0x24ae0239ce4ae019c50e5f1e3655a13ab4b6501afbfafc3144b8b5c1f9f7e391",
"hash": "0x4e0b9ef583e1fb698e71d5c30fa96798fea6027811de730eb2204c0d14537ba5",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down Expand Up @@ -1909,6 +1909,15 @@
}
},
"name": "tokens_owed_y"
},
{
"layout": {
"leaf": {
"key": "0xaa6cd0bf",
"ty": 9
}
},
"name": "created_at"
}
],
"name": "Position"
Expand Down Expand Up @@ -2706,6 +2715,11 @@
"name": "tokens_owed_y",
"type": 21,
"typeName": "TokenAmount"
},
{
"name": "created_at",
"type": 9,
"typeName": "u64"
}
]
}
Expand Down
Binary file modified sdk/contracts/invariant/invariant.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion sdk/contracts/psp22/psp22.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0xb4082b7d4aedb8479925aa96a383ea722dc1a7e9b29ad5a4d5f95ad707db4e09",
"hash": "0x25f86558683436a17b386cc41583659ffffb69d87c70e995945a2baf1a299074",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
Binary file modified sdk/contracts/psp22/psp22.wasm
Binary file not shown.
12 changes: 6 additions & 6 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.13",
"version": "0.2.14",
"collaborators": [
"Invariant Labs"
],
Expand Down Expand Up @@ -61,7 +61,7 @@
"@polkadot/api": "^10.12.4",
"@polkadot/api-contract": "^10.12.4",
"@scio-labs/use-inkathon": "^0.6.3",
"@invariant-labs/a0-sdk-wasm": "^0.1.21",
"@invariant-labs/a0-sdk-wasm": "^0.1.22",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
Expand Down
16 changes: 15 additions & 1 deletion sdk/src/abis/invariant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const abi = `
{
"source": {
"hash": "0x24ae0239ce4ae019c50e5f1e3655a13ab4b6501afbfafc3144b8b5c1f9f7e391",
"hash": "0x4e0b9ef583e1fb698e71d5c30fa96798fea6027811de730eb2204c0d14537ba5",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down Expand Up @@ -1910,6 +1910,15 @@ export const abi = `
}
},
"name": "tokens_owed_y"
},
{
"layout": {
"leaf": {
"key": "0xaa6cd0bf",
"ty": 9
}
},
"name": "created_at"
}
],
"name": "Position"
Expand Down Expand Up @@ -2707,6 +2716,11 @@ export const abi = `
"name": "tokens_owed_y",
"type": 21,
"typeName": "TokenAmount"
},
{
"name": "created_at",
"type": 9,
"typeName": "u64"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/abis/psp22.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const abi = `
{
"source": {
"hash": "0xb4082b7d4aedb8479925aa96a383ea722dc1a7e9b29ad5a4d5f95ad707db4e09",
"hash": "0x25f86558683436a17b386cc41583659ffffb69d87c70e995945a2baf1a299074",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const objectEquals = (
) => {
for (const key in object) {
if (!keys.includes(key)) {
assert.deepEqual(object[key], expectedObject[key])
assert.deepEqual(object[key], expectedObject[key], `Key missing: ${key}`)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/wasm/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const MAX_POOL_PAIRS_RETURNED: usize =

pub const ACCOUNT_ID_SIZE: usize = 32 * 8;
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 POSITION_SIZE: usize = POOL_KEY_SIZE + 128 + 32 + 32 + 128 + 128 + 64 + 128 + 128 + 64;
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);
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/wasm/storage/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ pub struct Position {
pub last_block_number: u64,
pub tokens_owed_x: TokenAmount,
pub tokens_owed_y: TokenAmount,
#[tsify(type = "bigint")]
pub created_at: u64,
}
38 changes: 28 additions & 10 deletions sdk/tests/get-all.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,20 @@ describe('get-all', async () => {
newPoolKey(token0Address, token1Address, feeTier),
SQRT_PRICE_DENOMINATOR
)
for (let i = 0; i < 60; i++) {
for (let i = 0n; i < POSITIONS_ENTRIES_LIMIT + 10n; i++) {
await invariant.createPosition(
account,
poolKey,
-BigInt((i + 1) * 10),
BigInt((i + 1) * 10),
(i + 1n) * -10n,
(i + 1n) * 10n,
1000000n,
SQRT_PRICE_DENOMINATOR,
0n
)
}

const pages = await invariant.getAllPositions(account.address, 50n)
assert.equal(pages.map(page => page.entries).flat(1).length, 51)
const pages = await invariant.getAllPositions(account.address, POSITIONS_ENTRIES_LIMIT - 10n)
assert.equal(pages.map(page => page.entries).flat(1).length, Number(POSITIONS_ENTRIES_LIMIT))

for (const { index, entries } of pages) {
for (const [positionIndex, [position, pool]] of entries.entries()) {
Expand Down Expand Up @@ -224,7 +224,10 @@ describe('get-all', async () => {
newPoolKey(token0Address, token1Address, feeTier),
SQRT_PRICE_DENOMINATOR
)
for (let i = 0; i < 200; i++) {
const skippedPages = [1, 3]
const fullPagesCount = Math.max(...skippedPages) + 1
const positionsCount = fullPagesCount * Number(POSITIONS_ENTRIES_LIMIT)
for (let i = 0; i < positionsCount; i++) {
await invariant.createPosition(
account,
poolKey,
Expand All @@ -236,8 +239,11 @@ describe('get-all', async () => {
)
}

const pages = await invariant.getAllPositions(account.address, undefined, [1, 3])
assert.equal(pages.map(page => page.entries).flat(1).length, 102)
const pages = await invariant.getAllPositions(account.address, undefined, skippedPages)
const expectedPages =
Math.floor(positionsCount / Number(POSITIONS_ENTRIES_LIMIT)) - skippedPages.length
const expectedPositions = expectedPages * Number(POSITIONS_ENTRIES_LIMIT)
assert.equal(pages.map(page => page.entries).flat(1).length, expectedPositions)

for (const { index, entries } of pages) {
for (const [positionIndex, [position, pool]] of entries.entries()) {
Expand Down Expand Up @@ -265,6 +271,10 @@ describe('get-all', async () => {
newPoolKey(token0Address, token1Address, feeTier),
SQRT_PRICE_DENOMINATOR
)

const skippedPages = [0, 1]
const fullPagesCount = Math.max(...skippedPages) + 2
const positionsCount = fullPagesCount * Number(POSITIONS_ENTRIES_LIMIT) + 10
for (let i = 0; i < 160; i++) {
await invariant.createPosition(
account,
Expand All @@ -277,8 +287,16 @@ describe('get-all', async () => {
)
}

const pages = await invariant.getAllPositions(account.address, 140n, [0, 1])
assert.equal(pages.map(page => page.entries).flat(1).length, 51)
const queriedPositions = positionsCount - 20
const pages = await invariant.getAllPositions(
account.address,
BigInt(queriedPositions),
skippedPages
)
const expectedPages =
Math.ceil(queriedPositions / Number(POSITIONS_ENTRIES_LIMIT)) - skippedPages.length
const expectedPositions = expectedPages * Number(POSITIONS_ENTRIES_LIMIT)
assert.equal(pages.map(page => page.entries).flat(1).length, expectedPositions)

for (const { index, entries } of pages) {
for (const [positionIndex, [position, pool]] of entries.entries()) {
Expand Down
4 changes: 2 additions & 2 deletions sdk/tests/get-positions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('get-positions', async () => {
feeReceiver: account.address
}

objectEquals(result[0][0][0], firstExpectedPosition, ['lastBlockNumber'])
objectEquals(result[0][0][0], firstExpectedPosition, ['lastBlockNumber', 'createdAt'])
objectEquals(result[0][0][1], firstExpectedPool, ['startTimestamp', 'lastTimestamp'])

const secondExpectedPosition = {
Expand All @@ -95,7 +95,7 @@ describe('get-positions', async () => {
feeReceiver: account.address
}

objectEquals(result[0][1][0], secondExpectedPosition, ['lastBlockNumber'])
objectEquals(result[0][1][0], secondExpectedPosition, ['lastBlockNumber', 'createdAt'])
objectEquals(result[0][1][1], secondExpectedPool, ['startTimestamp', 'lastTimestamp'])
})

Expand Down
20 changes: 12 additions & 8 deletions sdk/tests/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ describe('get-liquidity-by-x', async () => {
feeGrowthInsideY: 0n,
lastBlockNumber: 0n,
tokensOwedX: 0n,
tokensOwedY: 0n
tokensOwedY: 0n,
createdAt: 0n
}

await objectEquals(position, expectedPosition, ['lastBlockNumber'])
objectEquals(position, expectedPosition, ['lastBlockNumber', 'createdAt'])
}
// above range
{
Expand Down Expand Up @@ -168,9 +169,10 @@ describe('get-liquidity-by-x', async () => {
feeGrowthInsideY: 0n,
lastBlockNumber: 0n,
tokensOwedX: 0n,
tokensOwedY: 0n
tokensOwedY: 0n,
createdAt: 0n
}
await objectEquals(position, expectedPosition, ['lastBlockNumber'])
objectEquals(position, expectedPosition, ['lastBlockNumber', 'createdAt'])
}
})
})
Expand Down Expand Up @@ -254,9 +256,10 @@ describe('get-liquidity-by-y', async () => {
feeGrowthInsideY: 0n,
lastBlockNumber: 0n,
tokensOwedX: 0n,
tokensOwedY: 0n
tokensOwedY: 0n,
createdAt: 0n
}
await objectEquals(position, expectedPosition, ['lastBlockNumber'])
objectEquals(position, expectedPosition, ['lastBlockNumber', 'createdAt'])
}
// in range
{
Expand Down Expand Up @@ -303,9 +306,10 @@ describe('get-liquidity-by-y', async () => {
feeGrowthInsideY: 0n,
lastBlockNumber: 0n,
tokensOwedX: 0n,
tokensOwedY: 0n
tokensOwedY: 0n,
createdAt: 0n
}
await objectEquals(position, expectedPosition, ['lastBlockNumber'])
objectEquals(position, expectedPosition, ['lastBlockNumber', 'createdAt'])
}
// above range
{
Expand Down
10 changes: 6 additions & 4 deletions sdk/tests/position.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ describe('position', async () => {
feeGrowthInsideY: 0n,
lastBlockNumber: 0n,
tokensOwedX: 0n,
tokensOwedY: 0n
tokensOwedY: 0n,
createdAt: 0n
}
await objectEquals(position, expectedPosition, ['lastBlockNumber'])
objectEquals(position, expectedPosition, ['lastBlockNumber', 'createdAt'])
})
it('calculate token amounts from position liquidity', async () => {
const position = await invariant.getPosition(account.address, 0n)
Expand Down Expand Up @@ -162,9 +163,10 @@ describe('position', async () => {
feeGrowthInsideY: 0n,
lastBlockNumber: 0n,
tokensOwedX: 0n,
tokensOwedY: 0n
tokensOwedY: 0n,
createdAt: 0n
}
await objectEquals(position, expectedPosition, ['lastBlockNumber'])
objectEquals(position, expectedPosition, ['lastBlockNumber', 'createdAt'])
}
})

Expand Down
2 changes: 2 additions & 0 deletions src/contracts/storage/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Position {
pub last_block_number: u64,
pub tokens_owed_x: TokenAmount,
pub tokens_owed_y: TokenAmount,
pub created_at: u64,
}

impl Position {
Expand Down Expand Up @@ -188,6 +189,7 @@ impl Position {
last_block_number: block_number,
tokens_owed_x: TokenAmount::new(0),
tokens_owed_y: TokenAmount::new(0),
created_at: current_timestamp,
};

let (required_x, required_y) = unwrap!(position.modify(
Expand Down
Loading
Loading