feat: add flexible nonce parameter support
- Add nonce_account and current_nonce to trade parameters - Remove hardcoded NonceCache dependency from nonce_manager - Update examples and documentation for new nonce usage - Fix nonce documentation errors and improve clarity
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
use anyhow::anyhow;
|
||||
use solana_hash::Hash;
|
||||
use solana_sdk::{instruction::Instruction, signature::Keypair, signer::Signer};
|
||||
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair, signer::Signer};
|
||||
use solana_system_interface::instruction::advance_nonce_account;
|
||||
|
||||
use crate::common::nonce_cache::NonceCache;
|
||||
|
||||
/// Add nonce advance instruction to the instruction set
|
||||
///
|
||||
/// Nonce functionality is only used when nonce_pubkey is provided
|
||||
@@ -13,36 +10,25 @@ use crate::common::nonce_cache::NonceCache;
|
||||
pub fn add_nonce_instruction(
|
||||
instructions: &mut Vec<Instruction>,
|
||||
payer: &Keypair,
|
||||
nonce_account: Option<Pubkey>,
|
||||
current_nonce: Option<Hash>,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let nonce_cache = NonceCache::get_instance();
|
||||
let nonce_info = nonce_cache.get_nonce_info();
|
||||
|
||||
// Only check if nonce_account exists
|
||||
if let Some(nonce_pubkey) = nonce_info.nonce_account {
|
||||
if nonce_info.used {
|
||||
return Err(anyhow!("Nonce is used"));
|
||||
}
|
||||
if nonce_info.current_nonce == Hash::default() {
|
||||
return Err(anyhow!("Nonce is not ready"));
|
||||
}
|
||||
|
||||
// Create Solana system nonce advance instruction - using system program ID
|
||||
let nonce_advance_ix = advance_nonce_account(&nonce_pubkey, &payer.pubkey());
|
||||
|
||||
if nonce_account.is_some() && current_nonce.is_some() {
|
||||
let nonce_advance_ix = advance_nonce_account(&nonce_account.unwrap(), &payer.pubkey());
|
||||
instructions.push(nonce_advance_ix);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get blockhash for transaction
|
||||
/// If nonce account is used, return blockhash from nonce, otherwise return the provided recent_blockhash
|
||||
pub fn get_transaction_blockhash(recent_blockhash: Hash) -> Hash {
|
||||
let nonce_cache = NonceCache::get_instance();
|
||||
let nonce_info = nonce_cache.get_nonce_info();
|
||||
|
||||
if nonce_info.nonce_account.is_some() {
|
||||
nonce_info.current_nonce
|
||||
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()
|
||||
} else {
|
||||
recent_blockhash
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user