btcpayserver/BTCPayServer/Components/StoreWalletBalance/StoreWalletBalance.cs
d11n 641bdcff31
Histograms: Add Lightning data and API endpoints (#6217)
* Histograms: Add Lightning data and API endpoints

Ported over from the mobile-working-branch.

Adds histogram data for Lightning and exposes the wallet/lightning histogram data via the API. It also add a dashboard graph for the Lightning balance.

Caveat: The Lightning histogram is calculated by using the current channel balance and going backwards through as much invoices and transactions as we have. The "start" of the LN graph data might not be accurate though. That's because we don't track (and not even have) the LN onchain data. It is calculated by using the current channel balance and going backwards through as much invoices and transactions as we have. So the historic graph data for LN is basically a best effort of trying to reconstruct it with what we have: The LN channel transactions.

* More timeframes

* Refactoring: Remove redundant WalletHistogram types

* Remove store property from dashboard tile view models

* JS error fixes
2024-11-05 21:40:37 +09:00

86 lines
3.0 KiB
C#

#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Stores;
using BTCPayServer.Services.Wallets;
using Microsoft.AspNetCore.Mvc;
using StoreData = BTCPayServer.Data.StoreData;
namespace BTCPayServer.Components.StoreWalletBalance;
public class StoreWalletBalance : ViewComponent
{
private const HistogramType DefaultType = HistogramType.Week;
private readonly StoreRepository _storeRepo;
private readonly CurrencyNameTable _currencies;
private readonly WalletHistogramService _walletHistogramService;
private readonly BTCPayWalletProvider _walletProvider;
private readonly BTCPayNetworkProvider _networkProvider;
private readonly PaymentMethodHandlerDictionary _handlers;
public StoreWalletBalance(
StoreRepository storeRepo,
CurrencyNameTable currencies,
WalletHistogramService walletHistogramService,
BTCPayWalletProvider walletProvider,
BTCPayNetworkProvider networkProvider,
PaymentMethodHandlerDictionary handlers)
{
_storeRepo = storeRepo;
_currencies = currencies;
_walletProvider = walletProvider;
_networkProvider = networkProvider;
_walletHistogramService = walletHistogramService;
_handlers = handlers;
}
public async Task<IViewComponentResult> InvokeAsync(StoreData store)
{
var cryptoCode = _networkProvider.DefaultNetwork.CryptoCode;
var walletId = new WalletId(store.Id, cryptoCode);
var data = await _walletHistogramService.GetHistogram(store, walletId, DefaultType);
var defaultCurrency = store.GetStoreBlob().DefaultCurrency;
var vm = new StoreWalletBalanceViewModel
{
StoreId = store.Id,
CryptoCode = cryptoCode,
CurrencyData = _currencies.GetCurrencyData(defaultCurrency, true),
DefaultCurrency = defaultCurrency,
WalletId = walletId,
Type = DefaultType
};
if (data != null)
{
vm.Balance = data.Balance;
vm.Series = data.Series;
vm.Labels = data.Labels;
}
else
{
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(3));
var wallet = _walletProvider.GetWallet(_networkProvider.DefaultNetwork);
var derivation = store.GetDerivationSchemeSettings(_handlers, walletId.CryptoCode);
var network = _handlers.GetBitcoinHandler(walletId.CryptoCode).Network;
if (derivation is not null)
{
var balance = await wallet.GetBalance(derivation.AccountDerivation, cts.Token);
vm.Balance = balance.Available.GetValue(network);
}
else
{
vm.MissingWalletConfig = true;
}
}
return View(vm);
}
}