2022-06-14 07:36:22 +02:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
using BTCPayServer.Lightning;
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
|
|
|
using BTCPayServer.Payments;
|
|
|
|
using BTCPayServer.Payments.Lightning;
|
|
|
|
using BTCPayServer.Services;
|
2022-07-04 04:03:16 +02:00
|
|
|
using BTCPayServer.Services.Rates;
|
2022-06-14 07:36:22 +02:00
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Components.StoreLightningBalance;
|
|
|
|
|
|
|
|
public class StoreLightningBalance : ViewComponent
|
|
|
|
{
|
|
|
|
private readonly StoreRepository _storeRepo;
|
2022-07-04 04:03:16 +02:00
|
|
|
private readonly CurrencyNameTable _currencies;
|
2022-06-14 07:36:22 +02:00
|
|
|
private readonly BTCPayServerOptions _btcpayServerOptions;
|
|
|
|
private readonly BTCPayNetworkProvider _networkProvider;
|
|
|
|
private readonly LightningClientFactoryService _lightningClientFactory;
|
|
|
|
private readonly IOptions<LightningNetworkOptions> _lightningNetworkOptions;
|
|
|
|
private readonly IOptions<ExternalServicesOptions> _externalServiceOptions;
|
|
|
|
|
|
|
|
public StoreLightningBalance(
|
|
|
|
StoreRepository storeRepo,
|
2022-07-04 04:03:16 +02:00
|
|
|
CurrencyNameTable currencies,
|
2022-06-14 07:36:22 +02:00
|
|
|
BTCPayNetworkProvider networkProvider,
|
|
|
|
BTCPayServerOptions btcpayServerOptions,
|
|
|
|
LightningClientFactoryService lightningClientFactory,
|
|
|
|
IOptions<LightningNetworkOptions> lightningNetworkOptions,
|
|
|
|
IOptions<ExternalServicesOptions> externalServiceOptions)
|
|
|
|
{
|
|
|
|
_storeRepo = storeRepo;
|
2022-07-04 04:03:16 +02:00
|
|
|
_currencies = currencies;
|
2022-06-14 07:36:22 +02:00
|
|
|
_networkProvider = networkProvider;
|
|
|
|
_btcpayServerOptions = btcpayServerOptions;
|
|
|
|
_externalServiceOptions = externalServiceOptions;
|
|
|
|
_lightningClientFactory = lightningClientFactory;
|
|
|
|
_lightningNetworkOptions = lightningNetworkOptions;
|
|
|
|
}
|
|
|
|
|
2022-07-04 04:03:16 +02:00
|
|
|
public async Task<IViewComponentResult> InvokeAsync(StoreLightningBalanceViewModel vm)
|
2022-06-14 07:36:22 +02:00
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
if (vm.Store == null)
|
|
|
|
throw new ArgumentNullException(nameof(vm.Store));
|
|
|
|
if (vm.CryptoCode == null)
|
|
|
|
throw new ArgumentNullException(nameof(vm.CryptoCode));
|
|
|
|
|
2022-07-04 04:03:16 +02:00
|
|
|
vm.DefaultCurrency = vm.Store.GetStoreBlob().DefaultCurrency;
|
|
|
|
vm.CurrencyData = _currencies.GetCurrencyData(vm.DefaultCurrency, true);
|
|
|
|
|
2023-01-06 14:18:07 +01:00
|
|
|
if (vm.InitialRendering)
|
|
|
|
return View(vm);
|
|
|
|
|
2022-07-04 04:03:16 +02:00
|
|
|
try
|
2022-06-14 07:36:22 +02:00
|
|
|
{
|
2022-07-06 05:40:16 +02:00
|
|
|
var lightningClient = GetLightningClient(vm.Store, vm.CryptoCode);
|
2022-07-04 04:03:16 +02:00
|
|
|
var balance = await lightningClient.GetBalance();
|
|
|
|
vm.Balance = balance;
|
|
|
|
vm.TotalOnchain = balance.OnchainBalance != null
|
2023-01-06 14:18:07 +01:00
|
|
|
? (balance.OnchainBalance.Confirmed ?? 0L) + (balance.OnchainBalance.Reserved ?? 0L) +
|
2022-07-08 10:55:26 +09:00
|
|
|
(balance.OnchainBalance.Unconfirmed ?? 0L)
|
2022-07-04 04:03:16 +02:00
|
|
|
: null;
|
|
|
|
vm.TotalOffchain = balance.OffchainBalance != null
|
2023-01-06 14:18:07 +01:00
|
|
|
? (balance.OffchainBalance.Opening ?? 0) + (balance.OffchainBalance.Local ?? 0) +
|
|
|
|
(balance.OffchainBalance.Closing ?? 0)
|
2022-07-04 04:03:16 +02:00
|
|
|
: null;
|
2022-06-14 07:36:22 +02:00
|
|
|
}
|
2022-07-04 04:03:16 +02:00
|
|
|
catch (NotSupportedException)
|
2022-06-14 07:36:22 +02:00
|
|
|
{
|
2022-07-04 04:03:16 +02:00
|
|
|
// not all implementations support balance fetching
|
|
|
|
vm.ProblemDescription = "Your node does not support balance fetching.";
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// general error
|
|
|
|
vm.ProblemDescription = "Could not fetch Lightning balance.";
|
2022-06-14 07:36:22 +02:00
|
|
|
}
|
|
|
|
return View(vm);
|
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-07-06 05:40:16 +02:00
|
|
|
private ILightningClient GetLightningClient(StoreData store, string cryptoCode)
|
2022-06-14 07:36:22 +02:00
|
|
|
{
|
2022-07-06 05:40:16 +02:00
|
|
|
var network = _networkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
|
|
|
var id = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike);
|
2022-06-14 07:36:22 +02:00
|
|
|
var existing = store.GetSupportedPaymentMethods(_networkProvider)
|
|
|
|
.OfType<LightningSupportedPaymentMethod>()
|
|
|
|
.FirstOrDefault(d => d.PaymentId == id);
|
2023-01-06 14:18:07 +01:00
|
|
|
if (existing == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (existing.GetExternalLightningUrl() is { } connectionString)
|
2022-06-14 07:36:22 +02:00
|
|
|
{
|
|
|
|
return _lightningClientFactory.Create(connectionString, network);
|
|
|
|
}
|
2022-07-06 05:40:16 +02:00
|
|
|
if (existing.IsInternalNode && _lightningNetworkOptions.Value.InternalLightningByCryptoCode.TryGetValue(cryptoCode, out var internalLightningNode))
|
2022-06-14 07:36:22 +02:00
|
|
|
{
|
|
|
|
return _lightningClientFactory.Create(internalLightningNode, network);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|