Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.getpara.com/llms.txt

Use this file to discover all available pages before exploring further.

Optimize your Solana transactions by setting compute unit limits and priority fees. This helps ensure transactions succeed during network congestion and controls execution costs.
import { useParaSolanaSigner, useParaSolanaSignAndSend } from "@getpara/react-native-wallet/solana";
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 transactionMessage = 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 txSignature = await signAndSendAsync({ transactionMessage });
    console.log("Transaction sent with priority fee:", txSignature);

    return txSignature;
  };

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

Next Steps