safecoin_client/
nonce_utils.rs

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