From a83adb70adebf4528d558372998ac348bcc6c148 Mon Sep 17 00:00:00 2001 From: thotbot Date: Thu, 19 Dec 2019 00:16:00 +0000 Subject: [PATCH] Transfer: improve warning flow --- main.qml | 19 ++++++++++----- pages/Transfer.qml | 40 ++++++++++++++----------------- src/libwalletqt/Wallet.cpp | 10 ++++++++ src/libwalletqt/Wallet.h | 2 ++ src/libwalletqt/WalletManager.cpp | 2 +- src/libwalletqt/WalletManager.h | 2 +- 6 files changed, 45 insertions(+), 30 deletions(-) diff --git a/main.qml b/main.qml index 7e28a4c6..536c5216 100644 --- a/main.qml +++ b/main.qml @@ -401,6 +401,13 @@ ApplicationWindow { return path.replace(/.*[\/\\]/, '').replace(/\.keys$/, '') } + function getUnlockedBalance() { + if(!currentWallet){ + return 0 + } + return currentWallet.unlockedBalance() + } + function updateBalance() { if (!currentWallet) return; @@ -408,8 +415,8 @@ ApplicationWindow { var balance = "?.??"; var balanceU = "?.??"; if(!hideBalanceForced && !persistentSettings.hideBalance){ - balance = walletManager.displayAmount(currentWallet.balance(currentWallet.currentSubaddressAccount)); - balanceU = walletManager.displayAmount(currentWallet.unlockedBalance(currentWallet.currentSubaddressAccount)); + balance = walletManager.displayAmount(currentWallet.balance()); + balanceU = walletManager.displayAmount(currentWallet.unlockedBalance()); } if (persistentSettings.fiatPriceEnabled) { @@ -858,24 +865,24 @@ ApplicationWindow { if (amount !== "(all)") { var amountxmr = walletManager.amountFromString(amount); console.log("integer amount: ", amountxmr); - console.log("integer unlocked",currentWallet.unlockedBalance) + console.log("integer unlocked", currentWallet.unlockedBalance()) if (amountxmr <= 0) { hideProcessingSplash() informationPopup.title = qsTr("Error") + translationManager.emptyString; informationPopup.text = qsTr("Amount is wrong: expected number from %1 to %2") .arg(walletManager.displayAmount(0)) - .arg(walletManager.maximumAllowedAmountAsSting()) + .arg(walletManager.displayAmount(currentWallet.unlockedBalance())) + translationManager.emptyString informationPopup.icon = StandardIcon.Critical informationPopup.onCloseCallback = null informationPopup.open() return; - } else if (amountxmr > currentWallet.unlockedBalance) { + } else if (amountxmr > currentWallet.unlockedBalance()) { hideProcessingSplash() informationPopup.title = qsTr("Error") + translationManager.emptyString; informationPopup.text = qsTr("Insufficient funds. Unlocked balance: %1") - .arg(walletManager.displayAmount(currentWallet.unlockedBalance)) + .arg(walletManager.displayAmount(currentWallet.unlockedBalance())) + translationManager.emptyString informationPopup.icon = StandardIcon.Critical diff --git a/pages/Transfer.qml b/pages/Transfer.qml index 9c298773..0f6cc7c3 100644 --- a/pages/Transfer.qml +++ b/pages/Transfer.qml @@ -193,6 +193,7 @@ Rectangle { amountLine.cursorPosition = 1; } } + amountLine.error = walletManager.amountFromString(amountLine.text) > appWindow.getUnlockedBalance() } validator: RegExpValidator { @@ -444,22 +445,8 @@ Rectangle { } } - function checkInformation(amount, address, payment_id, nettype) { - address = address.trim() - payment_id = payment_id.trim() - - var amount_ok = amount.length > 0 - var address_ok = walletManager.addressValid(address, nettype) - var payment_id_ok = payment_id.length == 0 || (payment_id.length == 64 && walletManager.paymentIdValid(payment_id)) - var ipid = walletManager.paymentIdFromAddress(address, nettype) - if (ipid.length > 0 && payment_id.length > 0) - payment_id_ok = false - - addressLine.error = !address_ok - amountLine.error = !amount_ok - paymentIdLine.error = !payment_id_ok - - return amount_ok && address_ok && payment_id_ok + function checkInformation(amount, address, nettype) { + return amount.length > 0 && walletManager.amountFromString(amountLine.text) <= appWindow.getUnlockedBalance() && TxUtils.checkAddress(address, nettype) } } // pageRoot @@ -505,7 +492,7 @@ Rectangle { id: saveTxButton text: qsTr("Create tx file") + translationManager.emptyString visible: appWindow.viewOnly - enabled: pageRoot.checkInformation(amountLine.text, addressLine.text, paymentIdLine.text, appWindow.persistentSettings.nettype) + enabled: pageRoot.checkInformation(amountLine.text, addressLine.text, appWindow.persistentSettings.nettype) small: true onClicked: { console.log("Transfer: saveTx Clicked") @@ -772,7 +759,7 @@ Rectangle { } // There are sufficient unlocked funds available - if(parseFloat(amountLine.text) > parseFloat(middlePanel.unlockedBalanceText)){ + if(walletManager.amountFromString(amountLine.text) > appWindow.getUnlockedBalance()){ root.sendButtonWarning = qsTr("Amount is more than unlocked balance.") + translationManager.emptyString; return false; } @@ -782,10 +769,19 @@ Rectangle { return false; } - // The transactional information is correct - if(!pageRoot.checkInformation(amountLine.text, addressLine.text, paymentIdLine.text, appWindow.persistentSettings.nettype)){ - if(amountLine.text && addressLine.text) - root.sendButtonWarning = qsTr("Transaction information is incorrect.") + translationManager.emptyString; + if (addressLine.text == "") { + return false; + } + + // Address is valid + if(!TxUtils.checkAddress(addressLine.text, appWindow.persistentSettings.nettype)){ + root.sendButtonWarning = qsTr("Address is invalid.") + translationManager.emptyString; + return false; + } + + // Amount is nonzero + if (!amountLine.text || parseFloat(amountLine.text) <= 0) { + root.sendButtonWarning = qsTr("Enter an amount.") + translationManager.emptyString; return false; } diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp index 7b122481..02f640d6 100644 --- a/src/libwalletqt/Wallet.cpp +++ b/src/libwalletqt/Wallet.cpp @@ -289,6 +289,11 @@ bool Wallet::viewOnly() const return m_walletImpl->watchOnly(); } +quint64 Wallet::balance() const +{ + return balance(m_currentSubaddressAccount); +} + quint64 Wallet::balance(quint32 accountIndex) const { return m_walletImpl->balance(accountIndex); @@ -299,6 +304,11 @@ quint64 Wallet::balanceAll() const return m_walletImpl->balanceAll(); } +quint64 Wallet::unlockedBalance() const +{ + return unlockedBalance(m_currentSubaddressAccount); +} + quint64 Wallet::unlockedBalance(quint32 accountIndex) const { return m_walletImpl->unlockedBalance(accountIndex); diff --git a/src/libwalletqt/Wallet.h b/src/libwalletqt/Wallet.h index 30861c78..a6add1da 100644 --- a/src/libwalletqt/Wallet.h +++ b/src/libwalletqt/Wallet.h @@ -160,10 +160,12 @@ public: Q_INVOKABLE void setTrustedDaemon(bool arg); //! returns balance + Q_INVOKABLE quint64 balance() const; Q_INVOKABLE quint64 balance(quint32 accountIndex) const; Q_INVOKABLE quint64 balanceAll() const; //! returns unlocked balance + Q_INVOKABLE quint64 unlockedBalance() const; Q_INVOKABLE quint64 unlockedBalance(quint32 accountIndex) const; Q_INVOKABLE quint64 unlockedBalanceAll() const; diff --git a/src/libwalletqt/WalletManager.cpp b/src/libwalletqt/WalletManager.cpp index eab5aad2..5a892f1e 100644 --- a/src/libwalletqt/WalletManager.cpp +++ b/src/libwalletqt/WalletManager.cpp @@ -260,7 +260,7 @@ quint64 WalletManager::maximumAllowedAmount() const return Monero::Wallet::maximumAllowedAmount(); } -QString WalletManager::maximumAllowedAmountAsSting() const +QString WalletManager::maximumAllowedAmountAsString() const { return WalletManager::displayAmount(WalletManager::maximumAllowedAmount()); } diff --git a/src/libwalletqt/WalletManager.h b/src/libwalletqt/WalletManager.h index 54d2a29f..a37d0466 100644 --- a/src/libwalletqt/WalletManager.h +++ b/src/libwalletqt/WalletManager.h @@ -135,7 +135,7 @@ public: Q_INVOKABLE quint64 maximumAllowedAmount() const; // QML JS engine doesn't support unsigned integers - Q_INVOKABLE QString maximumAllowedAmountAsSting() const; + Q_INVOKABLE QString maximumAllowedAmountAsString() const; Q_INVOKABLE bool paymentIdValid(const QString &payment_id) const; Q_INVOKABLE bool addressValid(const QString &address, NetworkType::Type nettype) const;