solana_rpc_client_nonce_utils/
lib.rs

1//! Durable transaction nonce helpers.
2
3pub mod blockhash_query;
4pub mod nonblocking;
5
6pub use crate::nonblocking::{
7    account_identity_ok, data_from_account, data_from_state, state_from_account, Error,
8};
9use {
10    solana_rpc_client::rpc_client::RpcClient,
11    solana_sdk::{account::Account, commitment_config::CommitmentConfig, pubkey::Pubkey},
12};
13
14/// Get a nonce account from the network.
15///
16/// This is like [`RpcClient::get_account`] except:
17///
18/// - it returns this module's [`Error`] type,
19/// - it returns an error if any of the checks from [`account_identity_ok`] fail.
20pub fn get_account(rpc_client: &RpcClient, nonce_pubkey: &Pubkey) -> Result<Account, Error> {
21    get_account_with_commitment(rpc_client, nonce_pubkey, CommitmentConfig::default())
22}
23
24/// Get a nonce account from the network.
25///
26/// This is like [`RpcClient::get_account_with_commitment`] except:
27///
28/// - it returns this module's [`Error`] type,
29/// - it returns an error if the account does not exist,
30/// - it returns an error if any of the checks from [`account_identity_ok`] fail.
31pub fn get_account_with_commitment(
32    rpc_client: &RpcClient,
33    nonce_pubkey: &Pubkey,
34    commitment: CommitmentConfig,
35) -> Result<Account, Error> {
36    rpc_client
37        .get_account_with_commitment(nonce_pubkey, commitment)
38        .map_err(|e| Error::Client(format!("{e}")))
39        .and_then(|result| {
40            result
41                .value
42                .ok_or_else(|| Error::Client(format!("AccountNotFound: pubkey={nonce_pubkey}")))
43        })
44        .and_then(|a| account_identity_ok(&a).map(|()| a))
45}