subcoin_rpc_bitcoind/
types.rs

1//! Bitcoin Core compatible JSON-RPC response types.
2//!
3//! These types match the JSON structure returned by Bitcoin Core's RPC API,
4//! enabling compatibility with tools like electrs.
5
6use bitcoin::{BlockHash, Txid};
7use serde::{Deserialize, Serialize};
8
9/// Response for `getblockchaininfo` RPC.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct GetBlockchainInfo {
12    /// Current network name (main, test, signet, regtest).
13    pub chain: String,
14    /// The current number of blocks processed in the server.
15    pub blocks: u32,
16    /// The current number of headers we have validated.
17    pub headers: u32,
18    /// The hash of the currently best block.
19    pub bestblockhash: BlockHash,
20    /// The current difficulty.
21    pub difficulty: f64,
22    /// Estimate of verification progress [0..1].
23    pub verificationprogress: f64,
24    /// Whether initial block download is complete.
25    pub initialblockdownload: bool,
26    /// Total amount of work in active chain, in hexadecimal.
27    pub chainwork: String,
28    /// The estimated size of the block and undo files on disk.
29    pub size_on_disk: u64,
30    /// If the blocks are subject to pruning.
31    pub pruned: bool,
32    /// Any network and blockchain warnings.
33    pub warnings: Vec<String>,
34}
35
36/// Response for `getnetworkinfo` RPC.
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct GetNetworkInfo {
39    /// The server version.
40    pub version: u32,
41    /// The server subversion string.
42    pub subversion: String,
43    /// The protocol version.
44    pub protocolversion: u32,
45    /// The services we offer to the network (hex string).
46    pub localservices: String,
47    /// True if local relay is active.
48    pub localrelay: bool,
49    /// The time offset.
50    pub timeoffset: i64,
51    /// Whether p2p networking is enabled.
52    pub networkactive: bool,
53    /// The number of connections.
54    pub connections: u32,
55    /// The number of inbound connections.
56    pub connections_in: u32,
57    /// The number of outbound connections.
58    pub connections_out: u32,
59    /// Minimum relay fee rate for transactions in BTC/kvB.
60    pub relayfee: f64,
61    /// Minimum fee rate increment for mempool limiting or replacement in BTC/kvB.
62    pub incrementalfee: f64,
63    /// List of local addresses.
64    pub localaddresses: Vec<LocalAddress>,
65    /// Any network and blockchain warnings.
66    pub warnings: Vec<String>,
67}
68
69/// Local address information.
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct LocalAddress {
72    /// Network address.
73    pub address: String,
74    /// Network port.
75    pub port: u16,
76    /// Relative score.
77    pub score: u32,
78}
79
80/// Response for `getblock` RPC with verbosity=1 (JSON without tx details).
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct GetBlock {
83    /// The block hash.
84    pub hash: BlockHash,
85    /// The number of confirmations.
86    pub confirmations: i32,
87    /// The block size in bytes.
88    pub size: u32,
89    /// The block size excluding witness data.
90    pub strippedsize: u32,
91    /// The block weight.
92    pub weight: u32,
93    /// The block height or index.
94    pub height: u32,
95    /// The block version.
96    pub version: i32,
97    /// The block version formatted in hexadecimal.
98    #[serde(rename = "versionHex")]
99    pub version_hex: String,
100    /// The merkle root.
101    pub merkleroot: String,
102    /// The transaction ids.
103    pub tx: Vec<Txid>,
104    /// The block time in UNIX epoch time.
105    pub time: u32,
106    /// The median block time in UNIX epoch time.
107    pub mediantime: u32,
108    /// The nonce.
109    pub nonce: u32,
110    /// The bits.
111    pub bits: String,
112    /// The difficulty.
113    pub difficulty: f64,
114    /// Expected number of hashes required to produce the current chain.
115    pub chainwork: String,
116    /// The number of transactions in the block.
117    #[serde(rename = "nTx")]
118    pub n_tx: u32,
119    /// The hash of the previous block.
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub previousblockhash: Option<BlockHash>,
122    /// The hash of the next block.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub nextblockhash: Option<BlockHash>,
125}
126
127/// Response for `getblockheader` RPC with verbose=true.
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct GetBlockHeader {
130    /// The block hash.
131    pub hash: BlockHash,
132    /// The number of confirmations.
133    pub confirmations: i32,
134    /// The block height or index.
135    pub height: u32,
136    /// The block version.
137    pub version: i32,
138    /// The block version formatted in hexadecimal.
139    #[serde(rename = "versionHex")]
140    pub version_hex: String,
141    /// The merkle root.
142    pub merkleroot: String,
143    /// The block time in UNIX epoch time.
144    pub time: u32,
145    /// The median block time in UNIX epoch time.
146    pub mediantime: u32,
147    /// The nonce.
148    pub nonce: u32,
149    /// The bits.
150    pub bits: String,
151    /// The difficulty.
152    pub difficulty: f64,
153    /// Expected number of hashes required to produce the current chain.
154    pub chainwork: String,
155    /// The number of transactions in the block.
156    #[serde(rename = "nTx")]
157    pub n_tx: u32,
158    /// The hash of the previous block.
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub previousblockhash: Option<BlockHash>,
161    /// The hash of the next block.
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub nextblockhash: Option<BlockHash>,
164}
165
166/// Response for `getmempoolinfo` RPC.
167#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct GetMempoolInfo {
169    /// True if the mempool is fully loaded.
170    pub loaded: bool,
171    /// Current tx count.
172    pub size: u64,
173    /// Sum of all virtual transaction sizes.
174    pub bytes: u64,
175    /// Total memory usage for the mempool.
176    pub usage: u64,
177    /// Total fees for all transactions in the mempool in BTC.
178    pub total_fee: f64,
179    /// Maximum memory usage for the mempool.
180    pub maxmempool: u64,
181    /// Minimum fee rate in BTC/kvB for tx to be accepted.
182    pub mempoolminfee: f64,
183    /// Current minimum relay fee for transactions.
184    pub minrelaytxfee: f64,
185    /// Minimum fee rate increment for mempool limiting or replacement in BTC/kvB.
186    pub incrementalrelayfee: f64,
187    /// Current number of transactions that haven't passed initial broadcast yet.
188    pub unbroadcastcount: u64,
189    /// True if mempool accepts RBF without signaling inspection.
190    pub fullrbf: bool,
191}
192
193/// Response for `getmempoolentry` RPC.
194#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct GetMempoolEntry {
196    /// Virtual transaction size.
197    pub vsize: u64,
198    /// Transaction weight.
199    pub weight: u64,
200    /// Transaction fee in BTC.
201    pub fee: f64,
202    /// Transaction fee with fee deltas used for mining priority in BTC.
203    pub modifiedfee: f64,
204    /// Local time transaction entered pool in seconds since 1 Jan 1970 GMT.
205    pub time: i64,
206    /// Block height when transaction entered pool.
207    pub height: u32,
208    /// Number of in-mempool descendant transactions (including this one).
209    pub descendantcount: u64,
210    /// Virtual transaction size of in-mempool descendants (including this one).
211    pub descendantsize: u64,
212    /// Modified fees of in-mempool descendants (including this one) in BTC.
213    pub descendantfees: u64,
214    /// Number of in-mempool ancestor transactions (including this one).
215    pub ancestorcount: u64,
216    /// Virtual transaction size of in-mempool ancestors (including this one).
217    pub ancestorsize: u64,
218    /// Modified fees of in-mempool ancestors (including this one) in BTC.
219    pub ancestorfees: u64,
220    /// Hash of serialized transaction, including witness data.
221    pub wtxid: Txid,
222    /// Unconfirmed transactions used as inputs for this transaction.
223    pub depends: Vec<Txid>,
224    /// Unconfirmed transactions spending outputs from this transaction.
225    pub spentby: Vec<Txid>,
226    /// Whether this transaction could be replaced due to BIP125.
227    #[serde(rename = "bip125-replaceable")]
228    pub bip125_replaceable: bool,
229    /// Whether this transaction is currently unbroadcast.
230    pub unbroadcast: bool,
231}
232
233/// Response for `estimatesmartfee` RPC.
234#[derive(Debug, Clone, Serialize, Deserialize)]
235pub struct EstimateSmartFee {
236    /// Estimate fee rate in BTC/kvB.
237    #[serde(skip_serializing_if = "Option::is_none")]
238    pub feerate: Option<f64>,
239    /// Errors encountered during processing.
240    #[serde(skip_serializing_if = "Option::is_none")]
241    pub errors: Option<Vec<String>>,
242    /// Block number where estimate was found.
243    pub blocks: u32,
244}
245
246/// Response for `getrawtransaction` RPC with verbose=true.
247#[derive(Debug, Clone, Serialize, Deserialize)]
248pub struct GetRawTransaction {
249    /// Whether specified block is in the active chain or not.
250    #[serde(skip_serializing_if = "Option::is_none")]
251    pub in_active_chain: Option<bool>,
252    /// The serialized, hex-encoded data for the transaction.
253    pub hex: String,
254    /// The transaction id.
255    pub txid: Txid,
256    /// The transaction hash (differs from txid for witness transactions).
257    pub hash: Txid,
258    /// The serialized transaction size.
259    pub size: u32,
260    /// The virtual transaction size.
261    pub vsize: u32,
262    /// The transaction's weight.
263    pub weight: u32,
264    /// The version.
265    pub version: i32,
266    /// The lock time.
267    pub locktime: u32,
268    /// The transaction inputs.
269    pub vin: Vec<Vin>,
270    /// The transaction outputs.
271    pub vout: Vec<Vout>,
272    /// The block hash.
273    #[serde(skip_serializing_if = "Option::is_none")]
274    pub blockhash: Option<BlockHash>,
275    /// Number of confirmations.
276    #[serde(skip_serializing_if = "Option::is_none")]
277    pub confirmations: Option<i32>,
278    /// The block time in UNIX epoch time.
279    #[serde(skip_serializing_if = "Option::is_none")]
280    pub blocktime: Option<u32>,
281    /// Same as blocktime.
282    #[serde(skip_serializing_if = "Option::is_none")]
283    pub time: Option<u32>,
284}
285
286/// Transaction input.
287#[derive(Debug, Clone, Serialize, Deserialize)]
288pub struct Vin {
289    /// The transaction id.
290    #[serde(skip_serializing_if = "Option::is_none")]
291    pub txid: Option<Txid>,
292    /// The output number.
293    #[serde(skip_serializing_if = "Option::is_none")]
294    pub vout: Option<u32>,
295    /// The script.
296    #[serde(rename = "scriptSig", skip_serializing_if = "Option::is_none")]
297    pub script_sig: Option<ScriptSig>,
298    /// Hex-encoded witness data.
299    #[serde(skip_serializing_if = "Option::is_none")]
300    pub txinwitness: Option<Vec<String>>,
301    /// The script sequence number.
302    pub sequence: u32,
303    /// Coinbase data (for coinbase transactions).
304    #[serde(skip_serializing_if = "Option::is_none")]
305    pub coinbase: Option<String>,
306}
307
308/// Script signature.
309#[derive(Debug, Clone, Serialize, Deserialize)]
310pub struct ScriptSig {
311    /// The assembly representation.
312    pub asm: String,
313    /// The hex representation.
314    pub hex: String,
315}
316
317/// Transaction output.
318#[derive(Debug, Clone, Serialize, Deserialize)]
319pub struct Vout {
320    /// The value in BTC.
321    pub value: f64,
322    /// Index.
323    pub n: u32,
324    /// The script pubkey.
325    #[serde(rename = "scriptPubKey")]
326    pub script_pub_key: ScriptPubKey,
327}
328
329/// Script pubkey.
330#[derive(Debug, Clone, Serialize, Deserialize)]
331pub struct ScriptPubKey {
332    /// The assembly representation.
333    pub asm: String,
334    /// The raw output script bytes, hex-encoded.
335    pub hex: String,
336    /// The type (e.g., pubkeyhash, scripthash, witness_v0_keyhash).
337    #[serde(rename = "type")]
338    pub script_type: String,
339    /// The Bitcoin address (if available).
340    #[serde(skip_serializing_if = "Option::is_none")]
341    pub address: Option<String>,
342}