From b7a081b9a4d87d9209c855fe810138507cca4917 Mon Sep 17 00:00:00 2001 From: Max Dignan Date: Wed, 15 Sep 2021 10:52:57 -0400 Subject: [PATCH] Show total balances for each currency on List Wallets Page --- BTCPayServer/Controllers/WalletsController.cs | 26 +++++++++++++++++-- .../WalletViewModels/ListWalletsViewModel.cs | 4 +++ BTCPayServer/Views/Wallets/ListWallets.cshtml | 5 ++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/BTCPayServer/Controllers/WalletsController.cs b/BTCPayServer/Controllers/WalletsController.cs index e0a19789c..28ac22090 100644 --- a/BTCPayServer/Controllers/WalletsController.cs +++ b/BTCPayServer/Controllers/WalletsController.cs @@ -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 GetBalanceString(BTCPayWallet wallet, DerivationStrategyBase derivationStrategy) + private static async Task 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 GetBalanceString(BTCPayWallet wallet, DerivationStrategyBase derivationStrategy) + { + try + { + return (await GetBalanceAsMoney(wallet, derivationStrategy)).ShowMoney(wallet.Network); } catch { diff --git a/BTCPayServer/Models/WalletViewModels/ListWalletsViewModel.cs b/BTCPayServer/Models/WalletViewModels/ListWalletsViewModel.cs index b17c21899..ce0c6d1bf 100644 --- a/BTCPayServer/Models/WalletViewModels/ListWalletsViewModel.cs +++ b/BTCPayServer/Models/WalletViewModels/ListWalletsViewModel.cs @@ -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 BalanceForCryptoCode { get; set; } = new Dictionary(); public List Wallets { get; set; } = new List(); } } diff --git a/BTCPayServer/Views/Wallets/ListWallets.cshtml b/BTCPayServer/Views/Wallets/ListWallets.cshtml index a69c0d72d..242029aac 100644 --- a/BTCPayServer/Views/Wallets/ListWallets.cshtml +++ b/BTCPayServer/Views/Wallets/ListWallets.cshtml @@ -16,6 +16,11 @@ + Total Balance: + @foreach(var balanceForACrypto in @Model.BalanceForCryptoCode) + { +
@(balanceForACrypto.Value.ShowMoney(balanceForACrypto.Key) + " - " + balanceForACrypto.Key.CryptoCode)
+ }