1use std::fmt;
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum NetworkType {
8 Mainnet,
9 Testnet,
10 Devnet,
11 Simnet,
12}
13
14impl serde::Serialize for NetworkType {
15 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
16 where
17 S: serde::Serializer,
18 {
19 match self {
20 NetworkType::Mainnet => serializer.serialize_str("mainnet"),
21 NetworkType::Testnet => serializer.serialize_str("testnet"),
22 NetworkType::Devnet => serializer.serialize_str("devnet"),
23 NetworkType::Simnet => serializer.serialize_str("simnet"),
24 }
25 }
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct RpcRequest {
30 pub jsonrpc: String,
31 pub id: u64,
32 pub method: String,
33 pub params: Option<serde_json::Value>,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct RpcResponse<T> {
38 pub jsonrpc: String,
39 pub id: u64,
40 pub result: Option<T>,
41 pub error: Option<RpcError>,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct RpcError {
46 pub code: i32,
47 pub message: String,
48 pub data: Option<serde_json::Value>,
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct Block {
54 pub header: BlockHeader,
55 pub transactions: Vec<Transaction>,
56 pub verbose_data: Option<BlockVerboseData>,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct BlockHeader {
61 pub version: u16,
62 pub parents: Vec<String>,
63 pub hash_merkle_root: String,
64 pub accepted_id_merkle_root: String,
65 pub utxo_commitment: String,
66 pub timestamp: u64,
67 pub bits: u32,
68 pub nonce: u64,
69 pub daa_score: u64,
70 pub blue_work: String,
71 pub blue_score: u64,
72 pub pruning_point: String,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct BlockVerboseData {
77 pub hash: String,
78 pub difficulty: f64,
79 pub selected_parent_hash: String,
80 pub transaction_ids: Vec<String>,
81 pub is_header_only: bool,
82 pub blue_score: u64,
83 pub children_hashes: Vec<String>,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct Transaction {
88 pub version: u16,
89 pub inputs: Vec<TransactionInput>,
90 pub outputs: Vec<TransactionOutput>,
91 pub lock_time: u64,
92 pub subnetwork_id: String,
93 pub gas: u64,
94 pub payload: String,
95 pub verbose_data: Option<TransactionVerboseData>,
96}
97
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct TransactionInput {
100 pub previous_outpoint: Outpoint,
101 pub signature_script: String,
102 pub sequence: u64,
103 pub sig_op_count: u8,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct TransactionOutput {
108 pub value: u64,
109 pub script_public_key: ScriptPublicKey,
110 pub verbose_data: Option<TransactionOutputVerboseData>,
111}
112
113#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct TransactionVerboseData {
115 pub transaction_id: String,
116 pub hash: String,
117 pub mass: u64,
118 pub block_hash: String,
119 pub block_time: u64,
120}
121
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct TransactionOutputVerboseData {
124 pub script_public_key_type: String,
125 pub script_public_key_address: String,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct Outpoint {
130 pub transaction_id: String,
131 pub index: u32,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct ScriptPublicKey {
136 pub version: u16,
137 pub script_public_key: String,
138}
139
140#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct UtxoEntry {
142 pub amount: u64,
143 pub script_public_key: ScriptPublicKey,
144 pub block_daa_score: u64,
145 pub is_coinbase: bool,
146}
147
148#[derive(Debug, Clone, Serialize, Deserialize)]
149pub struct NodeInfo {
150 pub server_version: String,
151 pub is_utxo_indexed: bool,
152 pub is_synced: bool,
153 pub has_notify_command: bool,
154 pub mempool_size: u64,
155 pub block_count: u64,
156 pub header_count: u64,
157 pub virtual_daa_score: u64,
158 pub network: String,
159}
160
161#[derive(Debug, Clone, Serialize, Deserialize)]
162pub struct NetworkInfo {
163 pub network_name: String,
164 pub block_count: u64,
165 pub header_count: u64,
166 pub virtual_daa_score: u64,
167 pub mempool_size: u64,
168 pub server_version: String,
169}
170
171#[derive(Debug, Clone, Serialize, Deserialize)]
172pub struct Balance {
173 pub balance: u64,
174 pub address: String,
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct SubmitTransactionResponse {
179 pub transaction_id: String,
180 pub error: Option<String>,
181}
182
183#[derive(Debug, Clone, Serialize, Deserialize)]
184pub struct EstimateNetworkHashesPerSecondResponse {
185 pub network_hashes_per_second: String,
186}
187
188#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct MempoolEntry {
190 pub fee: u64,
191 pub transaction: Transaction,
192 pub is_orphan: bool,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
196pub struct DaaScoreTimestampEstimate {
197 pub timestamp: u64,
198 pub daa_score: u64,
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize)]
203pub struct VirtualChainChangedNotification {
204 pub removed_chain_block_hashes: Vec<String>,
205 pub added_chain_block_hashes: Vec<String>,
206 pub accepted_transaction_ids: Vec<AcceptedTransactionIds>,
207}
208
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct AcceptedTransactionIds {
211 pub accepting_block_hash: String,
212 pub accepted_transaction_ids: Vec<String>,
213}
214
215#[derive(Debug, Clone, Serialize, Deserialize)]
216pub struct UtxosChangedNotification {
217 pub added: Vec<UtxoChange>,
218 pub removed: Vec<UtxoChange>,
219}
220
221#[derive(Debug, Clone, Serialize, Deserialize)]
222pub struct UtxoChange {
223 pub address: String,
224 pub outpoint: Outpoint,
225 pub utxo_entry: UtxoEntry,
226}
227
228#[derive(Debug)]
229pub enum KaspaError {
230 HttpError(reqwest::Error),
231 JsonError(serde_json::Error),
232 RpcError { code: i32, message: String },
233 InvalidResponse(String),
234 ConnectionError(String),
235 AuthenticationError,
236 InvalidAddress(String),
237 Custom(String),
238 Error(String),
239}
240
241impl fmt::Display for KaspaError {
242 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
243 match self {
244 KaspaError::HttpError(e) => write!(f, "HTTP request failed: {}", e),
245 KaspaError::JsonError(e) => write!(f, "JSON serialization failed: {}", e),
246 KaspaError::RpcError { code, message } => {
247 write!(f, "RPC error: code={}, message={}", code, message)
248 }
249 KaspaError::InvalidResponse(msg) => write!(f, "Invalid response format: {}", msg),
250 KaspaError::ConnectionError(msg) => write!(f, "Connection failed: {}", msg),
251 KaspaError::AuthenticationError => write!(f, "Authentication failed"),
252 KaspaError::InvalidAddress(msg) => write!(f, "Invalid address: {}", msg),
253 KaspaError::Custom(msg) => write!(f, "{}", msg),
254 KaspaError::Error(msg) => write!(f, "{}", msg),
255 }
256 }
257}
258
259impl std::error::Error for KaspaError {
260 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
261 match self {
262 KaspaError::HttpError(e) => Some(e),
263 KaspaError::JsonError(e) => Some(e),
264 _ => None,
265 }
266 }
267}
268
269impl From<reqwest::Error> for KaspaError {
270 fn from(err: reqwest::Error) -> Self {
271 KaspaError::HttpError(err)
272 }
273}
274
275impl From<serde_json::Error> for KaspaError {
276 fn from(err: serde_json::Error) -> Self {
277 KaspaError::JsonError(err)
278 }
279}
280
281pub type Result<T> = std::result::Result<T, KaspaError>;