Skip to content

Commit

Permalink
update lock amount calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
dongnguyen9186 committed Oct 16, 2024
1 parent b8e33cc commit 5b45712
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"decimal.js": "^10.4.1",
"dotenv": "^16.0.1",
"invariant": "^2.2.4",
"@meteora-ag/stake-for-fee": "1.0.7"
"@meteora-ag/stake-for-fee": "1.0.8-rc.2"
},
"devDependencies": {
"@tsconfig/recommended": "^1.0.1",
Expand Down
20 changes: 12 additions & 8 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ export default class AmmImpl implements AmmImplementation {
skipBAta?: boolean;
},
) {
const { vaultProgram, ammProgram } = createProgram(connection, opt?.programId);
const { vaultProgram, ammProgram, stakeForFeeProgram } = createProgram(connection, opt?.programId);

const [
{ vaultPda: aVault, tokenVaultPda: aTokenVault, lpMintPda: aLpMintPda },
Expand Down Expand Up @@ -576,16 +576,14 @@ export default class AmmImpl implements AmmImplementation {
const [mintMetadata, _mintMetadataBump] = deriveMintMetadata(lpMint);
const lpAmount = sqrt(tokenAAmount.mul(tokenBAmount));
const feeWrapperPercent = opt?.stakeLiquidity?.percent?.gt(new Decimal(0))
? opt.stakeLiquidity.percent
? Decimal.min(opt?.stakeLiquidity?.percent, new Decimal(1))
: new Decimal(0);

const feeWrapperLockAmount = new BN(
new Decimal(lpAmount.toString()).mul(feeWrapperPercent).toFixed(0, Decimal.ROUND_DOWN),
);
const userLockAmount = feeWrapperPercent.gt(new Decimal(0)) ? new BN(
new Decimal(lpAmount.toString()).mul(new Decimal(1).minus(feeWrapperPercent)).toFixed(0, Decimal.ROUND_DOWN),
) : U64_MAX;

const userLockAmount = feeWrapperPercent.gt(new Decimal(0)) ? lpAmount.sub(feeWrapperLockAmount) : U64_MAX;

const createPoolPostInstructions: TransactionInstruction[] = [];
if (opt?.lockLiquidity) {
const [lockEscrowPK] = deriveLockEscrowPda(poolPubkey, payer, ammProgram.programId);
Expand Down Expand Up @@ -686,17 +684,23 @@ export default class AmmImpl implements AmmImplementation {
if (feeWrapperLockAmount.gt(new BN(0))) {
const feeVaultConfig = await StakeForFee.getConfigs(connection);

const createStakeForFeeTx = await StakeForFee.createFeeVault(
const latestBlockHashStakeProgram = await stakeForFeeProgram.provider.connection.getLatestBlockhash(
stakeForFeeProgram.provider.connection.commitment,
);

const createStakeForFeeTx = await StakeForFee.createFeeVault2(
connection,
poolPubkey,
tokenAMint,
payer,
feeVaultConfig[0].publicKey,
tokenAMint,
tokenBMint,
);

const newCreateStakeForFeeTx = new Transaction({
feePayer: payer,
...latestBlockHash,
...latestBlockHashStakeProgram,
}).add(createStakeForFeeTx);

resultTx.push(newCreateStakeForFeeTx);
Expand Down
4 changes: 3 additions & 1 deletion ts-client/src/amm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
VaultIdl,
PROGRAM_ID as VAULT_PROGRAM_ID,
} from '@mercurial-finance/vault-sdk';
import { STAKE_FOR_FEE_PROGRAM_ID, IDL as StakeForFeeIDL, StakeForFee as StakeForFeeIdl } from '@meteora-ag/stake-for-fee';
import { AnchorProvider, BN, Program } from '@coral-xyz/anchor';
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Expand Down Expand Up @@ -65,8 +66,9 @@ export const createProgram = (connection: Connection, programId?: string) => {
const provider = new AnchorProvider(connection, {} as any, AnchorProvider.defaultOptions());
const ammProgram = new Program<AmmIdl>(AmmIDL, programId ?? PROGRAM_ID, provider);
const vaultProgram = new Program<VaultIdl>(VaultIDL, VAULT_PROGRAM_ID, provider);
const stakeForFeeProgram = new Program<StakeForFeeIdl>(StakeForFeeIDL, STAKE_FOR_FEE_PROGRAM_ID, provider);

return { provider, ammProgram, vaultProgram };
return { provider, ammProgram, vaultProgram, stakeForFeeProgram };
};

/**
Expand Down

0 comments on commit 5b45712

Please sign in to comment.