Understanding Solana gas fees settings Compute Budget: SetComputeUnitPrice & SetComputeUnitLimit
In this article i make an attempt to make understandable the different variables involved in setting the transaction gas fees for a solana transaction. If you have been confused about this as much as I have been then this might be helpful
Formulae for calculation of solana gas fees
Fees_in_MicroLamports= Compute Unit Price X Compute Limit
1Mircro_to_Lamport = 0.000001
Fees_in_Lamport= Fees_in_MicroLamports * 1Micro_To_Lamports
Basic unit fee for each transaction in sol = .000005
Basic Unit fee in lamports = 5000
Txn_total_Lamports=Fees_inLamports + Basic Unit fee in Lamport
1Lamport= 0.000000001Sol
Final fees in lamports = Txn_total_Lamports * 1Lamport
So to make the math above clear let’s have a practical example
Let’s assume we are having the following variables for our gas fees settings;
SetComputeUnitPrice= 1_073
SetComputeUnitLimit=200_000
200000 * 1073 = 214,600,000 (MicroLamports)
1MicroLamports = 0.000001Lamports
214,600,000ML * 0.000001 = 214.6 Lamports
Basic Fee = 0.000005 SOL
0.000005 + 0.000002146 = 0.0000052146 SOL
1 Lamports = 0.00000001// Minimum unit
Round up the fraction from the smallest unit
Final Fee = 0.00005215 SOL
I hope this article helps you in clarifying the mathematics behind solana transactions fees.
REVERSE ENGINEER SOLANA GAS FEES
To add more clarity to this article. Let’s take for an example I wish to pay 0.002 sol for a specific transaction on-chain. With Sol at $237.69 this is $0.47538
Important to note that there are also other compute limitations to be aware of:
- Max Compute per block: 48 million CU
- Max Compute per account per block: 12 million CU
Variables Recap:
- Final Fee in SOL: 0.002 SOL
- Basic Transaction Fee in SOL: 0.000005 SOL
- Max Compute per Block: 48 million CU
- Max Compute per Account per Block: 12 million CU
- Conversion:
- 1SOL= 1,000,000,000Lamports
- 1 Lamport = 1,000,000 MicroLamports
Step-by-Step Calculation
Step 1: Determine Total Lamports Needed
Convert 0.002 SOL into Lamports
Total Fee in Lamports= 0.002×1,000,000,000=2,000,000 Lamports
Step 2: Substract the Basic Transaction Fee
Convert the basic transaction fee into Lamports:
Basic Fee in Lamports=0.000005×1,000,000,000=5,000 Lamports
Subtract the basic fee:
Lamports for Compute Units=2,000,000−5,000=1,995,000 Lamports
Step 3: Convert Lamports to MicroLamports
Since 1 Lamport = 1,000,000 MicroLamports
MicroLamport CU=1,995,000×1,000,000=1,995,000,000,000 MicroLamports
Step 4: Relationship Between Compute Unit Price and Limit
- Compute Limit: Affects the scalability of the transaction in terms of the number of compute operations it can handle.
- Compute Unit Price: Adjusts to ensure the total fee matches the desired amount, based on the limit.
The gas fee in MicroLamports is given by:
Fees_in_MicroLamports=Compute Unit Price×Compute Limit
From Step 3:
Compute Unit Price×Compute Limit=1,995,000,000,000
Step 5: Choose a Compute Limit
You need to select a Compute Limit (≤12 million CU per account):
Let’s pick the maximum Compute Limit: 12,000,000 CU (Note if the block you want to fill has more than 12million Units Already your transaction will fail because block limit is 48M CU)
Now, lets solve for the Compute Unit Price:
Compute Unit Price=1,995,000,000,000/ 12,000,000 =166,250 MicroLamports per CU
Step 6: Verify
With Compute Unit Price=166,250 MicroLamports and Compute Limit=12,000,000
Fees_in_MicroLamports= 166,250 × 12,000,000 =1,995,000,000,000 MicroLamports
Convert back to Lamports:
Fees_in_Lamports = 1,995,000,000,000/1,000,000 =1,995,000 Lamports
Add the Basic Fee:
Total Fee in Lamports=1,995,000+5,000=2,000,000 Lamports
Convert to SOL:
Total Fee in SOL= 2,000,000/ 1,000,000,000= 0.002 SOL
To pay a gas fee of 0.002 SOL:
- Compute Unit Price: 166,250 MicroLamports
- Compute Limit: 12,000,000 CU
Now I actually Implemented the above for a swap transaction here.
I actually paid the amount below.
Fee0.0002428 SOL ($0.05775)
Priority Fee 0.0002328 SOL ($0.05537)
Compute Units Consumed 39,423.
So after some research I realised I set a CU of 12,000,000 but my transaction only consumed 39,423 CU. The actual gas fee is proportional to the units consumed, not the limit. As a result, the fee was far less than the 0.002 SOL I expected.
I realised also giving custom CU and CUP to your transaction, it’s actually setting the Priority Fees in sol because the Total fees paid is calculated as;
Total Fee Paid=Gas Fee in SOL+ Priority Fee in SOL+ Basic Fee in SOL.
I hope this new additional insights helps you optimized your transactions onchain.
WAGMI! ❤