From 91405479f81d5df8ae0e384b71d1ba79b613ac0c Mon Sep 17 00:00:00 2001 From: Rodrigo <39995243+RodriFS@users.noreply.github.com> Date: Wed, 23 Jul 2025 09:22:24 +0200 Subject: [PATCH] Filter UTXOs by wallet for sending ignoreOutputs to nbxplorer --- src/Data/Repositories/FUTXORepository.cs | 26 +++++++++++++++++++ .../Interfaces/IFMUTXORepository.cs | 1 + src/Rpc/NodeGuardService.cs | 11 +++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Data/Repositories/FUTXORepository.cs b/src/Data/Repositories/FUTXORepository.cs index abea522e..616c9a35 100644 --- a/src/Data/Repositories/FUTXORepository.cs +++ b/src/Data/Repositories/FUTXORepository.cs @@ -144,5 +144,31 @@ public async Task> GetLockedUTXOs(int? ignoredWalletWithdrawalReque return result; } + + public async Task> GetLockedUTXOsByWalletId(int walletId) + { + await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync(); + + var walletWithdrawalRequestsLockedUTXOs = await applicationDbContext.WalletWithdrawalRequests + .Include(x => x.UTXOs) + .Where(x => x.Status == WalletWithdrawalRequestStatus.Pending || + x.Status == WalletWithdrawalRequestStatus.PSBTSignaturesPending || + x.Status == WalletWithdrawalRequestStatus.FinalizingPSBT || + x.Status == WalletWithdrawalRequestStatus.OnChainConfirmationPending) + .Where(x => x.WalletId == walletId) + .SelectMany(x => x.UTXOs) + .ToListAsync(); + + var channelOperationRequestsLockedUTXOs = await applicationDbContext.ChannelOperationRequests.Include(x => x.Utxos) + .Where(x => x.Status == ChannelOperationRequestStatus.Pending || + x.Status == ChannelOperationRequestStatus.PSBTSignaturesPending || + x.Status == ChannelOperationRequestStatus.FinalizingPSBT || + x.Status == ChannelOperationRequestStatus.OnChainConfirmationPending) + .Where(x => x.WalletId == walletId) + .SelectMany(x => x.Utxos) + .ToListAsync(); + + return walletWithdrawalRequestsLockedUTXOs.Union(channelOperationRequestsLockedUTXOs).ToList(); + } } } diff --git a/src/Data/Repositories/Interfaces/IFMUTXORepository.cs b/src/Data/Repositories/Interfaces/IFMUTXORepository.cs index 28c79f7f..bc2e45b3 100644 --- a/src/Data/Repositories/Interfaces/IFMUTXORepository.cs +++ b/src/Data/Repositories/Interfaces/IFMUTXORepository.cs @@ -42,4 +42,5 @@ public interface IFMUTXORepository /// /// Task> GetLockedUTXOs(int? ignoredWalletWithdrawalRequestId = null, int? ignoredChannelOperationRequestId = null); + Task> GetLockedUTXOsByWalletId(int walletId); } \ No newline at end of file diff --git a/src/Rpc/NodeGuardService.cs b/src/Rpc/NodeGuardService.cs index 548261f0..0d5d8b6b 100644 --- a/src/Rpc/NodeGuardService.cs +++ b/src/Rpc/NodeGuardService.cs @@ -1003,10 +1003,19 @@ public override async Task GetAvailableUtxos(GetAvailableUtxos throw new Exception("Derivation strategy not found for wallet with id {walletId}"); } - var lockedUtxos = await _fmutxoRepository.GetLockedUTXOs(); + var walletUtxos = await _nbXplorerService.GetUTXOsAsync(derivationStrategy); + var lockedUtxos = await _fmutxoRepository.GetLockedUTXOsByWalletId(wallet.Id); var ignoreOutpoints = new List(); var listLocked = lockedUtxos.Select(utxo => $"{utxo.TxId}-{utxo.OutputIndex}").ToList(); var listFrozen = await _coinSelectionService.GetFrozenUTXOs(); + + // filter frozen list by only including UTXOs that belong to the wallet + // TODO: find a way to add wallet id to the UTXOTag model + listFrozen = listFrozen + .Where(utxo => walletUtxos.Confirmed.UTXOs.Any(u => u.Outpoint.ToString() == utxo) || + walletUtxos.Unconfirmed.UTXOs.Any(u => u.Outpoint.ToString() == utxo)) + .ToList(); + ignoreOutpoints.AddRange(listLocked); ignoreOutpoints.AddRange(listFrozen);