Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 545df70

Browse files
committed
stake-pool-cli: Add function to simulate tx for CUs used
1 parent bf8afd1 commit 545df70

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

stake-pool/cli/src/client.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ use {
77
rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig},
88
rpc_filter::{Memcmp, RpcFilterType},
99
},
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},
1115
spl_stake_pool::{
1216
find_withdraw_authority_program_address,
1317
state::{StakePool, ValidatorList},
1418
},
1519
std::collections::HashSet,
1620
};
1721

18-
type Error = Box<dyn std::error::Error>;
22+
pub(crate) type Error = Box<dyn std::error::Error>;
1923

2024
pub fn get_stake_pool(
2125
rpc_client: &RpcClient,
@@ -146,3 +150,38 @@ pub(crate) fn get_all_stake(
146150
.map(|(address, _)| address)
147151
.collect())
148152
}
153+
154+
/// Helper function to add a compute unit limit instruction to a given set
155+
/// of instructions
156+
///
157+
/// Returns true if the instruction was added, false if it wasn't. The false
158+
/// case is used for offline signing, where we cannot access the network.
159+
pub(crate) fn add_compute_unit_limit_from_simulation(
160+
rpc_client: &RpcClient,
161+
instructions: &mut Vec<Instruction>,
162+
payer: &Pubkey,
163+
blockhash: &Hash,
164+
) -> Result<(), Error> {
165+
// add a max compute unit limit instruction for the simulation
166+
const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000;
167+
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
168+
MAX_COMPUTE_UNIT_LIMIT,
169+
));
170+
171+
let transaction = Transaction::new_unsigned(Message::new_with_blockhash(
172+
instructions,
173+
Some(payer),
174+
blockhash,
175+
));
176+
let simulation_result = rpc_client.simulate_transaction(&transaction)?.value;
177+
let units_consumed = simulation_result
178+
.units_consumed
179+
.ok_or("No units consumed on simulation")?;
180+
// Overwrite the compute unit limit instruction with the actual units consumed
181+
let compute_unit_limit = u32::try_from(units_consumed)?;
182+
instructions
183+
.last_mut()
184+
.expect("Compute budget instruction was added earlier")
185+
.data = ComputeBudgetInstruction::set_compute_unit_limit(compute_unit_limit).data;
186+
Ok(())
187+
}

stake-pool/cli/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub(crate) struct Config {
6868
no_update: bool,
6969
}
7070

71-
type Error = Box<dyn std::error::Error>;
7271
type CommandResult = Result<(), Error>;
7372

7473
const STAKE_STATE_LEN: usize = 200;

0 commit comments

Comments
 (0)