use std::str::FromStr;
use derive_more::{From, Into};
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};
use solana_sdk::commitment_config::{
CommitmentConfig as CommitmentConfigOriginal, CommitmentLevel as CommitmentLevelOriginal,
};
use solders_traits::handle_py_err;
#[pyclass(module = "solders.commitment_config")]
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CommitmentLevel {
Processed,
Confirmed,
Finalized,
}
impl Default for CommitmentLevel {
fn default() -> Self {
CommitmentLevelOriginal::default().into()
}
}
#[pymethods]
impl CommitmentLevel {
#[staticmethod]
#[pyo3(name = "from_string")]
pub fn new_from_str(s: &str) -> PyResult<Self> {
handle_py_err(CommitmentLevelOriginal::from_str(s))
}
#[staticmethod]
#[pyo3(name = "default")]
pub fn new_default() -> Self {
Self::default()
}
}
impl From<CommitmentLevelOriginal> for CommitmentLevel {
#![allow(deprecated)]
fn from(c: CommitmentLevelOriginal) -> Self {
match c {
CommitmentLevelOriginal::Processed => Self::Processed,
CommitmentLevelOriginal::Confirmed => Self::Confirmed,
CommitmentLevelOriginal::Finalized => Self::Finalized,
CommitmentLevelOriginal::Max => Self::Finalized,
CommitmentLevelOriginal::Recent => Self::Processed,
CommitmentLevelOriginal::Root => Self::Finalized,
CommitmentLevelOriginal::Single => Self::Confirmed,
CommitmentLevelOriginal::SingleGossip => Self::Confirmed,
}
}
}
impl From<CommitmentLevel> for CommitmentLevelOriginal {
fn from(c: CommitmentLevel) -> Self {
match c {
CommitmentLevel::Processed => Self::Processed,
CommitmentLevel::Confirmed => Self::Confirmed,
CommitmentLevel::Finalized => Self::Finalized,
}
}
}
#[pyclass(module = "solders.commitment_config", subclass)]
#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, PartialEq, Eq, Hash, From, Into)]
pub struct CommitmentConfig(CommitmentConfigOriginal);
#[pymethods]
impl CommitmentConfig {
#[new]
pub fn new(commitment: CommitmentLevel) -> Self {
CommitmentConfigOriginal {
commitment: commitment.into(),
}
.into()
}
#[getter]
pub fn commitment(&self) -> CommitmentLevel {
self.0.commitment.into()
}
#[staticmethod]
#[pyo3(name = "from_string")]
pub fn new_from_str(s: &str) -> PyResult<Self> {
handle_py_err(CommitmentConfigOriginal::from_str(s))
}
#[staticmethod]
pub fn processed() -> Self {
CommitmentConfigOriginal::processed().into()
}
#[staticmethod]
pub fn confirmed() -> Self {
CommitmentConfigOriginal::confirmed().into()
}
#[staticmethod]
pub fn finalized() -> Self {
CommitmentConfigOriginal::finalized().into()
}
#[staticmethod]
#[pyo3(name = "default")]
pub fn new_default() -> Self {
Self::default()
}
pub fn is_finalized(&self) -> bool {
self.0.is_finalized()
}
pub fn is_confirmed(&self) -> bool {
self.0.is_confirmed()
}
pub fn is_at_least_confirmed(&self) -> bool {
self.0.is_at_least_confirmed()
}
}
impl From<CommitmentLevel> for CommitmentConfig {
fn from(c: CommitmentLevel) -> Self {
CommitmentConfig::new(c)
}
}
impl From<CommitmentLevel> for CommitmentConfigOriginal {
fn from(c: CommitmentLevel) -> Self {
CommitmentConfig::from(c).into()
}
}
impl From<CommitmentConfig> for CommitmentLevel {
fn from(c: CommitmentConfig) -> Self {
c.commitment()
}
}
impl From<CommitmentLevelOriginal> for CommitmentConfig {
fn from(c: CommitmentLevelOriginal) -> Self {
CommitmentLevel::from(c).into()
}
}
impl From<CommitmentConfigOriginal> for CommitmentLevel {
fn from(c: CommitmentConfigOriginal) -> Self {
CommitmentConfig::from(c).into()
}
}