mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 06:21:44 +01:00
Dashboard balance fixes (#3876)
* Fix wallet balance for case crypto code == default currency * Handle old NBXplorer backend case * Cleanup
This commit is contained in:
parent
b2a5b3c3c4
commit
c943303a45
5 changed files with 66 additions and 34 deletions
|
@ -20,7 +20,6 @@ public class StoreLightningBalance : ViewComponent
|
||||||
{
|
{
|
||||||
private string _cryptoCode;
|
private string _cryptoCode;
|
||||||
private readonly StoreRepository _storeRepo;
|
private readonly StoreRepository _storeRepo;
|
||||||
private readonly BTCPayNetworkBase _network;
|
|
||||||
private readonly BTCPayServerOptions _btcpayServerOptions;
|
private readonly BTCPayServerOptions _btcpayServerOptions;
|
||||||
private readonly BTCPayNetworkProvider _networkProvider;
|
private readonly BTCPayNetworkProvider _networkProvider;
|
||||||
private readonly LightningClientFactoryService _lightningClientFactory;
|
private readonly LightningClientFactoryService _lightningClientFactory;
|
||||||
|
@ -41,8 +40,7 @@ public class StoreLightningBalance : ViewComponent
|
||||||
_externalServiceOptions = externalServiceOptions;
|
_externalServiceOptions = externalServiceOptions;
|
||||||
_lightningClientFactory = lightningClientFactory;
|
_lightningClientFactory = lightningClientFactory;
|
||||||
_lightningNetworkOptions = lightningNetworkOptions;
|
_lightningNetworkOptions = lightningNetworkOptions;
|
||||||
_network = _networkProvider.DefaultNetwork;
|
_cryptoCode = _networkProvider.DefaultNetwork.CryptoCode;
|
||||||
_cryptoCode = _network.CryptoCode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IViewComponentResult> InvokeAsync(StoreData store)
|
public async Task<IViewComponentResult> InvokeAsync(StoreData store)
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
using BTCPayServer.Data;
|
using BTCPayServer.Data;
|
||||||
using BTCPayServer.Lightning;
|
using BTCPayServer.Lightning;
|
||||||
using BTCPayServer.Models;
|
|
||||||
using BTCPayServer.Models.StoreViewModels;
|
|
||||||
|
|
||||||
namespace BTCPayServer.Components.StoreLightningBalance;
|
namespace BTCPayServer.Components.StoreLightningBalance;
|
||||||
|
|
||||||
|
|
|
@ -25,27 +25,44 @@
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
<header class="mb-3">
|
<header class="mb-3">
|
||||||
@if (Model.Balance is not null)
|
@if (Model.Balance != null)
|
||||||
{
|
{
|
||||||
<div class="balance" id="Balance-@Model.CryptoCode">
|
<div class="balance" id="Balance-@Model.CryptoCode">
|
||||||
<h3 class="d-inline-block me-1">@Model.Balance</h3>
|
<h3 class="d-inline-block me-1">@Model.Balance</h3>
|
||||||
<span class="text-secondary fw-semibold">@Model.CryptoCode</span>
|
<span class="text-secondary fw-semibold">@Model.CryptoCode</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="balance" id="Balance-@Model.DefaultCurrency">
|
@if (Model.CryptoCode != Model.DefaultCurrency)
|
||||||
<h3 class="d-inline-block" id="DefaultCurrencyBalance"></h3>
|
{
|
||||||
<span class="text-secondary fw-semibold">@Model.DefaultCurrency</span>
|
<div class="balance" id="Balance-@Model.DefaultCurrency">
|
||||||
|
<h3 class="d-inline-block" id="DefaultCurrencyBalance"></h3>
|
||||||
|
<span class="text-secondary fw-semibold">@Model.DefaultCurrency</span>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@if (Model.Series != null)
|
||||||
|
{
|
||||||
|
<div class="btn-group mt-1" role="group" aria-label="Filter">
|
||||||
|
<input type="radio" class="btn-check" name="filter" id="filter-week" value="week" @(Model.Type == WalletHistogramType.Week ? "checked" : "")>
|
||||||
|
<label class="btn btn-link" for="filter-week">1W</label>
|
||||||
|
<input type="radio" class="btn-check" name="filter" id="filter-month" value="month" @(Model.Type == WalletHistogramType.Month ? "checked" : "")>
|
||||||
|
<label class="btn btn-link" for="filter-month">1M</label>
|
||||||
|
<input type="radio" class="btn-check" name="filter" id="filter-year" value="year" @(Model.Type == WalletHistogramType.Year ? "checked" : "")>
|
||||||
|
<label class="btn btn-link" for="filter-year">1Y</label>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
<div class="btn-group mt-1" role="group" aria-label="Filter">
|
|
||||||
<input type="radio" class="btn-check" name="filter" id="filter-week" value="week" @(Model.Type == WalletHistogramType.Week ? "checked" : "")>
|
|
||||||
<label class="btn btn-link" for="filter-week">1W</label>
|
|
||||||
<input type="radio" class="btn-check" name="filter" id="filter-month" value="month" @(Model.Type == WalletHistogramType.Month ? "checked" : "")>
|
|
||||||
<label class="btn btn-link" for="filter-month">1M</label>
|
|
||||||
<input type="radio" class="btn-check" name="filter" id="filter-year" value="year" @(Model.Type == WalletHistogramType.Year ? "checked" : "")>
|
|
||||||
<label class="btn btn-link" for="filter-year">1Y</label>
|
|
||||||
</div>
|
|
||||||
</header>
|
</header>
|
||||||
<div class="ct-chart"></div>
|
@if (Model.Series != null)
|
||||||
|
{
|
||||||
|
<div class="ct-chart"></div>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<p>
|
||||||
|
We would like to show you a chart of your balance.
|
||||||
|
Please <a href="https://github.com/dgarage/NBXplorer/blob/master/docs/Postgres-Migration.md" target="_blank" rel="noreferrer noopener">migrate to the new NBXplorer backend</a>
|
||||||
|
for that data to become available.
|
||||||
|
</p>
|
||||||
|
}
|
||||||
<script>
|
<script>
|
||||||
(function () {
|
(function () {
|
||||||
const balance = @Safe.Json(Model.Balance);
|
const balance = @Safe.Json(Model.Balance);
|
||||||
|
@ -61,8 +78,6 @@
|
||||||
const $defaultBalance = document.getElementById(`Balance-${defaultCurrency}`);
|
const $defaultBalance = document.getElementById(`Balance-${defaultCurrency}`);
|
||||||
const $defaultCurrencyBalance = document.getElementById('DefaultCurrencyBalance');
|
const $defaultCurrencyBalance = document.getElementById('DefaultCurrencyBalance');
|
||||||
|
|
||||||
$defaultBalance.style.display = 'none';
|
|
||||||
|
|
||||||
const id = `StoreWalletBalance-${storeId}`;
|
const id = `StoreWalletBalance-${storeId}`;
|
||||||
const baseUrl = @Safe.Json(Url.Action("WalletHistogram", "UIWallets", new { walletId = Model.WalletId, type = WalletHistogramType.Week }));
|
const baseUrl = @Safe.Json(Url.Action("WalletHistogram", "UIWallets", new { walletId = Model.WalletId, type = WalletHistogramType.Week }));
|
||||||
const chartOpts = {
|
const chartOpts = {
|
||||||
|
@ -77,10 +92,16 @@
|
||||||
|
|
||||||
const render = data => {
|
const render = data => {
|
||||||
let { series, labels, balance } = data;
|
let { series, labels, balance } = data;
|
||||||
if (rate)
|
|
||||||
series = data.series.map(i => toDefaultCurrency(i, rate));
|
|
||||||
if (balance)
|
if (balance)
|
||||||
document.querySelector(`#${id} h3`).innerText = balance;
|
document.querySelector(`#${id} h3`).innerText = balance;
|
||||||
|
if (cryptoCode !== defaultCurrency) {
|
||||||
|
$cryptoBalance.style.display = rate ? 'none' : 'block';
|
||||||
|
$defaultBalance.style.display = rate ? 'block' : 'none';
|
||||||
|
}
|
||||||
|
if (!series) return;
|
||||||
|
|
||||||
|
if (rate)
|
||||||
|
series = data.series.map(i => toDefaultCurrency(i, rate));
|
||||||
const min = Math.min(...series);
|
const min = Math.min(...series);
|
||||||
const max = Math.max(...series);
|
const max = Math.max(...series);
|
||||||
const low = Math.max(min - ((max - min) / 5), 0);
|
const low = Math.max(min - ((max - min) / 5), 0);
|
||||||
|
@ -147,8 +168,6 @@
|
||||||
rate = null;
|
rate = null;
|
||||||
render(data);
|
render(data);
|
||||||
}
|
}
|
||||||
$cryptoBalance.style.display = rate ? 'none' : 'block';
|
|
||||||
$defaultBalance.style.display = rate ? 'block' : 'none';
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BTCPayServer.Data;
|
using BTCPayServer.Data;
|
||||||
using BTCPayServer.Services;
|
using BTCPayServer.Services;
|
||||||
|
@ -12,6 +13,7 @@ using BTCPayServer.Services.Wallets;
|
||||||
using Dapper;
|
using Dapper;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using NBitcoin;
|
||||||
using NBXplorer;
|
using NBXplorer;
|
||||||
using NBXplorer.Client;
|
using NBXplorer.Client;
|
||||||
|
|
||||||
|
@ -19,44 +21,60 @@ namespace BTCPayServer.Components.StoreWalletBalance;
|
||||||
|
|
||||||
public class StoreWalletBalance : ViewComponent
|
public class StoreWalletBalance : ViewComponent
|
||||||
{
|
{
|
||||||
private string _cryptoCode;
|
|
||||||
private const WalletHistogramType DefaultType = WalletHistogramType.Week;
|
private const WalletHistogramType DefaultType = WalletHistogramType.Week;
|
||||||
|
|
||||||
private readonly StoreRepository _storeRepo;
|
private readonly StoreRepository _storeRepo;
|
||||||
private readonly CurrencyNameTable _currencies;
|
private readonly CurrencyNameTable _currencies;
|
||||||
private readonly WalletHistogramService _walletHistogramService;
|
private readonly WalletHistogramService _walletHistogramService;
|
||||||
|
private readonly BTCPayWalletProvider _walletProvider;
|
||||||
|
private readonly BTCPayNetworkProvider _networkProvider;
|
||||||
|
|
||||||
public StoreWalletBalance(
|
public StoreWalletBalance(
|
||||||
StoreRepository storeRepo,
|
StoreRepository storeRepo,
|
||||||
CurrencyNameTable currencies,
|
CurrencyNameTable currencies,
|
||||||
WalletHistogramService walletHistogramService,
|
WalletHistogramService walletHistogramService,
|
||||||
|
BTCPayWalletProvider walletProvider,
|
||||||
BTCPayNetworkProvider networkProvider)
|
BTCPayNetworkProvider networkProvider)
|
||||||
{
|
{
|
||||||
_storeRepo = storeRepo;
|
_storeRepo = storeRepo;
|
||||||
_currencies = currencies;
|
_currencies = currencies;
|
||||||
|
_walletProvider = walletProvider;
|
||||||
_walletHistogramService = walletHistogramService;
|
_walletHistogramService = walletHistogramService;
|
||||||
_cryptoCode = networkProvider.DefaultNetwork.CryptoCode;
|
_networkProvider = networkProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IViewComponentResult> InvokeAsync(StoreData store)
|
public async Task<IViewComponentResult> InvokeAsync(StoreData store)
|
||||||
{
|
{
|
||||||
var walletId = new WalletId(store.Id, _cryptoCode);
|
var cryptoCode = _networkProvider.DefaultNetwork.CryptoCode;
|
||||||
|
var walletId = new WalletId(store.Id, cryptoCode);
|
||||||
var data = await _walletHistogramService.GetHistogram(store, walletId, DefaultType);
|
var data = await _walletHistogramService.GetHistogram(store, walletId, DefaultType);
|
||||||
var defaultCurrency = store.GetStoreBlob().DefaultCurrency;
|
var defaultCurrency = store.GetStoreBlob().DefaultCurrency;
|
||||||
|
|
||||||
var vm = new StoreWalletBalanceViewModel
|
var vm = new StoreWalletBalanceViewModel
|
||||||
{
|
{
|
||||||
Store = store,
|
Store = store,
|
||||||
CryptoCode = _cryptoCode,
|
CryptoCode = cryptoCode,
|
||||||
CurrencyData = _currencies.GetCurrencyData(defaultCurrency, true),
|
CurrencyData = _currencies.GetCurrencyData(defaultCurrency, true),
|
||||||
DefaultCurrency = defaultCurrency,
|
DefaultCurrency = defaultCurrency,
|
||||||
WalletId = walletId,
|
WalletId = walletId,
|
||||||
Series = data?.Series,
|
|
||||||
Labels = data?.Labels,
|
|
||||||
Balance = data?.Balance,
|
|
||||||
Type = DefaultType
|
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(_networkProvider, walletId.CryptoCode);
|
||||||
|
var balance = await wallet.GetBalance(derivation.AccountDerivation, cts.Token);
|
||||||
|
vm.Balance = balance.Available.GetValue();
|
||||||
|
}
|
||||||
|
|
||||||
return View(vm);
|
return View(vm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,6 @@ public class StoreWalletBalanceViewModel
|
||||||
public StoreData Store { get; set; }
|
public StoreData Store { get; set; }
|
||||||
public WalletId WalletId { get; set; }
|
public WalletId WalletId { get; set; }
|
||||||
public WalletHistogramType Type { get; set; }
|
public WalletHistogramType Type { get; set; }
|
||||||
public IList<string> Labels { get; set; } = new List<string>();
|
public IList<string> Labels { get; set; }
|
||||||
public IList<decimal> Series { get; set; } = new List<decimal>();
|
public IList<decimal> Series { get; set; }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue