Skip to content

Commit

Permalink
fix: optimize cu
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithgun committed Nov 27, 2024
1 parent d662b7f commit 9667353
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
40 changes: 29 additions & 11 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,14 +439,22 @@ export default class AmmImpl implements AmmImplementation {
const ixs: Array<Transaction | TransactionInstruction | (Transaction | TransactionInstruction)[]> = [];

if (preInstructions.length) {
ixs.push(preInstructions);
// https://explorer.solana.com/tx/3G95TWMmTLbZ7aAgyGmZd8JFk19ye8whVB5cXhYCCsUWSUsnuVMqPMXbPiY5WaF4zE2Sz7CG5e4jTj8NQbCnUG14?cluster=devnet
// Create 2 dynamic vault consume around 190k CU. Each create ATA + Wrap SOL around 23k
const setComputeUnitLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 300_000,
});
ixs.push([setComputeUnitLimitIx, ...preInstructions]);
}

// https://explorer.solana.com/tx/4X37hBoUNwpmHKNGpQB3M72xDA7yhFKhbYrkBQUK4skBGD7P9LdWgb2WpvFGHcxYi13e9bdwhetsqmULeW7nUDbW?cluster=devnet
// https://explorer.solana.com/tx/3xMGCQ9GAqSMZH1uhXXc1quZit61DXr1KsbVxdBN1zCkQy1GTXBGuWgxzWfMEdecdRi859m3mYQKuLyemvR18VHS?cluster=devnet
// Create dynamic pool consume around 287k, create lock escrow + lock liquidity around 84k
const setComputeUnitLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 1_400_000,
units: 450_000,
});
ixs.push([setComputeUnitLimitIx, createPermissionlessPoolTx]);

if (feeWrapperLockAmount.gt(new BN(0))) {
const preInstructions: TransactionInstruction[] = [];
const createFeeVaultIxs = await StakeForFee.createFeeVaultInstructions(
Expand Down Expand Up @@ -697,11 +705,16 @@ export default class AmmImpl implements AmmImplementation {
const ixs: Array<Transaction | TransactionInstruction | (Transaction | TransactionInstruction)[]> = [];

if (preInstructions.length) {
ixs.push(preInstructions);
// Create 2 dynamic vault consume around 190k CU. Each create ATA + Wrap SOL around 23k
const setComputeUnitLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 300_000,
});
ixs.push([setComputeUnitLimitIx, ...preInstructions]);
}

// Create dynamic pool consume around 287k, create lock escrow + lock liquidity around 84k
const setComputeUnitLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 1_400_000,
units: 450_000,
});

ixs.push([setComputeUnitLimitIx, createPermissionlessPoolTx]);
Expand Down Expand Up @@ -966,11 +979,16 @@ export default class AmmImpl implements AmmImplementation {
const ixs: Array<Transaction | TransactionInstruction | (Transaction | TransactionInstruction)[]> = [];

if (preInstructions.length) {
ixs.push(preInstructions);
// Create 2 dynamic vault consume around 190k CU. Each create ATA + Wrap SOL around 23k
const setComputeUnitLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 300_000,
});
ixs.push([setComputeUnitLimitIx, ...preInstructions]);
}

// Create dynamic pool consume around 287k, create lock escrow + lock liquidity around 84k
const setComputeUnitLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 1_400_000,
units: 450_000,
});

ixs.push([setComputeUnitLimitIx, createPermissionlessPoolTx]);
Expand Down Expand Up @@ -1051,7 +1069,7 @@ export default class AmmImpl implements AmmImplementation {

ixs.push(swapTx);
}
const resultTx: Transaction[] = await createTransactions(connection, ixs, payer);
const resultTx: Transaction[] = await createTransactions(connection, ixs, payer);

return resultTx;
}
Expand Down Expand Up @@ -2454,15 +2472,15 @@ export default class AmmImpl implements AmmImplementation {
createEscrowAtaIx && preInstructions.push(createEscrowAtaIx);

const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(amount, opt?.stakeLiquidity?.percent);

if (feeWrapperLockAmount.gt(new BN(0))) {
const { stakeForFeeProgram } = createProgram(this.program.provider.connection);

const vaultKey = deriveFeeVault(this.address, STAKE_FOR_FEE_PROGRAM_ID);
const vaultState = await getFeeVaultState(vaultKey, stakeForFeeProgram);

if (!vaultState) {
throw new Error(`Fee vault not found for pool ${this.address.toBase58()}`);
throw new Error(`Fee vault not found for pool ${this.address.toBase58()}`);
}

const [lockEscrowFeeVaultPK] = deriveLockEscrowPda(this.address, vaultKey, this.program.programId);
Expand All @@ -2475,7 +2493,7 @@ export default class AmmImpl implements AmmImplementation {
);

createEscrowFeeVaultAtaIx && stakeForFeeInstructions.push(createEscrowFeeVaultAtaIx);

const lockTx = await this.program.methods
.lock(feeWrapperLockAmount)
.accounts({
Expand Down
10 changes: 5 additions & 5 deletions ts-client/src/examples/create_pool_and_lock_liquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function loadKeypairFromFile(filename: string): Keypair {
}

const mainnetConnection = new Connection('https://api.devnet.solana.com');
const payerKP = loadKeypairFromFile('~/.config/solana/id.json');
const payerKP = loadKeypairFromFile(`${process.env.HOME}/.config/solana/id.json`);
const payerWallet = new Wallet(payerKP);
console.log('payer %s', payerKP.publicKey);

Expand Down Expand Up @@ -109,8 +109,8 @@ async function createPoolAndLockLiquidity(
*/
async function main() {
// 1. Token A/B address of the pool.
const tokenAMint = new PublicKey('BjhBG7jkHYMBMos2HtRdFrw8rvSguBe5c3a3EJYXhyUf');
const tokenBMint = new PublicKey('9KMeJp868Pdk8PrJEkwoAHMA1ctdxfVhe2TjeS4BcWjs');
const tokenAMint = new PublicKey('CQYP65PUMLbxNLsuvs1Ee5RfmP4SYSN29f9srr4mNsq9');
const tokenBMint = new PublicKey('AQW65ziZ9ZFYPZZEujXy3CanHnSgYPVsnGdW9iUApuMV');

// 2. Configuration address for the pool. It will decide the fees of the pool.
const config = new PublicKey('21PjsfQVgrn56jSypUT5qXwwSjwKWvuoBCKbVZrgTLz4');
Expand All @@ -130,8 +130,8 @@ async function main() {
];

// 4. Amount of token A and B to be deposited to the pool, and will be locked.
let tokenAAmount = new BN(100_000);
let tokenBAmount = new BN(500_000);
let tokenAAmount = new BN(100000);
let tokenBAmount = new BN(100000);

await createPoolAndLockLiquidity(tokenAMint, tokenBMint, tokenAAmount, tokenBAmount, config, allocations);
}
Expand Down

0 comments on commit 9667353

Please sign in to comment.