mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 18:11:36 +01:00
51690b47a3
* Automated Transfer processors This PR introduces a few things: * Payouts can now be directly nested under a store instead of through a pull payment. * The Wallet Send screen now has an option to "schedule" instead of simply creating a transaction. When you click on schedule, all transaction destinations are converted into approved payouts. Any options relating to fees or coin selection are discarded. * There is a new concept introduced, called "Transfer Processors". Transfer Processors are services for stores that process payouts that are awaiting payment. Each processor specifies which payment methods it can handle. BTCPay Server will have some forms of transfer processors baked in but it has been designed to allow the Plugin System to provide additional processors. * The initial transfer processors provided are "automated processors", for on chain and lightning payment methods. They can be configured to process payouts every X amount of minutes. For on-chain, this means payments are batched into one transaction, resulting in more efficient and cheaper fees for processing. * * fix build * extract * remove magic string stuff * fix error message when scheduling * Paginate migration * add payout count to payment method tab * remove unused var * add protip * optimzie payout migration dramatically * Remove useless double condition * Fix bunch of warnings * Remove warning * Remove warnigns * Rename to Payout processors * fix typo Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com>
123 lines
4.0 KiB
C#
123 lines
4.0 KiB
C#
#nullable enable
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
namespace BTCPayServer;
|
|
|
|
public class LightningAddressService
|
|
{
|
|
private readonly ApplicationDbContextFactory _applicationDbContextFactory;
|
|
private readonly IMemoryCache _memoryCache;
|
|
|
|
public LightningAddressService(ApplicationDbContextFactory applicationDbContextFactory, IMemoryCache memoryCache)
|
|
{
|
|
_applicationDbContextFactory = applicationDbContextFactory;
|
|
_memoryCache = memoryCache;
|
|
}
|
|
|
|
public async Task<List<LightningAddressData>> Get(LightningAddressQuery query)
|
|
{
|
|
await using var context = _applicationDbContextFactory.CreateContext();
|
|
return await GetCore(context, query);
|
|
}
|
|
|
|
private async Task<List<LightningAddressData>> GetCore(ApplicationDbContext context, LightningAddressQuery query)
|
|
{
|
|
IQueryable<LightningAddressData?> queryable = context.LightningAddresses.AsQueryable();
|
|
query.Usernames = query.Usernames?.Select(NormalizeUsername)?.ToArray();
|
|
if (query.Usernames is not null)
|
|
{
|
|
queryable = queryable.Where(data => query.Usernames.Contains(data!.Username));
|
|
}
|
|
|
|
if (query.StoreIds is not null)
|
|
{
|
|
queryable = queryable.Where(data => query.StoreIds.Contains(data!.StoreDataId));
|
|
}
|
|
|
|
#pragma warning disable CS8619 // Nullability of reference types in value doesn't match target type.
|
|
return await queryable.ToListAsync();
|
|
#pragma warning restore CS8619 // Nullability of reference types in value doesn't match target type.
|
|
}
|
|
|
|
public async Task<LightningAddressData?> ResolveByAddress(string username)
|
|
{
|
|
return await _memoryCache.GetOrCreateAsync(GetKey(username), async entry =>
|
|
{
|
|
var result = await Get(new LightningAddressQuery() {Usernames = new[] {username}});
|
|
return result.FirstOrDefault();
|
|
});
|
|
}
|
|
|
|
private string NormalizeUsername(string username)
|
|
{
|
|
return username.ToLowerInvariant();
|
|
}
|
|
|
|
public async Task<bool> Set(LightningAddressData data)
|
|
{
|
|
await using var context = _applicationDbContextFactory.CreateContext();
|
|
var result = (await GetCore(context, new LightningAddressQuery() {Usernames = new[] {data.Username}}))
|
|
.FirstOrDefault();
|
|
if (result is not null)
|
|
{
|
|
if (result.StoreDataId != data.StoreDataId)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
context.Remove(result);
|
|
}
|
|
|
|
data.Username = NormalizeUsername(data.Username);
|
|
await context.AddAsync(data);
|
|
await context.SaveChangesAsync();
|
|
_memoryCache.Remove(GetKey(data.Username));
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> Remove(string username, string? storeId = null)
|
|
{
|
|
await using var context = _applicationDbContextFactory.CreateContext();
|
|
var x = (await GetCore(context, new LightningAddressQuery() {Usernames = new[] {username}})).FirstOrDefault();
|
|
if (x is null) return true;
|
|
if (storeId is not null && x.StoreDataId != storeId)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
context.Remove(x);
|
|
await context.SaveChangesAsync();
|
|
_memoryCache.Remove(GetKey(username));
|
|
return true;
|
|
}
|
|
|
|
public async Task Set(LightningAddressData data, ApplicationDbContext context)
|
|
{
|
|
var result = (await GetCore(context, new LightningAddressQuery() {Usernames = new[] {data.Username}}))
|
|
.FirstOrDefault();
|
|
if (result is not null)
|
|
{
|
|
if (result.StoreDataId != data.StoreDataId)
|
|
{
|
|
return;
|
|
}
|
|
|
|
context.Remove(result);
|
|
}
|
|
|
|
await context.AddAsync(data);
|
|
}
|
|
|
|
|
|
private string GetKey(string username)
|
|
{
|
|
username = NormalizeUsername(username);
|
|
return $"{nameof(LightningAddressService)}_{username}";
|
|
}
|
|
}
|