From 1d5a3cd934105942462f2f7ae1fee29aecc3884d Mon Sep 17 00:00:00 2001 From: startup-dreamer Date: Sun, 14 Jan 2024 02:33:28 +0530 Subject: [PATCH] chore: move Balance out of keychain module --- crates/bdk/src/wallet/mod.rs | 2 +- crates/bitcoind_rpc/tests/test_emitter.rs | 3 +- crates/chain/src/keychain.rs | 56 ----------------- crates/chain/src/lib.rs | 1 + crates/chain/src/tx_graph.rs | 60 ++++++++++++++++++- crates/chain/tests/test_indexed_tx_graph.rs | 4 +- crates/chain/tests/test_tx_graph_conflicts.rs | 2 +- 7 files changed, 64 insertions(+), 64 deletions(-) diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index ce152cb5e..aeb6d7544 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -19,7 +19,7 @@ use alloc::{ sync::Arc, vec::Vec, }; -pub use bdk_chain::keychain::Balance; +pub use bdk_chain::Balance; use bdk_chain::{ indexed_tx_graph, keychain::{self, KeychainTxOutIndex}, diff --git a/crates/bitcoind_rpc/tests/test_emitter.rs b/crates/bitcoind_rpc/tests/test_emitter.rs index 521124e5d..2dac3cefa 100644 --- a/crates/bitcoind_rpc/tests/test_emitter.rs +++ b/crates/bitcoind_rpc/tests/test_emitter.rs @@ -3,9 +3,8 @@ use std::collections::{BTreeMap, BTreeSet}; use bdk_bitcoind_rpc::Emitter; use bdk_chain::{ bitcoin::{Address, Amount, BlockHash, Txid}, - keychain::Balance, local_chain::{self, CheckPoint, LocalChain}, - Append, BlockId, IndexedTxGraph, SpkTxOutIndex, + Append, Balance, BlockId, IndexedTxGraph, SpkTxOutIndex, }; use bitcoin::{ address::NetworkChecked, block::Header, hash_types::TxMerkleNode, hashes::Hash, diff --git a/crates/chain/src/keychain.rs b/crates/chain/src/keychain.rs index 63972a0ad..aad933a68 100644 --- a/crates/chain/src/keychain.rs +++ b/crates/chain/src/keychain.rs @@ -80,62 +80,6 @@ impl AsRef> for ChangeSet { } } -/// Balance, differentiated into various categories. -#[derive(Debug, PartialEq, Eq, Clone, Default)] -#[cfg_attr( - feature = "serde", - derive(serde::Deserialize, serde::Serialize), - serde(crate = "serde_crate",) -)] -pub struct Balance { - /// All coinbase outputs not yet matured - pub immature: u64, - /// Unconfirmed UTXOs generated by a wallet tx - pub trusted_pending: u64, - /// Unconfirmed UTXOs received from an external wallet - pub untrusted_pending: u64, - /// Confirmed and immediately spendable balance - pub confirmed: u64, -} - -impl Balance { - /// Get sum of trusted_pending and confirmed coins. - /// - /// This is the balance you can spend right now that shouldn't get cancelled via another party - /// double spending it. - pub fn trusted_spendable(&self) -> u64 { - self.confirmed + self.trusted_pending - } - - /// Get the whole balance visible to the wallet. - pub fn total(&self) -> u64 { - self.confirmed + self.trusted_pending + self.untrusted_pending + self.immature - } -} - -impl core::fmt::Display for Balance { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!( - f, - "{{ immature: {}, trusted_pending: {}, untrusted_pending: {}, confirmed: {} }}", - self.immature, self.trusted_pending, self.untrusted_pending, self.confirmed - ) - } -} - -impl core::ops::Add for Balance { - type Output = Self; - - fn add(self, other: Self) -> Self { - Self { - immature: self.immature + other.immature, - trusted_pending: self.trusted_pending + other.trusted_pending, - untrusted_pending: self.untrusted_pending + other.untrusted_pending, - confirmed: self.confirmed + other.confirmed, - } - } -} - #[cfg(test)] mod test { use super::*; diff --git a/crates/chain/src/lib.rs b/crates/chain/src/lib.rs index 206566971..0dc5ec4f8 100644 --- a/crates/chain/src/lib.rs +++ b/crates/chain/src/lib.rs @@ -32,6 +32,7 @@ pub mod local_chain; mod tx_data_traits; pub mod tx_graph; pub use tx_data_traits::*; +pub use tx_graph::Balance; pub use tx_graph::TxGraph; mod chain_oracle; pub use chain_oracle::*; diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index ef15d7e7d..bb9013965 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -73,8 +73,8 @@ //! [`insert_txout`]: TxGraph::insert_txout use crate::{ - collections::*, keychain::Balance, local_chain::LocalChain, Anchor, Append, BlockId, - ChainOracle, ChainPosition, FullTxOut, + collections::*, local_chain::LocalChain, Anchor, Append, BlockId, ChainOracle, ChainPosition, + FullTxOut, }; use alloc::collections::vec_deque::VecDeque; use alloc::vec::Vec; @@ -134,6 +134,62 @@ impl<'a, T, A> Deref for TxNode<'a, T, A> { } } +/// Balance, differentiated into various categories. +#[derive(Debug, PartialEq, Eq, Clone, Default)] +#[cfg_attr( + feature = "serde", + derive(serde::Deserialize, serde::Serialize), + serde(crate = "serde_crate",) +)] +pub struct Balance { + /// All coinbase outputs not yet matured + pub immature: u64, + /// Unconfirmed UTXOs generated by a wallet tx + pub trusted_pending: u64, + /// Unconfirmed UTXOs received from an external wallet + pub untrusted_pending: u64, + /// Confirmed and immediately spendable balance + pub confirmed: u64, +} + +impl Balance { + /// Get sum of trusted_pending and confirmed coins. + /// + /// This is the balance you can spend right now that shouldn't get cancelled via another party + /// double spending it. + pub fn trusted_spendable(&self) -> u64 { + self.confirmed + self.trusted_pending + } + + /// Get the whole balance visible to the wallet. + pub fn total(&self) -> u64 { + self.confirmed + self.trusted_pending + self.untrusted_pending + self.immature + } +} + +impl core::fmt::Display for Balance { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!( + f, + "{{ immature: {}, trusted_pending: {}, untrusted_pending: {}, confirmed: {} }}", + self.immature, self.trusted_pending, self.untrusted_pending, self.confirmed + ) + } +} + +impl core::ops::Add for Balance { + type Output = Self; + + fn add(self, other: Self) -> Self { + Self { + immature: self.immature + other.immature, + trusted_pending: self.trusted_pending + other.trusted_pending, + untrusted_pending: self.untrusted_pending + other.untrusted_pending, + confirmed: self.confirmed + other.confirmed, + } + } +} + /// Internal representation of a transaction node of a [`TxGraph`]. /// /// This can either be a whole transaction, or a partial transaction (where we only have select diff --git a/crates/chain/tests/test_indexed_tx_graph.rs b/crates/chain/tests/test_indexed_tx_graph.rs index 41b1d4d3e..ca43a39b5 100644 --- a/crates/chain/tests/test_indexed_tx_graph.rs +++ b/crates/chain/tests/test_indexed_tx_graph.rs @@ -5,9 +5,9 @@ use std::collections::BTreeSet; use bdk_chain::{ indexed_tx_graph::{self, IndexedTxGraph}, - keychain::{self, Balance, KeychainTxOutIndex}, + keychain::{self, KeychainTxOutIndex}, local_chain::LocalChain, - tx_graph, BlockId, ChainPosition, ConfirmationHeightAnchor, + tx_graph, Balance, BlockId, ChainPosition, ConfirmationHeightAnchor, }; use bitcoin::{secp256k1::Secp256k1, OutPoint, Script, ScriptBuf, Transaction, TxIn, TxOut}; use miniscript::Descriptor; diff --git a/crates/chain/tests/test_tx_graph_conflicts.rs b/crates/chain/tests/test_tx_graph_conflicts.rs index 8ac440f3e..287dd1814 100644 --- a/crates/chain/tests/test_tx_graph_conflicts.rs +++ b/crates/chain/tests/test_tx_graph_conflicts.rs @@ -3,7 +3,7 @@ mod common; use std::collections::{BTreeSet, HashSet}; -use bdk_chain::{keychain::Balance, BlockId}; +use bdk_chain::{Balance, BlockId}; use bitcoin::{OutPoint, Script}; use common::*;