if let Some(previous_fee) = params.bumping_fee {
if fee < previous_fee.absolute {
return Err(CreateTxError::FeeTooLow {
- required: Amount::from_sat(previous_fee.absolute),
+ required: previous_fee.absolute,
});
}
}
});
}
}
- (rate, 0)
+ (rate, Amount::ZERO)
}
};
}
if self.is_mine(script_pubkey.clone()) {
- received += Amount::from_sat(value);
+ received += value;
}
let new_out = TxOut {
script_pubkey: script_pubkey.clone(),
- value: Amount::from_sat(value),
+ value,
};
tx.output.push(new_out);
- outgoing += Amount::from_sat(value);
+ outgoing += value;
}
- fee_amount += (fee_rate * tx.weight()).to_sat();
+ fee_amount += fee_rate * tx.weight();
let (required_utxos, optional_utxos) =
self.preselect_utxos(¶ms, Some(current_height.to_consensus_u32()));
required_utxos.clone(),
optional_utxos.clone(),
fee_rate,
- outgoing.to_sat() + fee_amount,
+ outgoing.to_sat() + fee_amount.to_sat(),
&drain_script,
) {
Ok(res) => res,
coin_selection::single_random_draw(
required_utxos,
optional_utxos,
- outgoing.to_sat() + fee_amount,
+ outgoing.to_sat() + fee_amount.to_sat(),
&drain_script,
fee_rate,
rng,
}
},
};
- fee_amount += coin_selection.fee_amount;
+ fee_amount += Amount::from_sat(coin_selection.fee_amount);
let excess = &coin_selection.excess;
tx.input = coin_selection
match excess {
NoChange {
remaining_amount, ..
- } => fee_amount += remaining_amount,
+ } => fee_amount += Amount::from_sat(*remaining_amount),
Change { amount, fee } => {
if self.is_mine(drain_script.clone()) {
received += Amount::from_sat(*amount);
}
- fee_amount += fee;
+ fee_amount += Amount::from_sat(*fee);
// create drain output
let drain_output = TxOut {
recipients: tx
.output
.into_iter()
- .map(|txout| (txout.script_pubkey, txout.value.to_sat()))
+ .map(|txout| (txout.script_pubkey, txout.value))
.collect(),
utxos: original_utxos,
bumping_fee: Some(tx_builder::PreviousFee {
- absolute: fee.to_sat(),
+ absolute: fee,
rate: fee_rate,
}),
..Default::default()
//TODO: TxParams should eventually be exposed publicly.
#[derive(Default, Debug, Clone)]
pub(crate) struct TxParams {
- pub(crate) recipients: Vec<(ScriptBuf, u64)>,
+ pub(crate) recipients: Vec<(ScriptBuf, Amount)>,
pub(crate) drain_wallet: bool,
pub(crate) drain_to: Option<ScriptBuf>,
pub(crate) fee_policy: Option<FeePolicy>,
#[derive(Clone, Copy, Debug)]
pub(crate) struct PreviousFee {
- pub absolute: u64,
+ pub absolute: Amount,
pub rate: FeeRate,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum FeePolicy {
FeeRate(FeeRate),
- FeeAmount(u64),
+ FeeAmount(Amount),
}
impl Default for FeePolicy {
/// overshoot it slightly since adding a change output to drain the remaining
/// excess might not be viable.
pub fn fee_absolute(&mut self, fee_amount: Amount) -> &mut Self {
- self.params.fee_policy = Some(FeePolicy::FeeAmount(fee_amount.to_sat()));
+ self.params.fee_policy = Some(FeePolicy::FeeAmount(fee_amount));
self
}
/// Replace the recipients already added with a new list
pub fn set_recipients(&mut self, recipients: Vec<(ScriptBuf, Amount)>) -> &mut Self {
- self.params.recipients = recipients
- .into_iter()
- .map(|(script, amount)| (script, amount.to_sat()))
- .collect();
+ self.params.recipients = recipients;
self
}
/// Add a recipient to the internal list
pub fn add_recipient(&mut self, script_pubkey: ScriptBuf, amount: Amount) -> &mut Self {
- self.params
- .recipients
- .push((script_pubkey, amount.to_sat()));
+ self.params.recipients.push((script_pubkey, amount));
self
}
// licenses.
use bitcoin::secp256k1::{All, Secp256k1};
-use bitcoin::{absolute, relative, Script, Sequence};
+use bitcoin::{absolute, relative, Amount, Script, Sequence};
use miniscript::{MiniscriptKey, Satisfier, ToPublicKey};
fn is_dust(&self, script: &Script) -> bool;
}
+impl IsDust for Amount {
+ fn is_dust(&self, script: &Script) -> bool {
+ *self < script.minimal_non_dust()
+ }
+}
+
impl IsDust for u64 {
fn is_dust(&self, script: &Script) -> bool {
- *self < script.minimal_non_dust().to_sat()
+ Amount::from_sat(*self).is_dust(script)
}
}
use bdk_wallet::file_store::Store;
use bdk_wallet::Wallet;
use std::io::Write;
-use std::str::FromStr;
use bdk_electrum::electrum_client;
use bdk_electrum::BdkElectrumClient;
+use bdk_wallet::bitcoin::Amount;
use bdk_wallet::bitcoin::Network;
-use bdk_wallet::bitcoin::{Address, Amount};
use bdk_wallet::chain::collections::HashSet;
use bdk_wallet::{KeychainKind, SignOptions};
println!("Generated Address: {}", address);
let balance = wallet.balance();
- println!("Wallet balance before syncing: {} sats", balance.total());
+ println!("Wallet balance before syncing: {}", balance.total());
print!("Syncing...");
let client = BdkElectrumClient::new(electrum_client::Client::new(ELECTRUM_URL)?);
wallet.persist(&mut db)?;
let balance = wallet.balance();
- println!("Wallet balance after syncing: {} sats", balance.total());
+ println!("Wallet balance after syncing: {}", balance.total());
if balance.total() < SEND_AMOUNT {
println!(
- "Please send at least {} sats to the receiving address",
+ "Please send at least {} to the receiving address",
SEND_AMOUNT
);
std::process::exit(0);
}
- let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?
- .require_network(Network::Testnet)?;
-
let mut tx_builder = wallet.build_tx();
tx_builder
- .add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
+ .add_recipient(address.script_pubkey(), SEND_AMOUNT)
.enable_rbf();
let mut psbt = tx_builder.finish()?;
println!("Next unused address: ({}) {}", address.index, address);
let balance = wallet.balance();
- println!("Wallet balance before syncing: {} sats", balance.total());
+ println!("Wallet balance before syncing: {}", balance.total());
print!("Syncing...");
let client = esplora_client::Builder::new(ESPLORA_URL).build_async()?;
println!();
let balance = wallet.balance();
- println!("Wallet balance after syncing: {} sats", balance.total());
+ println!("Wallet balance after syncing: {}", balance.total());
if balance.total() < SEND_AMOUNT {
println!(
- "Please send at least {} sats to the receiving address",
+ "Please send at least {} to the receiving address",
SEND_AMOUNT
);
std::process::exit(0);
);
let balance = wallet.balance();
- println!("Wallet balance before syncing: {} sats", balance.total());
+ println!("Wallet balance before syncing: {}", balance.total());
print!("Syncing...");
let client = esplora_client::Builder::new(ESPLORA_URL).build_blocking();
let update = client.full_scan(request, STOP_GAP, PARALLEL_REQUESTS)?;
wallet.apply_update(update)?;
- if let Some(changeset) = wallet.take_staged() {
- db.append_changeset(&changeset)?;
- }
+ wallet.persist(&mut db)?;
println!();
let balance = wallet.balance();
- println!("Wallet balance after syncing: {} sats", balance.total());
+ println!("Wallet balance after syncing: {}", balance.total());
if balance.total() < SEND_AMOUNT {
println!(
- "Please send at least {} sats to the receiving address",
+ "Please send at least {} to the receiving address",
SEND_AMOUNT
);
std::process::exit(0);
);
let balance = wallet.balance();
- println!("Wallet balance before syncing: {} sats", balance.total());
+ println!("Wallet balance before syncing: {}", balance.total());
let wallet_tip = wallet.latest_checkpoint();
println!(
wallet_tip_end.height(),
wallet_tip_end.hash()
);
- println!("Wallet balance is {} sats", balance.total());
+ println!("Wallet balance is {}", balance.total());
println!(
"Wallet has {} transactions and {} utxos",
wallet.transactions().count(),