cbe_sdk/compute_budget.rs
1//! The compute budget native program.
2
3#![cfg(feature = "full")]
4
5use {
6 crate::instruction::Instruction,
7 borsh::{BorshDeserialize, BorshSerialize},
8};
9
10crate::declare_id!("ComputeBudget111111111111111111111111111111");
11
12/// Compute Budget Instructions
13#[derive(
14 AbiExample,
15 AbiEnumVisitor,
16 BorshDeserialize,
17 BorshSerialize,
18 Clone,
19 Debug,
20 Deserialize,
21 PartialEq,
22 Eq,
23 Serialize,
24)]
25pub enum ComputeBudgetInstruction {
26 /// Deprecated
27 // TODO: after feature remove_deprecated_request_unit_ix::id() is activated, replace it with 'unused'
28 RequestUnitsDeprecated {
29 /// Units to request
30 units: u32,
31 /// Additional fee to add
32 additional_fee: u32,
33 },
34 /// Request a specific transaction-wide program heap region size in bytes.
35 /// The value requested must be a multiple of 1024. This new heap region
36 /// size applies to each program executed in the transaction, including all
37 /// calls to CPIs.
38 RequestHeapFrame(u32),
39 /// Set a specific compute unit limit that the transaction is allowed to consume.
40 SetComputeUnitLimit(u32),
41 /// Set a compute unit price in "micro-scoobies" to pay a higher transaction
42 /// fee for higher transaction prioritization.
43 SetComputeUnitPrice(u64),
44 /// Set a specific transaction-wide account data size limit, in bytes, is allowed to allocate.
45 SetAccountsDataSizeLimit(u32),
46}
47
48impl ComputeBudgetInstruction {
49 /// Create a `ComputeBudgetInstruction::RequestHeapFrame` `Instruction`
50 pub fn request_heap_frame(bytes: u32) -> Instruction {
51 Instruction::new_with_borsh(id(), &Self::RequestHeapFrame(bytes), vec![])
52 }
53
54 /// Create a `ComputeBudgetInstruction::SetComputeUnitLimit` `Instruction`
55 pub fn set_compute_unit_limit(units: u32) -> Instruction {
56 Instruction::new_with_borsh(id(), &Self::SetComputeUnitLimit(units), vec![])
57 }
58
59 /// Create a `ComputeBudgetInstruction::SetComputeUnitPrice` `Instruction`
60 pub fn set_compute_unit_price(micro_scoobies: u64) -> Instruction {
61 Instruction::new_with_borsh(id(), &Self::SetComputeUnitPrice(micro_scoobies), vec![])
62 }
63
64 /// Create a `ComputeBudgetInstruction::SetAccountsDataSizeLimit` `Instruction`
65 pub fn set_accounts_data_size_limit(bytes: u32) -> Instruction {
66 Instruction::new_with_borsh(id(), &Self::SetAccountsDataSizeLimit(bytes), vec![])
67 }
68
69 /// Serialize Instruction using borsh, this is only used in runtime::cost_model::tests but compilation
70 /// can't be restricted as it's used across packages
71 // #[cfg(test)]
72 pub fn pack(self) -> Result<Vec<u8>, std::io::Error> {
73 self.try_to_vec()
74 }
75}