2025-10-07 21:20:00 +08:00
|
|
|
use crate::common::SolanaRpcClient;
|
2025-09-04 16:32:34 +08:00
|
|
|
use solana_hash::Hash;
|
2025-05-29 18:53:58 +08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2025-09-04 16:32:34 +08:00
|
|
|
use tracing::error;
|
2025-05-29 18:53:58 +08:00
|
|
|
|
2025-09-20 14:00:45 +08:00
|
|
|
/// DurableNonceInfo structure to store durable nonce-related information
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct DurableNonceInfo {
|
|
|
|
|
/// Nonce account address
|
|
|
|
|
pub nonce_account: Option<Pubkey>,
|
|
|
|
|
/// Current nonce value
|
|
|
|
|
pub current_nonce: Option<Hash>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 21:20:00 +08:00
|
|
|
/// Fetch nonce information using RPC
|
|
|
|
|
pub async fn fetch_nonce_info(
|
|
|
|
|
rpc: &SolanaRpcClient,
|
|
|
|
|
nonce_account: Pubkey,
|
|
|
|
|
) -> Option<DurableNonceInfo> {
|
|
|
|
|
match rpc.get_account(&nonce_account).await {
|
2026-04-11 18:43:18 +08:00
|
|
|
Ok(account) => {
|
|
|
|
|
// Parse nonce account manually: first 4 bytes is version, then 4 bytes authority type
|
|
|
|
|
// For initialized nonce: version=0, authority_type=0, then authority (32 bytes), then blockhash (32 bytes), then fee_calculator
|
|
|
|
|
if account.data.len() >= 80 {
|
|
|
|
|
// Skip version (4) + authority_type (4) + authority (32) = 40 bytes
|
|
|
|
|
// Then blockhash is at offset 40
|
|
|
|
|
let blockhash_bytes: [u8; 32] = account.data[40..72].try_into().ok()?;
|
|
|
|
|
return Some(DurableNonceInfo {
|
|
|
|
|
nonce_account: Some(nonce_account),
|
|
|
|
|
current_nonce: Some(Hash::from(blockhash_bytes)),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
error!("Nonce account data too short");
|
2025-09-04 16:32:34 +08:00
|
|
|
}
|
2026-04-11 18:43:18 +08:00
|
|
|
}
|
2025-10-07 21:20:00 +08:00
|
|
|
Err(e) => {
|
|
|
|
|
error!("Failed to get nonce account information: {:?}", e);
|
2025-09-04 16:32:34 +08:00
|
|
|
}
|
2025-05-29 18:53:58 +08:00
|
|
|
}
|
2025-10-07 21:20:00 +08:00
|
|
|
None
|
2025-05-29 18:53:58 +08:00
|
|
|
}
|