use crate::speed::FastRpcClient;
use crate::Result;
use solana_sdk::{
hash::Hash,
instruction::Instruction,
message::Message,
pubkey::Pubkey,
signature::Keypair,
signer::Signer,
system_instruction,
transaction::Transaction,
};
use spl_associated_token_account::get_associated_token_address;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
const DEFAULT_BLOCKHASH_REFRESH_MS: u64 = 400;
#[derive(Debug, Clone)]
struct CachedBlockhash {
hash: Hash,
fetched_at: Instant,
}
impl CachedBlockhash {
fn new(hash: Hash) -> Self {
Self {
hash,
fetched_at: Instant::now(),
}
}
fn is_valid(&self) -> bool {
self.fetched_at.elapsed() < Duration::from_secs(30)
}
}
pub struct FastTransactionBuilder {
payer: Arc<Keypair>,
cached_blockhash: RwLock<Option<CachedBlockhash>>,
blockhash_refresh_ms: u64,
refresh_handle: RwLock<Option<tokio::task::JoinHandle<()>>>,
}
impl FastTransactionBuilder {
pub fn new(payer: Keypair) -> Self {
Self {
payer: Arc::new(payer),
cached_blockhash: RwLock::new(None),
blockhash_refresh_ms: DEFAULT_BLOCKHASH_REFRESH_MS,
refresh_handle: RwLock::new(None),
}
}
pub fn with_refresh_interval(mut self, refresh_ms: u64) -> Self {
self.blockhash_refresh_ms = refresh_ms;
self
}
pub fn payer(&self) -> Pubkey {
self.payer.pubkey()
}
pub async fn start_blockhash_refresh(&self, rpc: &FastRpcClient) {
self.stop_blockhash_refresh().await;
if let Ok(hash) = rpc.get_latest_blockhash_fast().await {
let mut guard = self.cached_blockhash.write().await;
*guard = Some(CachedBlockhash::new(hash));
}
}
pub async fn stop_blockhash_refresh(&self) {
let mut handle = self.refresh_handle.write().await;
if let Some(h) = handle.take() {
h.abort();
}
}
pub async fn refresh_blockhash(&self, rpc: &FastRpcClient) -> Result<Hash> {
let hash = rpc.get_latest_blockhash_fast().await?;
let mut guard = self.cached_blockhash.write().await;
*guard = Some(CachedBlockhash::new(hash));
Ok(hash)
}
pub async fn get_cached_blockhash(&self) -> Hash {
let guard = self.cached_blockhash.read().await;
match &*guard {
Some(cached) if cached.is_valid() => cached.hash,
_ => Hash::default(),
}
}
pub async fn has_valid_blockhash(&self) -> bool {
let guard = self.cached_blockhash.read().await;
matches!(&*guard, Some(cached) if cached.is_valid())
}
pub async fn build_sol_transfer(&self, to: &Pubkey, lamports: u64) -> Transaction {
let ix = system_instruction::transfer(&self.payer.pubkey(), to, lamports);
self.build_transaction(vec![ix]).await
}
pub async fn build_sol_transfer_with_memo(
&self,
to: &Pubkey,
lamports: u64,
memo: &str,
) -> Transaction {
let transfer_ix = system_instruction::transfer(&self.payer.pubkey(), to, lamports);
let memo_ix = spl_memo_instruction(memo);
self.build_transaction(vec![transfer_ix, memo_ix]).await
}
pub async fn build_token_transfer(
&self,
mint: &Pubkey,
from_ata: &Pubkey,
to_ata: &Pubkey,
amount: u64,
) -> Transaction {
let ix = spl_token::instruction::transfer(
&spl_token::id(),
from_ata,
to_ata,
&self.payer.pubkey(),
&[],
amount,
)
.expect("Failed to create transfer instruction");
self.build_transaction(vec![ix]).await
}
pub async fn build_token_transfer_with_ata(
&self,
mint: &Pubkey,
to_owner: &Pubkey,
amount: u64,
) -> Transaction {
let from_ata = self.get_ata(&self.payer.pubkey(), mint);
let to_ata = self.get_ata(to_owner, mint);
let create_ata_ix = spl_associated_token_account::instruction::create_associated_token_account_idempotent(
&self.payer.pubkey(),
to_owner,
mint,
&spl_token::id(),
);
let transfer_ix = spl_token::instruction::transfer(
&spl_token::id(),
&from_ata,
&to_ata,
&self.payer.pubkey(),
&[],
amount,
)
.expect("Failed to create transfer instruction");
self.build_transaction(vec![create_ata_ix, transfer_ix]).await
}
pub fn get_ata(&self, owner: &Pubkey, mint: &Pubkey) -> Pubkey {
get_associated_token_address(owner, mint)
}
pub async fn build_transaction(&self, instructions: Vec<Instruction>) -> Transaction {
let blockhash = self.get_cached_blockhash().await;
let message = Message::new(&instructions, Some(&self.payer.pubkey()));
let mut tx = Transaction::new_unsigned(message);
tx.message.recent_blockhash = blockhash;
tx
}
pub async fn build_and_sign(&self, instructions: Vec<Instruction>) -> Transaction {
let blockhash = self.get_cached_blockhash().await;
let message = Message::new(&instructions, Some(&self.payer.pubkey()));
Transaction::new(&[self.payer.as_ref()], message, blockhash)
}
pub fn sign_transaction(&self, tx: &mut Transaction, blockhash: Hash) {
tx.message.recent_blockhash = blockhash;
tx.sign(&[self.payer.as_ref()], blockhash);
}
pub async fn build_with_priority_fee(
&self,
instructions: Vec<Instruction>,
compute_units: u32,
priority_fee_microlamports: u64,
) -> Transaction {
let mut all_instructions = Vec::with_capacity(instructions.len() + 2);
all_instructions.push(
solana_sdk::compute_budget::ComputeBudgetInstruction::set_compute_unit_limit(compute_units),
);
all_instructions.push(
solana_sdk::compute_budget::ComputeBudgetInstruction::set_compute_unit_price(priority_fee_microlamports),
);
all_instructions.extend(instructions);
self.build_transaction(all_instructions).await
}
pub async fn build_multi_transfer(&self, transfers: Vec<(Pubkey, u64)>) -> Transaction {
let instructions: Vec<_> = transfers
.into_iter()
.map(|(to, lamports)| system_instruction::transfer(&self.payer.pubkey(), &to, lamports))
.collect();
self.build_transaction(instructions).await
}
pub fn estimate_fee(&self, instruction_count: usize) -> u64 {
5000 * (instruction_count as u64 + 1)
}
}
fn spl_memo_instruction(memo: &str) -> Instruction {
Instruction {
program_id: spl_memo_program_id(),
accounts: vec![],
data: memo.as_bytes().to_vec(),
}
}
fn spl_memo_program_id() -> Pubkey {
"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"
.parse()
.unwrap()
}
#[derive(Debug, Clone)]
pub struct TransactionTemplate {
pub instructions: Vec<Instruction>,
pub compute_units: u32,
pub description: String,
}
impl TransactionTemplate {
pub fn new(instructions: Vec<Instruction>, compute_units: u32, description: &str) -> Self {
Self {
instructions,
compute_units,
description: description.to_string(),
}
}
pub async fn build(&self, builder: &FastTransactionBuilder) -> Transaction {
builder.build_transaction(self.instructions.clone()).await
}
pub async fn build_with_priority(
&self,
builder: &FastTransactionBuilder,
priority_fee_microlamports: u64,
) -> Transaction {
builder
.build_with_priority_fee(
self.instructions.clone(),
self.compute_units,
priority_fee_microlamports,
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use solana_sdk::native_token::LAMPORTS_PER_SOL;
#[test]
fn test_get_ata() {
let payer = Keypair::new();
let builder = FastTransactionBuilder::new(payer);
let owner = Pubkey::new_unique();
let mint = Pubkey::new_unique();
let ata = builder.get_ata(&owner, &mint);
let expected = get_associated_token_address(&owner, &mint);
assert_eq!(ata, expected);
}
#[tokio::test]
async fn test_build_sol_transfer() {
let payer = Keypair::new();
let builder = FastTransactionBuilder::new(payer);
let recipient = Pubkey::new_unique();
let tx = builder.build_sol_transfer(&recipient, LAMPORTS_PER_SOL).await;
assert_eq!(tx.message.instructions.len(), 1);
}
#[tokio::test]
async fn test_build_multi_transfer() {
let payer = Keypair::new();
let builder = FastTransactionBuilder::new(payer);
let transfers = vec![
(Pubkey::new_unique(), 1_000_000),
(Pubkey::new_unique(), 2_000_000),
(Pubkey::new_unique(), 3_000_000),
];
let tx = builder.build_multi_transfer(transfers).await;
assert_eq!(tx.message.instructions.len(), 3);
}
#[tokio::test]
async fn test_build_with_priority_fee() {
let payer = Keypair::new();
let builder = FastTransactionBuilder::new(payer);
let recipient = Pubkey::new_unique();
let ix = system_instruction::transfer(&builder.payer(), &recipient, 1_000_000);
let tx = builder
.build_with_priority_fee(vec![ix], 200_000, 5000)
.await;
assert_eq!(tx.message.instructions.len(), 3);
}
#[test]
fn test_estimate_fee() {
let payer = Keypair::new();
let builder = FastTransactionBuilder::new(payer);
let fee = builder.estimate_fee(1);
assert_eq!(fee, 10000); }
#[tokio::test]
async fn test_transaction_template() {
let payer = Keypair::new();
let builder = FastTransactionBuilder::new(payer);
let recipient = Pubkey::new_unique();
let ix = system_instruction::transfer(&builder.payer(), &recipient, 1_000_000);
let template = TransactionTemplate::new(vec![ix], 200_000, "SOL transfer");
let tx = template.build(&builder).await;
assert_eq!(tx.message.instructions.len(), 1);
}
}