From 0467e12aaef94f8279861d7fe1846dc5bf816cc1 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Thu, 4 Nov 2021 23:29:22 +0530 Subject: [PATCH] Add optional fee rate to a transaction --- src/bdk.udl | 2 +- src/lib.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/bdk.udl b/src/bdk.udl index c5cd9a2af8..dc2a996098 100644 --- a/src/bdk.udl +++ b/src/bdk.udl @@ -149,7 +149,7 @@ interface OnlineWallet { interface PartiallySignedBitcoinTransaction { [Throws=BdkError] - constructor([ByRef] OnlineWallet wallet, string recipient, u64 amount); + constructor([ByRef] OnlineWallet wallet, string recipient, u64 amount, float? fee_rate); }; dictionary ExtendedKeyInfo { diff --git a/src/lib.rs b/src/lib.rs index f1892f2c62..1510fee445 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ use bdk::keys::bip39::{Language, Mnemonic, MnemonicType}; use bdk::keys::{DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey}; use bdk::miniscript::BareCtx; use bdk::wallet::AddressIndex; -use bdk::{ConfirmationTime, Error, SignOptions, Wallet}; +use bdk::{ConfirmationTime, Error, FeeRate, SignOptions, Wallet}; use std::convert::TryFrom; use std::str::FromStr; use std::sync::{Mutex, MutexGuard}; @@ -178,13 +178,21 @@ struct PartiallySignedBitcoinTransaction { } impl PartiallySignedBitcoinTransaction { - fn new(online_wallet: &OnlineWallet, recipient: String, amount: u64) -> Result { + fn new( + online_wallet: &OnlineWallet, + recipient: String, + amount: u64, + fee_rate: Option, // satoshis per vbyte + ) -> Result { let wallet = online_wallet.get_wallet(); match Address::from_str(&recipient) { Ok(address) => { let (psbt, _) = { let mut builder = wallet.build_tx(); builder.add_recipient(address.script_pubkey(), amount); + if let Some(sat_per_vb) = fee_rate { + builder.fee_rate(FeeRate::from_sat_per_vb(sat_per_vb)); + } builder.finish()? }; Ok(PartiallySignedBitcoinTransaction {