Skip to content

Commit

Permalink
reference id in withdrawals (#387)
Browse files Browse the repository at this point in the history
* fixed justfile

* added referenceId to withdrawal request

* added getByReferenceIds to withdrawal request repository

* added GetWithdrawalsRequestStatusByReferenceIds to nodeguard api

* added just stop to justfile

* unified response for GetWithdrawalsRequestStatus and GetWithdrawalsRequestStatusByReferenceIds
  • Loading branch information
RodriFS authored Jul 23, 2024
1 parent 7958c7b commit 3a011ec
Show file tree
Hide file tree
Showing 9 changed files with 1,388 additions and 18 deletions.
7 changes: 5 additions & 2 deletions .justfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ build:
run:
cd {{PROJECT_DIR}} && dotnet run

stop:
killall -9 NodeGuard

test:
dotnet test

Expand All @@ -70,9 +73,9 @@ add-license-cs:
go install github.com/fbiville/headache/cmd/headache@latest
headache --configuration ./configuration-cs.json
add-migration name:
cd {{PROJECT_DIR}} dotnet ef migrations add --context ApplicationDbContext {{name}}
cd {{PROJECT_DIR}} && dotnet ef migrations add --context ApplicationDbContext {{name}}
remove-migration:
cd {{PROJECT_DIR}} dotnet ef migrations remove --context ApplicationDbContext
cd {{PROJECT_DIR}} && dotnet ef migrations remove --context ApplicationDbContext
mine:
while true; do docker exec polar-n1-backend1 bitcoin-cli -regtest -rpcuser=polaruser -rpcpassword=polarpass -generate 1; sleep 60; done

Expand Down
7 changes: 6 additions & 1 deletion src/Data/Models/WalletWithdrawalRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class WalletWithdrawalRequest : Entity, IEquatable<WalletWithdrawalReques
/// For additional info required by the requestor
/// </summary>
public string? RequestMetadata { get; set; }

/// <summary>
/// Recommended fee type selected by the user to be applied at the moment of the operation, this cannot be changed once the template PSBT is created nor signed
/// </summary>
Expand Down Expand Up @@ -201,6 +201,11 @@ public bool Equals(WalletWithdrawalRequest? other)

public List<FMUTXO> UTXOs { get; set; }

/// <summary>
/// This is a optional field that you can used to link withdrawals with externally-generated IDs (e.g. a withdrawal/settlement that belongs to an elenpay store)
/// </summary>
public string? ReferenceId { get; set; }

#endregion Relationships

public override int GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@

namespace NodeGuard.Data.Repositories.Interfaces;

public interface IWalletWithdrawalRequestRepository: IBitcoinRequestRepository
public interface IWalletWithdrawalRequestRepository : IBitcoinRequestRepository
{
Task<WalletWithdrawalRequest?> GetById(int id);

Task<List<WalletWithdrawalRequest>> GetByIds(List<int> ids);
Task<List<WalletWithdrawalRequest>> GetByReferenceIds(List<string> referenceIds);

Task<List<WalletWithdrawalRequest>> GetAll();

Task<List<WalletWithdrawalRequest>> GetUnsignedPendingRequestsByUser(string userId);

Task<List<WalletWithdrawalRequest>> GetAllUnsignedPendingRequests();

Task<(bool, string?)> AddAsync(WalletWithdrawalRequest type);

Task<(bool, string?)> AddRangeAsync(List<WalletWithdrawalRequest> type);
Expand Down
16 changes: 12 additions & 4 deletions src/Data/Repositories/WalletWithdrawalRequestRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public async Task<List<WalletWithdrawalRequest>> GetByIds(List<int> ids)
return await applicationDbContext.WalletWithdrawalRequests.Where(wr => ids.Contains(wr.Id)).ToListAsync();
}

public async Task<List<WalletWithdrawalRequest>> GetByReferenceIds(List<string> referenceIds)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();

return await applicationDbContext.WalletWithdrawalRequests.Where(wr => !string.IsNullOrEmpty(wr.ReferenceId) && referenceIds.Contains(wr.ReferenceId)).ToListAsync();
}


public async Task<List<WalletWithdrawalRequest>> GetAll()
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();
Expand All @@ -102,7 +110,7 @@ public async Task<List<WalletWithdrawalRequest>> GetUnsignedPendingRequestsByUse
.AsSplitQuery()
.ToListAsync();
}

public async Task<List<WalletWithdrawalRequest>> GetAllUnsignedPendingRequests()
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();
Expand All @@ -124,7 +132,7 @@ public async Task<List<WalletWithdrawalRequest>> GetAllUnsignedPendingRequests()
type.SetUpdateDatetime();

//Verify that the wallet has enough funds calling nbxplorer
var wallet = await applicationDbContext.Wallets.Include(x=> x.Keys).SingleOrDefaultAsync(x => x.Id == type.WalletId);
var wallet = await applicationDbContext.Wallets.Include(x => x.Keys).SingleOrDefaultAsync(x => x.Id == type.WalletId);

if (wallet == null)
{
Expand All @@ -149,7 +157,7 @@ public async Task<List<WalletWithdrawalRequest>> GetAllUnsignedPendingRequests()

var requestMoneyAmount = new Money(type.Amount, MoneyUnit.BTC);

if ((Money) balance.Confirmed < requestMoneyAmount)
if ((Money)balance.Confirmed < requestMoneyAmount)
{
return (false, $"The wallet {type.Wallet.Name} does not have enough funds to complete this withdrawal request. The wallet has {balance.Confirmed} BTC and the withdrawal request is for {requestMoneyAmount} BTC.");
}
Expand Down Expand Up @@ -248,7 +256,7 @@ public async Task<List<WalletWithdrawalRequest>> GetAllUnsignedPendingRequests()
}
catch (Exception e)
{
_logger.LogError(e, "Error while getting UTXOs from wallet withdrawal request: {RequestId}", request.Id);
_logger.LogError(e, "Error while getting UTXOs from wallet withdrawal request: {RequestId}", request.Id);
result.Item1 = false;
}

Expand Down
Loading

0 comments on commit 3a011ec

Please sign in to comment.