Skip to content

Commit

Permalink
Merge pull request #180 from Elenpay/refactor_on_coin_selection
Browse files Browse the repository at this point in the history
Refactor on default coin selection
  • Loading branch information
RodriFS authored May 30, 2023
2 parents 4066c37 + 426125f commit c812ccd
Show file tree
Hide file tree
Showing 17 changed files with 551 additions and 255 deletions.
7 changes: 0 additions & 7 deletions src/Areas/Identity/Pages/Account/LoginWith2fa.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable

using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using FundsManager.Data.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace FundsManager.Areas.Identity.Pages.Account
{
Expand Down
12 changes: 5 additions & 7 deletions src/Data/Models/ChannelOperationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public enum OperationRequestType
Close = 2
}

public class ChannelOperationRequest : Entity, IEquatable<ChannelOperationRequest>
public class ChannelOperationRequest : Entity, IEquatable<ChannelOperationRequest>, IBitcoinRequest
{
/// <summary>
/// Amount in satoshis
Expand All @@ -78,9 +78,7 @@ public class ChannelOperationRequest : Entity, IEquatable<ChannelOperationReques
public decimal Amount
{
get => new Money(SatsAmount, MoneyUnit.Satoshi).ToDecimal(MoneyUnit.BTC);
set
{
}
set { }
}

public string? Description { get; set; }
Expand Down Expand Up @@ -130,7 +128,7 @@ public decimal Amount
public bool AreAllRequiredHumanSignaturesCollected => CheckSignatures();

[NotMapped]
public int NumberOfSignaturesCollected => ChannelOperationRequestPsbts == null ? 0 : ChannelOperationRequestPsbts.Count(x =>!x.IsFinalisedPSBT && !x.IsTemplatePSBT && !x.IsInternalWalletPSBT);
public int NumberOfSignaturesCollected => ChannelOperationRequestPsbts == null ? 0 : ChannelOperationRequestPsbts.Count(x => !x.IsFinalisedPSBT && !x.IsTemplatePSBT && !x.IsInternalWalletPSBT);

/// <summary>
/// This is the JobId provided by Quartz of the job executing this request.
Expand All @@ -150,15 +148,15 @@ private bool CheckSignatures()
var userPSBTsCount = NumberOfSignaturesCollected;

//We add the internal Wallet signature
if (Wallet != null && Wallet.IsHotWallet) return ChannelOperationRequestPsbts.Count(x=> x.IsTemplatePSBT) == 1;
if (Wallet != null && Wallet.IsHotWallet) return ChannelOperationRequestPsbts.Count(x => x.IsTemplatePSBT) == 1;
userPSBTsCount++;

if (userPSBTsCount == Wallet.MofN)
{
result = true;
}
}

return result;
}

Expand Down
38 changes: 38 additions & 0 deletions src/Data/Models/Interfaces/IBitcoinRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* NodeGuard
* Copyright (C) 2023 Elenpay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

namespace FundsManager.Data.Models;

public enum BitcoinRequestType
{
ChannelOperation,
WalletWithdrawal
}

public interface IBitcoinRequest
{
public int Id { get; set; }

/// <summary>
/// Amount in satoshis
/// </summary>
public long SatsAmount { get; }

public Wallet? Wallet { get; set; }
}
8 changes: 4 additions & 4 deletions src/Data/Models/WalletWithdrawalRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.Schema;
using NBitcoin;

namespace FundsManager.Data.Models
Expand Down Expand Up @@ -63,7 +63,7 @@ public enum WalletWithdrawalRequestStatus
/// <summary>
/// Requests to withdraw funds from a FM-managed multisig wallet
/// </summary>
public class WalletWithdrawalRequest : Entity, IEquatable<WalletWithdrawalRequest>
public class WalletWithdrawalRequest : Entity, IEquatable<WalletWithdrawalRequest>, IBitcoinRequest
{
public WalletWithdrawalRequestStatus Status { get; set; }

Expand Down Expand Up @@ -118,7 +118,7 @@ public class WalletWithdrawalRequest : Entity, IEquatable<WalletWithdrawalReques
/// For additional info required by the requestor
/// </summary>
public string? RequestMetadata { get; set; }

/// <summary>
/// Check that the number of signatures (not finalised psbt nor internal wallet psbt or template psbt are gathered and increases by one to count on the internal wallet signature
/// </summary>
Expand All @@ -131,7 +131,7 @@ private bool CheckSignatures()
{
return true;
}

if (WalletWithdrawalRequestPSBTs != null && WalletWithdrawalRequestPSBTs.Any())
{
var numberOfSignaturesCollected = NumberOfSignaturesCollected;
Expand Down
3 changes: 2 additions & 1 deletion src/Data/Repositories/ChannelOperationRequestRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public async Task<List<ChannelOperationRequest>> GetUnsignedPendingRequestsByUse
{
await _notificationService.NotifyRequestSigners(type.WalletId.Value, "/channel-requests");
}

return valueTuple;
}

Expand Down Expand Up @@ -153,7 +154,7 @@ public async Task<List<ChannelOperationRequest>> GetUnsignedPendingRequestsByUse
return _repository.Update(strippedType, applicationDbContext);
}

public async Task<(bool, string?)> AddUTXOs(ChannelOperationRequest type, List<FMUTXO> utxos)
public async Task<(bool, string?)> AddUTXOs(IBitcoinRequest type, List<FMUTXO> utxos)
{
if (type == null) throw new ArgumentNullException(nameof(type));
if (utxos.Count == 0) throw new ArgumentException("Value cannot be an empty collection.", nameof(utxos));
Expand Down
33 changes: 33 additions & 0 deletions src/Data/Repositories/Interfaces/IBitcoinRequestRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* NodeGuard
* Copyright (C) 2023 Elenpay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

using FundsManager.Data.Models;

namespace FundsManager.Data.Repositories.Interfaces;

public interface IBitcoinRequestRepository
{
/// <summary>
/// Adds to the many-to-many collection the list of utxos provided
/// </summary>
/// <param name="type"></param>
/// <param name="utxos"></param>
/// <returns></returns>
Task<(bool, string?)> AddUTXOs(IBitcoinRequest type, List<FMUTXO> utxos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
*
*/

using FundsManager.Data.Models;
using FundsManager.Data.Models;

namespace FundsManager.Data.Repositories.Interfaces;

public interface IChannelOperationRequestRepository
public interface IChannelOperationRequestRepository : IBitcoinRequestRepository
{
Task<ChannelOperationRequest?> GetById(int id);

Expand All @@ -45,7 +45,7 @@ public interface IChannelOperationRequestRepository
/// <param name="type"></param>
/// <param name="utxos"></param>
/// <returns></returns>
Task<(bool, string?)> AddUTXOs(ChannelOperationRequest type, List<FMUTXO> utxos);
Task<(bool, string?)> AddUTXOs(IBitcoinRequest type, List<FMUTXO> utxos);

/// <summary>
/// Returns those requests that can have a PSBT locked until they are confirmed / rejected / cancelled
Expand Down
3 changes: 1 addition & 2 deletions src/Data/Repositories/Interfaces/IFMUTXORepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ public interface IFMUTXORepository
/// Gets the current list of UTXOs locked on requests ChannelOperationRequest / WalletWithdrawalRequest by passing its id if wants to remove it from the resulting set
/// </summary>
/// <returns></returns>
Task<List<FMUTXO>> GetLockedUTXOs(int? ignoredWalletWithdrawalRequestId = null,
int? ignoredChannelOperationRequestId = null);
Task<List<FMUTXO>> GetLockedUTXOs(int? ignoredWalletWithdrawalRequestId = null, int? ignoredChannelOperationRequestId = null);
}
Loading

0 comments on commit c812ccd

Please sign in to comment.