Skip to content

Commit

Permalink
feat: add bumpfee finish related error
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed May 1, 2024
1 parent 431ab90 commit d8cdc0a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 51 deletions.
7 changes: 1 addition & 6 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ namespace bdk {};
// bdk crate - error module
// ------------------------------------------------------------------------

[Error]
enum Alpha3Error {
"Generic"
};

[Error]
interface Bip39Error {
BadWordCount(u64 word_count);
Expand Down Expand Up @@ -348,7 +343,7 @@ interface BumpFeeTxBuilder {

BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence);

[Throws=Alpha3Error]
[Throws=CreateTxError]
Psbt finish([ByRef] Wallet wallet);
};

Expand Down
70 changes: 33 additions & 37 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bdk::descriptor::DescriptorError as BdkDescriptorError;
use bdk::wallet::error::BuildFeeBumpError;
use bdk::wallet::signer::SignerError as BdkSignerError;
use bdk::wallet::tx_builder::{AddUtxoError, AllowShrinkingError};
use bdk::wallet::{NewError, NewOrLoadError};
use bdk::wallet::NewOrLoadError;
use bdk_esplora::esplora_client::{Error as BdkEsploraError, Error};
use bdk_file_store::FileError as BdkFileError;
use bdk_file_store::IterError;
Expand All @@ -23,12 +23,6 @@ use bdk::bitcoin::bip32;
use bdk::wallet::error::CreateTxError as BdkCreateTxError;
use std::convert::TryInto;

#[derive(Debug, thiserror::Error)]
pub enum Alpha3Error {
#[error("generic error in ffi")]
Generic,
}

#[derive(Debug, thiserror::Error)]
pub enum Bip32Error {
#[error("Cannot derive from a hardened key")]
Expand Down Expand Up @@ -510,6 +504,38 @@ impl From<AddUtxoError> for CreateTxError {
}
}

impl From<AllowShrinkingError> for CreateTxError {
fn from(error: AllowShrinkingError) -> Self {
match error {
AllowShrinkingError::MissingScriptPubKey(_script) => {
CreateTxError::ChangePolicyDescriptor
}
}
}
}

impl From<BuildFeeBumpError> for CreateTxError {
fn from(error: BuildFeeBumpError) -> Self {
match error {
BuildFeeBumpError::UnknownUtxo(outpoint) => CreateTxError::UnknownUtxo {
outpoint: outpoint.to_string(),
},
BuildFeeBumpError::TransactionNotFound(txid) => CreateTxError::UnknownUtxo {
outpoint: txid.to_string(),
},
BuildFeeBumpError::TransactionConfirmed(txid) => CreateTxError::UnknownUtxo {
outpoint: txid.to_string(),
},
BuildFeeBumpError::IrreplaceableTransaction(txid) => CreateTxError::UnknownUtxo {
outpoint: txid.to_string(),
},
BuildFeeBumpError::FeeRateUnavailable => CreateTxError::FeeRateTooLow {
required: "unavailable".to_string(),
},
}
}
}

impl From<BdkDescriptorError> for DescriptorError {
fn from(error: BdkDescriptorError) -> Self {
match error {
Expand Down Expand Up @@ -644,30 +670,6 @@ impl From<std::io::Error> for PersistenceError {
}
}

impl From<AllowShrinkingError> for Alpha3Error {
fn from(_: AllowShrinkingError) -> Self {
Alpha3Error::Generic
}
}

impl From<BuildFeeBumpError> for Alpha3Error {
fn from(_: BuildFeeBumpError) -> Self {
Alpha3Error::Generic
}
}

impl From<AddUtxoError> for Alpha3Error {
fn from(_: AddUtxoError) -> Self {
Alpha3Error::Generic
}
}

impl From<bdk::bitcoin::bip32::Error> for Alpha3Error {
fn from(_: bdk::bitcoin::bip32::Error) -> Self {
Alpha3Error::Generic
}
}

impl From<BdkBip32Error> for Bip32Error {
fn from(error: BdkBip32Error) -> Self {
match error {
Expand Down Expand Up @@ -696,12 +698,6 @@ impl From<BdkBip32Error> for Bip32Error {
}
}

impl From<NewError<std::io::Error>> for Alpha3Error {
fn from(_: NewError<std::io::Error>) -> Self {
Alpha3Error::Generic
}
}

impl From<BdkCalculateFeeError> for CalculateFeeError {
fn from(error: BdkCalculateFeeError) -> Self {
match error {
Expand Down
1 change: 0 additions & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::bitcoin::Transaction;
use crate::bitcoin::TxOut;
use crate::descriptor::Descriptor;
use crate::error::AddressError;
use crate::error::Alpha3Error;
use crate::error::Bip32Error;
use crate::error::Bip39Error;
use crate::error::CalculateFeeError;
Expand Down
18 changes: 11 additions & 7 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Script, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
Alpha3Error, CalculateFeeError, CannotConnectError, CreateTxError, PersistenceError,
SignerError, TxidParseError, WalletCreationError,
CalculateFeeError, CannotConnectError, CreateTxError, PersistenceError, SignerError,
TxidParseError, WalletCreationError,
};
use crate::types::{AddressIndex, AddressInfo, Balance, CanonicalTx, LocalOutput, ScriptAmount};

Expand Down Expand Up @@ -582,13 +582,17 @@ impl BumpFeeTxBuilder {
})
}

pub(crate) fn finish(&self, wallet: &Wallet) -> Result<Arc<Psbt>, Alpha3Error> {
let txid = Txid::from_str(self.txid.as_str()).map_err(|_| Alpha3Error::Generic)?;
pub(crate) fn finish(&self, wallet: &Wallet) -> Result<Arc<Psbt>, CreateTxError> {
let txid = Txid::from_str(self.txid.as_str()).map_err(|_| CreateTxError::UnknownUtxo {
outpoint: self.txid.clone(),
})?;
let mut wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_fee_bump(txid)?;
let mut tx_builder = wallet.build_fee_bump(txid).map_err(CreateTxError::from)?;
tx_builder.fee_rate(self.fee_rate.0);
if let Some(allow_shrinking) = &self.allow_shrinking {
tx_builder.allow_shrinking(allow_shrinking.0.clone())?;
tx_builder
.allow_shrinking(allow_shrinking.0.clone())
.map_err(CreateTxError::from)?;
}
if let Some(rbf) = &self.rbf {
match *rbf {
Expand All @@ -600,7 +604,7 @@ impl BumpFeeTxBuilder {
}
}
}
let psbt: BdkPsbt = tx_builder.finish().map_err(|_| Alpha3Error::Generic)?;
let psbt: BdkPsbt = tx_builder.finish()?;

Ok(Arc::new(psbt.into()))
}
Expand Down

0 comments on commit d8cdc0a

Please sign in to comment.