Created
May 24, 2026 09:46
-
-
Save 0ex-d/b4bf393768e7cdb9571c4866926f8dda to your computer and use it in GitHub Desktop.
snippet for Compute Budget Buffering post 01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 4) Client helper pattern (JS): strip old limit, resimulate, add fresh buffered limit | |
| stripComputeUnitLimit(tx); | |
| const consumed = sim.value.unitsConsumed || 200000; | |
| const limit = Math.max(Math.ceil(consumed * 1.15), 50_000); | |
| tx.instructions.unshift( | |
| ComputeBudgetProgram.setComputeUnitLimit({ units: limit }) | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1) Simulate first, then buffer compute units | |
| let simulated_units: u64 = rpc_simulated_units.unwrap_or(200_000); | |
| let buffered_units = ((simulated_units as f64) * 1.15).ceil() as u32; | |
| let final_limit = buffered_units.max(50_000); | |
| let cu_limit_ix = ComputeBudgetInstruction::set_compute_unit_limit(final_limit); | |
| // 2) Hard-cap user-provided compute settings (relayer-side guard) | |
| const MAX_COMPUTE_LIMIT: u32 = 1_000_000; | |
| const MAX_COMPUTE_PRICE: u64 = 1_000; | |
| if requested_limit > MAX_COMPUTE_LIMIT { | |
| return Err("compute unit limit too high"); | |
| } | |
| if requested_price > MAX_COMPUTE_PRICE { | |
| return Err("compute unit price too high"); | |
| } | |
| // 3) Bundle safety rule: reject tx if CU limit ix is missing | |
| let has_cu_limit = tx.message.instructions().iter().any(|ix| { | |
| ix.program_id == COMPUTE_BUDGET_PROGRAM_ID && ix.data.first() == Some(&2) // SetComputeUnitLimit | |
| }); | |
| if !has_cu_limit { | |
| return Err("simulate, add buffered CU limit, then re-sign"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment