Show total balances for each currency on List Wallets Page

This commit is contained in:
Max Dignan 2021-09-15 10:52:57 -04:00 committed by Andrew Camilleri
parent 88c925017d
commit b7a081b9a4
3 changed files with 33 additions and 2 deletions

View file

@ -259,10 +259,20 @@ namespace BTCPayServer.Controllers
{
walletVm.Balance = "";
}
walletVm.Network = wallet.Network;
walletVm.CryptoCode = wallet.Network.CryptoCode;
walletVm.StoreId = wallet.Store.Id;
walletVm.Id = new WalletId(wallet.Store.Id, wallet.Network.CryptoCode);
walletVm.StoreName = wallet.Store.StoreName;
if (wallets.BalanceForCryptoCode.ContainsKey(wallet.Network))
{
wallets.BalanceForCryptoCode[wallet.Network] = wallets.BalanceForCryptoCode[wallet.Network].Add(await GetBalanceAsMoney(wallet.Wallet, wallet.DerivationStrategy));
}
else
{
wallets.BalanceForCryptoCode[wallet.Network] = await GetBalanceAsMoney(wallet.Wallet, wallet.DerivationStrategy);
}
}
return View(wallets);
@ -1061,13 +1071,25 @@ namespace BTCPayServer.Controllers
return CurrentStore.GetDerivationSchemeSettings(NetworkProvider, walletId.CryptoCode);
}
private static async Task<string> GetBalanceString(BTCPayWallet wallet, DerivationStrategyBase derivationStrategy)
private static async Task<IMoney> GetBalanceAsMoney(BTCPayWallet wallet, DerivationStrategyBase derivationStrategy)
{
using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
try
{
var b = await wallet.GetBalance(derivationStrategy, cts.Token);
return (b.Available ?? b.Total).ShowMoney(wallet.Network);
return (b.Available ?? b.Total);
}
catch
{
return NBitcoin.Money.Zero;
}
}
private static async Task<string> GetBalanceString(BTCPayWallet wallet, DerivationStrategyBase derivationStrategy)
{
try
{
return (await GetBalanceAsMoney(wallet, derivationStrategy)).ShowMoney(wallet.Network);
}
catch
{

View file

@ -1,4 +1,6 @@
using System.Collections.Generic;
using NBitcoin;
using BTCPayServer;
namespace BTCPayServer.Models.WalletViewModels
{
@ -12,8 +14,10 @@ namespace BTCPayServer.Models.WalletViewModels
public string Balance { get; set; }
public bool IsOwner { get; set; }
public WalletId Id { get; set; }
public BTCPayNetwork Network { get; set; }
}
public Dictionary<BTCPayNetwork, IMoney> BalanceForCryptoCode { get; set; } = new Dictionary<BTCPayNetwork, IMoney>();
public List<WalletViewModel> Wallets { get; set; } = new List<WalletViewModel>();
}
}

View file

@ -16,6 +16,11 @@
<span class="fa fa-question-circle-o text-secondary" title="More information..."></span>
</a>
</small>
Total Balance:
@foreach(var balanceForACrypto in @Model.BalanceForCryptoCode)
{
<div>@(balanceForACrypto.Value.ShowMoney(balanceForACrypto.Key) + " - " + balanceForACrypto.Key.CryptoCode)</div>
}
</h2>
</div>
</div>