pub trait RpcClientExt {
// Required methods
fn estimate_compute_units_unsigned_tx<'a, I: Signers + ?Sized>(
&self,
transaction: &Transaction,
_signers: &'a I,
) -> Result<Vec<u64>, Box<dyn Error + 'static>>;
fn estimate_compute_units_msg<'a, I: Signers + ?Sized>(
&self,
msg: &Message,
signers: &'a I,
) -> Result<u64, Box<dyn Error + 'static>>;
fn optimize_compute_units_unsigned_tx<'a, I: Signers + ?Sized>(
&self,
unsigned_transaction: &mut Transaction,
signers: &'a I,
) -> Result<u32, Box<dyn Error + 'static>>;
fn optimize_compute_units_msg<'a, I: Signers + ?Sized>(
&self,
message: &mut Message,
signers: &'a I,
) -> Result<u32, Box<dyn Error + 'static>>;
}Required Methods§
Sourcefn estimate_compute_units_unsigned_tx<'a, I: Signers + ?Sized>(
&self,
transaction: &Transaction,
_signers: &'a I,
) -> Result<Vec<u64>, Box<dyn Error + 'static>>
fn estimate_compute_units_unsigned_tx<'a, I: Signers + ?Sized>( &self, transaction: &Transaction, _signers: &'a I, ) -> Result<Vec<u64>, Box<dyn Error + 'static>>
Estimates compute units for an unsigned transaction
Returns a vector of compute unit values for each transaction processed. If any transaction fails, returns an error with detailed failure information.
Sourcefn estimate_compute_units_msg<'a, I: Signers + ?Sized>(
&self,
msg: &Message,
signers: &'a I,
) -> Result<u64, Box<dyn Error + 'static>>
fn estimate_compute_units_msg<'a, I: Signers + ?Sized>( &self, msg: &Message, signers: &'a I, ) -> Result<u64, Box<dyn Error + 'static>>
Estimates compute units for a message
Simulates the transaction on the network to determine compute unit usage.
Sourcefn optimize_compute_units_unsigned_tx<'a, I: Signers + ?Sized>(
&self,
unsigned_transaction: &mut Transaction,
signers: &'a I,
) -> Result<u32, Box<dyn Error + 'static>>
fn optimize_compute_units_unsigned_tx<'a, I: Signers + ?Sized>( &self, unsigned_transaction: &mut Transaction, signers: &'a I, ) -> Result<u32, Box<dyn Error + 'static>>
Optimizes compute units for an unsigned transaction
Adds a compute budget instruction to the transaction to limit compute units to the optimal amount needed based on simulation.
Sourcefn optimize_compute_units_msg<'a, I: Signers + ?Sized>(
&self,
message: &mut Message,
signers: &'a I,
) -> Result<u32, Box<dyn Error + 'static>>
fn optimize_compute_units_msg<'a, I: Signers + ?Sized>( &self, message: &mut Message, signers: &'a I, ) -> Result<u32, Box<dyn Error + 'static>>
Optimizes compute units for a message
Adds a compute budget instruction to the message to limit compute units to the optimal amount needed based on simulation.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl RpcClientExt for RpcClient
impl RpcClientExt for RpcClient
Source§fn optimize_compute_units_msg<'a, I: Signers + ?Sized>(
&self,
message: &mut Message,
signers: &'a I,
) -> Result<u32, Box<dyn Error + 'static>>
fn optimize_compute_units_msg<'a, I: Signers + ?Sized>( &self, message: &mut Message, signers: &'a I, ) -> Result<u32, Box<dyn Error + 'static>>
Simulates the transaction to get compute units used for the transaction and adds an instruction to the message to request only the required compute units from the ComputeBudget program to complete the transaction with this Message.
use solana_client::rpc_client::RpcClient;
use solana_client_ext::RpcClientExt;
use solana_sdk::{
message::Message, signature::Keypair, signer::Signer, system_instruction,
transaction::Transaction,
};
fn main() {
let rpc_client = RpcClient::new("https://api.devnet.solana.com");
let keypair = Keypair::new();
let keypair2 = Keypair::new();
let created_ix = system_instruction::transfer(&keypair.pubkey(), &keypair2.pubkey(), 10000);
let mut msg = Message::new(&[created_ix], Some(&keypair.pubkey()));
let optimized_cu = rpc_client
.optimize_compute_units_msg(&mut msg, &[&keypair])
.unwrap();
println!("Optimized compute units: {}", optimized_cu);
let tx = Transaction::new(&[&keypair], msg, rpc_client.get_latest_blockhash().unwrap());
let result = rpc_client
.send_and_confirm_transaction_with_spinner(&tx)
.unwrap();
println!(
"Transaction signature: https://explorer.solana.com/tx/{}?cluster=devnet",
result
);
}