solders_commitment_config/
lib.rs

1use std::str::FromStr;
2
3use derive_more::{From, Into};
4use pyo3::prelude::*;
5use serde::{Deserialize, Serialize};
6use solana_sdk::commitment_config::{
7    CommitmentConfig as CommitmentConfigOriginal, CommitmentLevel as CommitmentLevelOriginal,
8};
9
10use solders_traits::handle_py_err;
11
12/// RPC request `commitment <https://docs.solana.com/developing/clients/jsonrpc-api#configuring-state-commitment>`_ options.
13#[pyclass(module = "solders.commitment_config")]
14#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
15pub enum CommitmentLevel {
16    /// The highest slot of the heaviest fork processed by the node. Ledger state at this slot is
17    /// not derived from a confirmed or finalized block, but if multiple forks are present, is from
18    /// the fork the validator believes is most likely to finalize.
19    Processed,
20
21    /// The highest slot that has been voted on by supermajority of the cluster, ie. is confirmed.
22    /// Confirmation incorporates votes from gossip and replay. It does not count votes on
23    /// descendants of a block, only direct votes on that block, and upholds "optimistic
24    /// confirmation" guarantees in release 1.3 and onwards.
25    Confirmed,
26
27    /// The highest slot having reached max vote lockout, as recognized by a supermajority of the
28    /// cluster.
29    Finalized,
30}
31
32impl Default for CommitmentLevel {
33    fn default() -> Self {
34        CommitmentLevelOriginal::default().into()
35    }
36}
37
38#[pymethods]
39impl CommitmentLevel {
40    #[staticmethod]
41    #[pyo3(name = "from_string")]
42    pub fn new_from_str(s: &str) -> PyResult<Self> {
43        handle_py_err(CommitmentLevelOriginal::from_str(s))
44    }
45
46    /// Create a new default instance.
47    #[staticmethod]
48    #[pyo3(name = "default")]
49    pub fn new_default() -> Self {
50        Self::default()
51    }
52}
53
54impl From<CommitmentLevelOriginal> for CommitmentLevel {
55    #![allow(deprecated)]
56    fn from(c: CommitmentLevelOriginal) -> Self {
57        match c {
58            CommitmentLevelOriginal::Processed => Self::Processed,
59            CommitmentLevelOriginal::Confirmed => Self::Confirmed,
60            CommitmentLevelOriginal::Finalized => Self::Finalized,
61            CommitmentLevelOriginal::Max => Self::Finalized,
62            CommitmentLevelOriginal::Recent => Self::Processed,
63            CommitmentLevelOriginal::Root => Self::Finalized,
64            CommitmentLevelOriginal::Single => Self::Confirmed,
65            CommitmentLevelOriginal::SingleGossip => Self::Confirmed,
66        }
67    }
68}
69
70impl From<CommitmentLevel> for CommitmentLevelOriginal {
71    fn from(c: CommitmentLevel) -> Self {
72        match c {
73            CommitmentLevel::Processed => Self::Processed,
74            CommitmentLevel::Confirmed => Self::Confirmed,
75            CommitmentLevel::Finalized => Self::Finalized,
76        }
77    }
78}
79
80/// Wrapper object for ``CommitmentLevel``.
81///
82/// Args:
83///     commitment (CommitmentLevel): Bank state to query.
84#[pyclass(module = "solders.commitment_config", subclass)]
85#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, PartialEq, Eq, Hash, From, Into)]
86pub struct CommitmentConfig(CommitmentConfigOriginal);
87
88#[pymethods]
89impl CommitmentConfig {
90    #[new]
91    pub fn new(commitment: CommitmentLevel) -> Self {
92        CommitmentConfigOriginal {
93            commitment: commitment.into(),
94        }
95        .into()
96    }
97
98    #[getter]
99    pub fn commitment(&self) -> CommitmentLevel {
100        self.0.commitment.into()
101    }
102
103    /// Create from a string.
104    #[staticmethod]
105    #[pyo3(name = "from_string")]
106    pub fn new_from_str(s: &str) -> PyResult<Self> {
107        handle_py_err(CommitmentConfigOriginal::from_str(s))
108    }
109
110    /// Create a new instance with ``processed`` commitment.
111    #[staticmethod]
112    pub fn processed() -> Self {
113        CommitmentConfigOriginal::processed().into()
114    }
115
116    /// Create a new instance with ``confirmed`` commitment.
117    #[staticmethod]
118    pub fn confirmed() -> Self {
119        CommitmentConfigOriginal::confirmed().into()
120    }
121
122    /// Create a new instance with ``finalized`` commitment.
123    #[staticmethod]
124    pub fn finalized() -> Self {
125        CommitmentConfigOriginal::finalized().into()
126    }
127
128    /// Create a new default instance.
129    #[staticmethod]
130    #[pyo3(name = "default")]
131    pub fn new_default() -> Self {
132        Self::default()
133    }
134
135    /// Check if using ``finalized`` commitment.
136    pub fn is_finalized(&self) -> bool {
137        self.0.is_finalized()
138    }
139
140    /// Check if using ``confirmed`` commitment.
141    pub fn is_confirmed(&self) -> bool {
142        self.0.is_confirmed()
143    }
144
145    /// Check if using at least ``confirmed`` commitment.
146    pub fn is_at_least_confirmed(&self) -> bool {
147        self.0.is_at_least_confirmed()
148    }
149}
150
151impl From<CommitmentLevel> for CommitmentConfig {
152    fn from(c: CommitmentLevel) -> Self {
153        CommitmentConfig::new(c)
154    }
155}
156
157impl From<CommitmentLevel> for CommitmentConfigOriginal {
158    fn from(c: CommitmentLevel) -> Self {
159        CommitmentConfig::from(c).into()
160    }
161}
162
163impl From<CommitmentConfig> for CommitmentLevel {
164    fn from(c: CommitmentConfig) -> Self {
165        c.commitment()
166    }
167}
168
169impl From<CommitmentLevelOriginal> for CommitmentConfig {
170    fn from(c: CommitmentLevelOriginal) -> Self {
171        CommitmentLevel::from(c).into()
172    }
173}
174
175impl From<CommitmentConfigOriginal> for CommitmentLevel {
176    fn from(c: CommitmentConfigOriginal) -> Self {
177        CommitmentConfig::from(c).into()
178    }
179}