Skip to content

Commit

Permalink
fix: cu limit
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithgun committed Dec 12, 2024
1 parent 8c0dd5b commit 35bfb01
Show file tree
Hide file tree
Showing 4 changed files with 473 additions and 129 deletions.
5 changes: 3 additions & 2 deletions ts-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
"dependencies": {
"@coral-xyz/anchor": "^0.28.0",
"@coral-xyz/borsh": "^0.28.0",
"@solana-developers/helpers": "^2.5.6",
"@solana/buffer-layout": "^4.0.1",
"bn.js": "^5.2.1",
"express": "^4.19.2",
"@solana/spl-token": "^0.4.6",
"@solana/web3.js": "^1.91.6",
"bn.js": "^5.2.1",
"decimal.js": "^10.4.2",
"express": "^4.19.2",
"gaussian": "^1.3.0"
},
"keywords": [],
Expand Down
8 changes: 8 additions & 0 deletions ts-client/src/dlmm/helpers/computeUnit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// https://solscan.io/tx/4ryJKTB1vYmGU6YnUWwbLps18FaJjiTwgRozcgdP8RFcwp7zUZi85vgWE7rARNx2NvzDJiM9CUWArqzY7LHv38WL
export const DEFAULT_ADD_LIQUIDITY_CU = 800_000;
// https://solscan.io/tx/49AGqJLki7DWcXWU9xdvLDmtTJFjfhpEUYKBbMjkDmjqQ9xJfvkrqTuSKX9Ac5cewsnPp9X818WsskzscwU72b4i
export const DEFAULT_REMOVE_LIQUIDITY_CU = 600_000;
// https://solscan.io/tx/3FGmkv9vpmdePVqm37A9ULhNvhZKFuKysVeXjqHAFNJvRczvoJp6c1rpiF2SwUQUNsQ25rhR81Az6wbZqb7Dhv6y
export const DEFAULT_CLAIM_FEE_CU = 400_000;
export const DEFAULT_CLAIM_REWARD_CU = 400_000;
export const DEFAULT_COMBINED_CLAIM_FEE_AND_REWARD_CU = 600_000;
69 changes: 66 additions & 3 deletions ts-client/src/dlmm/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from "@solana/web3.js";
import { Bin, ClmmProgram, GetOrCreateATAResponse } from "../types";
import { Rounding, mulShr, shlDiv } from "./math";
import { getSimulationComputeUnits } from "@solana-developers/helpers";

export * from "./derive";
export * from "./binArray";
Expand Down Expand Up @@ -197,8 +198,70 @@ export async function chunkedGetMultipleAccountInfos(
return accountInfos;
}

export const computeBudgetIx = () => {
return ComputeBudgetProgram.setComputeUnitLimit({
units: 1_400_000,
/**
* Gets the estimated compute unit usage with a buffer.
* @param connection A Solana connection object.
* @param buffer The buffer to add to the estimated compute unit usage. Max value is 1. Default value is 0.1 if not provided.
* @returns The estimated compute unit usage with the buffer.
*/
export const getEstimatedComputeUnitUsageWithBuffer = async (
connection: Connection,
instructions: TransactionInstruction[],
feePayer: PublicKey,
buffer?: number
) => {
if (!buffer) {
buffer = 0.1;
}
// Avoid negative value
buffer = Math.max(0, buffer);
// Limit buffer to 1
buffer = Math.min(1, buffer);

const estimatedComputeUnitUsage = await getSimulationComputeUnits(
connection,
instructions,
feePayer,
[]
);

const estimatedComputeUnitUsageWithBuffer = Math.min(
estimatedComputeUnitUsage * (1 + buffer),
1_400_000
);

return estimatedComputeUnitUsageWithBuffer;
};

/**
* Gets the estimated compute unit usage with a buffer and converts it to a SetComputeUnitLimit instruction.
* If the estimated compute unit usage cannot be retrieved, returns a SetComputeUnitLimit instruction with the fallback unit.
* @param connection A Solana connection object.
* @param instructions The instructions of the transaction to simulate.
* @param feePayer The public key of the fee payer.
* @param fallbackUnit The fallback compute unit limit.
* @param buffer The buffer to add to the estimated compute unit usage. Max value is 1. Default value is 0.1 if not provided.
* @returns A SetComputeUnitLimit instruction with the estimated compute unit usage.
*/
export const getEstimatedComputeUnitIxWithBuffer = async (
connection: Connection,
instructions: TransactionInstruction[],
feePayer: PublicKey,
fallbackUnit?: number,
buffer?: number
) => {
const units = await getEstimatedComputeUnitUsageWithBuffer(
connection,
instructions,
feePayer,
buffer
).catch((error) => {
console.error("Error::getEstimatedComputeUnitUsageWithBuffer", error);
if (fallbackUnit) {
return fallbackUnit;
}
throw error;
});

return ComputeBudgetProgram.setComputeUnitLimit({ units });
};
Loading

0 comments on commit 35bfb01

Please sign in to comment.