Skip to content

Commit

Permalink
fix: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
markettes committed Jun 26, 2024
1 parent 7e79ce6 commit 50ce070
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Services/CoinSelectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,48 +119,54 @@ public async Task LockUTXOs(List<UTXO> selectedUTXOs, IBitcoinRequest bitcoinReq
var utxos = await _fmutxoRepository.GetByWalletId(bitcoinRequest.Wallet.Id);

var utxosToSave = new List<FMUTXO>();
var utxosToAdd = new List<FMUTXO>();
var utxosToAddToTransaction = new List<FMUTXO>();
// We check the selected UTXOs against the ones we have in the database to see if they are already there
foreach (var fmutxo in selectedFMUTXOs)
{
if (utxos.Contains(fmutxo))
{
utxosToAdd.Add(fmutxo);
// If they are in the DB we add them to the transaction
utxosToAddToTransaction.Add(fmutxo);
}
else
{
// If they are not in the DB we add them
fmutxo.WalletId = bitcoinRequest.Wallet.Id;
fmutxo.SetCreationDatetime();
fmutxo.SetUpdateDatetime();
utxosToSave.Add(fmutxo);
}
}

// If there's any UTXO to save we do it
if (utxosToSave.Any())
{
(bool success, string? reason) = await _fmutxoRepository.AddRangeAsync(utxosToSave);
if (!success)
{
_logger.LogError($"Could not save the utxos that were not selected for request:{bitcoinRequest.Id}");
utxosToAdd = new List<FMUTXO>();
utxosToAddToTransaction = new List<FMUTXO>();
}

foreach (var fmutxo in utxosToSave)
{
// We get the final UTXO from the DB to add to the transaction
var finalUtxo = await _fmutxoRepository.GetByOutpoint(fmutxo.TxId, fmutxo.OutputIndex);
utxosToAdd.Add(finalUtxo);
utxosToAddToTransaction.Add(finalUtxo);
}
}

if (utxosToAdd.Count < selectedUTXOs.Count)
// If we couldn't save the UTXOs we fail to construct the transaction
if (utxosToAddToTransaction.Count < selectedUTXOs.Count)
{
utxosToAdd = new List<FMUTXO>();
utxosToAddToTransaction = new List<FMUTXO>();
}

var addUTXOsOperation = await GetRepository(requestType).AddUTXOs(bitcoinRequest, utxosToAdd);
var addUTXOsOperation = await GetRepository(requestType).AddUTXOs(bitcoinRequest, utxosToAddToTransaction);
if (!addUTXOsOperation.Item1)
{
_logger.LogError(
$"Could not add the following utxos({utxosToAdd.Humanize()}) to op request:{bitcoinRequest.Id}");
$"Could not add the following utxos({utxosToAddToTransaction.Humanize()}) to op request:{bitcoinRequest.Id}");
}
}

Expand Down

0 comments on commit 50ce070

Please sign in to comment.