Skip to content

Commit

Permalink
feat: addition of the tag manually_frozen
Browse files Browse the repository at this point in the history
  • Loading branch information
markettes committed Aug 29, 2024
1 parent b63d8cf commit c05b9ae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class Constants
public static decimal MAX_TX_FEE_RATIO = 0.5m;

public const string IsFrozenTag = "frozen";
public const string IsManuallyFrozenTag = "manually_frozen";

// Constants for the NBXplorer API
public static int SCAN_GAP_LIMIT = 1000;
Expand Down
16 changes: 12 additions & 4 deletions src/Pages/Wallets.razor
Original file line number Diff line number Diff line change
Expand Up @@ -1154,8 +1154,16 @@
.Select((u) => (
u.Outpoint,
u,
tagsMap[u.Outpoint].Where(t => t.Key != Constants.IsFrozenTag).ToList(),
tagsMap[u.Outpoint].Any(t => t.Key == Constants.IsFrozenTag && t.Value == "true")
tagsMap[u.Outpoint]
.Where(t => t.Key != Constants.IsFrozenTag && t.Key != Constants.IsManuallyFrozenTag)
.ToList(),
// Check if the UTXO is frozen, has been manually frozen or has been manually unfrozen
(tagsMap[u.Outpoint]
.Any(t => t.Key == Constants.IsFrozenTag && t.Value == "true") ||
tagsMap[u.Outpoint]
.Any(t => t.Key == Constants.IsManuallyFrozenTag && t.Value == "true")) &&
!tagsMap[u.Outpoint]
.Any(t => t.Key == Constants.IsManuallyFrozenTag && t.Value == "false")
)).ToList();
_detailsTransactions = await NBXplorerService.GetTransactionsAsync(derivationStrategyBase);
Expand Down Expand Up @@ -1703,8 +1711,8 @@
private async Task ToggleUtxoFreeze(bool newValue, UTXO utxo)
{
var tag = await UTXOTagRepository.GetByKeyAndOutpoint(Constants.IsFrozenTag, utxo.Outpoint.ToString());
var (saved, _) = UTXOTagRepository.Update(new UTXOTag {Key = Constants.IsFrozenTag, Value = newValue ? "true" : "false", Outpoint = utxo.Outpoint.ToString(), Id = tag?.Id ?? 0 });
var tag = await UTXOTagRepository.GetByKeyAndOutpoint(Constants.IsManuallyFrozenTag, utxo.Outpoint.ToString());
var (saved, _) = UTXOTagRepository.Update(new UTXOTag {Key = Constants.IsManuallyFrozenTag, Value = newValue ? "true" : "false", Outpoint = utxo.Outpoint.ToString(), Id = tag?.Id ?? 0 });
if (!saved)
{
ToastService.ShowError("Error while updating the UTXO status");
Expand Down
4 changes: 1 addition & 3 deletions src/Rpc/NodeGuardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -951,11 +951,9 @@ public override async Task<GetUtxosResponse> GetAvailableUtxos(GetAvailableUtxos
}

var lockedUtxos = await _fmutxoRepository.GetLockedUTXOs();
var frozenUtxos = await _utxoTagRepository.GetByKeyValue(Constants.IsFrozenTag, "true");

var ignoreOutpoints = new List<string>();
var listLocked = lockedUtxos.Select(utxo => $"{utxo.TxId}-{utxo.OutputIndex}").ToList();
var listFrozen = frozenUtxos.Select(utxo => utxo.Outpoint).ToList();
var listFrozen = await _coinSelectionService.GetFrozenUTXOs();
ignoreOutpoints.AddRange(listLocked);
ignoreOutpoints.AddRange(listFrozen);

Expand Down
27 changes: 25 additions & 2 deletions src/Services/CoinSelectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public interface ICoinSelectionService
/// <param name="bitcoinRequest"></param>
/// <param name="requestType"></param>
public Task<List<UTXO>> GetLockedUTXOsForRequest(IBitcoinRequest bitcoinRequest, BitcoinRequestType requestType);

/// <summary>
/// Gets the frozen UTXOs
/// </summary>
public Task<List<string>> GetFrozenUTXOs();

public Task<(List<ICoin> coins, List<UTXO> selectedUTXOs)> GetTxInputCoins(
List<UTXO> availableUTXOs,
Expand Down Expand Up @@ -146,9 +151,8 @@ public async Task<List<UTXO>> GetLockedUTXOsForRequest(IBitcoinRequest bitcoinRe
private async Task<List<UTXO>> FilterLockedFrozenUTXOs(UTXOChanges? utxoChanges)
{
var lockedUTXOs = await _fmutxoRepository.GetLockedUTXOs();
var frozenUTXOs = await _utxoTagRepository.GetByKeyValue(Constants.IsFrozenTag, "true");
var listLocked = lockedUTXOs.Select(utxo => $"{utxo.TxId}-{utxo.OutputIndex}").ToList();
var listFrozen = frozenUTXOs.Select(utxo => utxo.Outpoint).ToList();
var listFrozen = await GetFrozenUTXOs();
var frozenAndLockedOutpoints = new List<string>();
frozenAndLockedOutpoints.AddRange(listLocked);
frozenAndLockedOutpoints.AddRange(listFrozen);
Expand All @@ -171,6 +175,25 @@ private async Task<List<UTXO>> FilterLockedFrozenUTXOs(UTXOChanges? utxoChanges)

return availableUTXOs;
}

public async Task<List<string>> GetFrozenUTXOs()
{
var frozenUTXOs = await _utxoTagRepository.GetByKeyValue(Constants.IsFrozenTag, "true");
var manuallyFrozenUTXOs = await _utxoTagRepository.GetByKeyValue(Constants.IsManuallyFrozenTag, "true");
var manuallyUnfrozenUTXOs = await _utxoTagRepository.GetByKeyValue(Constants.IsManuallyFrozenTag, "false");
var listFrozen = frozenUTXOs.Select(utxo => utxo.Outpoint).ToList();
var listManuallyFrozen = manuallyFrozenUTXOs.Select(utxo => utxo.Outpoint).ToList();
var listManuallyUnfrozen = manuallyUnfrozenUTXOs.Select(utxo => utxo.Outpoint).ToList();

// Merge manually frozen and frozen UTXOs and remove manually unfrozen UTXOs
List<string> frozenUTXOsList =
listFrozen
.Union(listManuallyFrozen)
.Except(listManuallyUnfrozen)
.ToList();

return frozenUTXOsList;
}

public async Task<List<UTXO>> GetAvailableUTXOsAsync(DerivationStrategyBase derivationStrategy)
{
Expand Down

0 comments on commit c05b9ae

Please sign in to comment.