Dashboard balance fixes (#3876)

* Fix wallet balance for case crypto code == default currency

* Handle old NBXplorer backend case

* Cleanup
This commit is contained in:
d11n 2022-06-20 07:31:22 +02:00 committed by GitHub
parent b2a5b3c3c4
commit c943303a45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 34 deletions

View file

@ -20,7 +20,6 @@ public class StoreLightningBalance : ViewComponent
{
private string _cryptoCode;
private readonly StoreRepository _storeRepo;
private readonly BTCPayNetworkBase _network;
private readonly BTCPayServerOptions _btcpayServerOptions;
private readonly BTCPayNetworkProvider _networkProvider;
private readonly LightningClientFactoryService _lightningClientFactory;
@ -41,8 +40,7 @@ public class StoreLightningBalance : ViewComponent
_externalServiceOptions = externalServiceOptions;
_lightningClientFactory = lightningClientFactory;
_lightningNetworkOptions = lightningNetworkOptions;
_network = _networkProvider.DefaultNetwork;
_cryptoCode = _network.CryptoCode;
_cryptoCode = _networkProvider.DefaultNetwork.CryptoCode;
}
public async Task<IViewComponentResult> InvokeAsync(StoreData store)

View file

@ -1,8 +1,5 @@
using System.Collections.Generic;
using BTCPayServer.Data;
using BTCPayServer.Lightning;
using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels;
namespace BTCPayServer.Components.StoreLightningBalance;

View file

@ -25,27 +25,44 @@
}
</div>
<header class="mb-3">
@if (Model.Balance is not null)
@if (Model.Balance != null)
{
<div class="balance" id="Balance-@Model.CryptoCode">
<h3 class="d-inline-block me-1">@Model.Balance</h3>
<span class="text-secondary fw-semibold">@Model.CryptoCode</span>
</div>
<div class="balance" id="Balance-@Model.DefaultCurrency">
<h3 class="d-inline-block" id="DefaultCurrencyBalance"></h3>
<span class="text-secondary fw-semibold">@Model.DefaultCurrency</span>
@if (Model.CryptoCode != Model.DefaultCurrency)
{
<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 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>
<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>
(function () {
const balance = @Safe.Json(Model.Balance);
@ -61,8 +78,6 @@
const $defaultBalance = document.getElementById(`Balance-${defaultCurrency}`);
const $defaultCurrencyBalance = document.getElementById('DefaultCurrencyBalance');
$defaultBalance.style.display = 'none';
const id = `StoreWalletBalance-${storeId}`;
const baseUrl = @Safe.Json(Url.Action("WalletHistogram", "UIWallets", new { walletId = Model.WalletId, type = WalletHistogramType.Week }));
const chartOpts = {
@ -77,10 +92,16 @@
const render = data => {
let { series, labels, balance } = data;
if (rate)
series = data.series.map(i => toDefaultCurrency(i, rate));
if (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 max = Math.max(...series);
const low = Math.max(min - ((max - min) / 5), 0);
@ -147,8 +168,6 @@
rate = null;
render(data);
}
$cryptoBalance.style.display = rate ? 'none' : 'block';
$defaultBalance.style.display = rate ? 'block' : 'none';
});
});
})();

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Services;
@ -12,6 +13,7 @@ using BTCPayServer.Services.Wallets;
using Dapper;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using NBitcoin;
using NBXplorer;
using NBXplorer.Client;
@ -19,44 +21,60 @@ namespace BTCPayServer.Components.StoreWalletBalance;
public class StoreWalletBalance : ViewComponent
{
private string _cryptoCode;
private const WalletHistogramType DefaultType = WalletHistogramType.Week;
private readonly StoreRepository _storeRepo;
private readonly CurrencyNameTable _currencies;
private readonly WalletHistogramService _walletHistogramService;
private readonly BTCPayWalletProvider _walletProvider;
private readonly BTCPayNetworkProvider _networkProvider;
public StoreWalletBalance(
StoreRepository storeRepo,
CurrencyNameTable currencies,
WalletHistogramService walletHistogramService,
BTCPayWalletProvider walletProvider,
BTCPayNetworkProvider networkProvider)
{
_storeRepo = storeRepo;
_currencies = currencies;
_walletProvider = walletProvider;
_walletHistogramService = walletHistogramService;
_cryptoCode = networkProvider.DefaultNetwork.CryptoCode;
_networkProvider = networkProvider;
}
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 defaultCurrency = store.GetStoreBlob().DefaultCurrency;
var vm = new StoreWalletBalanceViewModel
{
Store = store,
CryptoCode = _cryptoCode,
CryptoCode = cryptoCode,
CurrencyData = _currencies.GetCurrencyData(defaultCurrency, true),
DefaultCurrency = defaultCurrency,
WalletId = walletId,
Series = data?.Series,
Labels = data?.Labels,
Balance = data?.Balance,
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);
}
}

View file

@ -14,6 +14,6 @@ public class StoreWalletBalanceViewModel
public StoreData Store { get; set; }
public WalletId WalletId { get; set; }
public WalletHistogramType Type { get; set; }
public IList<string> Labels { get; set; } = new List<string>();
public IList<decimal> Series { get; set; } = new List<decimal>();
public IList<string> Labels { get; set; }
public IList<decimal> Series { get; set; }
}