Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reference id in withdrawals #387

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/// 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 @@ -195,12 +195,17 @@
public ApplicationUser UserRequestor { get; set; }
public int WalletId { get; set; }

public Wallet Wallet { get; set; }

Check warning on line 198 in src/Data/Models/WalletWithdrawalRequest.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Nullability of reference types in type of parameter 'value' of 'void WalletWithdrawalRequest.Wallet.set' doesn't match implicitly implemented member 'void IBitcoinRequest.Wallet.set' (possibly because of nullability attributes).

public List<WalletWithdrawalRequestPSBT> WalletWithdrawalRequestPSBTs { get; set; }

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();
qustavo marked this conversation as resolved.
Show resolved Hide resolved
}


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
Loading