diff --git a/BTCPayServer/Controllers/WalletsController.cs b/BTCPayServer/Controllers/WalletsController.cs index 4c144f9ca..0abf1d930 100644 --- a/BTCPayServer/Controllers/WalletsController.cs +++ b/BTCPayServer/Controllers/WalletsController.cs @@ -13,6 +13,7 @@ using BTCPayServer.Models; using BTCPayServer.Models.WalletViewModels; using BTCPayServer.Security; using BTCPayServer.Services; +using BTCPayServer.Services.Rates; using BTCPayServer.Services.Stores; using BTCPayServer.Services.Wallets; using LedgerWallet; @@ -41,17 +42,22 @@ namespace BTCPayServer.Controllers private readonly ExplorerClientProvider _explorerProvider; private readonly IFeeProviderFactory _feeRateProvider; private readonly BTCPayWalletProvider _walletProvider; - + BTCPayRateProviderFactory _RateProvider; + CurrencyNameTable _currencyTable; public WalletsController(StoreRepository repo, + CurrencyNameTable currencyTable, BTCPayNetworkProvider networkProvider, UserManager userManager, IOptions mvcJsonOptions, NBXplorerDashboard dashboard, + BTCPayRateProviderFactory rateProvider, ExplorerClientProvider explorerProvider, IFeeProviderFactory feeRateProvider, BTCPayWalletProvider walletProvider) { + _currencyTable = currencyTable; _Repo = repo; + _RateProvider = rateProvider; _NetworkProvider = networkProvider; _userManager = userManager; _mvcJsonOptions = mvcJsonOptions; @@ -118,9 +124,30 @@ namespace BTCPayServer.Controllers if (paymentMethod == null) return NotFound(); + + var storeData = store.GetStoreBlob(); + var rateRules = store.GetStoreBlob().GetRateRules(_NetworkProvider); + rateRules.GlobalMultiplier = 1.0m; + var currencyPair = new Rating.CurrencyPair(paymentMethod.PaymentId.CryptoCode, storeData.DefaultLang ?? "USD"); WalletModel model = new WalletModel(); model.ServerUrl = GetLedgerWebsocketUrl(this.HttpContext, walletId.CryptoCode, paymentMethod.DerivationStrategyBase); model.CryptoCurrency = walletId.CryptoCode; + + using (CancellationTokenSource cts = new CancellationTokenSource()) + { + try + { + cts.CancelAfter(TimeSpan.FromSeconds(5)); + var result = await _RateProvider.FetchRate(currencyPair, rateRules).WithCancellation(cts.Token); + if (result.Value != null) + { + model.Rate = result.Value; + model.Divisibility = _currencyTable.GetNumberFormatInfo(currencyPair.Right, true).CurrencyDecimalDigits; + model.Fiat = currencyPair.Right; + } + } + catch { } + } return View(model); } diff --git a/BTCPayServer/Models/WalletViewModels/WalletModel.cs b/BTCPayServer/Models/WalletViewModels/WalletModel.cs index 191e42336..75ef3bb46 100644 --- a/BTCPayServer/Models/WalletViewModels/WalletModel.cs +++ b/BTCPayServer/Models/WalletViewModels/WalletModel.cs @@ -15,5 +15,8 @@ namespace BTCPayServer.Models.WalletViewModels get; set; } + public decimal? Rate { get; set; } + public int Divisibility { get; set; } + public string Fiat { get; set; } } } diff --git a/BTCPayServer/Views/Wallets/WalletSend.cshtml b/BTCPayServer/Views/Wallets/WalletSend.cshtml index c2190e312..7c7f36147 100644 --- a/BTCPayServer/Views/Wallets/WalletSend.cshtml +++ b/BTCPayServer/Views/Wallets/WalletSend.cshtml @@ -41,7 +41,14 @@
- +
+ +
+ +
+
+ + @**@
- @section Scripts - { - - - - } + + + +} diff --git a/BTCPayServer/wwwroot/js/StoreWallet.js b/BTCPayServer/wwwroot/js/StoreWallet.js index e3f091659..db3cee050 100644 --- a/BTCPayServer/wwwroot/js/StoreWallet.js +++ b/BTCPayServer/wwwroot/js/StoreWallet.js @@ -1,4 +1,15 @@ -$(function () { + +function updateFiatValue() { + if (srvModel.rate !== null) { + var fiatValue = $("#fiatValue"); + fiatValue.css("display", "inline"); + var amountValue = parseFloat($("#amount-textbox").val()); + if (!isNaN(amountValue)) { + fiatValue.text("= " + (srvModel.rate * amountValue).toFixed(srvModel.divisibility) + " " + srvModel.fiat); + } + } +} +$(function () { var ledgerDetected = false; var bridge = new ledgerwebsocket.LedgerWebSocketBridge(srvModel.serverUrl); var recommendedFees = "";