use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
commitment_config::CommitmentConfig,
compute_budget::ComputeBudgetInstruction,
hash::Hash,
instruction::Instruction,
message::Message,
pubkey::Pubkey,
signature::{Keypair, Signature, Signer},
transaction::Transaction,
};
use crate::{Result, ToolkitError};
pub const MAX_TRANSACTION_SIZE: usize = 1232;
pub const MAX_ACCOUNTS_PER_TX: usize = 64;
pub const DEFAULT_COMPUTE_UNITS: u32 = 200_000;
#[derive(Debug, Clone)]
pub struct TransactionConfig {
pub compute_units: Option<u32>,
pub priority_fee_micro_lamports: Option<u64>,
pub simulate_before_send: bool,
pub skip_preflight: bool,
pub max_retries: u8,
pub commitment: CommitmentConfig,
}
impl Default for TransactionConfig {
fn default() -> Self {
Self {
compute_units: None,
priority_fee_micro_lamports: None,
simulate_before_send: true,
skip_preflight: false,
max_retries: 3,
commitment: CommitmentConfig::confirmed(),
}
}
}
impl TransactionConfig {
pub fn fast() -> Self {
Self {
simulate_before_send: false,
skip_preflight: true,
max_retries: 1,
..Default::default()
}
}
pub fn reliable() -> Self {
Self {
simulate_before_send: true,
skip_preflight: false,
max_retries: 5,
commitment: CommitmentConfig::finalized(),
..Default::default()
}
}
pub fn with_compute_units(mut self, units: u32) -> Self {
self.compute_units = Some(units);
self
}
pub fn with_priority_fee(mut self, micro_lamports: u64) -> Self {
self.priority_fee_micro_lamports = Some(micro_lamports);
self
}
}
#[derive(Debug, Clone)]
pub struct BatchResult {
pub successful: Vec<Signature>,
pub failed: Vec<(usize, String)>,
pub instructions_processed: usize,
}
impl BatchResult {
pub fn all_succeeded(&self) -> bool {
self.failed.is_empty()
}
pub fn success_rate(&self) -> f64 {
let total = self.successful.len() + self.failed.len();
if total == 0 {
100.0
} else {
(self.successful.len() as f64 / total as f64) * 100.0
}
}
}
#[derive(Default)]
pub struct TransactionBuilder {
instructions: Vec<Instruction>,
signers: Vec<Pubkey>,
config: TransactionConfig,
}
impl TransactionBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn with_config(config: TransactionConfig) -> Self {
Self {
config,
..Default::default()
}
}
pub fn add_instruction(mut self, instruction: Instruction) -> Self {
self.instructions.push(instruction);
self
}
pub fn add_instructions(mut self, instructions: Vec<Instruction>) -> Self {
self.instructions.extend(instructions);
self
}
pub fn add_signer(mut self, signer: Pubkey) -> Self {
if !self.signers.contains(&signer) {
self.signers.push(signer);
}
self
}
pub fn compute_units(mut self, units: u32) -> Self {
self.config.compute_units = Some(units);
self
}
pub fn priority_fee(mut self, micro_lamports: u64) -> Self {
self.config.priority_fee_micro_lamports = Some(micro_lamports);
self
}
pub fn instruction_count(&self) -> usize {
self.instructions.len()
}
pub fn estimate_accounts(&self) -> usize {
let mut accounts = std::collections::HashSet::new();
for ix in &self.instructions {
accounts.insert(ix.program_id);
for meta in &ix.accounts {
accounts.insert(meta.pubkey);
}
}
accounts.len()
}
pub fn might_exceed_limits(&self) -> bool {
self.estimate_accounts() >= MAX_ACCOUNTS_PER_TX - 5
}
pub fn build_instructions(self) -> Vec<Instruction> {
let mut instructions = Vec::new();
if let Some(units) = self.config.compute_units {
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(units));
}
if let Some(fee) = self.config.priority_fee_micro_lamports {
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(fee));
}
instructions.extend(self.instructions);
instructions
}
pub fn build(self, payer: &Pubkey, _recent_blockhash: Hash) -> Transaction {
let instructions = self.build_instructions();
let message = Message::new(&instructions, Some(payer));
Transaction::new_unsigned(message)
}
}
pub struct BatchExecutor {
client: RpcClient,
config: TransactionConfig,
}
impl BatchExecutor {
pub fn new(rpc_url: &str) -> Self {
Self {
client: RpcClient::new_with_commitment(
rpc_url.to_string(),
CommitmentConfig::confirmed(),
),
config: TransactionConfig::default(),
}
}
pub fn with_config(rpc_url: &str, config: TransactionConfig) -> Self {
Self {
client: RpcClient::new_with_commitment(rpc_url.to_string(), config.commitment),
config,
}
}
pub async fn execute_transaction(
&self,
instructions: Vec<Instruction>,
signers: &[&Keypair],
) -> Result<Signature> {
if signers.is_empty() {
return Err(ToolkitError::SigningError("No signers provided".to_string()));
}
let payer = signers[0];
let mut all_instructions = Vec::new();
if let Some(units) = self.config.compute_units {
all_instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(units));
}
if let Some(fee) = self.config.priority_fee_micro_lamports {
all_instructions.push(ComputeBudgetInstruction::set_compute_unit_price(fee));
}
all_instructions.extend(instructions);
let recent_blockhash = self.client.get_latest_blockhash().await?;
let message = Message::new(&all_instructions, Some(&payer.pubkey()));
let transaction = Transaction::new(signers, message, recent_blockhash);
if self.config.simulate_before_send {
let sim_result = self.client.simulate_transaction(&transaction).await?;
if let Some(err) = sim_result.value.err {
return Err(ToolkitError::TransactionError(format!(
"Simulation failed: {:?}",
err
)));
}
}
let mut last_error = None;
for attempt in 0..=self.config.max_retries {
match self.client.send_and_confirm_transaction(&transaction).await {
Ok(sig) => return Ok(sig),
Err(e) => {
last_error = Some(e);
if attempt < self.config.max_retries {
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
}
}
}
}
Err(ToolkitError::TransactionError(format!(
"Failed after {} retries: {:?}",
self.config.max_retries,
last_error
)))
}
pub fn split_into_batches(&self, instructions: Vec<Instruction>, max_per_batch: usize) -> Vec<Vec<Instruction>> {
let effective_max = max_per_batch.min(MAX_ACCOUNTS_PER_TX / 4); instructions.chunks(effective_max).map(|c| c.to_vec()).collect()
}
pub async fn execute_batches(
&self,
instruction_batches: Vec<Vec<Instruction>>,
signers: &[&Keypair],
) -> Result<BatchResult> {
let mut result = BatchResult {
successful: Vec::new(),
failed: Vec::new(),
instructions_processed: 0,
};
for (batch_idx, instructions) in instruction_batches.into_iter().enumerate() {
let ix_count = instructions.len();
match self.execute_transaction(instructions, signers).await {
Ok(sig) => {
result.successful.push(sig);
result.instructions_processed += ix_count;
}
Err(e) => {
result.failed.push((batch_idx, e.to_string()));
}
}
}
Ok(result)
}
pub async fn execute_all(
&self,
instructions: Vec<Instruction>,
signers: &[&Keypair],
max_per_batch: usize,
) -> Result<BatchResult> {
let batches = self.split_into_batches(instructions, max_per_batch);
self.execute_batches(batches, signers).await
}
}
pub struct ParallelBatchExecutor {
client: RpcClient,
config: TransactionConfig,
max_concurrent: usize,
}
impl ParallelBatchExecutor {
pub fn new(rpc_url: &str, max_concurrent: usize) -> Self {
Self {
client: RpcClient::new_with_commitment(
rpc_url.to_string(),
CommitmentConfig::confirmed(),
),
config: TransactionConfig::default(),
max_concurrent,
}
}
pub fn with_config(rpc_url: &str, config: TransactionConfig, max_concurrent: usize) -> Self {
Self {
client: RpcClient::new_with_commitment(rpc_url.to_string(), config.commitment),
config,
max_concurrent,
}
}
pub async fn execute_parallel(
&self,
transaction_instructions: Vec<Vec<Instruction>>,
signers: &[&Keypair],
) -> Result<BatchResult> {
use futures::stream::{self, StreamExt};
if signers.is_empty() {
return Err(ToolkitError::SigningError("No signers provided".to_string()));
}
let payer = signers[0];
let recent_blockhash = self.client.get_latest_blockhash().await?;
let transactions: Vec<_> = transaction_instructions
.iter()
.map(|instructions| {
let mut all_instructions = Vec::new();
if let Some(units) = self.config.compute_units {
all_instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(units));
}
if let Some(fee) = self.config.priority_fee_micro_lamports {
all_instructions.push(ComputeBudgetInstruction::set_compute_unit_price(fee));
}
all_instructions.extend(instructions.clone());
let message = Message::new(&all_instructions, Some(&payer.pubkey()));
Transaction::new(signers, message, recent_blockhash)
})
.collect();
let results: Vec<_> = stream::iter(transactions.into_iter().enumerate())
.map(|(idx, tx)| {
let client = &self.client;
async move {
match client.send_and_confirm_transaction(&tx).await {
Ok(sig) => (idx, Ok(sig)),
Err(e) => (idx, Err(e.to_string())),
}
}
})
.buffer_unordered(self.max_concurrent)
.collect()
.await;
let mut batch_result = BatchResult {
successful: Vec::new(),
failed: Vec::new(),
instructions_processed: 0,
};
for (idx, result) in results {
let idx: usize = idx;
match result {
Ok(sig) => {
batch_result.successful.push(sig);
batch_result.instructions_processed += transaction_instructions[idx].len();
}
Err(e) => {
batch_result.failed.push((idx, e));
}
}
}
Ok(batch_result)
}
}
pub fn estimate_transaction_size(instructions: &[Instruction], num_signers: usize) -> usize {
let mut size = 0;
size += num_signers * 64;
size += 3;
let mut accounts = std::collections::HashSet::new();
for ix in instructions {
accounts.insert(ix.program_id);
for meta in &ix.accounts {
accounts.insert(meta.pubkey);
}
}
size += accounts.len() * 32;
size += 32;
size += 1;
for ix in instructions {
size += 1; size += 1; size += ix.accounts.len(); size += 2; size += ix.data.len(); }
size
}
pub fn will_fit_in_transaction(instructions: &[Instruction], num_signers: usize) -> bool {
estimate_transaction_size(instructions, num_signers) <= MAX_TRANSACTION_SIZE
}
#[cfg(test)]
mod tests {
use super::*;
use solana_sdk::system_instruction;
#[test]
fn test_transaction_config_default() {
let config = TransactionConfig::default();
assert!(config.simulate_before_send);
assert!(!config.skip_preflight);
assert_eq!(config.max_retries, 3);
}
#[test]
fn test_transaction_config_fast() {
let config = TransactionConfig::fast();
assert!(!config.simulate_before_send);
assert!(config.skip_preflight);
}
#[test]
fn test_transaction_builder() {
let payer = Pubkey::new_unique();
let to = Pubkey::new_unique();
let builder = TransactionBuilder::new()
.add_instruction(system_instruction::transfer(&payer, &to, 1000))
.compute_units(100_000)
.priority_fee(1000);
assert_eq!(builder.instruction_count(), 1);
}
#[test]
fn test_batch_result() {
let result = BatchResult {
successful: vec![Signature::default()],
failed: vec![],
instructions_processed: 5,
};
assert!(result.all_succeeded());
assert_eq!(result.success_rate(), 100.0);
}
#[test]
fn test_estimate_transaction_size() {
let from = Pubkey::new_unique();
let to = Pubkey::new_unique();
let instructions = vec![system_instruction::transfer(&from, &to, 1000)];
let size = estimate_transaction_size(&instructions, 1);
assert!(size > 0);
assert!(size < MAX_TRANSACTION_SIZE);
}
}