Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/anti_fee_sniping.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]
use bdk_testenv::{bitcoincore_rpc::RpcApi, TestEnv};
use bdk_tx::{
filter_unspendable_now, group_by_spk, selection_algorithm_lowest_fee_bnb, FeeStrategy, Output,
filter_unspendable, group_by_spk, selection_algorithm_lowest_fee_bnb, FeeStrategy, Output,
PsbtParams, ScriptSource, SelectorParams,
};
use bitcoin::{absolute::LockTime, key::Secp256k1, Amount, FeeRate, Sequence};
Expand Down Expand Up @@ -71,7 +71,7 @@ fn main() -> anyhow::Result<()> {
let selection = wallet
.all_candidates()
.regroup(group_by_spk())
.filter(filter_unspendable_now(tip_height, tip_time))
.filter(filter_unspendable(tip_height, Some(tip_time)))
.into_selection(
selection_algorithm_lowest_fee_bnb(longterm_feerate, 100_000),
SelectorParams::new(
Expand Down
32 changes: 20 additions & 12 deletions examples/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use bdk_chain::{
};
use bdk_coin_select::{ChangePolicy, DrainWeights};
use bdk_testenv::{bitcoincore_rpc::RpcApi, TestEnv};
use bdk_tx::{CanonicalUnspents, Input, InputCandidates, RbfParams, TxStatus, TxWithStatus};
use bdk_tx::{
CanonicalUnspents, ConfirmationStatus, Input, InputCandidates, RbfParams, TxWithStatus,
};
use bitcoin::{absolute, Address, Amount, BlockHash, OutPoint, Transaction, TxOut, Txid};
use miniscript::{
plan::{Assets, Plan},
Expand Down Expand Up @@ -71,17 +73,22 @@ impl Wallet {
)
}

/// TODO: Add to chain sources.
/// Info for the block at the tip.
///
/// Returns a tuple of:
/// - Tip's height. I.e. `tip.height`
/// - Tip's MTP. I.e. `MTP(tip.height)`
pub fn tip_info(
&self,
client: &impl RpcApi,
) -> anyhow::Result<(absolute::Height, absolute::Time)> {
let tip = self.chain.tip().block_id();
let tip_info = client.get_block_header_info(&tip.hash)?;
let tip_height = absolute::Height::from_consensus(tip.height)?;
let tip_time =
absolute::Time::from_consensus(tip_info.median_time.unwrap_or(tip_info.time) as _)?;
Ok((tip_height, tip_time))
let tip_hash = self.chain.tip().block_id().hash;
let tip_info = client.get_block_header_info(&tip_hash)?;
let tip_height = absolute::Height::from_consensus(tip_info.height as u32)?;
let tip_mtp = absolute::Time::from_consensus(
tip_info.median_time.expect("must have median time") as _,
)?;
Ok((tip_height, tip_mtp))
}

// TODO: Maybe create an `AssetsBuilder` or `AssetsExt` that makes it easier to add
Expand Down Expand Up @@ -112,15 +119,16 @@ impl Wallet {
}

pub fn canonical_txs(&self) -> impl Iterator<Item = TxWithStatus<Arc<Transaction>>> + '_ {
pub fn status_from_position(pos: ChainPosition<ConfirmationBlockTime>) -> Option<TxStatus> {
pub fn status_from_position(
pos: ChainPosition<ConfirmationBlockTime>,
) -> Option<ConfirmationStatus> {
match pos {
bdk_chain::ChainPosition::Confirmed { anchor, .. } => Some(TxStatus {
bdk_chain::ChainPosition::Confirmed { anchor, .. } => Some(ConfirmationStatus {
height: absolute::Height::from_consensus(
anchor.confirmation_height_upper_bound(),
)
.expect("must convert to height"),
time: absolute::Time::from_consensus(anchor.confirmation_time as _)
.expect("must convert from time"),
prev_mtp: None, // TODO: Use `CheckPoint::prev_mtp`
}),
bdk_chain::ChainPosition::Unconfirmed { .. } => None,
}
Expand Down
6 changes: 3 additions & 3 deletions examples/synopsis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bdk_testenv::{bitcoincore_rpc::RpcApi, TestEnv};
use bdk_tx::{
filter_unspendable_now, group_by_spk, selection_algorithm_lowest_fee_bnb, FeeStrategy, Output,
filter_unspendable, group_by_spk, selection_algorithm_lowest_fee_bnb, FeeStrategy, Output,
PsbtParams, ScriptSource, SelectorParams, Signer,
};
use bitcoin::{key::Secp256k1, Amount, FeeRate, Sequence};
Expand Down Expand Up @@ -39,7 +39,7 @@ fn main() -> anyhow::Result<()> {
println!("Received {txid}");
println!("Balance (pending): {}", wallet.balance());

let (tip_height, tip_time) = wallet.tip_info(env.rpc_client())?;
let (tip_height, tip_mtp) = wallet.tip_info(env.rpc_client())?;
let longterm_feerate = FeeRate::from_sat_per_vb_unchecked(1);

let recipient_addr = env
Expand All @@ -51,7 +51,7 @@ fn main() -> anyhow::Result<()> {
let selection = wallet
.all_candidates()
.regroup(group_by_spk())
.filter(filter_unspendable_now(tip_height, tip_time))
.filter(filter_unspendable(tip_height, Some(tip_mtp)))
.into_selection(
selection_algorithm_lowest_fee_bnb(longterm_feerate, 100_000),
SelectorParams::new(
Expand Down
7 changes: 4 additions & 3 deletions src/canonical_unspents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ use bitcoin::{psbt, OutPoint, Sequence, Transaction, TxOut, Txid};
use miniscript::{bitcoin, plan::Plan};

use crate::{
collections::HashMap, input::CoinbaseMismatch, FromPsbtInputError, Input, RbfSet, TxStatus,
collections::HashMap, input::CoinbaseMismatch, ConfirmationStatus, FromPsbtInputError, Input,
RbfSet,
};

/// Tx with confirmation status.
pub type TxWithStatus<T> = (T, Option<TxStatus>);
pub type TxWithStatus<T> = (T, Option<ConfirmationStatus>);

/// Our canonical view of unspent outputs.
#[derive(Debug, Clone)]
pub struct CanonicalUnspents {
txs: HashMap<Txid, Arc<Transaction>>,
statuses: HashMap<Txid, TxStatus>,
statuses: HashMap<Txid, ConfirmationStatus>,
spends: HashMap<OutPoint, Txid>,
}

Expand Down
Loading