Skip to content

Commit

Permalink
fix(blockchain-link): use constant and increased compute unit limit f…
Browse files Browse the repository at this point in the history
…or Solana transactions

In an ideal world over-reserving compute units would not result in faster transaction confirmation but if we optimize the compute unit limit to match the amount of CUs the transaction requires (+ a 20% buffer) the transactions simply do not go through in time.

Perhaps this could be revisited at some point in the future e.g. when [this PR](anza-xyz/agave#187) is released and validators update.
  • Loading branch information
gabrielKerekes committed Mar 25, 2024
1 parent 5ebba00 commit 078cbe8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
15 changes: 6 additions & 9 deletions packages/blockchain-link/src/workers/solana/fee.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Connection, Message, PublicKey, VersionedTransaction } from '@solana/web3.js';
import { Connection, Message, PublicKey } from '@solana/web3.js';
import BigNumber from 'bignumber.js';

const COMPUTE_BUDGET_PROGRAM_ID = 'ComputeBudget111111111111111111111111111111';
const DEFAULT_COMPUTE_UNIT_PRICE = 100_000; // micro-lamports, value taken from other wallets
const DEFAULT_COMPUTE_UNIT_LIMIT = 50_000; // sending tokens with token account creation requires ~28K units
// sending tokens with token account creation requires ~28K units. However we over-reserve for now
// since otherwise the transactions don't seem to go through otherwise. This can perhaps be changed
// if e.g. https://github.com/anza-xyz/agave/pull/187 is merged.
const DEFAULT_COMPUTE_UNIT_LIMIT = 200_000;

export const getBaseFee = async (api: Connection, message: Message) => {
// Remove ComputeBudget instructions from the message when estimating the base fee
Expand Down Expand Up @@ -39,13 +42,7 @@ export const getPriorityFee = async (api: Connection, message: Message) => {
lockedWritableAccounts: affectedAccounts.map(a => new PublicKey(a)),
});

// https://solana.com/developers/guides/advanced/how-to-request-optimal-compute#special-considerations
const simulationResult = await api.simulateTransaction(new VersionedTransaction(message));
const simulatedUnitsConsumed = simulationResult.value.unitsConsumed;
const computeUnitLimit =
simulatedUnitsConsumed != null
? Math.ceil(simulatedUnitsConsumed * 1.2) // 20% buffer - 10% is recommended by the article above, but we double it just to be sure
: DEFAULT_COMPUTE_UNIT_LIMIT;
const computeUnitLimit = DEFAULT_COMPUTE_UNIT_LIMIT;

const networkPriorityFee = recentFees.map(a => a.prioritizationFee).sort((a, b) => b - a)[
Math.floor(recentFees.length / 4)
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/data/defaultFeeLevels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ const SOLANA_FEE_INFO: FeeInfoWithLevels = {
{
label: 'normal',
feePerUnit: '100000',
feeLimit: '50000',
feePerTx: '10000',
feeLimit: '200000',
feePerTx: '25000',
blocks: -1,
},
],
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/utils/wallet/solanaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type PriorityFees = { computeUnitPrice: string; computeUnitLimit: string };

export const dummyPriorityFeesForFeeEstimation: PriorityFees = {
computeUnitPrice: '100000',
computeUnitLimit: '50000',
computeUnitLimit: '200000',
};

const addPriorityFees = async (transaction: Transaction, priorityFees: PriorityFees) => {
Expand Down

0 comments on commit 078cbe8

Please sign in to comment.