Skip to main content
Optimize your Solana transactions by setting compute unit limits and priority fees. This helps ensure transactions succeed during network congestion and controls execution costs.

Setup Solana Libraries First

import { useParaSolanaSigner, useParaSolanaSignAndSend } from '@getpara/react-sdk';
import { Address } from '@solana/addresses';
import {
  createSolanaRpc,
  pipe,
  createTransactionMessage,
  setTransactionMessageFeePayer,
  setTransactionMessageLifetimeUsingBlockhash,
  appendTransactionMessageInstruction,
  lamports,
} from '@solana/kit';
import { getTransferSolInstruction } from '@solana-program/system';
import {
  getSetComputeUnitLimitInstruction,
  getSetComputeUnitPriceInstruction,
} from '@solana-program/compute-budget';

const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");

function ComputeUnitsExample() {
  const { solanaSigner, isLoading } = useParaSolanaSigner({ rpc });
  const { signAndSendAsync, isPending } = useParaSolanaSignAndSend(solanaSigner);

  const sendWithPriorityFee = async () => {
    if (!solanaSigner) return;

    const recipient = "RECIPIENT_ADDRESS" as Address;
    const response = await rpc.getLatestBlockhash().send();
    const { blockhash, lastValidBlockHeight } = response.value;

    const { value: recentFees } = await rpc.getRecentPrioritizationFees().send();
    const avgFee = recentFees.reduce((sum, fee) => sum + fee.prioritizationFee, 0n) / BigInt(recentFees.length);
    const priorityFee = (avgFee * 120n) / 100n;

    const transaction = pipe(
      createTransactionMessage({ version: "legacy" }),
      tx => setTransactionMessageFeePayer(solanaSigner.address, tx),
      tx => setTransactionMessageLifetimeUsingBlockhash({ blockhash, lastValidBlockHeight }, tx),
      tx => appendTransactionMessageInstruction(getSetComputeUnitLimitInstruction({ units: 200000 }), tx),
      tx => appendTransactionMessageInstruction(getSetComputeUnitPriceInstruction({ microLamports: priorityFee }), tx),
      tx => appendTransactionMessageInstruction(
        getTransferSolInstruction({
          source: solanaSigner,
          destination: recipient,
          amount: lamports(100_000_000n),
        }),
        tx
      )
    );

    const result = await signAndSendAsync({ transactions: [transaction] });

    return result;
  };

  return <button onClick={sendWithPriorityFee}>Send with Priority Fee</button>;
}

Next Steps

Execute Transactions

Get Transaction Status

Configure RPC