From dd37d00edd3842622820d8080bb66336527b4ef7 Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Tue, 29 Oct 2024 10:19:29 +0700 Subject: [PATCH 1/3] fix: check user lock amount gt 0 to lock liquidity --- ts-client/src/amm/index.ts | 52 +++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/ts-client/src/amm/index.ts b/ts-client/src/amm/index.ts index 61986920..6805b87a 100644 --- a/ts-client/src/amm/index.ts +++ b/ts-client/src/amm/index.ts @@ -2530,31 +2530,37 @@ export default class AmmImpl implements AmmImplementation { postInstructions.push(lockTx); } - const lockTx = await this.program.methods - .lock(userLockAmount) - .accounts({ - pool: this.address, - lockEscrow: lockEscrowPK, - owner: payer, - lpMint: this.poolState.lpMint, - sourceTokens: userAta, - escrowVault: escrowAta, - tokenProgram: TOKEN_PROGRAM_ID, - aVault: this.poolState.aVault, - bVault: this.poolState.bVault, - aVaultLp: this.poolState.aVaultLp, - bVaultLp: this.poolState.bVaultLp, - aVaultLpMint: this.vaultA.vaultState.lpMint, - bVaultLpMint: this.vaultB.vaultState.lpMint, - }) - .postInstructions(postInstructions) - .preInstructions(preInstructions) - .transaction(); - - return new Transaction({ + const transaction = new Transaction({ feePayer: payer, ...(await this.program.provider.connection.getLatestBlockhash(this.program.provider.connection.commitment)), - }).add(lockTx); + }); + + if (userLockAmount.gt(new BN(0))) { + const lockTx = await this.program.methods + .lock(userLockAmount) + .accounts({ + pool: this.address, + lockEscrow: lockEscrowPK, + owner: payer, + lpMint: this.poolState.lpMint, + sourceTokens: userAta, + escrowVault: escrowAta, + tokenProgram: TOKEN_PROGRAM_ID, + aVault: this.poolState.aVault, + bVault: this.poolState.bVault, + aVaultLp: this.poolState.aVaultLp, + bVaultLp: this.poolState.bVaultLp, + aVaultLpMint: this.vaultA.vaultState.lpMint, + bVaultLpMint: this.vaultB.vaultState.lpMint, + }) + .postInstructions(postInstructions) + .preInstructions(preInstructions) + .transaction(); + + return transaction.add(lockTx); + } + + return transaction.add(...postInstructions); } public async claimLockFee(owner: PublicKey, maxAmount: BN): Promise { From 0ce1021d7b9bde8489dfcf7b11894d6a9fbab8de Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Tue, 29 Oct 2024 10:22:47 +0700 Subject: [PATCH 2/3] chore: rename stake for fee ixs --- ts-client/src/amm/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ts-client/src/amm/index.ts b/ts-client/src/amm/index.ts index 6805b87a..b2097b55 100644 --- a/ts-client/src/amm/index.ts +++ b/ts-client/src/amm/index.ts @@ -2451,7 +2451,7 @@ export default class AmmImpl implements AmmImplementation { const [lockEscrowPK] = deriveLockEscrowPda(this.address, owner, this.program.programId); const preInstructions: TransactionInstruction[] = []; - const postInstructions: Array = []; + const stakeForFeeInstructions: Array = []; const lockEscrowAccount = await this.program.account.lockEscrow.fetchNullable(lockEscrowPK); if (!lockEscrowAccount) { @@ -2494,7 +2494,7 @@ export default class AmmImpl implements AmmImplementation { this.poolState.tokenBMint, ); - postInstructions.push(...createFeeVaultIxs); + stakeForFeeInstructions.push(...createFeeVaultIxs); } const [lockEscrowFeeVaultPK] = deriveLockEscrowPda(this.address, vaultKey, this.program.programId); @@ -2506,7 +2506,7 @@ export default class AmmImpl implements AmmImplementation { payer, ); - createEscrowFeeVaultAtaIx && postInstructions.push(createEscrowFeeVaultAtaIx); + createEscrowFeeVaultAtaIx && stakeForFeeInstructions.push(createEscrowFeeVaultAtaIx); const lockTx = await this.program.methods .lock(feeWrapperLockAmount) @@ -2527,7 +2527,7 @@ export default class AmmImpl implements AmmImplementation { }) .instruction(); - postInstructions.push(lockTx); + stakeForFeeInstructions.push(lockTx); } const transaction = new Transaction({ @@ -2553,14 +2553,14 @@ export default class AmmImpl implements AmmImplementation { aVaultLpMint: this.vaultA.vaultState.lpMint, bVaultLpMint: this.vaultB.vaultState.lpMint, }) - .postInstructions(postInstructions) + .postInstructions(stakeForFeeInstructions) .preInstructions(preInstructions) .transaction(); return transaction.add(lockTx); } - return transaction.add(...postInstructions); + return transaction.add(...stakeForFeeInstructions); } public async claimLockFee(owner: PublicKey, maxAmount: BN): Promise { From 580ea2c1336c95c57d4433a2d84110508709c5bf Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Mon, 11 Nov 2024 17:18:33 +0700 Subject: [PATCH 3/3] throw error if fee vault not found when lock liquidity for fee vault --- CHANGELOG.md | 6 ++++++ ts-client/package.json | 2 +- ts-client/src/amm/index.ts | 16 ++++------------ 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d609108..85de7c4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security +## @mercurial-finance/dynamic-amm-sdk [1.1.11] - PR[#170](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/pull/170) + +### Fixed + +- Fix user lock amount was 0 + ## @mercurial-finance/dynamic-amm-sdk [1.1.10] - PR[#174](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/pull/174) ### Changed diff --git a/ts-client/package.json b/ts-client/package.json index 04640055..131ec6c1 100644 --- a/ts-client/package.json +++ b/ts-client/package.json @@ -1,6 +1,6 @@ { "name": "@mercurial-finance/dynamic-amm-sdk", - "version": "1.1.10", + "version": "1.1.11", "description": "Mercurial Vaults SDK is a typescript library that allows you to interact with Mercurial v2's AMM.", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", diff --git a/ts-client/src/amm/index.ts b/ts-client/src/amm/index.ts index b2097b55..83d04ad3 100644 --- a/ts-client/src/amm/index.ts +++ b/ts-client/src/amm/index.ts @@ -2478,23 +2478,15 @@ export default class AmmImpl implements AmmImplementation { createEscrowAtaIx && preInstructions.push(createEscrowAtaIx); const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(amount, opt?.stakeLiquidity?.percent); - + if (feeWrapperLockAmount.gt(new BN(0))) { const { stakeForFeeProgram } = createProgram(this.program.provider.connection); const vaultKey = deriveFeeVault(this.address, STAKE_FOR_FEE_PROGRAM_ID); const vaultState = await getFeeVaultState(vaultKey, stakeForFeeProgram); - if (!vaultState) { - const createFeeVaultIxs = await StakeForFee.createFeeVaultInstructions( - this.program.provider.connection, - this.address, - this.poolState.tokenAMint, - payer, - this.poolState.tokenAMint, - this.poolState.tokenBMint, - ); - stakeForFeeInstructions.push(...createFeeVaultIxs); + if (!vaultState) { + throw new Error(`Fee vault not found for pool ${this.address.toBase58()}`); } const [lockEscrowFeeVaultPK] = deriveLockEscrowPda(this.address, vaultKey, this.program.programId); @@ -2507,7 +2499,7 @@ export default class AmmImpl implements AmmImplementation { ); createEscrowFeeVaultAtaIx && stakeForFeeInstructions.push(createEscrowFeeVaultAtaIx); - + const lockTx = await this.program.methods .lock(feeWrapperLockAmount) .accounts({