use bitcoin::util::amount::Amount;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(
Serialize, Deserialize, JsonSchema, Clone, Copy, Debug, Ord, PartialOrd, PartialEq, Eq,
)]
#[serde(transparent)]
pub struct AmountF64(
)
#[schemars(with = "f64")]
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
Amount,
);
impl From<Amount> for AmountF64 {
fn from(a: Amount) -> AmountF64 {
AmountF64(a)
}
}
impl From<AmountF64> for Amount {
fn from(a: AmountF64) -> Amount {
a.0
}
}
#[derive(
Serialize, Deserialize, JsonSchema, Clone, Copy, Debug, Ord, PartialOrd, PartialEq, Eq,
)]
#[serde(transparent)]
pub struct AmountU64(
)
#[schemars(with = "u64")]
#[serde(with = "bitcoin::util::amount::serde::as_sat")]
Amount,
);
impl From<Amount> for AmountU64 {
fn from(a: Amount) -> AmountU64 {
AmountU64(a)
}
}
impl From<u64> for AmountU64 {
fn from(a: u64) -> Self {
AmountU64(Amount::from_sat(a))
}
}
impl From<AmountU64> for Amount {
fn from(a: AmountU64) -> Amount {
a.0
}
}
impl From<AmountU64> for u64 {
fn from(a: AmountU64) -> u64 {
a.0.as_sat()
}
}
#[derive(Serialize, Deserialize, JsonSchema, Clone, Copy, Debug)]
pub struct AmountRange {
#[serde(rename = "min_btc", skip_serializing_if = "Option::is_none", default)]
min: Option<AmountF64>,
#[serde(rename = "max_btc", skip_serializing_if = "Option::is_none", default)]
max: Option<AmountF64>,
}
impl AmountRange {
pub fn new() -> AmountRange {
AmountRange {
min: None,
max: None,
}
}
pub fn update_range(&mut self, amount: Amount) {
self.min = std::cmp::min(self.min, Some(amount.into()));
self.max = std::cmp::max(self.max, Some(amount.into()));
}
pub fn max(&self) -> Amount {
self.max.unwrap_or(Amount::min_value().into()).0
}
}