-
Notifications
You must be signed in to change notification settings - Fork 43
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
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b8e33cc
feat: add fee wrapper for create amm pool
dongnguyen9186 5b45712
update lock amount calculation
dongnguyen9186 54c034f
add create ata for stake for fee vault
dongnguyen9186 310693c
make new func for create new memecoin mint pool
dongnguyen9186 a33f29d
fee wrapper for burn and create memecoin pool
dongnguyen9186 16ea85a
fix: check user lock amount
00xSam d88d9f3
update changelog
00xSam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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; | ||
|
@@ -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); | ||
|
||
|
@@ -487,6 +491,9 @@ export default class AmmImpl implements AmmImplementation { | |
inAmount: BN; | ||
minAmountOut: BN; | ||
}; | ||
stakeLiquidity?: { | ||
percent?: Decimal; | ||
}; | ||
skipAAta?: boolean; | ||
skipBAta?: boolean; | ||
}, | ||
|
@@ -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 | ||
: 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be
|
||
new Decimal(lpAmount.toString()).mul(new Decimal(1).minus(feeWrapperPercent)).toFixed(0, Decimal.ROUND_DOWN), | ||
) : U64_MAX; | ||
|
||
const createPoolPostInstructions: TransactionInstruction[] = []; | ||
if (opt?.lockLiquidity) { | ||
|
@@ -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, | ||
|
@@ -604,6 +622,7 @@ export default class AmmImpl implements AmmImplementation { | |
bVaultLpMint, | ||
}) | ||
.instruction(); | ||
|
||
createPoolPostInstructions.push(lockIx); | ||
} | ||
|
||
|
@@ -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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)