mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 14:04:26 +01:00
* GreenField: Wallet API * more work * wip * rough fiunish of transaction sending api * Allow to create tx without broadcasting and small fixes * Refactor Wallet Receive feature ad add greenfield api for address reserve for wallet * add wallet api client * add docs * fix json converter tags * fixes and add wallet tests * fix tests * fix rebase * fixes * just pass the tests already * ugggh * small cleanup * revert int support in numeric string converter and make block id as native number in json * fix LN endpoint * try fix flaky test * Revert "try fix flaky test" This reverts commit 2e0d256325b892f7741325dcbab01196f74d182a. * try fix other flaky test * return proepr error if fee rate could not be fetched * try fix test again * reduce fee related logic for wallet api * try reduce code changes for pr scope * change auth logic for initial release of wallet api
103 lines
4.0 KiB
C#
103 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BTCPayServer.Services
|
|
{
|
|
public class WalletRepository
|
|
{
|
|
private readonly ApplicationDbContextFactory _ContextFactory;
|
|
|
|
public WalletRepository(ApplicationDbContextFactory contextFactory)
|
|
{
|
|
_ContextFactory = contextFactory ?? throw new ArgumentNullException(nameof(contextFactory));
|
|
}
|
|
|
|
public async Task SetWalletInfo(WalletId walletId, WalletBlobInfo blob)
|
|
{
|
|
if (walletId == null)
|
|
throw new ArgumentNullException(nameof(walletId));
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
{
|
|
var walletData = new WalletData() { Id = walletId.ToString() };
|
|
walletData.SetBlobInfo(blob);
|
|
var entity = await ctx.Wallets.AddAsync(walletData);
|
|
entity.State = EntityState.Modified;
|
|
try
|
|
{
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateException) // Does not exists
|
|
{
|
|
entity.State = EntityState.Added;
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<Dictionary<string, WalletTransactionInfo>> GetWalletTransactionsInfo(WalletId walletId, string[] transactionIds = null)
|
|
{
|
|
if (walletId == null)
|
|
throw new ArgumentNullException(nameof(walletId));
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
{
|
|
return (await ctx.WalletTransactions
|
|
.Where(w => w.WalletDataId == walletId.ToString())
|
|
.Where(data => transactionIds == null || transactionIds.Contains(data.TransactionId))
|
|
.Select(w => w)
|
|
.ToArrayAsync())
|
|
.ToDictionary(w => w.TransactionId, w => w.GetBlobInfo());
|
|
}
|
|
}
|
|
|
|
public async Task<WalletBlobInfo> GetWalletInfo(WalletId walletId)
|
|
{
|
|
if (walletId == null)
|
|
throw new ArgumentNullException(nameof(walletId));
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
{
|
|
var data = await ctx.Wallets
|
|
.Where(w => w.Id == walletId.ToString())
|
|
.Select(w => w)
|
|
.FirstOrDefaultAsync();
|
|
return data?.GetBlobInfo() ?? new WalletBlobInfo();
|
|
}
|
|
}
|
|
|
|
public async Task SetWalletTransactionInfo(WalletId walletId, string transactionId, WalletTransactionInfo walletTransactionInfo)
|
|
{
|
|
if (walletId == null)
|
|
throw new ArgumentNullException(nameof(walletId));
|
|
if (transactionId == null)
|
|
throw new ArgumentNullException(nameof(transactionId));
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
{
|
|
var walletData = new WalletTransactionData() { WalletDataId = walletId.ToString(), TransactionId = transactionId };
|
|
walletData.SetBlobInfo(walletTransactionInfo);
|
|
var entity = await ctx.WalletTransactions.AddAsync(walletData);
|
|
entity.State = EntityState.Modified;
|
|
try
|
|
{
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateException) // Does not exists
|
|
{
|
|
entity.State = EntityState.Added;
|
|
try
|
|
{
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateException) // the Wallet does not exists in the DB
|
|
{
|
|
await SetWalletInfo(walletId, new WalletBlobInfo());
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|