solana_compute_budget_interface/
lib.rs1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
4
5#[cfg(feature = "borsh")]
6use borsh::{BorshDeserialize, BorshSerialize};
7use solana_instruction::Instruction;
8pub use solana_sdk_ids::compute_budget::{check_id, id, ID};
9
10#[cfg_attr(
12 feature = "frozen-abi",
13 derive(
14 solana_frozen_abi_macro::AbiExample,
15 solana_frozen_abi_macro::AbiEnumVisitor
16 )
17)]
18#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))]
19#[cfg_attr(
20 feature = "serde",
21 derive(serde_derive::Deserialize, serde_derive::Serialize)
22)]
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub enum ComputeBudgetInstruction {
25 Unused, RequestHeapFrame(u32),
31 SetComputeUnitLimit(u32),
33 SetComputeUnitPrice(u64),
36 SetLoadedAccountsDataSizeLimit(u32),
38}
39
40macro_rules! to_instruction {
41 ($discriminator: expr, $num: expr, $num_type: ty) => {{
42 let mut data = [0u8; ::core::mem::size_of::<$num_type>() + 1];
43 data[0] = $discriminator;
44 data[1..].copy_from_slice(&$num.to_le_bytes());
45 Instruction {
46 program_id: id(),
47 data: data.to_vec(),
48 accounts: vec![],
49 }
50 }};
51}
52
53impl ComputeBudgetInstruction {
54 pub fn request_heap_frame(bytes: u32) -> Instruction {
56 to_instruction!(1, bytes, u32)
57 }
58
59 pub fn set_compute_unit_limit(units: u32) -> Instruction {
61 to_instruction!(2, units, u32)
62 }
63
64 pub fn set_compute_unit_price(micro_lamports: u64) -> Instruction {
66 to_instruction!(3, micro_lamports, u64)
67 }
68
69 #[cfg(feature = "dev-context-only-utils")]
72 pub fn pack(self) -> Result<Vec<u8>, borsh::io::Error> {
73 borsh::to_vec(&self)
74 }
75
76 pub fn set_loaded_accounts_data_size_limit(bytes: u32) -> Instruction {
78 to_instruction!(4, bytes, u32)
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn test_to_instruction() {
88 let ix = ComputeBudgetInstruction::set_compute_unit_limit(257);
89 assert_eq!(ix.data, vec![2, 1, 1, 0, 0]);
90 let ix = ComputeBudgetInstruction::set_compute_unit_price(u64::MAX);
91 assert_eq!(ix.data, vec![3, 255, 255, 255, 255, 255, 255, 255, 255]);
92 }
93}