Add BumpFeeTxBuilder

This commit is contained in:
Steve Myers 2022-05-02 19:23:47 -07:00
parent 3b243efefd
commit 9f903932dc
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
3 changed files with 85 additions and 1 deletions

View File

@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Update BDK to version 0.18.0
- Add BumpFeeTxBuilder to bump the fee on an unconfirmed tx created by the Wallet
## [v0.5.0]

View File

@ -158,6 +158,15 @@ interface TxBuilder {
PartiallySignedBitcoinTransaction build([ByRef] Wallet wallet);
};
interface BumpFeeTxBuilder {
constructor(string txid, float new_fee_rate);
BumpFeeTxBuilder allow_shrinking(string address);
BumpFeeTxBuilder enable_rbf();
BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence);
[Throws=BdkError]
PartiallySignedBitcoinTransaction build([ByRef] Wallet wallet);
};
dictionary ExtendedKeyInfo {
string mnemonic;
string xprv;

View File

@ -1,7 +1,7 @@
use bdk::bitcoin::hashes::hex::ToHex;
use bdk::bitcoin::secp256k1::Secp256k1;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{Address, Network, Script};
use bdk::bitcoin::{Address, Network, Script, Txid};
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
use bdk::blockchain::{
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
@ -440,4 +440,78 @@ impl TxBuilder {
}
}
struct BumpFeeTxBuilder {
txid: String,
fee_rate: f32,
rbf: Option<RbfValue>,
allow_shrinking: Option<String>,
}
impl BumpFeeTxBuilder {
fn new(txid: String, fee_rate: f32) -> Self {
Self {
txid,
fee_rate,
allow_shrinking: None,
rbf: None,
}
}
fn allow_shrinking(&self, address: String) -> Arc<Self> {
Arc::new(Self {
txid: self.txid.clone(),
fee_rate: self.fee_rate,
allow_shrinking: Some(address),
rbf: self.rbf.clone(),
})
}
fn enable_rbf(&self) -> Arc<Self> {
Arc::new(Self {
txid: self.txid.clone(),
fee_rate: self.fee_rate,
rbf: Some(RbfValue::Default),
allow_shrinking: self.allow_shrinking.clone(),
})
}
fn enable_rbf_with_sequence(&self, nsequence: u32) -> Arc<Self> {
Arc::new(Self {
txid: self.txid.clone(),
fee_rate: self.fee_rate,
rbf: Some(RbfValue::Value(nsequence)),
allow_shrinking: self.allow_shrinking.clone(),
})
}
fn build(&self, wallet: &Wallet) -> Result<Arc<PartiallySignedBitcoinTransaction>, Error> {
let wallet = wallet.get_wallet();
let txid = Txid::from_str(self.txid.as_str())?;
let mut tx_builder = wallet.build_fee_bump(txid)?;
tx_builder.fee_rate(FeeRate::from_sat_per_vb(self.fee_rate));
if let Some(rbf) = &self.rbf {
match *rbf {
RbfValue::Default => {
tx_builder.enable_rbf();
}
RbfValue::Value(nsequence) => {
tx_builder.enable_rbf_with_sequence(nsequence);
}
}
}
if let Some(allow_shrinking) = &self.allow_shrinking {
let address =
Address::from_str(allow_shrinking).map_err(|e| Error::Generic(e.to_string()))?;
let script = address.script_pubkey();
tx_builder.allow_shrinking(script)?;
}
tx_builder
.finish()
.map(|(psbt, _)| PartiallySignedBitcoinTransaction {
internal: Mutex::new(psbt),
})
.map(Arc::new)
}
}
uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send);