Skip to content

Commit

Permalink
fix: calcaulateLockAmounts
Browse files Browse the repository at this point in the history
  • Loading branch information
00xSam committed Nov 29, 2024
1 parent d5ab188 commit 176e7bd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.18] - PR[#185](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/pull/185)

### Fixed

- Fix `calculateLockAmounts` util not calculating `userLockAmount` correctly if `feeWrapperRatio` is `null` or zero

## @mercurial-finance/dynamic-amm-sdk [1.1.17] - PR[#183](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/pull/183)

### Changed
Expand Down
2 changes: 1 addition & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mercurial-finance/dynamic-amm-sdk",
"version": "1.1.17",
"version": "1.1.18",
"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",
Expand Down
22 changes: 11 additions & 11 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export default class AmmImpl implements AmmImplementation {
minAmountOut: BN;
};
stakeLiquidity?: {
percent?: Decimal;
ratio?: Decimal;
};
activationPoint?: BN;
},
Expand Down Expand Up @@ -362,7 +362,7 @@ export default class AmmImpl implements AmmImplementation {
const activationPoint = opt?.activationPoint || null;

const lpAmount = sqrt(tokenAAmount.mul(tokenBAmount));
const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(lpAmount, opt?.stakeLiquidity?.percent);
const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(lpAmount, opt?.stakeLiquidity?.ratio);

const createPoolPostInstructions: TransactionInstruction[] = [];
if (opt?.lockLiquidity && userLockAmount.gt(new BN(0))) {
Expand Down Expand Up @@ -446,7 +446,7 @@ export default class AmmImpl implements AmmImplementation {
units: 1_400_000,
});
ixs.push([setComputeUnitLimitIx, createPermissionlessPoolTx]);

if (feeWrapperLockAmount.gt(new BN(0))) {
const preInstructions: TransactionInstruction[] = [];
const createFeeVaultIxs = await StakeForFee.createFeeVaultInstructions(
Expand Down Expand Up @@ -762,7 +762,7 @@ export default class AmmImpl implements AmmImplementation {
minAmountOut: BN;
};
stakeLiquidity?: {
percent?: Decimal;
ratio?: Decimal;
};
skipAAta?: boolean;
skipBAta?: boolean;
Expand Down Expand Up @@ -888,7 +888,7 @@ export default class AmmImpl implements AmmImplementation {

const [mintMetadata, _mintMetadataBump] = deriveMintMetadata(lpMint);
const lpAmount = sqrt(tokenAAmount.mul(tokenBAmount));
const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(lpAmount, opt?.stakeLiquidity?.percent);
const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(lpAmount, opt?.stakeLiquidity?.ratio);

const createPoolPostInstructions: TransactionInstruction[] = [];
if (opt?.lockLiquidity && userLockAmount.gt(new BN(0))) {
Expand Down Expand Up @@ -1051,7 +1051,7 @@ export default class AmmImpl implements AmmImplementation {

ixs.push(swapTx);
}
const resultTx: Transaction[] = await createTransactions(connection, ixs, payer);
const resultTx: Transaction[] = await createTransactions(connection, ixs, payer);

return resultTx;
}
Expand Down Expand Up @@ -2419,7 +2419,7 @@ export default class AmmImpl implements AmmImplementation {
feePayer?: PublicKey,
opt?: {
stakeLiquidity?: {
percent?: Decimal;
ratio?: Decimal;
};
},
): Promise<Transaction> {
Expand Down Expand Up @@ -2453,16 +2453,16 @@ export default class AmmImpl implements AmmImplementation {
createUserAtaIx && preInstructions.push(createUserAtaIx);
createEscrowAtaIx && preInstructions.push(createEscrowAtaIx);

const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(amount, opt?.stakeLiquidity?.percent);
const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(amount, opt?.stakeLiquidity?.ratio);

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) {
throw new Error(`Fee vault not found for pool ${this.address.toBase58()}`);
throw new Error(`Fee vault not found for pool ${this.address.toBase58()}`);
}

const [lockEscrowFeeVaultPK] = deriveLockEscrowPda(this.address, vaultKey, this.program.programId);
Expand All @@ -2475,7 +2475,7 @@ export default class AmmImpl implements AmmImplementation {
);

createEscrowFeeVaultAtaIx && stakeForFeeInstructions.push(createEscrowFeeVaultAtaIx);

const lockTx = await this.program.methods
.lock(feeWrapperLockAmount)
.accounts({
Expand Down
12 changes: 6 additions & 6 deletions ts-client/src/amm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,15 +994,15 @@ export async function createMint(
return { tx: transaction, mintAccount };
}

export const calculateLockAmounts = (amount: BN, feeWrapperPercent?: Decimal) => {
const safeFeeWrapperPercent = feeWrapperPercent?.gt(new Decimal(0))
? Decimal.min(feeWrapperPercent, new Decimal(1))
: new Decimal(0);
export const calculateLockAmounts = (amount: BN, feeWrapperRatio = new Decimal(0)) => {
if (feeWrapperRatio?.lt(0) || feeWrapperRatio?.gt(1)) {
throw new Error('Fee wrapper percent should be between 0 and 100');
}

const feeWrapperLockAmount = new BN(
new Decimal(amount.toString()).mul(safeFeeWrapperPercent).toFixed(0, Decimal.ROUND_DOWN),
new Decimal(amount.toString()).mul(feeWrapperRatio).toFixed(0, Decimal.ROUND_DOWN),
);
const userLockAmount = safeFeeWrapperPercent.gt(new Decimal(0)) ? amount.sub(feeWrapperLockAmount) : U64_MAX;
const userLockAmount = amount.sub(feeWrapperLockAmount);

return {
feeWrapperLockAmount,
Expand Down

0 comments on commit 176e7bd

Please sign in to comment.