Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fee wrapper for create amm pool #168

Merged
merged 7 commits into from
Oct 26, 2024
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
3 changes: 2 additions & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"bn.js": "5.2.1",
"decimal.js": "^10.4.1",
"dotenv": "^16.0.1",
"invariant": "^2.2.4"
"invariant": "^2.2.4",
"@meteora-ag/stake-for-fee": "1.0.7"
},
"devDependencies": {
"@tsconfig/recommended": "^1.0.1",
Expand Down
77 changes: 76 additions & 1 deletion ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
TOKEN_PROGRAM_ID,
} from '@solana/spl-token';
import VaultImpl, { calculateWithdrawableAmount, getVaultPdas } from '@mercurial-finance/vault-sdk';
import StakeForFee, { deriveFeeVault, STAKE_FOR_FEE_PROGRAM_ID } from '@meteora-ag/stake-for-fee';
import invariant from 'invariant';
import {
AccountType,
Expand Down Expand Up @@ -70,6 +71,8 @@ import {
deriveProtocolTokenFee,
} from './utils';
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
import Decimal from 'decimal.js';
import sqrt from 'bn-sqrt';

type Opt = {
cluster: Cluster;
Expand Down Expand Up @@ -316,6 +319,7 @@ export default class AmmImpl implements AmmImplementation {
getOrCreateATAInstruction(tokenAMint, payer, connection),
getOrCreateATAInstruction(tokenBMint, payer, connection),
]);

createPayerTokenAIx && preInstructions.push(createPayerTokenAIx);
createPayerTokenBIx && preInstructions.push(createPayerTokenBIx);

Expand Down Expand Up @@ -487,6 +491,9 @@ export default class AmmImpl implements AmmImplementation {
inAmount: BN;
minAmountOut: BN;
};
stakeLiquidity?: {
percent?: Decimal;
};
skipAAta?: boolean;
skipBAta?: boolean;
},
Expand Down Expand Up @@ -567,6 +574,17 @@ 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
Copy link
Contributor

@codewithgun codewithgun Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to make sure user doesn't pass in > 100%
opt.stakeLiquidity.percent.min(Decimal::ONE)

: 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be userLockAmount = feeWrapperPercent.gt(new Decimal(0)) ? lpAmount.sub(feeWrapperLockAmount) : U64_MAX
If not, there's some precision loss will be remained in the user wallet.
Example:

lpAmount = 133
feeFarmLockPct = 0.1
userLockPct = 0.9

feeWrapperLockAmount = 133 * 0.1 = RoundDown(13.3) = 13
userLockAmount = 133 * (1 - 0.1) = RoundDown(119.7) = 119
119 + 13 != 133

new Decimal(lpAmount.toString()).mul(new Decimal(1).minus(feeWrapperPercent)).toFixed(0, Decimal.ROUND_DOWN),
) : U64_MAX;

const createPoolPostInstructions: TransactionInstruction[] = [];
if (opt?.lockLiquidity) {
Expand All @@ -587,7 +605,7 @@ export default class AmmImpl implements AmmImplementation {

createEscrowAtaIx && createPoolPostInstructions.push(createEscrowAtaIx);
const lockIx = await ammProgram.methods
.lock(U64_MAX)
.lock(userLockAmount)
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
Expand All @@ -604,6 +622,7 @@ export default class AmmImpl implements AmmImplementation {
bVaultLpMint,
})
.instruction();

createPoolPostInstructions.push(lockIx);
}

Expand Down Expand Up @@ -664,6 +683,62 @@ export default class AmmImpl implements AmmImplementation {

resultTx.push(createPoolTx);

if (feeWrapperLockAmount.gt(new BN(0))) {
const feeVaultConfig = await StakeForFee.getConfigs(connection);

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

const newCreateStakeForFeeTx = new Transaction({
feePayer: payer,
...latestBlockHash,
}).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);
}

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