use std::collections::{BTreeMap, BTreeSet};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::{Address, BlockHash, Txid};
use crate::{
domain::{Amount, ExactDecimal, Timestamp},
error::{Error, ValidationError},
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Satoshis(u64);
impl Satoshis {
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
pub fn from_decimal(value: &str) -> Result<Self, Error> {
let amount = Amount::from_decimal(value, Some(8))?;
u64::try_from(amount.raw())
.map(Self)
.map_err(|_| ValidationError::BitcoinAmountOverflow.into())
}
#[must_use]
pub const fn raw(self) -> u64 {
self.0
}
#[must_use]
pub fn amount(self) -> Amount {
Amount::new(crate::domain::U256::from(self.0), Some(8))
}
}
impl Serialize for Satoshis {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(&self.0)
}
}
impl<'de> Deserialize<'de> for Satoshis {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::from_decimal(&String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct MempoolDelta(i128);
impl MempoolDelta {
#[must_use]
pub fn from_sums(funded: u64, spent: u64) -> Self {
Self(i128::from(funded) - i128::from(spent))
}
#[must_use]
pub const fn raw(self) -> i128 {
self.0
}
}
impl Serialize for MempoolDelta {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(&self.0)
}
}
impl<'de> Deserialize<'de> for MempoolDelta {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let text = String::deserialize(deserializer)?;
let negative = text.starts_with('-');
let digits = text.strip_prefix('-').unwrap_or(&text);
let absolute = Satoshis::from_decimal(digits)
.map_err(serde::de::Error::custom)?
.raw();
if negative && absolute == 0 {
return Err(serde::de::Error::custom("invalid signed satoshi value"));
}
let value = i128::from(absolute);
Ok(Self(if negative { -value } else { value }))
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AddressBalance {
address: Address,
confirmed: Satoshis,
mempool_delta: MempoolDelta,
confirmed_tx_count: u64,
mempool_tx_count: u64,
}
impl AddressBalance {
pub fn from_stats(
address: Address,
funded: u64,
spent: u64,
mempool_funded: u64,
mempool_spent: u64,
confirmed_tx_count: u64,
mempool_tx_count: u64,
) -> Result<Self, Error> {
let confirmed = funded
.checked_sub(spent)
.ok_or(ValidationError::BitcoinBalanceInconsistent)?;
Ok(Self {
address,
confirmed: Satoshis::new(confirmed),
mempool_delta: MempoolDelta::from_sums(mempool_funded, mempool_spent),
confirmed_tx_count,
mempool_tx_count,
})
}
#[must_use]
pub const fn address(&self) -> &Address {
&self.address
}
#[must_use]
pub const fn confirmed(&self) -> Satoshis {
self.confirmed
}
#[must_use]
pub const fn mempool_delta(&self) -> MempoolDelta {
self.mempool_delta
}
#[must_use]
pub const fn confirmed_tx_count(&self) -> u64 {
self.confirmed_tx_count
}
#[must_use]
pub const fn mempool_tx_count(&self) -> u64 {
self.mempool_tx_count
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BlockReference {
height: u64,
hash: BlockHash,
timestamp: Timestamp,
}
impl BlockReference {
#[must_use]
pub const fn new(height: u64, hash: BlockHash, timestamp: Timestamp) -> Self {
Self {
height,
hash,
timestamp,
}
}
#[must_use]
pub const fn height(self) -> u64 {
self.height
}
#[must_use]
pub const fn hash(self) -> BlockHash {
self.hash
}
#[must_use]
pub const fn timestamp(self) -> Timestamp {
self.timestamp
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(
tag = "kind",
content = "block",
rename_all = "snake_case",
deny_unknown_fields
)]
pub enum TransactionStatus {
Unconfirmed,
Confirmed(BlockReference),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum HistoryCursor {
Recent,
Confirmed {
after: Option<Txid>,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Completeness {
Complete,
MayHaveMore,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HistoryEntry {
txid: Txid,
fee: Satoshis,
status: TransactionStatus,
}
impl HistoryEntry {
#[must_use]
pub const fn new(txid: Txid, fee: Satoshis, status: TransactionStatus) -> Self {
Self { txid, fee, status }
}
#[must_use]
pub const fn txid(&self) -> Txid {
self.txid
}
#[must_use]
pub const fn fee(&self) -> Satoshis {
self.fee
}
#[must_use]
pub const fn status(&self) -> TransactionStatus {
self.status
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "HistoryFields")]
pub struct HistoryPage {
address: Address,
cursor: HistoryCursor,
entries: Vec<HistoryEntry>,
}
impl HistoryPage {
pub fn new(
address: Address,
cursor: HistoryCursor,
entries: Vec<HistoryEntry>,
) -> Result<Self, Error> {
let mut seen = BTreeSet::new();
let mut confirmed = 0;
let mut mempool = 0;
for entry in &entries {
if !seen.insert(entry.txid) {
return Err(ValidationError::InvalidBitcoinHistory.into());
}
match entry.status {
TransactionStatus::Confirmed(_) => confirmed += 1,
TransactionStatus::Unconfirmed => mempool += 1,
}
}
if confirmed > 25
|| mempool > 50
|| (matches!(cursor, HistoryCursor::Confirmed { .. }) && mempool != 0)
{
return Err(ValidationError::InvalidBitcoinHistory.into());
}
if let HistoryCursor::Confirmed { after: Some(after) } = cursor
&& seen.contains(&after)
{
return Err(ValidationError::InvalidBitcoinHistory.into());
}
Ok(Self {
address,
cursor,
entries,
})
}
#[must_use]
pub const fn address(&self) -> &Address {
&self.address
}
#[must_use]
pub const fn cursor(&self) -> HistoryCursor {
self.cursor
}
#[must_use]
pub fn entries(&self) -> &[HistoryEntry] {
&self.entries
}
#[must_use]
pub fn confirmed_completeness(&self) -> Completeness {
if self
.entries
.iter()
.filter(|entry| matches!(entry.status, TransactionStatus::Confirmed(_)))
.count()
== 25
{
Completeness::MayHaveMore
} else {
Completeness::Complete
}
}
#[must_use]
pub fn mempool_completeness(&self) -> Option<Completeness> {
matches!(self.cursor, HistoryCursor::Recent).then(|| {
if self
.entries
.iter()
.filter(|entry| entry.status == TransactionStatus::Unconfirmed)
.count()
== 50
{
Completeness::MayHaveMore
} else {
Completeness::Complete
}
})
}
#[must_use]
pub fn next_cursor(&self) -> Option<HistoryCursor> {
if self.confirmed_completeness() == Completeness::Complete {
return None;
}
self.entries
.iter()
.rev()
.find(|entry| matches!(entry.status, TransactionStatus::Confirmed(_)))
.map(|entry| HistoryCursor::Confirmed {
after: Some(entry.txid),
})
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct HistoryFields {
address: Address,
cursor: HistoryCursor,
entries: Vec<HistoryEntry>,
}
impl TryFrom<HistoryFields> for HistoryPage {
type Error = Error;
fn try_from(value: HistoryFields) -> Result<Self, Error> {
Self::new(value.address, value.cursor, value.entries)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(transparent)]
pub struct FeeEstimates(BTreeMap<u64, ExactDecimal>);
impl FeeEstimates {
pub fn new(entries: impl IntoIterator<Item = (u64, ExactDecimal)>) -> Result<Self, Error> {
let mut values = BTreeMap::new();
for (target, rate) in entries {
if target == 0 || rate.is_negative() || values.insert(target, rate).is_some() {
return Err(ValidationError::InvalidBitcoinFeeEstimate.into());
}
}
Ok(Self(values))
}
#[must_use]
pub const fn rates(&self) -> &BTreeMap<u64, ExactDecimal> {
&self.0
}
}
impl<'de> Deserialize<'de> for FeeEstimates {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct FeesVisitor;
impl<'de> serde::de::Visitor<'de> for FeesVisitor {
type Value = FeeEstimates;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("exact Bitcoin fee estimates")
}
fn visit_map<A: serde::de::MapAccess<'de>>(
self,
mut map: A,
) -> Result<Self::Value, A::Error> {
let mut entries = Vec::new();
while let Some((key, rate)) = map.next_entry::<String, ExactDecimal>()? {
let target = canonical_target(&key).map_err(serde::de::Error::custom)?;
entries.push((target, rate));
}
FeeEstimates::new(entries).map_err(serde::de::Error::custom)
}
}
deserializer.deserialize_map(FeesVisitor)
}
}
pub(crate) fn canonical_target(value: &str) -> Result<u64, Error> {
if value.is_empty()
|| value.starts_with('0')
|| !value.bytes().all(|byte| byte.is_ascii_digit())
{
return Err(ValidationError::InvalidBitcoinFeeEstimate.into());
}
value
.parse()
.map_err(|_| ValidationError::InvalidBitcoinFeeEstimate.into())
}