Files
sol-trade-sdk/src/trading/common/nonce_manager.rs
T

41 lines
1.4 KiB
Rust
Raw Normal View History

use solana_hash::Hash;
2025-09-19 18:02:11 +08:00
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair, signer::Signer};
use solana_system_interface::instruction::advance_nonce_account;
2025-09-20 14:00:45 +08:00
use crate::common::nonce_cache::DurableNonceInfo;
/// Add nonce advance instruction to the instruction set
///
/// 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
pub fn add_nonce_instruction(
instructions: &mut Vec<Instruction>,
payer: &Keypair,
2025-09-20 14:00:45 +08:00
// nonce_account: Option<Pubkey>,
// current_nonce: Option<Hash>,
durable_nonce: Option<DurableNonceInfo>,
) -> Result<(), anyhow::Error> {
2025-09-20 14:00:45 +08:00
if let Some(durable_nonce) = durable_nonce {
let nonce_advance_ix = advance_nonce_account(&durable_nonce.nonce_account.unwrap(), &payer.pubkey());
instructions.push(nonce_advance_ix);
}
2025-09-20 14:00:45 +08:00
Ok(())
}
/// 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(
2025-09-20 14:00:45 +08:00
recent_blockhash: Option<Hash>,
durable_nonce: Option<DurableNonceInfo>,
// nonce_account: Option<Pubkey>,
// current_nonce: Option<Hash>,
2025-09-19 18:02:11 +08:00
) -> Hash {
2025-09-20 14:00:45 +08:00
if let Some(durable_nonce) = durable_nonce {
durable_nonce.current_nonce.unwrap()
} else {
2025-09-20 14:00:45 +08:00
recent_blockhash.unwrap()
}
}