Skip to content

Commit

Permalink
add create ata for stake for fee vault
Browse files Browse the repository at this point in the history
  • Loading branch information
dongnguyen9186 committed Oct 16, 2024
1 parent 5b45712 commit 54c034f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 60 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.8-rc.2"
"@meteora-ag/stake-for-fee": "1.0.8-rc.8"
},
"devDependencies": {
"@tsconfig/recommended": "^1.0.1",
Expand Down
116 changes: 57 additions & 59 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ export default class AmmImpl implements AmmImplementation {
new Decimal(lpAmount.toString()).mul(feeWrapperPercent).toFixed(0, Decimal.ROUND_DOWN),
);
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 @@ -681,67 +681,65 @@ export default class AmmImpl implements AmmImplementation {

resultTx.push(createPoolTx);

if (feeWrapperLockAmount.gt(new BN(0))) {
const feeVaultConfig = await StakeForFee.getConfigs(connection);
if (feeWrapperLockAmount.gt(new BN(0))) {
const feeVaultConfig = await StakeForFee.getConfigs(connection);
const createFeeVaultTx = await StakeForFee.createFeeVaultWithParams(
connection,
poolPubkey,
tokenAMint,
payer,
feeVaultConfig[0].publicKey,
tokenAMint,
tokenBMint,
);

const latestBlockHashStakeProgram = await stakeForFeeProgram.provider.connection.getLatestBlockhash(
stakeForFeeProgram.provider.connection.commitment,
const newCreateFeeVaultTx = new Transaction({
feePayer: payer,
...latestBlockHash,
}).add(createFeeVaultTx);

resultTx.push(newCreateFeeVaultTx);

const vaultKey = deriveFeeVault(poolPubkey, STAKE_FOR_FEE_PROGRAM_ID);
const [lockEscrowFeeVaultPK] = deriveLockEscrowPda(poolPubkey, vaultKey, ammProgram.programId);

const preLockFeeVaultInstructions: Array<TransactionInstruction> = [];

const [escrowFeeVaultAta, createEscrowFeeVaultAtaIx] = await getOrCreateATAInstruction(
lpMint,
lockEscrowFeeVaultPK,
connection,
payer,
);

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

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

resultTx.push(newCreateStakeForFeeTx);

const vaultKey = deriveFeeVault(poolPubkey, STAKE_FOR_FEE_PROGRAM_ID);

const [lockEscrowFeeVaultPK] = deriveLockEscrowPda(poolPubkey, vaultKey, ammProgram.programId);

const [escrowFeeVaultAta] = await getOrCreateATAInstruction(
lpMint,
lockEscrowFeeVaultPK,
connection,
payer,
);

const lockTx = await ammProgram.methods
.lock(feeWrapperLockAmount)
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowFeeVaultPK,
owner: payer,
lpMint,
sourceTokens: payerPoolLp,
escrowVault: escrowFeeVaultAta,
tokenProgram: TOKEN_PROGRAM_ID,
aVault,
bVault,
aVaultLp,
bVaultLp,
aVaultLpMint,
bVaultLpMint,
})
.transaction();

const feeWraperLockTx = new Transaction({
feePayer: payer,
...latestBlockHash,
}).add(lockTx);

resultTx.push(feeWraperLockTx);
}
createEscrowFeeVaultAtaIx && preLockFeeVaultInstructions.push(createEscrowFeeVaultAtaIx);

const lockTx = await ammProgram.methods
.lock(feeWrapperLockAmount)
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowFeeVaultPK,
owner: payer,
lpMint,
sourceTokens: payerPoolLp,
escrowVault: escrowFeeVaultAta,
tokenProgram: TOKEN_PROGRAM_ID,
aVault,
bVault,
aVaultLp,
bVaultLp,
aVaultLpMint,
bVaultLpMint,
})
.preInstructions(preLockFeeVaultInstructions)
.transaction();

const feeWraperLockTx = new Transaction({
feePayer: payer,
...latestBlockHash,
}).add(lockTx);
resultTx.push(feeWraperLockTx);
}

if (opt?.swapLiquidity) {
const protocolTokenFee = deriveProtocolTokenFee(poolPubkey, tokenBMint, ammProgram.programId);
Expand Down

0 comments on commit 54c034f

Please sign in to comment.