diff --git a/www/assets/js/crypto.js b/www/assets/js/crypto.js index 136bc33..0dc4716 100644 --- a/www/assets/js/crypto.js +++ b/www/assets/js/crypto.js @@ -246,4 +246,63 @@ function displayWalletBalance(address) { } }); -} \ No newline at end of file +} + +/** + * Setup an input for specifying amount to send in USD, with conversion to crypto. + * @param {string} walletAddress Detects cryptocurrency from wallet address + * @returns {undefined} + */ +function setupFiatConversion(walletAddress) { + apirequest(SETTINGS.apis.walletbalance, { + walletaddress: walletAddress + }, function (resp) { + if (resp.status != "OK") { + return; + } + if (resp.exchangerates.usd == -1) { + return; + } + $("#cryptoFiatInputItem").css("display", ""); + $("#cryptoAmountSendCurrencyLabel").text(resp.currency); + $("#cryptoAmountSendFiatLabel").text("$"); + $("#transactionAmountFiat").data("exchange-rate", resp.exchangerates.usd); + $("#transactionAmountFiat").data("cryptocurrency", resp.currency); + }); +} + +/** + * Hides the fiat conversion input box. + * @returns {undefined} + */ +function unsetupFiatConversion() { + $("#cryptoFiatInputItem").css("display", "none"); + $("#cryptoAmountSendCurrencyLabel").text(""); + $("#transactionAmountFiat").removeData("exchange-rate"); + $("#transactionAmountFiat").removeData("cryptocurrency"); +} + +$("#app").on("input change paste keyup", "#transactionAmountFiat", function () { + var fiatamount = parseFloat($("#transactionAmountFiat").val()); + var exchangerate = parseFloat($("#transactionAmountFiat").data("exchange-rate")); + $("#transactionAmount").val((fiatamount / exchangerate).toFixed(8)); + $("#transactionAmountFiat").val(fiatamount.toFixed(2)); +}); + +$("#app").on("paste blur", "#walletFromAddress", function () { + if (walletPubKeyRegex.test($("#walletFromAddress").val())) { + setupFiatConversion($("#walletFromAddress").val()); + } else { + unsetupFiatConversion(); + } +}); + +$("#app").on("input change paste keyup", "#transactionAmount", function () { + if ($("#cryptoFiatInputItem").css("display") == "none") { + return; + } + var amount = parseFloat($("#transactionAmount").val()); + var exchangerate = parseFloat($("#transactionAmountFiat").data("exchange-rate")); + $("#transactionAmountFiat").val((amount * exchangerate).toFixed(2)); + $("#transactionAmount").val(amount.toFixed(8)); +}); \ No newline at end of file diff --git a/www/pages/crypto.html b/www/pages/crypto.html index 2a7595a..704580c 100644 --- a/www/pages/crypto.html +++ b/www/pages/crypto.html @@ -73,6 +73,7 @@
  •   Scan Your Wallet Address
  • @@ -127,17 +128,27 @@
  • Step 4
  • - Enter the amount to send, in cryptocurrency (not in dollars). + Enter the amount to send.
  • +
  • +