1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Module for interacting with Solana [nonce accounts](https://solana.com/de/developers/guides/advanced/introduction-to-durable-nonces#nonce-account).
use UiAccount;
use ;
use system_program;
use Error;
/// Extracts the durable nonce value from the response of a `getAccountInfo` RPC call.
///
/// # Examples
///
/// ```rust
/// use sol_rpc_client::{nonce::nonce_from_account, SolRpcClient};
/// use sol_rpc_types::{RpcSources, SolanaCluster};
/// use solana_hash::Hash;
/// use solana_pubkey::pubkey;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use std::str::FromStr;
/// # use sol_rpc_client::fixtures::nonce_account;
/// # use sol_rpc_types::{AccountData, AccountEncoding, AccountInfo, MultiRpcResult};
/// let client = SolRpcClient::builder_for_ic()
/// # .with_stub_response(MultiRpcResult::Consistent(Ok(Some(nonce_account()))))
/// .with_rpc_sources(RpcSources::Default(SolanaCluster::Devnet))
/// .build();
///
/// let nonce_account = client
/// .get_account_info(pubkey!("8DedqKHx9ogFajbHtRnTM3pPr3MRyVKDtepEpUiaDXX"))
/// .send()
/// .await
/// .expect_consistent()
/// .unwrap()
/// .unwrap();
///
/// let durable_nonce = nonce_from_account(&nonce_account)
/// .unwrap();
///
/// assert_eq!(durable_nonce, Hash::from_str("6QK3LC8dsRtH2qVU47cSvgchPHNU72f1scvg2LuN2z7e").unwrap());
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// The method will return an instance of [`ExtractNonceError`] if the account data does not
/// correspond to a valid and properly encoded nonce account. See [`ExtractNonceError`] for
/// more details.
///
/// ```rust
/// use sol_rpc_client::{nonce::{ExtractNonceError, nonce_from_account}, SolRpcClient};
/// use sol_rpc_types::{RpcSources, SolanaCluster};
/// use solana_hash::Hash;
/// use solana_pubkey::pubkey;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use std::str::FromStr;
/// # use assert_matches::assert_matches;
/// # use sol_rpc_client::fixtures::usdc_account;
/// # use sol_rpc_types::{AccountData, AccountEncoding, AccountInfo, MultiRpcResult};
/// let client = SolRpcClient::builder_for_ic()
/// # .with_stub_response(MultiRpcResult::Consistent(Ok(Some(usdc_account()))))
/// .with_rpc_sources(RpcSources::Default(SolanaCluster::Mainnet))
/// .build();
///
/// // Fetch the USDC account data on Mainnet
/// let usdc_account = client
/// .get_account_info(pubkey!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"))
/// .send()
/// .await
/// .expect_consistent()
/// .unwrap()
/// .unwrap();
///
/// let durable_nonce = nonce_from_account(&usdc_account);
///
/// assert_matches!(durable_nonce, Err(ExtractNonceError::InvalidAccountOwner(_)));
/// # Ok(())
/// # }
/// ```
/// Errors that might happen when calling the [`nonce_from_account`] method.