2020-08-06 13:09:39 +02:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
use bitcoin::{Address, OutPoint, SigHashType};
|
2020-08-06 13:09:39 +02:00
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm};
|
|
|
|
|
|
|
|
// TODO: add a flag to ignore change outputs (make them unspendable)
|
2020-08-06 13:09:39 +02:00
|
|
|
#[derive(Debug, Default)]
|
2020-08-06 16:56:41 +02:00
|
|
|
pub struct TxBuilder<Cs: CoinSelectionAlgorithm> {
|
2020-08-06 13:09:39 +02:00
|
|
|
pub(crate) addressees: Vec<(Address, u64)>,
|
|
|
|
pub(crate) send_all: bool,
|
|
|
|
pub(crate) fee_perkb: Option<f32>,
|
|
|
|
pub(crate) policy_path: Option<BTreeMap<String, Vec<usize>>>,
|
|
|
|
pub(crate) utxos: Option<Vec<OutPoint>>,
|
|
|
|
pub(crate) unspendable: Option<Vec<OutPoint>>,
|
2020-08-06 16:56:41 +02:00
|
|
|
pub(crate) sighash: Option<SigHashType>,
|
|
|
|
pub(crate) shuffle_outputs: Option<bool>,
|
|
|
|
pub(crate) coin_selection: Cs,
|
2020-08-06 13:09:39 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
impl TxBuilder<DefaultCoinSelectionAlgorithm> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
2020-08-06 13:09:39 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn from_addressees(addressees: Vec<(Address, u64)>) -> Self {
|
|
|
|
Self::default().set_addressees(addressees)
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 13:09:39 +02:00
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
impl<Cs: CoinSelectionAlgorithm> TxBuilder<Cs> {
|
|
|
|
pub fn set_addressees(mut self, addressees: Vec<(Address, u64)>) -> Self {
|
|
|
|
self.addressees = addressees;
|
|
|
|
self
|
2020-08-06 13:09:39 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn add_addressee(mut self, address: Address, amount: u64) -> Self {
|
2020-08-06 13:09:39 +02:00
|
|
|
self.addressees.push((address, amount));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn send_all(mut self, send_all: bool) -> Self {
|
|
|
|
self.send_all = send_all;
|
2020-08-06 13:09:39 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn fee_rate(mut self, satoshi_per_vbyte: f32) -> Self {
|
2020-08-06 13:09:39 +02:00
|
|
|
self.fee_perkb = Some(satoshi_per_vbyte * 1e3);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn fee_rate_perkb(mut self, satoshi_per_kb: f32) -> Self {
|
2020-08-06 13:09:39 +02:00
|
|
|
self.fee_perkb = Some(satoshi_per_kb);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn policy_path(mut self, policy_path: BTreeMap<String, Vec<usize>>) -> Self {
|
2020-08-06 13:09:39 +02:00
|
|
|
self.policy_path = Some(policy_path);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn utxos(mut self, utxos: Vec<OutPoint>) -> Self {
|
2020-08-06 13:09:39 +02:00
|
|
|
self.utxos = Some(utxos);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn add_utxo(mut self, utxo: OutPoint) -> Self {
|
2020-08-06 13:09:39 +02:00
|
|
|
self.utxos.get_or_insert(vec![]).push(utxo);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn unspendable(mut self, unspendable: Vec<OutPoint>) -> Self {
|
2020-08-06 13:09:39 +02:00
|
|
|
self.unspendable = Some(unspendable);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:56:41 +02:00
|
|
|
pub fn add_unspendable(mut self, unspendable: OutPoint) -> Self {
|
2020-08-06 13:09:39 +02:00
|
|
|
self.unspendable.get_or_insert(vec![]).push(unspendable);
|
|
|
|
self
|
|
|
|
}
|
2020-08-06 16:56:41 +02:00
|
|
|
|
|
|
|
pub fn sighash(mut self, sighash: SigHashType) -> Self {
|
|
|
|
self.sighash = Some(sighash);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do_not_shuffle_outputs(mut self) -> Self {
|
|
|
|
self.shuffle_outputs = Some(false);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn coin_selection<P: CoinSelectionAlgorithm>(self, coin_selection: P) -> TxBuilder<P> {
|
|
|
|
TxBuilder {
|
|
|
|
addressees: self.addressees,
|
|
|
|
send_all: self.send_all,
|
|
|
|
fee_perkb: self.fee_perkb,
|
|
|
|
policy_path: self.policy_path,
|
|
|
|
utxos: self.utxos,
|
|
|
|
unspendable: self.unspendable,
|
|
|
|
sighash: self.sighash,
|
|
|
|
shuffle_outputs: self.shuffle_outputs,
|
|
|
|
coin_selection,
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 13:09:39 +02:00
|
|
|
}
|