Merge rust-bitcoin/rust-bitcoin#3672: units: Remove serde from amounts
77085a1fa1
units: Remove serde from amounts (Tobin C. Harding)
Pull request description:
The `Amount` and `SignedAmount` were not supposed to implement `serde` traits by design because doing so implicitly uses sats. We provide two modules `as_sat` and `as_btc` to allow users to explicitly serialize in their preferred format.
In commit: `d57ec019d5 Use Amount type for TxOut value field` derives were added for `serde` and we did not notice it during review.
ACKs for top commit:
apoelstra:
ACK 77085a1fa1f39e7358202e49189282e48a965747; successfully ran local tests
Tree-SHA512: ff4c1f34191e12d955e66737493af4b2dad761fa6f95eb334b86715906e70d2180e7f15d8b4af302d53fb0b1c8b5fb50b4924e9fcdee8824840ba555e73c1c17
This commit is contained in:
commit
1f731e9162
|
@ -1862,6 +1862,7 @@ mod tests {
|
||||||
#[serde(rename = "scriptPubKey")]
|
#[serde(rename = "scriptPubKey")]
|
||||||
script_pubkey: ScriptBuf,
|
script_pubkey: ScriptBuf,
|
||||||
#[serde(rename = "amountSats")]
|
#[serde(rename = "amountSats")]
|
||||||
|
#[serde(with = "crate::amount::serde::as_sat")]
|
||||||
value: Amount,
|
value: Amount,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -343,6 +343,7 @@ impl TxIn {
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
pub struct TxOut {
|
pub struct TxOut {
|
||||||
/// The value of the output, in satoshis.
|
/// The value of the output, in satoshis.
|
||||||
|
#[cfg_attr(feature = "serde", serde(with = "crate::amount::serde::as_sat"))]
|
||||||
pub value: Amount,
|
pub value: Amount,
|
||||||
/// The script which must be satisfied for the output to be spent.
|
/// The script which must be satisfied for the output to be spent.
|
||||||
pub script_pubkey: ScriptBuf,
|
pub script_pubkey: ScriptBuf,
|
||||||
|
|
|
@ -17,9 +17,9 @@ use super::{
|
||||||
|
|
||||||
/// A signed amount.
|
/// A signed amount.
|
||||||
///
|
///
|
||||||
/// The [`SignedAmount`] type can be used to express Bitcoin amounts that support
|
/// The [`SignedAmount`] type can be used to express Bitcoin amounts that support arithmetic and
|
||||||
/// arithmetic and conversion to various denominations.
|
/// conversion to various denominations. The `Amount` type does not implement `serde` traits but we
|
||||||
///
|
/// do provide modules for serializing as satoshis or bitcoin.
|
||||||
///
|
///
|
||||||
/// Warning!
|
/// Warning!
|
||||||
///
|
///
|
||||||
|
@ -29,6 +29,21 @@ use super::{
|
||||||
/// start with `checked_`. The operations from [`core::ops`] that [`Amount`]
|
/// start with `checked_`. The operations from [`core::ops`] that [`Amount`]
|
||||||
/// implements will panic when overflow or underflow occurs.
|
/// implements will panic when overflow or underflow occurs.
|
||||||
///
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # #[cfg(feature = "serde")] {
|
||||||
|
/// use serde::{Serialize, Deserialize};
|
||||||
|
/// use bitcoin_units::SignedAmount;
|
||||||
|
///
|
||||||
|
/// #[derive(Serialize, Deserialize)]
|
||||||
|
/// struct Foo {
|
||||||
|
/// // If you are using `rust-bitcoin` then `bitcoin::amount::serde::as_sat` also works.
|
||||||
|
/// #[serde(with = "bitcoin_units::amount::serde::as_sat")] // Also `serde::as_btc`.
|
||||||
|
/// amount: SignedAmount,
|
||||||
|
/// }
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct SignedAmount(i64);
|
pub struct SignedAmount(i64);
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,6 @@ use alloc::string::{String, ToString};
|
||||||
use core::str::FromStr;
|
use core::str::FromStr;
|
||||||
use core::{default, fmt, ops};
|
use core::{default, fmt, ops};
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
|
||||||
use ::serde::{Deserialize, Serialize};
|
|
||||||
#[cfg(feature = "arbitrary")]
|
#[cfg(feature = "arbitrary")]
|
||||||
use arbitrary::{Arbitrary, Unstructured};
|
use arbitrary::{Arbitrary, Unstructured};
|
||||||
|
|
||||||
|
@ -21,9 +19,9 @@ use crate::{FeeRate, Weight};
|
||||||
|
|
||||||
/// An amount.
|
/// An amount.
|
||||||
///
|
///
|
||||||
/// The [`Amount`] type can be used to express Bitcoin amounts that support
|
/// The [`Amount`] type can be used to express Bitcoin amounts that support arithmetic and
|
||||||
/// arithmetic and conversion to various denominations.
|
/// conversion to various denominations. The `Amount` type does not implement `serde` traits but we
|
||||||
///
|
/// do provide modules for serializing as satoshis or bitcoin.
|
||||||
///
|
///
|
||||||
/// Warning!
|
/// Warning!
|
||||||
///
|
///
|
||||||
|
@ -36,8 +34,22 @@ use crate::{FeeRate, Weight};
|
||||||
/// zero is considered an underflow and will cause a panic if you're not using
|
/// zero is considered an underflow and will cause a panic if you're not using
|
||||||
/// the checked arithmetic methods.
|
/// the checked arithmetic methods.
|
||||||
///
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # #[cfg(feature = "serde")] {
|
||||||
|
/// use serde::{Serialize, Deserialize};
|
||||||
|
/// use bitcoin_units::Amount;
|
||||||
|
///
|
||||||
|
/// #[derive(Serialize, Deserialize)]
|
||||||
|
/// struct Foo {
|
||||||
|
/// // If you are using `rust-bitcoin` then `bitcoin::amount::serde::as_sat` also works.
|
||||||
|
/// #[serde(with = "bitcoin_units::amount::serde::as_sat")] // Also `serde::as_btc`.
|
||||||
|
/// amount: Amount,
|
||||||
|
/// }
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
||||||
pub struct Amount(u64);
|
pub struct Amount(u64);
|
||||||
|
|
||||||
impl Amount {
|
impl Amount {
|
||||||
|
|
Loading…
Reference in New Issue