gemachain_sdk/
commitment_config.rs1#![allow(deprecated)]
2#![cfg(feature = "full")]
3
4use std::str::FromStr;
5use 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 #[deprecated(
15 since = "1.5.5",
16 note = "Please use CommitmentConfig::processed() instead"
17 )]
18 pub fn recent() -> Self {
19 Self {
20 commitment: CommitmentLevel::Recent,
21 }
22 }
23
24 #[deprecated(
25 since = "1.5.5",
26 note = "Please use CommitmentConfig::finalized() instead"
27 )]
28 pub fn max() -> Self {
29 Self {
30 commitment: CommitmentLevel::Max,
31 }
32 }
33
34 #[deprecated(
35 since = "1.5.5",
36 note = "Please use CommitmentConfig::finalized() instead"
37 )]
38 pub fn root() -> Self {
39 Self {
40 commitment: CommitmentLevel::Root,
41 }
42 }
43
44 #[deprecated(
45 since = "1.5.5",
46 note = "Please use CommitmentConfig::confirmed() instead"
47 )]
48 pub fn single() -> Self {
49 Self {
50 commitment: CommitmentLevel::Single,
51 }
52 }
53
54 #[deprecated(
55 since = "1.5.5",
56 note = "Please use CommitmentConfig::confirmed() instead"
57 )]
58 pub fn single_gossip() -> Self {
59 Self {
60 commitment: CommitmentLevel::SingleGossip,
61 }
62 }
63
64 pub fn finalized() -> Self {
65 Self {
66 commitment: CommitmentLevel::Finalized,
67 }
68 }
69
70 pub fn confirmed() -> Self {
71 Self {
72 commitment: CommitmentLevel::Confirmed,
73 }
74 }
75
76 pub fn processed() -> Self {
77 Self {
78 commitment: CommitmentLevel::Processed,
79 }
80 }
81
82 pub fn ok(self) -> Option<Self> {
83 if self == Self::default() {
84 None
85 } else {
86 Some(self)
87 }
88 }
89
90 pub fn is_finalized(&self) -> bool {
91 matches!(
92 &self.commitment,
93 CommitmentLevel::Finalized | CommitmentLevel::Max | CommitmentLevel::Root
94 )
95 }
96
97 pub fn is_confirmed(&self) -> bool {
98 matches!(
99 &self.commitment,
100 CommitmentLevel::Confirmed | CommitmentLevel::SingleGossip | CommitmentLevel::Single
101 )
102 }
103
104 pub fn is_processed(&self) -> bool {
105 matches!(
106 &self.commitment,
107 CommitmentLevel::Processed | CommitmentLevel::Recent
108 )
109 }
110
111 pub fn is_at_least_confirmed(&self) -> bool {
112 self.is_confirmed() || self.is_finalized()
113 }
114
115 pub fn use_deprecated_commitment(commitment: CommitmentConfig) -> Self {
116 match commitment.commitment {
117 CommitmentLevel::Finalized => CommitmentConfig::max(),
118 CommitmentLevel::Confirmed => CommitmentConfig::single_gossip(),
119 CommitmentLevel::Processed => CommitmentConfig::recent(),
120 _ => commitment,
121 }
122 }
123}
124
125impl FromStr for CommitmentConfig {
126 type Err = ParseCommitmentLevelError;
127
128 fn from_str(s: &str) -> Result<Self, Self::Err> {
129 CommitmentLevel::from_str(s).map(|commitment| Self { commitment })
130 }
131}
132
133#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
134#[serde(rename_all = "camelCase")]
135pub enum CommitmentLevel {
140 #[deprecated(
142 since = "1.5.5",
143 note = "Please use CommitmentLevel::Finalized instead"
144 )]
145 Max,
146
147 #[deprecated(
151 since = "1.5.5",
152 note = "Please use CommitmentLevel::Processed instead"
153 )]
154 Recent,
155
156 #[deprecated(
158 since = "1.5.5",
159 note = "Please use CommitmentLevel::Finalized instead"
160 )]
161 Root,
162
163 #[deprecated(
165 since = "1.5.5",
166 note = "Please use CommitmentLevel::Confirmed instead"
167 )]
168 Single,
169
170 #[deprecated(
177 since = "1.5.5",
178 note = "Please use CommitmentLevel::Confirmed instead"
179 )]
180 SingleGossip,
181
182 Processed,
186
187 Confirmed,
192
193 Finalized,
196}
197
198impl Default for CommitmentLevel {
199 fn default() -> Self {
200 Self::Finalized
201 }
202}
203
204impl FromStr for CommitmentLevel {
205 type Err = ParseCommitmentLevelError;
206
207 fn from_str(s: &str) -> Result<Self, Self::Err> {
208 match s {
209 "max" => Ok(CommitmentLevel::Max),
210 "recent" => Ok(CommitmentLevel::Recent),
211 "root" => Ok(CommitmentLevel::Root),
212 "single" => Ok(CommitmentLevel::Single),
213 "singleGossip" => Ok(CommitmentLevel::SingleGossip),
214 "processed" => Ok(CommitmentLevel::Processed),
215 "confirmed" => Ok(CommitmentLevel::Confirmed),
216 "finalized" => Ok(CommitmentLevel::Finalized),
217 _ => Err(ParseCommitmentLevelError::Invalid),
218 }
219 }
220}
221
222impl std::fmt::Display for CommitmentLevel {
223 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
224 let s = match self {
225 CommitmentLevel::Max => "max",
226 CommitmentLevel::Recent => "recent",
227 CommitmentLevel::Root => "root",
228 CommitmentLevel::Single => "single",
229 CommitmentLevel::SingleGossip => "singleGossip",
230 CommitmentLevel::Processed => "processed",
231 CommitmentLevel::Confirmed => "confirmed",
232 CommitmentLevel::Finalized => "finalized",
233 };
234 write!(f, "{}", s)
235 }
236}
237
238#[derive(Error, Debug)]
239pub enum ParseCommitmentLevelError {
240 #[error("invalid variant")]
241 Invalid,
242}