|
7 | 7 | rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig},
|
8 | 8 | rpc_filter::{Memcmp, RpcFilterType},
|
9 | 9 | },
|
10 |
| - solana_program::{borsh1::try_from_slice_unchecked, program_pack::Pack, pubkey::Pubkey, stake}, |
| 10 | + solana_program::{ |
| 11 | + borsh1::try_from_slice_unchecked, hash::Hash, instruction::Instruction, message::Message, |
| 12 | + program_pack::Pack, pubkey::Pubkey, stake, |
| 13 | + }, |
| 14 | + solana_sdk::{compute_budget::ComputeBudgetInstruction, transaction::Transaction}, |
11 | 15 | spl_stake_pool::{
|
12 | 16 | find_withdraw_authority_program_address,
|
13 | 17 | state::{StakePool, ValidatorList},
|
14 | 18 | },
|
15 | 19 | std::collections::HashSet,
|
16 | 20 | };
|
17 | 21 |
|
18 |
| -type Error = Box<dyn std::error::Error>; |
| 22 | +pub(crate) type Error = Box<dyn std::error::Error>; |
19 | 23 |
|
20 | 24 | pub fn get_stake_pool(
|
21 | 25 | rpc_client: &RpcClient,
|
@@ -146,3 +150,35 @@ pub(crate) fn get_all_stake(
|
146 | 150 | .map(|(address, _)| address)
|
147 | 151 | .collect())
|
148 | 152 | }
|
| 153 | + |
| 154 | +/// Helper function to add a compute unit limit instruction to a given set |
| 155 | +/// of instructions |
| 156 | +pub(crate) fn add_compute_unit_limit_from_simulation( |
| 157 | + rpc_client: &RpcClient, |
| 158 | + instructions: &mut Vec<Instruction>, |
| 159 | + payer: &Pubkey, |
| 160 | + blockhash: &Hash, |
| 161 | +) -> Result<(), Error> { |
| 162 | + // add a max compute unit limit instruction for the simulation |
| 163 | + const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000; |
| 164 | + instructions.push(ComputeBudgetInstruction::set_compute_unit_limit( |
| 165 | + MAX_COMPUTE_UNIT_LIMIT, |
| 166 | + )); |
| 167 | + |
| 168 | + let transaction = Transaction::new_unsigned(Message::new_with_blockhash( |
| 169 | + instructions, |
| 170 | + Some(payer), |
| 171 | + blockhash, |
| 172 | + )); |
| 173 | + let simulation_result = rpc_client.simulate_transaction(&transaction)?.value; |
| 174 | + let units_consumed = simulation_result |
| 175 | + .units_consumed |
| 176 | + .ok_or("No units consumed on simulation")?; |
| 177 | + // Overwrite the compute unit limit instruction with the actual units consumed |
| 178 | + let compute_unit_limit = u32::try_from(units_consumed)?; |
| 179 | + instructions |
| 180 | + .last_mut() |
| 181 | + .expect("Compute budget instruction was added earlier") |
| 182 | + .data = ComputeBudgetInstruction::set_compute_unit_limit(compute_unit_limit).data; |
| 183 | + Ok(()) |
| 184 | +} |
0 commit comments