Skip to content

Commit

Permalink
Merge pull request #67 from bitcoindevkit/allow-passing-a-fee-rate-wh…
Browse files Browse the repository at this point in the history
…en-creating-a-transaction

Add optional fee rate to a transaction
  • Loading branch information
artfuldev authored Nov 4, 2021
2 parents 94b07b9 + 0467e12 commit f6b099a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -178,13 +178,21 @@ struct PartiallySignedBitcoinTransaction {
}

impl PartiallySignedBitcoinTransaction {
fn new(online_wallet: &OnlineWallet, recipient: String, amount: u64) -> Result<Self, Error> {
fn new(
online_wallet: &OnlineWallet,
recipient: String,
amount: u64,
fee_rate: Option<f32>, // satoshis per vbyte
) -> Result<Self, Error> {
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 {
Expand Down

0 comments on commit f6b099a

Please sign in to comment.