From 783132a0125ae24cf0f8a83fce340ca4d621c317 Mon Sep 17 00:00:00 2001 From: NicolasDorier Date: Fri, 6 Oct 2017 11:07:22 +0900 Subject: [PATCH] Add balance of the store in the stores page --- BTCPayServer.Tests/README.md | 6 ++++++ BTCPayServer/Controllers/StoresController.cs | 8 ++++++-- BTCPayServer/Models/StoreViewModels/StoresViewModel.cs | 7 ++++++- BTCPayServer/Services/Wallets/BTCPayWallet.cs | 8 ++++++++ BTCPayServer/Views/Stores/ListStores.cshtml | 2 ++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/BTCPayServer.Tests/README.md b/BTCPayServer.Tests/README.md index d0ee34f55..652c0bca4 100644 --- a/BTCPayServer.Tests/README.md +++ b/BTCPayServer.Tests/README.md @@ -23,4 +23,10 @@ You can run the tests inside a container by running ``` docker-compose run --rm tests +``` + +The Bitcoin RPC server is exposed to the host, for example, you can send 0.23111090 BTC to mohu16LH66ptoWGEL1GtP6KHTBJYXMWhEf. + +``` +bitcoin-cli -regtest -rpcport=43782 -rpcuser=ceiwHEbqWI83 -rpcpassword=DwubwWsoo3 sendtoaddress "mohu16LH66ptoWGEL1GtP6KHTBJYXMWhEf" 0.23111090 ``` \ No newline at end of file diff --git a/BTCPayServer/Controllers/StoresController.cs b/BTCPayServer/Controllers/StoresController.cs index 465f29daf..8a8b8e0f6 100644 --- a/BTCPayServer/Controllers/StoresController.cs +++ b/BTCPayServer/Controllers/StoresController.cs @@ -86,13 +86,17 @@ namespace BTCPayServer.Controllers StoresViewModel result = new StoresViewModel(); result.StatusMessage = StatusMessage; var stores = await _Repo.GetStoresByUserId(GetUserId()); - foreach(var store in stores) + var balances = stores.Select(async s => string.IsNullOrEmpty(s.DerivationStrategy) ? Money.Zero : await _Wallet.GetBalance(s.DerivationStrategy)).ToArray(); + + for(int i = 0; i < stores.Length; i++) { + var store = stores[i]; result.Stores.Add(new StoresViewModel.StoreViewModel() { Id = store.Id, Name = store.StoreName, - WebSite = store.StoreWebsite + WebSite = store.StoreWebsite, + Balance = await balances[i] }); } return View(result); diff --git a/BTCPayServer/Models/StoreViewModels/StoresViewModel.cs b/BTCPayServer/Models/StoreViewModels/StoresViewModel.cs index 36dd9bac8..32fb2ea9d 100644 --- a/BTCPayServer/Models/StoreViewModels/StoresViewModel.cs +++ b/BTCPayServer/Models/StoreViewModels/StoresViewModel.cs @@ -1,4 +1,5 @@ -using System; +using NBitcoin; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -31,6 +32,10 @@ namespace BTCPayServer.Models.StoreViewModels { get; set; } + public Money Balance + { + get; set; + } } } } diff --git a/BTCPayServer/Services/Wallets/BTCPayWallet.cs b/BTCPayServer/Services/Wallets/BTCPayWallet.cs index 754d61c6b..3c4b1a84e 100644 --- a/BTCPayServer/Services/Wallets/BTCPayWallet.cs +++ b/BTCPayServer/Services/Wallets/BTCPayWallet.cs @@ -73,5 +73,13 @@ namespace BTCPayServer.Services.Wallets var tasks = transactions.Select(t => _Client.BroadcastAsync(t)).ToArray(); return Task.WhenAll(tasks); } + + public async Task GetBalance(string derivationStrategy) + { + var result = await _Client.SyncAsync(_DerivationStrategyFactory.Parse(derivationStrategy), null, true); + return result.Confirmed.UTXOs.Select(u => u.Output.Value) + .Concat(result.Unconfirmed.UTXOs.Select(u => u.Output.Value)) + .Sum(); + } } } diff --git a/BTCPayServer/Views/Stores/ListStores.cshtml b/BTCPayServer/Views/Stores/ListStores.cshtml index a56d74657..59ae08a8a 100644 --- a/BTCPayServer/Views/Stores/ListStores.cshtml +++ b/BTCPayServer/Views/Stores/ListStores.cshtml @@ -27,6 +27,7 @@ Name Website + Balance Actions @@ -40,6 +41,7 @@ { @store.WebSite } + @store.Balance Settings }