2025-06-17 23:32:20 +08:00
|
|
|
use solana_hash::Hash;
|
2025-09-19 18:02:11 +08:00
|
|
|
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair, signer::Signer};
|
2025-08-03 22:51:11 +08:00
|
|
|
use solana_system_interface::instruction::advance_nonce_account;
|
2025-06-17 23:32:20 +08:00
|
|
|
|
2025-09-04 16:32:34 +08:00
|
|
|
/// Add nonce advance instruction to the instruction set
|
2025-06-17 23:32:20 +08:00
|
|
|
///
|
2025-09-04 16:32:34 +08:00
|
|
|
/// Nonce functionality is only used when nonce_pubkey is provided
|
|
|
|
|
/// Returns error if nonce is locked, already used, or not ready
|
|
|
|
|
/// On success, locks and marks nonce as used
|
2025-06-17 23:32:20 +08:00
|
|
|
pub fn add_nonce_instruction(
|
2025-08-03 22:51:11 +08:00
|
|
|
instructions: &mut Vec<Instruction>,
|
|
|
|
|
payer: &Keypair,
|
2025-09-19 18:02:11 +08:00
|
|
|
nonce_account: Option<Pubkey>,
|
|
|
|
|
current_nonce: Option<Hash>,
|
2025-06-17 23:32:20 +08:00
|
|
|
) -> Result<(), anyhow::Error> {
|
2025-09-19 18:02:11 +08:00
|
|
|
if nonce_account.is_some() && current_nonce.is_some() {
|
|
|
|
|
let nonce_advance_ix = advance_nonce_account(&nonce_account.unwrap(), &payer.pubkey());
|
2025-06-17 23:32:20 +08:00
|
|
|
instructions.push(nonce_advance_ix);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 16:32:34 +08:00
|
|
|
/// Get blockhash for transaction
|
|
|
|
|
/// If nonce account is used, return blockhash from nonce, otherwise return the provided recent_blockhash
|
2025-09-19 18:02:11 +08:00
|
|
|
pub fn get_transaction_blockhash(
|
|
|
|
|
recent_blockhash: Hash,
|
|
|
|
|
nonce_account: Option<Pubkey>,
|
|
|
|
|
current_nonce: Option<Hash>,
|
|
|
|
|
) -> Hash {
|
|
|
|
|
if nonce_account.is_some() && current_nonce.is_some() {
|
|
|
|
|
current_nonce.unwrap()
|
2025-06-17 23:32:20 +08:00
|
|
|
} else {
|
|
|
|
|
recent_blockhash
|
|
|
|
|
}
|
|
|
|
|
}
|