Skip to main content

mina_sdk/
types.rs

1use serde::{Deserialize, Serialize};
2
3use crate::Currency;
4
5/// Sync status of a Mina daemon node.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
8pub enum SyncStatus {
9    Connecting,
10    Listening,
11    Offline,
12    Bootstrap,
13    Synced,
14    Catchup,
15}
16
17impl std::fmt::Display for SyncStatus {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Self::Connecting => write!(f, "CONNECTING"),
21            Self::Listening => write!(f, "LISTENING"),
22            Self::Offline => write!(f, "OFFLINE"),
23            Self::Bootstrap => write!(f, "BOOTSTRAP"),
24            Self::Synced => write!(f, "SYNCED"),
25            Self::Catchup => write!(f, "CATCHUP"),
26        }
27    }
28}
29
30/// Information about a connected peer.
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct PeerInfo {
33    pub peer_id: String,
34    pub host: String,
35    pub port: i64,
36}
37
38/// Comprehensive daemon status.
39#[derive(Debug, Clone)]
40pub struct DaemonStatus {
41    pub sync_status: SyncStatus,
42    pub blockchain_length: Option<i64>,
43    pub highest_block_length_received: Option<i64>,
44    pub uptime_secs: Option<i64>,
45    pub state_hash: Option<String>,
46    pub commit_id: Option<String>,
47    pub peers: Option<Vec<PeerInfo>>,
48}
49
50/// Account balance with total, liquid, and locked amounts.
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct AccountBalance {
53    pub total: Currency,
54    pub liquid: Option<Currency>,
55    pub locked: Option<Currency>,
56}
57
58/// Account data returned by the daemon.
59#[derive(Debug, Clone)]
60pub struct AccountData {
61    pub public_key: String,
62    pub nonce: u64,
63    pub balance: AccountBalance,
64    pub delegate: Option<String>,
65    pub token_id: Option<String>,
66}
67
68/// Block info from the best chain.
69#[derive(Debug, Clone)]
70pub struct BlockInfo {
71    pub state_hash: String,
72    pub height: u64,
73    pub global_slot_since_hard_fork: u64,
74    pub global_slot_since_genesis: u64,
75    pub creator_pk: String,
76    pub command_transaction_count: i64,
77}
78
79/// Result of a send_payment mutation.
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub struct SendPaymentResult {
82    pub id: String,
83    pub hash: String,
84    pub nonce: u64,
85}
86
87/// Result of a send_delegation mutation.
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub struct SendDelegationResult {
90    pub id: String,
91    pub hash: String,
92    pub nonce: u64,
93}
94
95/// Parameters for sending a payment transaction.
96///
97/// Built using a fluent API where each method documents its purpose:
98///
99/// ```
100/// use mina_sdk::{Payment, Currency};
101///
102/// let payment = Payment::sender("B62qsender...")
103///     .to("B62qreceiver...")
104///     .amount(Currency::from_nanomina(1_500_000_000))
105///     .fee(Currency::from_nanomina(10_000_000))
106///     .memo("coffee payment")
107///     .nonce(42);
108/// ```
109#[derive(Debug, Clone)]
110pub struct Payment {
111    pub sender: String,
112    pub receiver: String,
113    pub amount: Currency,
114    pub fee: Currency,
115    pub memo: Option<String>,
116    pub nonce: Option<u64>,
117}
118
119impl Payment {
120    /// Start building a payment with the sender's public key.
121    pub fn sender(sender: &str) -> Self {
122        Self {
123            sender: sender.to_string(),
124            receiver: String::new(),
125            amount: Currency::from_nanomina(0),
126            fee: Currency::from_nanomina(0),
127            memo: None,
128            nonce: None,
129        }
130    }
131
132    /// Set the receiver's public key.
133    pub fn to(mut self, receiver: &str) -> Self {
134        self.receiver = receiver.to_string();
135        self
136    }
137
138    /// Set the payment amount.
139    pub fn amount(mut self, amount: Currency) -> Self {
140        self.amount = amount;
141        self
142    }
143
144    /// Set the transaction fee.
145    pub fn fee(mut self, fee: Currency) -> Self {
146        self.fee = fee;
147        self
148    }
149
150    /// Set an optional memo.
151    pub fn memo(mut self, memo: &str) -> Self {
152        self.memo = Some(memo.to_string());
153        self
154    }
155
156    /// Set an explicit nonce (otherwise the daemon picks the next nonce).
157    pub fn nonce(mut self, nonce: u64) -> Self {
158        self.nonce = Some(nonce);
159        self
160    }
161}
162
163/// Parameters for sending a stake delegation transaction.
164///
165/// ```
166/// use mina_sdk::{Delegation, Currency};
167///
168/// let delegation = Delegation::sender("B62qsender...")
169///     .to("B62qdelegate...")
170///     .fee(Currency::from_nanomina(10_000_000))
171///     .memo("staking");
172/// ```
173#[derive(Debug, Clone)]
174pub struct Delegation {
175    pub sender: String,
176    pub delegate_to: String,
177    pub fee: Currency,
178    pub memo: Option<String>,
179    pub nonce: Option<u64>,
180}
181
182impl Delegation {
183    /// Start building a delegation with the sender's public key.
184    pub fn sender(sender: &str) -> Self {
185        Self {
186            sender: sender.to_string(),
187            delegate_to: String::new(),
188            fee: Currency::from_nanomina(0),
189            memo: None,
190            nonce: None,
191        }
192    }
193
194    /// Set the delegate's public key.
195    pub fn to(mut self, delegate_to: &str) -> Self {
196        self.delegate_to = delegate_to.to_string();
197        self
198    }
199
200    /// Set the transaction fee.
201    pub fn fee(mut self, fee: Currency) -> Self {
202        self.fee = fee;
203        self
204    }
205
206    /// Set an optional memo.
207    pub fn memo(mut self, memo: &str) -> Self {
208        self.memo = Some(memo.to_string());
209        self
210    }
211
212    /// Set an explicit nonce (otherwise the daemon picks the next nonce).
213    pub fn nonce(mut self, nonce: u64) -> Self {
214        self.nonce = Some(nonce);
215        self
216    }
217}
218
219/// A pooled user command from the transaction pool.
220#[derive(Debug, Clone, Serialize, Deserialize)]
221pub struct PooledUserCommand {
222    pub id: String,
223    pub hash: String,
224    pub kind: String,
225    pub nonce: String,
226    pub amount: String,
227    pub fee: String,
228    pub from: String,
229    pub to: String,
230}