solders_compute_budget/
lib.rs

1use pyo3::prelude::*;
2use solana_sdk::compute_budget::{ComputeBudgetInstruction, ID};
3use solders_instruction::Instruction;
4use solders_pubkey::Pubkey;
5
6/// Request a specific transaction-wide program heap region size in bytes.
7/// The value requested must be a multiple of 1024. This new heap region
8/// size applies to each program executed in the transaction, including all
9/// calls to CPIs.
10#[pyfunction]
11pub fn request_heap_frame(bytes_: u32) -> Instruction {
12    ComputeBudgetInstruction::request_heap_frame(bytes_).into()
13}
14
15/// Set a specific compute unit limit that the transaction is allowed to consume.
16#[pyfunction]
17pub fn set_compute_unit_limit(units: u32) -> Instruction {
18    ComputeBudgetInstruction::set_compute_unit_limit(units).into()
19}
20
21/// Set a compute unit price in "micro-lamports" to pay a higher transaction
22/// fee for higher transaction prioritization.
23#[pyfunction]
24pub fn set_compute_unit_price(micro_lamports: u64) -> Instruction {
25    ComputeBudgetInstruction::set_compute_unit_price(micro_lamports).into()
26}
27
28pub fn create_compute_budget_mod(py: Python<'_>) -> PyResult<&PyModule> {
29    let m = PyModule::new(py, "compute_budget")?;
30    m.add("ID", Pubkey(ID))?;
31    let funcs = [
32        wrap_pyfunction!(request_heap_frame, m)?,
33        wrap_pyfunction!(set_compute_unit_limit, m)?,
34        wrap_pyfunction!(set_compute_unit_price, m)?,
35    ];
36    for func in funcs {
37        m.add_function(func)?;
38    }
39    Ok(m)
40}