From d61457c57287b55f24ca35f6a87e0c07ea597868 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Sun, 25 Feb 2024 21:43:26 +0100 Subject: [PATCH] Fixed positions tests & updated namings --- contracts/invariant_tx.ral | 6 +- test/position.test.ts | 575 +++++++++++++++++++------------------ test/swap.test.ts | 30 +- 3 files changed, 307 insertions(+), 304 deletions(-) diff --git a/contracts/invariant_tx.ral b/contracts/invariant_tx.ral index d2e06628..351f21e0 100644 --- a/contracts/invariant_tx.ral +++ b/contracts/invariant_tx.ral @@ -30,8 +30,8 @@ TxScript CreatePosition( invariant: Invariant, token0: ByteVec, token1: ByteVec, - token0Amount: U256, - token1Amount: U256, + approvedTokens0: U256, + approvedTokens1: U256, index: U256, fee: U256, tickSpacing: U256, @@ -41,7 +41,7 @@ TxScript CreatePosition( slippageLimitLower: U256, slippageLimitUpper: U256 ) { - invariant.createPosition{callerAddress!() -> token0: token0Amount, token1: token1Amount} + invariant.createPosition{callerAddress!() -> token0: approvedTokens0, token1: approvedTokens1} (index, token0, token1, fee, tickSpacing, lowerTick, upperTick, liquidityDelta, slippageLimitLower, slippageLimitUpper) } diff --git a/test/position.test.ts b/test/position.test.ts index e8b2b69a..bf434533 100644 --- a/test/position.test.ts +++ b/test/position.test.ts @@ -109,8 +109,8 @@ describe('position tests', () => { invariant: invariant.contractId, token0: token0.contractInstance.contractId, token1: token1.contractInstance.contractId, - token0Amount: amount, - token1Amount: amount, + approvedTokens0: amount, + approvedTokens1: amount, index: 1n, fee: 100n, tickSpacing: 1n, @@ -192,288 +192,291 @@ describe('position tests', () => { expect(parsedUpperTick.secondsOutside).toBe(0n) }) - // test('remove position', async () => { - // let initAmount = 1000n - // let amount = 1000n + 100000n - - // const token0 = await deployTokenFaucet(sender, '', '', 0n, initAmount) - // await Withdraw.execute(sender, { - // initialFields: { - // token: token0.contractInstance.contractId, - // amount - // }, - // attoAlphAmount: DUST_AMOUNT * 2n - // }) - - // const token1 = await deployTokenFaucet(sender, '', '', 0n, initAmount) - // await Withdraw.execute(sender, { - // initialFields: { - // token: token1.contractInstance.contractId, - // amount - // }, - // attoAlphAmount: DUST_AMOUNT * 2n - // }) - - // const invariantResult = await deployInvariant(sender, 0n) - - // const invariant = Invariant.at(invariantResult.contractInstance.address) - - // await Init.execute(sender, { - // initialFields: { invariant: invariant.contractId }, - // attoAlphAmount: invariantDeployFee - // }) - - // await AddFeeTier.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // fee: 100n, - // tickSpacing: 1n - // }, - // attoAlphAmount: ONE_ALPH + DUST_AMOUNT * 2n - // }) - - // await CreatePool.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // fee: 100n, - // tickSpacing: 1n, - // initSqrtPrice: 1000000000000000000000000n, - // initTick: 0n - // }, - // attoAlphAmount: ONE_ALPH * 2n + DUST_AMOUNT * 2n - // }) - - // await InitPosition.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // fee: 100n, - // tickSpacing: 1n, - // lowerTick: -10n, - // upperTick: 10n - // }, - // attoAlphAmount: ONE_ALPH * 6n + DUST_AMOUNT * 2n - // }) - - // const lowerTickIndex = -10n - // const upperTickIndex = 10n - // const liquidityDelta = 10000000000n - - // await CreatePosition.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // token0Amount: amount, - // token1Amount: amount, - // index: 1n, - // fee: 100n, - // tickSpacing: 1n, - // lowerTick: lowerTickIndex, - // upperTick: upperTickIndex, - // liquidityDelta, - // slippageLimitLower: 1000000000000000000000000n, - // slippageLimitUpper: 1000000000000000000000000n - // }, - // tokens: [ - // { id: token0.contractInstance.contractId, amount }, - // { id: token1.contractInstance.contractId, amount } - // ] - // }) - - // const poolBefore = await invariant.methods.getPool({ - // args: { - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // fee: 100n, - // tickSpacing: 1n - // } - // }) - // const parsedPoolBefore = decodePool(poolBefore.returns) - // expect(parsedPoolBefore.liquidity).toBe(liquidityDelta) - - // const senderToken0BalanceBefore = await balanceOf(token0.contractInstance.contractId, sender.address) - // const senderToken1BalanceBefore = await balanceOf(token1.contractInstance.contractId, sender.address) - // const invariantToken0BalanceBefore = await balanceOf(token0.contractInstance.contractId, invariant.address) - // const invariantToken1BalanceBefore = await balanceOf(token1.contractInstance.contractId, invariant.address) - - // expect(senderToken0BalanceBefore).toBe(950n) - // expect(senderToken1BalanceBefore).toBe(950n) - // expect(invariantToken0BalanceBefore).toBe(50n) - // expect(invariantToken1BalanceBefore).toBe(50n) - - // await RemovePosition.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // index: 1n - // }, - // attoAlphAmount: ONE_ALPH + DUST_AMOUNT * 2n - // }) - - // const poolAfter = await invariant.methods.getPool({ - // args: { - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // fee: 100n, - // tickSpacing: 1n - // } - // }) - // const parsedPoolAfter = decodePool(poolAfter.returns) - // expect(parsedPoolAfter.liquidity).toBe(0n) - - // const senderToken0BalanceAfter = await balanceOf(token0.contractInstance.contractId, sender.address) - // const senderToken1BalanceAfter = await balanceOf(token1.contractInstance.contractId, sender.address) - // const invariantToken0BalanceAfter = await balanceOf(token0.contractInstance.contractId, invariant.address) - // const invariantToken1BalanceAfter = await balanceOf(token1.contractInstance.contractId, invariant.address) - - // expect(senderToken0BalanceAfter).toBe(999n) - // expect(senderToken1BalanceAfter).toBe(999n) - // expect(invariantToken0BalanceAfter).toBe(1n) - // expect(invariantToken1BalanceAfter).toBe(1n) - // }) - - // test('claim fee', async () => { - // let amount = 1000000n - - // const token0 = await deployTokenFaucet(sender, '', '', 0n, amount) - // await Withdraw.execute(sender, { - // initialFields: { - // token: token0.contractInstance.contractId, - // amount - // }, - // attoAlphAmount: DUST_AMOUNT * 2n - // }) - - // const token1 = await deployTokenFaucet(sender, '', '', 0n, amount) - // await Withdraw.execute(sender, { - // initialFields: { - // token: token1.contractInstance.contractId, - // amount - // }, - // attoAlphAmount: DUST_AMOUNT * 2n - // }) - - // const invariantResult = await deployInvariant(sender, 0n) - - // const invariant = Invariant.at(invariantResult.contractInstance.address) - - // await Init.execute(sender, { - // initialFields: { invariant: invariant.contractId }, - // attoAlphAmount: invariantDeployFee - // }) - - // const fee = 10000000000n - // const tickSpacing = 1n - - // await AddFeeTier.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // fee, - // tickSpacing - // }, - // attoAlphAmount: ONE_ALPH + DUST_AMOUNT * 2n - // }) - - // await CreatePool.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // fee, - // tickSpacing, - // initSqrtPrice: 1000000000000000000000000n, - // initTick: 0n - // }, - // attoAlphAmount: ONE_ALPH * 2n + DUST_AMOUNT * 2n - // }) - - // const lowerTickIndex = -10n - // const upperTickIndex = 10n - - // await InitPosition.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // fee, - // tickSpacing, - // lowerTick: lowerTickIndex, - // upperTick: upperTickIndex - // }, - // attoAlphAmount: ONE_ALPH * 6n + DUST_AMOUNT * 2n - // }) - - // const liquidityDelta = 100000000000000n - - // await CreatePosition.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // token0Amount: amount, - // token1Amount: amount, - // index: 1n, - // fee, - // tickSpacing, - // lowerTick: lowerTickIndex, - // upperTick: upperTickIndex, - // liquidityDelta, - // slippageLimitLower: 1000000000000000000000000n, - // slippageLimitUpper: 1000000000000000000000000n - // }, - // tokens: [ - // { id: token0.contractInstance.contractId, amount }, - // { id: token1.contractInstance.contractId, amount } - // ] - // }) - - // await Swap.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // token0: token0.contractInstance.contractId, - // token1: token1.contractInstance.contractId, - // fee, - // tickSpacing, - // xToY: true, - // amount: 100000n, - // byAmountIn: true, - // sqrtPriceLimit: 0n - // }, - // tokens: [ - // { id: token0.contractInstance.contractId, amount: 100000n }, - // { id: token1.contractInstance.contractId, amount: 100000n } - // ] - // }) - - // const senderToken0BalanceBefore = await balanceOf(token0.contractInstance.contractId, sender.address) - // const senderToken1BalanceBefore = await balanceOf(token1.contractInstance.contractId, sender.address) - // const invariantToken0BalanceBefore = await balanceOf(token0.contractInstance.contractId, invariant.address) - // const invariantToken1BalanceBefore = await balanceOf(token1.contractInstance.contractId, invariant.address) - - // expect(senderToken0BalanceBefore).toBe(500149n) - // expect(senderToken1BalanceBefore).toBe(500149n) - // expect(invariantToken0BalanceBefore).toBe(499851n) - // expect(invariantToken1BalanceBefore).toBe(499851n) - - // await ClaimFee.execute(sender, { - // initialFields: { - // invariant: invariant.contractId, - // index: 1n - // }, - // attoAlphAmount: DUST_AMOUNT * 2n - // }) - - // const senderToken0BalanceAfter = await balanceOf(token0.contractInstance.contractId, sender.address) - // const senderToken1BalanceAfter = await balanceOf(token1.contractInstance.contractId, sender.address) - // const invariantToken0BalanceAfter = await balanceOf(token0.contractInstance.contractId, invariant.address) - // const invariantToken1BalanceAfter = await balanceOf(token1.contractInstance.contractId, invariant.address) - - // expect(senderToken0BalanceAfter).toBe(500149n) - // expect(senderToken1BalanceAfter).toBe(501149n) - // expect(invariantToken0BalanceAfter).toBe(499851n) - // expect(invariantToken1BalanceAfter).toBe(498851n) - // }) + test('remove position', async () => { + let initAmount = 1000n + let amount = 1000n + + const token0 = await deployTokenFaucet(sender, '', '', 0n, initAmount) + await Withdraw.execute(sender, { + initialFields: { + token: token0.contractInstance.contractId, + amount + }, + attoAlphAmount: DUST_AMOUNT * 2n + }) + + const token1 = await deployTokenFaucet(sender, '', '', 0n, initAmount) + await Withdraw.execute(sender, { + initialFields: { + token: token1.contractInstance.contractId, + amount + }, + attoAlphAmount: DUST_AMOUNT * 2n + }) + + const invariantResult = await deployInvariant(sender, 0n) + + const invariant = Invariant.at(invariantResult.contractInstance.address) + + await Init.execute(sender, { + initialFields: { invariant: invariant.contractId }, + attoAlphAmount: invariantDeployFee + }) + + await AddFeeTier.execute(sender, { + initialFields: { + invariant: invariant.contractId, + fee: 100n, + tickSpacing: 1n + }, + attoAlphAmount: ONE_ALPH + DUST_AMOUNT * 2n + }) + + await CreatePool.execute(sender, { + initialFields: { + invariant: invariant.contractId, + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + fee: 100n, + tickSpacing: 1n, + initSqrtPrice: 1000000000000000000000000n, + initTick: 0n + }, + attoAlphAmount: ONE_ALPH * 2n + DUST_AMOUNT * 2n + }) + + await InitPosition.execute(sender, { + initialFields: { + invariant: invariant.contractId, + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + fee: 100n, + tickSpacing: 1n, + lowerTick: -10n, + upperTick: 10n + }, + attoAlphAmount: ONE_ALPH * 6n + DUST_AMOUNT * 2n + }) + + const lowerTickIndex = -10n + const upperTickIndex = 10n + const liquidityDelta = 10000000000n + + await CreatePosition.execute(sender, { + initialFields: { + invariant: invariant.contractId, + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + approvedTokens0: amount, + approvedTokens1: amount, + index: 1n, + fee: 100n, + tickSpacing: 1n, + lowerTick: lowerTickIndex, + upperTick: upperTickIndex, + liquidityDelta, + slippageLimitLower: 1000000000000000000000000n, + slippageLimitUpper: 1000000000000000000000000n + }, + tokens: [ + { id: token0.contractInstance.contractId, amount }, + { id: token1.contractInstance.contractId, amount } + ] + }) + + const poolBefore = await invariant.methods.getPool({ + args: { + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + fee: 100n, + tickSpacing: 1n + } + }) + const parsedPoolBefore = decodePool(poolBefore.returns) + expect(parsedPoolBefore.liquidity).toBe(liquidityDelta) + + const senderToken0BalanceBefore = await balanceOf(token0.contractInstance.contractId, sender.address) + const senderToken1BalanceBefore = await balanceOf(token1.contractInstance.contractId, sender.address) + const invariantToken0BalanceBefore = await balanceOf(token0.contractInstance.contractId, invariant.address) + const invariantToken1BalanceBefore = await balanceOf(token1.contractInstance.contractId, invariant.address) + + expect(senderToken0BalanceBefore).toBe(950n) + expect(senderToken1BalanceBefore).toBe(950n) + expect(invariantToken0BalanceBefore).toBe(50n) + expect(invariantToken1BalanceBefore).toBe(50n) + + await RemovePosition.execute(sender, { + initialFields: { + invariant: invariant.contractId, + index: 1n + }, + attoAlphAmount: ONE_ALPH + DUST_AMOUNT * 2n + }) + + const poolAfter = await invariant.methods.getPool({ + args: { + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + fee: 100n, + tickSpacing: 1n + } + }) + const parsedPoolAfter = decodePool(poolAfter.returns) + expect(parsedPoolAfter.liquidity).toBe(0n) + + const senderToken0BalanceAfter = await balanceOf(token0.contractInstance.contractId, sender.address) + const senderToken1BalanceAfter = await balanceOf(token1.contractInstance.contractId, sender.address) + const invariantToken0BalanceAfter = await balanceOf(token0.contractInstance.contractId, invariant.address) + const invariantToken1BalanceAfter = await balanceOf(token1.contractInstance.contractId, invariant.address) + + expect(senderToken0BalanceAfter).toBe(999n) + expect(senderToken1BalanceAfter).toBe(999n) + expect(invariantToken0BalanceAfter).toBe(1n) + expect(invariantToken1BalanceAfter).toBe(1n) + }) + + test('claim fee', async () => { + let amount = 1000000n + 100000n + + const token0 = await deployTokenFaucet(sender, '', '', 0n, amount) + await Withdraw.execute(sender, { + initialFields: { + token: token0.contractInstance.contractId, + amount: 1000000n + }, + attoAlphAmount: DUST_AMOUNT * 2n + }) + + const token1 = await deployTokenFaucet(sender, '', '', 0n, amount) + await Withdraw.execute(sender, { + initialFields: { + token: token1.contractInstance.contractId, + amount: 1000000n + }, + attoAlphAmount: DUST_AMOUNT * 2n + }) + + const [tokenX, tokenY] = + token0.contractInstance.contractId < token1.contractInstance.contractId ? [token0, token1] : [token1, token0] + + const invariantResult = await deployInvariant(sender, 0n) + + const invariant = Invariant.at(invariantResult.contractInstance.address) + + await Init.execute(sender, { + initialFields: { invariant: invariant.contractId }, + attoAlphAmount: invariantDeployFee + }) + + const fee = 10000000000n + const tickSpacing = 1n + + await AddFeeTier.execute(sender, { + initialFields: { + invariant: invariant.contractId, + fee, + tickSpacing + }, + attoAlphAmount: ONE_ALPH + DUST_AMOUNT * 2n + }) + + await CreatePool.execute(sender, { + initialFields: { + invariant: invariant.contractId, + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + fee, + tickSpacing, + initSqrtPrice: 1000000000000000000000000n, + initTick: 0n + }, + attoAlphAmount: ONE_ALPH * 2n + DUST_AMOUNT * 2n + }) + + const lowerTickIndex = -10n + const upperTickIndex = 10n + + await InitPosition.execute(sender, { + initialFields: { + invariant: invariant.contractId, + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + fee, + tickSpacing, + lowerTick: lowerTickIndex, + upperTick: upperTickIndex + }, + attoAlphAmount: ONE_ALPH * 6n + DUST_AMOUNT * 2n + }) + + const liquidityDelta = 100000000000000n + + await CreatePosition.execute(sender, { + initialFields: { + invariant: invariant.contractId, + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + approvedTokens0: 1000000n, + approvedTokens1: 1000000n, + index: 1n, + fee, + tickSpacing, + lowerTick: lowerTickIndex, + upperTick: upperTickIndex, + liquidityDelta, + slippageLimitLower: 1000000000000000000000000n, + slippageLimitUpper: 1000000000000000000000000n + }, + tokens: [ + { id: token0.contractInstance.contractId, amount: 1000000n }, + { id: token1.contractInstance.contractId, amount: 1000000n } + ] + }) + + await Swap.execute(sender, { + initialFields: { + invariant: invariant.contractId, + token0: token0.contractInstance.contractId, + token1: token1.contractInstance.contractId, + fee, + tickSpacing, + xToY: true, + amount: 100000n, + byAmountIn: true, + sqrtPriceLimit: 0n + }, + tokens: [ + { id: token0.contractInstance.contractId, amount: 100000n }, + { id: token1.contractInstance.contractId, amount: 100000n } + ] + }) + + const senderTokenXBalanceBefore = await balanceOf(tokenX.contractInstance.contractId, sender.address) + const senderTokenYBalanceBefore = await balanceOf(tokenY.contractInstance.contractId, sender.address) + const invariantTokenXBalanceBefore = await balanceOf(tokenX.contractInstance.contractId, invariant.address) + const invariantTokenYBalanceBefore = await balanceOf(tokenY.contractInstance.contractId, invariant.address) + + expect(senderTokenXBalanceBefore).toBe(400149n) + expect(senderTokenYBalanceBefore).toBe(599139n) + expect(invariantTokenXBalanceBefore).toBe(599851n) + expect(invariantTokenYBalanceBefore).toBe(400861n) + + await ClaimFee.execute(sender, { + initialFields: { + invariant: invariant.contractId, + index: 1n + }, + attoAlphAmount: DUST_AMOUNT * 2n + }) + + const senderTokenXBalanceAfter = await balanceOf(tokenX.contractInstance.contractId, sender.address) + const senderTokenYBalanceAfter = await balanceOf(tokenY.contractInstance.contractId, sender.address) + const invariantTokenXBalanceAfter = await balanceOf(tokenX.contractInstance.contractId, invariant.address) + const invariantTokenYBalanceAfter = await balanceOf(tokenY.contractInstance.contractId, invariant.address) + + expect(senderTokenXBalanceAfter).toBe(401149n) + expect(senderTokenYBalanceAfter).toBe(599139n) + expect(invariantTokenXBalanceAfter).toBe(598851n) + expect(invariantTokenYBalanceAfter).toBe(400861n) + }) }) diff --git a/test/swap.test.ts b/test/swap.test.ts index 8621b04a..67220090 100644 --- a/test/swap.test.ts +++ b/test/swap.test.ts @@ -22,7 +22,7 @@ describe('swap tests', () => { const lowerTickIndex = -20n const upperTickIndex = 10n - const amount = 20000000000000n + const amount = 1000000n + 1000n const token0 = await deployTokenFaucet(sender, '', '', amount, amount) const token1 = await deployTokenFaucet(sender, '', '', amount, amount) @@ -33,14 +33,14 @@ describe('swap tests', () => { await Withdraw.execute(sender, { initialFields: { token: token0.contractInstance.contractId, - amount: 10000000000000n + amount: 1000000n }, attoAlphAmount: DUST_AMOUNT * 2n }) await Withdraw.execute(sender, { initialFields: { token: token1.contractInstance.contractId, - amount: 10000000000000n + amount: 1000000n }, attoAlphAmount: DUST_AMOUNT * 2n }) @@ -89,8 +89,8 @@ describe('swap tests', () => { invariant: invariant.contractId, token0: token0.contractInstance.contractId, token1: token1.contractInstance.contractId, - token0Amount: 10000000000000n, - token1Amount: 10000000000000n, + approvedTokens0: 1000000n, + approvedTokens1: 1000000n, index: 1n, fee: fee, tickSpacing: tickSpacing, @@ -101,8 +101,8 @@ describe('swap tests', () => { slippageLimitUpper: 1000000000000000000000000n }, tokens: [ - { id: token0.contractInstance.contractId, amount: 10000000000000n }, - { id: token1.contractInstance.contractId, amount: 10000000000000n } + { id: token0.contractInstance.contractId, amount: 1000000n }, + { id: token1.contractInstance.contractId, amount: 1000000n } ] }) const position = await invariant.methods.getPosition({ @@ -343,8 +343,8 @@ describe('swap tests', () => { invariant: invariant.contractId, token0: token0.contractInstance.contractId, token1: token1.contractInstance.contractId, - token0Amount: senderBalance0, - token1Amount: senderBalance1, + approvedTokens0: senderBalance0, + approvedTokens1: senderBalance1, index: 1n, fee: fee, tickSpacing: tickSpacing, @@ -381,8 +381,8 @@ describe('swap tests', () => { invariant: invariant.contractId, token0: token0.contractInstance.contractId, token1: token1.contractInstance.contractId, - token0Amount: senderBalance0, - token1Amount: senderBalance1, + approvedTokens0: senderBalance0, + approvedTokens1: senderBalance1, index: 2n, fee: fee, tickSpacing: tickSpacing, @@ -658,8 +658,8 @@ describe('swap tests', () => { invariant: invariant.contractId, token0: token0.contractInstance.contractId, token1: token1.contractInstance.contractId, - token0Amount: senderBalance0, - token1Amount: senderBalance1, + approvedTokens0: senderBalance0, + approvedTokens1: senderBalance1, index: 1n, fee: fee, tickSpacing: tickSpacing, @@ -753,8 +753,8 @@ describe('swap tests', () => { invariant: invariant.contractId, token0: token0.contractInstance.contractId, token1: token1.contractInstance.contractId, - token0Amount: senderBalance0, - token1Amount: senderBalance1, + approvedTokens0: senderBalance0, + approvedTokens1: senderBalance1, index, fee: fee, tickSpacing: tickSpacing,