wallet_adapter/commitment.rs
1use crate::WalletError;
2
3/// The commitment level of a Solana transaction.
4///
5/// Note that deprecated commitments are converted into supported commitments.
6///
7/// `recent` is parsed as `processed`
8///
9/// `single` and `singleGossip` are parsed as `confirmed`
10///
11/// `root` and `max` are parsed as `finalized`,
12#[derive(Debug, PartialEq, Eq, Default, PartialOrd, Ord, Clone, Copy, Hash)]
13pub enum Commitment {
14 /// A transaction has been validated and recorded in the blockchain by a single node
15 Processed,
16 /// A transaction has been validated and recorded by a majority of nodes in the Solana cluster.
17 Confirmed,
18 /// A has been included in a block that has been committed to the blockchain by the Solana cluster
19 /// and is now irreversible.
20 #[default]
21 Finalized,
22}
23
24impl Commitment {
25 /// Get the commitment as a [str] format
26 pub fn as_str(&self) -> &str {
27 match self {
28 Self::Processed => "processed",
29 Self::Confirmed => "confirmed",
30 Self::Finalized => "finalized",
31 }
32 }
33}
34
35impl TryFrom<&str> for Commitment {
36 type Error = WalletError;
37
38 fn try_from(value: &str) -> Result<Self, Self::Error> {
39 let converted = match value {
40 "processed" | "recent" => Self::Processed,
41 "confirmed" | "single" | "singleGossip" => Self::Confirmed,
42 "finalized" | "root" | "max" => Self::Finalized,
43 _ => return Err(WalletError::UnsupportedCommitment(value.to_string())),
44 };
45
46 Ok(converted)
47 }
48}