Skip to content

Commit 97b543d

Browse files
committed
chore(api)!: use Amount for all value/fee fields
1 parent dbbe555 commit 97b543d

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/api.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub struct Vin {
5050
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
5151
pub struct Vout {
5252
/// The value of the output, in satoshis.
53-
pub value: u64,
53+
#[serde(with = "bitcoin::amount::serde::as_sat")]
54+
pub value: Amount,
5455
/// The ScriptPubKey that the output is locked to, as a [`ScriptBuf`].
5556
pub scriptpubkey: ScriptBuf,
5657
}
@@ -129,7 +130,8 @@ pub struct Tx {
129130
/// The confirmation status of the [`Transaction`].
130131
pub status: TxStatus,
131132
/// The fee amount paid by the [`Transaction`], in satoshis.
132-
pub fee: u64,
133+
#[serde(with = "bitcoin::amount::serde::as_sat")]
134+
pub fee: Amount,
133135
}
134136

135137
/// Information about a bitcoin [`Block`].
@@ -211,11 +213,13 @@ pub struct AddressTxsSummary {
211213
/// The number of funded [`TxOut`]s.
212214
pub funded_txo_count: u32,
213215
/// The sum of the funded [`TxOut`]s, in satoshis.
214-
pub funded_txo_sum: u64,
216+
#[serde(with = "bitcoin::amount::serde::as_sat")]
217+
pub funded_txo_sum: Amount,
215218
/// The number of spent [`TxOut`]s.
216219
pub spent_txo_count: u32,
217220
/// The sum of the spent [`TxOut`]s, in satoshis.
218-
pub spent_txo_sum: u64,
221+
#[serde(with = "bitcoin::amount::serde::as_sat")]
222+
pub spent_txo_sum: Amount,
219223
/// The total number of [`Transaction`]s.
220224
pub tx_count: u32,
221225
}
@@ -256,6 +260,7 @@ pub struct Utxo {
256260
/// The confirmation status of the [`TxOut`].
257261
pub status: UtxoStatus,
258262
/// The value of the [`TxOut`] as an [`Amount`].
263+
#[serde(with = "bitcoin::amount::serde::as_sat")]
259264
pub value: Amount,
260265
}
261266

@@ -267,7 +272,8 @@ pub struct MempoolStats {
267272
/// The total size of mempool [`Transaction`]s, in virtual bytes.
268273
pub vsize: usize,
269274
/// The total fee paid by mempool [`Transaction`]s, in satoshis.
270-
pub total_fee: u64,
275+
#[serde(with = "bitcoin::amount::serde::as_sat")]
276+
pub total_fee: Amount,
271277
/// The mempool's fee rate distribution histogram.
272278
///
273279
/// An array of `(feerate, vsize)` tuples, where each entry's `vsize` is the total vsize
@@ -282,11 +288,13 @@ pub struct MempoolRecentTx {
282288
/// The [`Transaction`]'s ID, as a [`Txid`].
283289
pub txid: Txid,
284290
/// The [`Amount`] of fees paid by the transaction, in satoshis.
285-
pub fee: u64,
291+
#[serde(with = "bitcoin::amount::serde::as_sat")]
292+
pub fee: Amount,
286293
/// The [`Transaction`]'s size, in virtual bytes.
287294
pub vsize: usize,
288295
/// Combined [`Amount`] of the [`Transaction`], in satoshis.
289-
pub value: u64,
296+
#[serde(with = "bitcoin::amount::serde::as_sat")]
297+
pub value: Amount,
290298
}
291299

292300
/// The result for a broadcasted package of [`Transaction`]s.
@@ -370,7 +378,7 @@ impl Tx {
370378
.iter()
371379
.cloned()
372380
.map(|vout| TxOut {
373-
value: Amount::from_sat(vout.value),
381+
value: vout.value,
374382
script_pubkey: vout.scriptpubkey,
375383
})
376384
.collect(),
@@ -398,7 +406,7 @@ impl Tx {
398406
.map(|vin| {
399407
vin.prevout.map(|po| TxOut {
400408
script_pubkey: po.scriptpubkey,
401-
value: Amount::from_sat(po.value),
409+
value: po.value,
402410
})
403411
})
404412
.collect()
@@ -408,11 +416,6 @@ impl Tx {
408416
pub fn weight(&self) -> Weight {
409417
Weight::from_wu(self.weight)
410418
}
411-
412-
/// Get the fee paid by a [`Tx`].
413-
pub fn fee(&self) -> Amount {
414-
Amount::from_sat(self.fee)
415-
}
416419
}
417420

418421
fn deserialize_witness<'de, D>(d: D) -> Result<Vec<Vec<u8>>, D::Error>

src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ mod test {
575575
assert_eq!(tx_info.to_tx(), tx_exp);
576576
assert_eq!(tx_info.size, tx_exp.total_size());
577577
assert_eq!(tx_info.weight(), tx_exp.weight());
578-
assert_eq!(tx_info.fee(), tx_res.fee.unwrap().unsigned_abs());
578+
assert_eq!(tx_info.fee, tx_res.fee.unwrap().unsigned_abs());
579579
assert!(tx_info.status.confirmed);
580580
assert_eq!(tx_info.status.block_height, Some(tx_block_height));
581581
assert_eq!(tx_info.status.block_hash, tx_res.block_hash);
@@ -1092,7 +1092,10 @@ mod test {
10921092
let address_stats_async = async_client.get_address_stats(&address).await.unwrap();
10931093
assert_eq!(address_stats_blocking, address_stats_async);
10941094
assert_eq!(address_stats_async.chain_stats.funded_txo_count, 1);
1095-
assert_eq!(address_stats_async.chain_stats.funded_txo_sum, 1000);
1095+
assert_eq!(
1096+
address_stats_async.chain_stats.funded_txo_sum,
1097+
Amount::from_sat(1000)
1098+
);
10961099
}
10971100

10981101
#[cfg(all(feature = "blocking", feature = "async"))]
@@ -1167,7 +1170,7 @@ mod test {
11671170
);
11681171
assert_eq!(
11691172
scripthash_stats_blocking_legacy.chain_stats.funded_txo_sum,
1170-
1000
1173+
Amount::from_sat(1000)
11711174
);
11721175
assert_eq!(scripthash_stats_blocking_legacy.chain_stats.tx_count, 1);
11731176

@@ -1187,7 +1190,7 @@ mod test {
11871190
scripthash_stats_blocking_p2sh_segwit
11881191
.chain_stats
11891192
.funded_txo_sum,
1190-
1000
1193+
Amount::from_sat(1000)
11911194
);
11921195
assert_eq!(
11931196
scripthash_stats_blocking_p2sh_segwit.chain_stats.tx_count,
@@ -1208,7 +1211,7 @@ mod test {
12081211
);
12091212
assert_eq!(
12101213
scripthash_stats_blocking_bech32.chain_stats.funded_txo_sum,
1211-
1000
1214+
Amount::from_sat(1000)
12121215
);
12131216
assert_eq!(scripthash_stats_blocking_bech32.chain_stats.tx_count, 1);
12141217

@@ -1226,7 +1229,7 @@ mod test {
12261229
);
12271230
assert_eq!(
12281231
scripthash_stats_blocking_bech32m.chain_stats.funded_txo_sum,
1229-
1000
1232+
Amount::from_sat(1000)
12301233
);
12311234
assert_eq!(scripthash_stats_blocking_bech32m.chain_stats.tx_count, 1);
12321235
}

0 commit comments

Comments
 (0)