// Magical Bitcoin Library // Written in 2020 by // Alekos Filini // // Copyright (c) 2020 Magical Bitcoin // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //! Transaction builder //! //! ## Example //! //! ``` //! # use std::str::FromStr; //! # use bitcoin::*; //! # use bdk::*; //! # use bdk::wallet::tx_builder::CreateTx; //! # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap(); //! // Create a transaction with one output to `to_address` of 50_000 satoshi, with a custom fee rate //! // of 5.0 satoshi/vbyte, only spending non-change outputs and with RBF signaling //! // enabled //! let builder = TxBuilder::with_recipients(vec![(to_address.script_pubkey(), 50_000)]) //! .fee_rate(FeeRate::from_sat_per_vb(5.0)) //! .do_not_spend_change() //! .enable_rbf(); //! # let builder: TxBuilder = builder; //! ``` use std::collections::BTreeMap; use std::collections::HashSet; use std::default::Default; use std::marker::PhantomData; use bitcoin::{OutPoint, Script, SigHashType, Transaction}; use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm}; use crate::database::Database; use crate::types::{FeeRate, ScriptType, UTXO}; /// Context in which the [`TxBuilder`] is valid pub trait TxBuilderContext: std::fmt::Debug + Default + Clone {} /// [`Wallet::create_tx`](super::Wallet::create_tx) context #[derive(Debug, Default, Clone)] pub struct CreateTx; impl TxBuilderContext for CreateTx {} /// [`Wallet::bump_fee`](super::Wallet::bump_fee) context #[derive(Debug, Default, Clone)] pub struct BumpFee; impl TxBuilderContext for BumpFee {} /// A transaction builder /// /// This structure contains the configuration that the wallet must follow to build a transaction. /// /// For an example see [this module](super::tx_builder)'s documentation; #[derive(Debug)] pub struct TxBuilder, Ctx: TxBuilderContext> { pub(crate) recipients: Vec<(Script, u64)>, pub(crate) drain_wallet: bool, pub(crate) single_recipient: Option