Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 203 additions & 40 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2734,16 +2734,136 @@ export default class AmmImpl implements AmmImplementation {
return transaction.add(...stakeForFeeInstructions);
}

public async claimLockFee(
owner: PublicKey,
maxAmount: BN,
payer?: PublicKey,
receiver?: PublicKey,
): Promise<Transaction> {
const [lockEscrowPK] = deriveLockEscrowPda(this.address, owner, this.program.programId);
const feeReceiver = receiver ? receiver : owner;
private async claimWithQuoteMintSol({
owner,
payer,
receiver,
tempWSolAcc,
lockEscrowPK,
}: {
owner: PublicKey;
payer: PublicKey;
receiver: PublicKey;
tempWSolAcc: PublicKey;
lockEscrowPK: PublicKey;
}): Promise<{
accounts: {
pool: PublicKey;
lockEscrow: PublicKey;
owner: PublicKey;
lpMint: PublicKey;
sourceTokens: PublicKey;
escrowVault: PublicKey;
tokenProgram: PublicKey;
aVault: PublicKey;
bVault: PublicKey;
aVaultLp: PublicKey;
bVaultLp: PublicKey;
aVaultLpMint: PublicKey;
bVaultLpMint: PublicKey;
vaultProgram: PublicKey;
aTokenVault: PublicKey;
bTokenVault: PublicKey;
userAToken: PublicKey;
userBToken: PublicKey;
};
preInstructions: TransactionInstruction[];
postInstructions: TransactionInstruction[];
}> {
const preInstructions: TransactionInstruction[] = [];
const postInstructions: TransactionInstruction[] = [];

const isTokenAMintNative = this.poolState.tokenAMint.toBase58() === NATIVE_MINT.toBase58();
const isTokenBMintNative = this.poolState.tokenBMint.toBase58() === NATIVE_MINT.toBase58();

const [
[userAta, createUserAtaIx],
[escrowAta, createEscrowAtaIx],
[tokenAAta, createTokenAAtaIx],
[tokenBAta, createTokenBAtaIx],
] = await Promise.all([
getOrCreateATAInstruction(this.poolState.lpMint, owner, this.program.provider.connection, payer),
getOrCreateATAInstruction(this.poolState.lpMint, lockEscrowPK, this.program.provider.connection, payer),
getOrCreateATAInstruction(
this.poolState.tokenAMint,
isTokenAMintNative ? tempWSolAcc : receiver,
this.program.provider.connection,
payer,
),
getOrCreateATAInstruction(
this.poolState.tokenBMint,
isTokenBMintNative ? tempWSolAcc : receiver,
this.program.provider.connection,
payer,
),
]);
createUserAtaIx && preInstructions.push(createUserAtaIx);
createEscrowAtaIx && preInstructions.push(createEscrowAtaIx);
createTokenAAtaIx && preInstructions.push(createTokenAAtaIx);
createTokenBAtaIx && preInstructions.push(createTokenBAtaIx);

const closeWrappedSOLIx = await unwrapSOLInstruction(tempWSolAcc, receiver);
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);

const accounts = {
pool: this.address,
lockEscrow: lockEscrowPK,
owner,
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,
vaultProgram: this.vaultProgram.programId,
aTokenVault: this.vaultA.vaultState.tokenVault,
bTokenVault: this.vaultB.vaultState.tokenVault,
userAToken: tokenAAta,
userBToken: tokenBAta,
};

return { accounts, preInstructions, postInstructions };
}

private async claimWithQuoteMintNotSol({
owner,
payer,
receiver,
lockEscrowPK,
}: {
owner: PublicKey;
payer: PublicKey;
receiver: PublicKey;
lockEscrowPK: PublicKey;
}): Promise<{
accounts: {
pool: PublicKey;
lockEscrow: PublicKey;
owner: PublicKey;
lpMint: PublicKey;
sourceTokens: PublicKey;
escrowVault: PublicKey;
tokenProgram: PublicKey;
aVault: PublicKey;
bVault: PublicKey;
aVaultLp: PublicKey;
bVaultLp: PublicKey;
aVaultLpMint: PublicKey;
bVaultLpMint: PublicKey;
vaultProgram: PublicKey;
aTokenVault: PublicKey;
bTokenVault: PublicKey;
userAToken: PublicKey;
userBToken: PublicKey;
};
preInstructions: TransactionInstruction[];
}> {
const preInstructions: TransactionInstruction[] = [];

const [
[userAta, createUserAtaIx],
[escrowAta, createEscrowAtaIx],
Expand All @@ -2752,47 +2872,90 @@ export default class AmmImpl implements AmmImplementation {
] = await Promise.all([
getOrCreateATAInstruction(this.poolState.lpMint, owner, this.program.provider.connection, payer),
getOrCreateATAInstruction(this.poolState.lpMint, lockEscrowPK, this.program.provider.connection, payer),
getOrCreateATAInstruction(this.poolState.tokenAMint, feeReceiver, this.program.provider.connection, payer),
getOrCreateATAInstruction(this.poolState.tokenBMint, feeReceiver, this.program.provider.connection, payer),
getOrCreateATAInstruction(this.poolState.tokenAMint, receiver, this.program.provider.connection, payer),
getOrCreateATAInstruction(this.poolState.tokenBMint, receiver, this.program.provider.connection, payer),
]);
createUserAtaIx && preInstructions.push(createUserAtaIx);
createEscrowAtaIx && preInstructions.push(createEscrowAtaIx);
createTokenAAtaIx && preInstructions.push(createTokenAAtaIx);
createTokenBAtaIx && preInstructions.push(createTokenBAtaIx);

const postInstructions: Array<TransactionInstruction> = [];
if ([this.poolState.tokenAMint.toBase58(), this.poolState.tokenBMint.toBase58()].includes(NATIVE_MINT.toBase58())) {
const closeWrappedSOLIx = await unwrapSOLInstruction(feeReceiver);
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
}
const accounts = {
pool: this.address,
lockEscrow: lockEscrowPK,
owner,
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,
vaultProgram: this.vaultProgram.programId,
aTokenVault: this.vaultA.vaultState.tokenVault,
bTokenVault: this.vaultB.vaultState.tokenVault,
userAToken: tokenAAta,
userBToken: tokenBAta,
};

const tx = await this.program.methods
.claimFee(maxAmount)
.accounts({
pool: this.address,
lockEscrow: lockEscrowPK,
return { accounts, preInstructions };
}

public async claimLockFee(
owner: PublicKey,
maxAmount: BN,
payer: PublicKey,
receiver?: PublicKey,
tempWSolAcc?: PublicKey,
): Promise<Transaction> {
const [lockEscrowPK] = deriveLockEscrowPda(this.address, owner, this.program.programId);

const isTokenAOrBNative =
this.poolState.tokenAMint.toBase58() === NATIVE_MINT.toBase58() ||
this.poolState.tokenBMint.toBase58() === NATIVE_MINT.toBase58();

let tx: Transaction;

if (isTokenAOrBNative) {
const tempWSol = receiver ? tempWSolAcc : owner;
const feeReceiver = receiver ? receiver : owner;

const result = await this.claimWithQuoteMintSol({
owner,
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,
vaultProgram: this.vaultProgram.programId,
aTokenVault: this.vaultA.vaultState.tokenVault,
bTokenVault: this.vaultB.vaultState.tokenVault,
userAToken: tokenAAta,
userBToken: tokenBAta,
})
.preInstructions(preInstructions)
.postInstructions(postInstructions)
.transaction();
payer,
receiver: feeReceiver,
tempWSolAcc: tempWSol!,
lockEscrowPK,
});

tx = await this.program.methods
.claimFee(maxAmount)
.accounts(result.accounts)
.preInstructions(result.preInstructions)
.postInstructions(result.postInstructions)
.transaction();
} else {
const feeReceiver = receiver ? receiver : owner;

const result = await this.claimWithQuoteMintNotSol({
owner,
payer,
receiver: feeReceiver,
lockEscrowPK,
});

tx = await this.program.methods
.claimFee(maxAmount)
.accounts(result.accounts)
.preInstructions(result.preInstructions)
.transaction();
}

return new Transaction({
feePayer: payer ? payer : owner,
feePayer: payer,
...(await this.program.provider.connection.getLatestBlockhash(this.program.provider.connection.commitment)),
}).add(tx);
}
Expand Down
17 changes: 9 additions & 8 deletions ts-client/src/amm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import {
VaultIdl,
PROGRAM_ID as VAULT_PROGRAM_ID,
} from '@meteora-ag/vault-sdk';
import {
STAKE_FOR_FEE_PROGRAM_ID,
IDL as StakeForFeeIDL,
StakeForFee as StakeForFeeIdl,
} from '@meteora-ag/m3m3';
import { STAKE_FOR_FEE_PROGRAM_ID, IDL as StakeForFeeIDL, StakeForFee as StakeForFeeIdl } from '@meteora-ag/m3m3';
import { AnchorProvider, BN, Program } from '@coral-xyz/anchor';
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Expand All @@ -24,7 +20,7 @@ import {
createCloseAccountInstruction,
getMinimumBalanceForRentExemptMint,
MintLayout,
createInitializeMintInstruction
createInitializeMintInstruction,
} from '@solana/spl-token';
import {
AccountInfo,
Expand Down Expand Up @@ -173,10 +169,15 @@ export const wrapSOLInstruction = (from: PublicKey, to: PublicKey, amount: bigin
];
};

export const unwrapSOLInstruction = async (owner: PublicKey) => {
export const unwrapSOLInstruction = async (owner: PublicKey, receiver?: PublicKey) => {
const wSolATAAccount = await getAssociatedTokenAccount(NATIVE_MINT, owner);
if (wSolATAAccount) {
const closedWrappedSolInstruction = createCloseAccountInstruction(wSolATAAccount, owner, owner, []);
const closedWrappedSolInstruction = createCloseAccountInstruction(
wSolATAAccount,
receiver ? receiver : owner,
owner,
[],
);
return closedWrappedSolInstruction;
}
return null;
Expand Down
Loading