Skip to content

Commit ae4114a

Browse files
committed
fix: correct confirmations for UTXOs in wallet FFI
1 parent 7316f44 commit ae4114a

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

key-wallet-ffi/src/utxo.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! UTXO management
22
3+
use crate::error::{FFIError, FFIErrorCode};
4+
use crate::managed_wallet::FFIManagedWalletInfo;
5+
use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface;
36
use std::ffi::CString;
47
use std::os::raw::c_char;
58
use std::ptr;
69

7-
use crate::error::{FFIError, FFIErrorCode};
8-
use crate::managed_wallet::FFIManagedWalletInfo;
9-
1010
/// UTXO structure for FFI
1111
#[repr(C)]
1212
pub struct FFIUTXO {
@@ -119,9 +119,9 @@ pub unsafe extern "C" fn managed_wallet_get_utxos(
119119
// Get script bytes
120120
let script_bytes = utxo.txout.script_pubkey.as_bytes().to_vec();
121121

122-
// Calculate confirmations (0 if unconfirmed)
123-
let confirmations = if utxo.is_confirmed {
124-
1
122+
let current_height = managed_info.inner().synced_height();
123+
let confirmations = if utxo.is_confirmed && utxo.height > 0 {
124+
current_height.saturating_sub(utxo.height) + 1
125125
} else {
126126
0
127127
};

key-wallet-ffi/src/utxo_tests.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ mod utxo_tests {
229229

230230
managed_info.accounts.insert(bip44_account);
231231

232-
let ffi_managed_info = FFIManagedWalletInfo::new(managed_info);
233-
232+
let mut ffi_managed_info = FFIManagedWalletInfo::new(managed_info);
233+
ffi_managed_info.inner_mut().update_chain_height(300);
234234
let result = unsafe {
235235
managed_wallet_get_utxos(&ffi_managed_info, &mut utxos_out, &mut count_out, error)
236236
};
@@ -249,19 +249,21 @@ mod utxo_tests {
249249
assert_eq!(utxos[0].vout, 0);
250250
assert_eq!(utxos[0].amount, 50000);
251251
assert_eq!(utxos[0].height, 100);
252-
assert_eq!(utxos[0].confirmations, 1);
252+
assert_eq!(utxos[0].confirmations, 201);
253253

254254
// Check second UTXO
255255
assert_eq!(utxos[1].txid[0], 1);
256256
assert_eq!(utxos[1].vout, 1);
257257
assert_eq!(utxos[1].amount, 100000);
258258
assert_eq!(utxos[1].height, 101);
259+
assert_eq!(utxos[1].confirmations, 200);
259260

260261
// Check third UTXO
261262
assert_eq!(utxos[2].txid[0], 2);
262263
assert_eq!(utxos[2].vout, 2);
263264
assert_eq!(utxos[2].amount, 150000);
264265
assert_eq!(utxos[2].height, 102);
266+
assert_eq!(utxos[2].confirmations, 199);
265267
}
266268

267269
// Clean up

0 commit comments

Comments
 (0)