Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- `AmmImpl.claimLockFee` to include `receiver` and `payer` parameters to indicate the receiver and payer of the fees.

## dynamic-amm [1.3.6] - PR #216

### Changed

- `AmmImpl.claimLockFee` to include `tempWSolAcc` parameter to indicate the temporary SOL account to receive the WSOL transaction fee.
- `unwrapSolInstruction` to include `receiver` parameter to indicate the receiver of the unwrapped SOL.
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": "@meteora-ag/dynamic-amm-sdk",
"version": "1.3.5",
"version": "1.3.6",
"description": "Meteora Dynamic Amm SDK is a typescript library that allows you to interact with Meteora's AMM.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
249 changes: 206 additions & 43 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ export default class AmmImpl implements AmmImplementation {

const postInstructions: Array<TransactionInstruction> = [];
if (NATIVE_MINT.equals(destinationToken)) {
const unwrapSOLIx = await unwrapSOLInstruction(owner);
const unwrapSOLIx = await unwrapSOLInstruction(owner, owner);
unwrapSOLIx && postInstructions.push(unwrapSOLIx);
}

Expand Down Expand Up @@ -2359,7 +2359,7 @@ export default class AmmImpl implements AmmImplementation {

const postInstructions: Array<TransactionInstruction> = [];
if ([this.tokenAMint.address.toBase58(), this.tokenBMint.address.toBase58()].includes(NATIVE_MINT.toBase58())) {
const closeWrappedSOLIx = await unwrapSOLInstruction(owner);
const closeWrappedSOLIx = await unwrapSOLInstruction(owner, owner);
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
}

Expand Down Expand Up @@ -2517,7 +2517,7 @@ export default class AmmImpl implements AmmImplementation {

const postInstructions: Array<TransactionInstruction> = [];
if ([this.tokenAMint.address.toBase58(), this.tokenBMint.address.toBase58()].includes(NATIVE_MINT.toBase58())) {
const closeWrappedSOLIx = await unwrapSOLInstruction(owner);
const closeWrappedSOLIx = await unwrapSOLInstruction(owner, owner);
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
}

Expand Down 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 && !receiver.equals(owner) ? 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