solana_sdk/
commitment_config.rs1#![cfg(feature = "full")]
4
5use {std::str::FromStr, thiserror::Error};
6
7#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, PartialEq, Eq, Hash)]
8#[serde(rename_all = "camelCase")]
9pub struct CommitmentConfig {
10 pub commitment: CommitmentLevel,
11}
12
13impl CommitmentConfig {
14 pub const fn finalized() -> Self {
15 Self {
16 commitment: CommitmentLevel::Finalized,
17 }
18 }
19
20 pub const fn confirmed() -> Self {
21 Self {
22 commitment: CommitmentLevel::Confirmed,
23 }
24 }
25
26 pub const fn processed() -> Self {
27 Self {
28 commitment: CommitmentLevel::Processed,
29 }
30 }
31
32 pub fn ok(self) -> Option<Self> {
33 if self == Self::default() {
34 None
35 } else {
36 Some(self)
37 }
38 }
39
40 pub fn is_finalized(&self) -> bool {
41 self.commitment == CommitmentLevel::Finalized
42 }
43
44 pub fn is_confirmed(&self) -> bool {
45 self.commitment == CommitmentLevel::Confirmed
46 }
47
48 pub fn is_processed(&self) -> bool {
49 self.commitment == CommitmentLevel::Processed
50 }
51
52 pub fn is_at_least_confirmed(&self) -> bool {
53 self.is_confirmed() || self.is_finalized()
54 }
55
56 #[deprecated(
57 since = "2.0.2",
58 note = "Returns self. Please do not use. Will be removed in the future."
59 )]
60 pub fn use_deprecated_commitment(commitment: CommitmentConfig) -> Self {
61 commitment
62 }
63}
64
65impl FromStr for CommitmentConfig {
66 type Err = ParseCommitmentLevelError;
67
68 fn from_str(s: &str) -> Result<Self, Self::Err> {
69 CommitmentLevel::from_str(s).map(|commitment| Self { commitment })
70 }
71}
72
73#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
74#[serde(rename_all = "camelCase")]
75pub enum CommitmentLevel {
80 Processed,
84
85 Confirmed,
90
91 Finalized,
94}
95
96impl Default for CommitmentLevel {
97 fn default() -> Self {
98 Self::Finalized
99 }
100}
101
102impl FromStr for CommitmentLevel {
103 type Err = ParseCommitmentLevelError;
104
105 fn from_str(s: &str) -> Result<Self, Self::Err> {
106 match s {
107 "processed" => Ok(CommitmentLevel::Processed),
108 "confirmed" => Ok(CommitmentLevel::Confirmed),
109 "finalized" => Ok(CommitmentLevel::Finalized),
110 _ => Err(ParseCommitmentLevelError::Invalid),
111 }
112 }
113}
114
115impl std::fmt::Display for CommitmentLevel {
116 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
117 let s = match self {
118 CommitmentLevel::Processed => "processed",
119 CommitmentLevel::Confirmed => "confirmed",
120 CommitmentLevel::Finalized => "finalized",
121 };
122 write!(f, "{s}")
123 }
124}
125
126#[derive(Error, Debug)]
127pub enum ParseCommitmentLevelError {
128 #[error("invalid variant")]
129 Invalid,
130}