2

It's probably a dumb question.

I am looking at this example specifically. The following code shows that it's added 300 as the Compute Unit limit, 20000 microLamports as the compute unit price.

This is the transaction detail at Solscan.io. It shows the Priority Fee is 0.000004 SOL. How is the Priority Fee 0.000004 SOL calculated? With 300 Compute Units and 20000 microLamports as the compute unit price, I can not get this number.

// import { ... } from "@solana/web3.js"
 
const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
  units: 300,
});
 
const addPriorityFee = ComputeBudgetProgram.setComputeUnitPrice({
  microLamports: 20000,
});
 
const transaction = new Transaction()
  .add(modifyComputeUnits)
  .add(addPriorityFee)
  .add(
    SystemProgram.transfer({
      fromPubkey: payer.publicKey,
      toPubkey: toAccount,
      lamports: 10000000,
    }),
  );

1 Answer 1

2

Priority fees work based on what the compute budget limit is, not how much you actually use. The transaction whose link you posted didn't set the Compute budget limit, so it gets set to the default, which is 200k CUs per instruction. Instructions related to the Compute Budget program do not get included in this for the sake of prioritization fee calculation though, so if we take just the 200k CUs for the transfer ix, we get

200_000 CU * 0.02 Lamports/CU = 4000 Lamports = 0.000004 SOL

Edit: To be clear, the way the code is written, it should have 3 instructions, with the second being the one that sets the CU budget to 300. If we look at the actual transaction onchain though, we can see they only added the first one, which sets the compute price, and the third one, which does the actual transfer. They forgot about the second one setting the CU budget when executing.

enter image description here

5
  • The first line is const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({ units: 300,});, isn't it the "the Compute budget limit"? Shouldn't it be 300 CPU * 0.02 Lamports/CU? Commented Aug 15 at 3:22
  • That's what it is in the code. but if you look at the actual transaction that got executed, they forgot to add that when they actually ran it. Edited my answer to make it more clear.
    – McBain
    Commented Aug 15 at 7:49
  • Thank you for the explanation! But since there are 2 instructions, why is it not 2 * 200_000 CU * 0.02 Lamports/CU = 8000 Lamports ? If I'm understanding it correctly, each instruction by default has 200k compute units limit. Since we have 2 instructions, it should be 400k compute units for the whole transaction? The Compute Budget Program instructions also cost CUs: solana.stackexchange.com/questions/3903/… Commented Aug 15 at 9:23
  • 1
    Good observation! If you check the code here, we see that for the sake of prioritization fees, the default CU budget for instructions related to the compute budget program do not get included: github.com/solana-labs/solana/blob/… Added this to my answer too to make it more clear.
    – McBain
    Commented Aug 15 at 10:08
  • Thanks a lot! It's all clear to me now! Commented Aug 17 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.