From d80034a1a4ec0df7faa8662df49be4eb1872abf7 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 10 Aug 2026 11:13:45 -0500 Subject: [PATCH] fix: return structured insufficient funds errors --- bdk-ffi/src/error.rs | 8 +++++--- bdk-ffi/src/tests/error.rs | 25 ++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index ea7f48cd9..80bec672c 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -1082,9 +1082,11 @@ impl From for CreateTxError { BdkCreateTxError::OutputBelowDustLimit(index) => CreateTxError::OutputBelowDustLimit { index: index as u64, }, - BdkCreateTxError::CoinSelection(e) => CreateTxError::CoinSelection { - error_message: e.to_string(), - }, + BdkCreateTxError::CoinSelection(e) => { + let needed = e.needed.to_sat(); + let available = e.available.to_sat(); + CreateTxError::InsufficientFunds { needed, available } + } BdkCreateTxError::NoRecipients => CreateTxError::NoRecipients, BdkCreateTxError::Psbt(e) => CreateTxError::Psbt { error_message: e.to_string(), diff --git a/bdk-ffi/src/tests/error.rs b/bdk-ffi/src/tests/error.rs index d130696d9..a232f7873 100644 --- a/bdk-ffi/src/tests/error.rs +++ b/bdk-ffi/src/tests/error.rs @@ -1,8 +1,11 @@ use crate::error::{ - Bip32Error, Bip39Error, CannotConnectError, DescriptorError, DescriptorKeyError, ElectrumError, - EsploraError, ExtractTxError, PsbtError, PsbtParseError, RequestBuilderError, SignerError, - TransactionError, TxidParseError, + Bip32Error, Bip39Error, CannotConnectError, CreateTxError, DescriptorError, DescriptorKeyError, + ElectrumError, EsploraError, ExtractTxError, PsbtError, PsbtParseError, RequestBuilderError, + SignerError, TransactionError, TxidParseError, }; +use bdk_wallet::bitcoin::Amount; +use bdk_wallet::coin_selection::InsufficientFunds as BdkInsufficientFunds; +use bdk_wallet::error::CreateTxError as BdkCreateTxError; #[test] fn test_error_bip32() { @@ -104,6 +107,22 @@ fn test_error_cannot_connect() { assert_eq!(format!("{}", error), "cannot include height: 42"); } +#[test] +fn test_error_create_tx_insufficient_funds_conversion() { + let error = BdkCreateTxError::CoinSelection(BdkInsufficientFunds { + needed: Amount::from_sat(50_000), + available: Amount::from_sat(30_000), + }); + + match CreateTxError::from(error) { + CreateTxError::InsufficientFunds { needed, available } => { + assert_eq!(needed, 50_000); + assert_eq!(available, 30_000); + } + other => panic!("expected insufficient funds error, got {:?}", other), + } +} + #[test] fn test_error_descriptor() { let cases = vec![