status-desktop/ui/imports/shared/status/StatusSNTTransactionModal.qml

278 lines
10 KiB
QML
Raw Normal View History

import QtQuick 2.13
import QtQuick.Controls 2.13
import QtQuick.Layouts 1.13
import QtQuick.Dialogs 1.3
import utils 1.0
import StatusQ.Controls 0.1
import shared.views 1.0
import shared.popups 1.0
import shared.stores 1.0
import shared.controls 1.0
// TODO: replace with StatusModal
ModalPopup {
id: root
property var store
property var stickersStore
property var contactsStore
readonly property var asset: JSON.parse(root.stickersStore.getStatusToken())
property string assetPrice
property string contractAddress
property var estimateGasFunction: (function(userAddress, uuid) { return 0; })
2021-07-05 12:34:56 +00:00
property var onSendTransaction: (function(userAddress, gasLimit, gasPrice, tipLimit, overallLimit, password){ return ""; })
property var onSuccess: (function(){})
property var asyncGasEstimateTarget
Component.onCompleted: {
root.stickersStore.fetchGasPrice();
gasSelector.estimateGas();
}
2021-07-05 12:34:56 +00:00
height: 540
2020-09-18 06:33:20 +00:00
//% "Authorize %1 %2"
title: qsTrId("authorize--1--2").arg(Utils.stripTrailingZeros(assetPrice)).arg(asset.symbol)
property MessageDialog sendingError: MessageDialog {
id: sendingError
2020-09-18 06:33:20 +00:00
//% "Error sending the transaction"
title: qsTrId("error-sending-the-transaction")
icon: StandardIcon.Critical
standardButtons: StandardButton.Ok
}
function setAsyncGasLimitResult(uuid, value) {
if (uuid === gasSelector.uuid) {
gasSelector.selectedGasLimit = value
2021-07-05 12:34:56 +00:00
gasSelector.defaultGasLimit = value
gasSelector.updateGasEthValue();
}
}
function sendTransaction() {
let responseStr = onSendTransaction(selectFromAccount.selectedAccount.address,
gasSelector.selectedGasLimit,
2022-02-28 12:30:36 +00:00
gasSelector.isEIP1559Enabled ? "" : gasSelector.selectedGasPrice,
2021-07-05 12:34:56 +00:00
gasSelector.selectedTipLimit,
gasSelector.selectedOverallLimit,
transactionSigner.enteredPassword);
let response = JSON.parse(responseStr)
if (!response.success) {
fix: prevent crash on generate account wrong password Fixes #2448. Currently, if a wrong password is entered when generating a wallet account, the app will crash due to attempting to decode a `GeneratedAccount ` from an rpc response containing only an error. With this PR, we are detecting if an error is returned in the response, and if so, raising a StatusGoException. This exception is caught in the call chain, and translated in to a `StatusGoError` which is serialised and sent to the QML view, where it is parsed and displayed as an invalid password error in the input box. refactor: remove string return values as error messages in wallet model In the wallet model, we were passing back empty strings for no error, or an error as a string. This is not only confusing, but does not benefit from leaning on the compiler and strong types. One has to read the entire code to understand if a string result is returned when there is no error instead of implicitly being able to understand there is no return type. To alleviate this, account creation fundtions that do not need to return a value have been changed to a void return type, and raise `StatusGoException` if there is an error encountered. This can be caught in the call chain and used as necessary (ie to pass to QML). refactor: move invalid password string detection to Utils Currently, we are reading returned view model values and checking to see if they include a known string from Status Go that means there was an invalid password used. This string was placed in the codebased in mulitple locations. This PR moves the string check to a Utils function and updates all the references to use the function in Utils.
2021-05-13 04:41:48 +00:00
if (Utils.isInvalidPasswordMessage(response.result)){
2020-09-18 06:33:20 +00:00
//% "Wrong password"
transactionSigner.validationError = qsTrId("wrong-password")
return
}
sendingError.text = response.result
return sendingError.open()
}
onSuccess();
root.close();
}
TransactionStackView {
id: stack
height: parent.height
anchors.fill: parent
anchors.leftMargin: Style.current.padding
anchors.rightMargin: Style.current.padding
initialItem: group1
2021-11-19 09:42:41 +00:00
isLastGroup: stack.currentGroup === group3
onGroupActivated: {
root.title = group.headerText
2020-09-17 09:08:31 +00:00
btnNext.text = group.footerText
}
TransactionFormGroup {
id: group1
2020-09-18 06:33:20 +00:00
//% "Authorize %1 %2"
headerText: qsTrId("authorize--1--2").arg(Utils.stripTrailingZeros(root.assetPrice)).arg(root.asset.symbol)
2020-09-18 06:33:20 +00:00
//% "Continue"
footerText: qsTrId("continue")
showBackBtn: false
StatusAccountSelector {
id: selectFromAccount
2021-11-19 09:42:41 +00:00
accounts: walletSectionAccounts.model
selectedAccount: {
2021-11-19 09:42:41 +00:00
const currAcc = walletSectionCurrent
if (currAcc.walletType !== Constants.watchWalletType) {
return currAcc
}
return null
}
2021-11-19 09:42:41 +00:00
currency: walletSection.currentCurrency
width: stack.width
2020-09-18 06:33:20 +00:00
//% "Choose account"
label: qsTrId("choose-account")
showBalanceForAssetSymbol: root.asset.symbol
minRequiredAssetBalance: root.assetPrice
onSelectedAccountChanged: if (isValid) { gasSelector.estimateGas() }
}
RecipientSelector {
id: selectRecipient
visible: false
accounts: root.stickersStore.walletAccounts
contactsStore: root.contactsStore
selectedRecipient: { "address": contractAddress, "type": RecipientSelector.Type.Address }
readOnly: true
onSelectedRecipientChanged: if (isValid) { gasSelector.estimateGas() }
}
Connections {
target: asyncGasEstimateTarget
onGasEstimateReturned: {
root.setAsyncGasLimitResult(uuid, estimate)
}
}
GasSelector {
id: gasSelector
anchors.top: selectFromAccount.bottom
anchors.topMargin: Style.current.padding
gasPrice: parseFloat(root.stickersStore.gasPrice)
getGasEthValue: root.stickersStore.getGasEthValue
getFiatValue: root.stickersStore.getFiatValue
defaultCurrency: root.stickersStore.getCurrentCurrency()
2022-02-28 12:30:36 +00:00
isEIP1559Enabled: root.store.isEIP1559Enabled()
suggestedFees: root.store.suggestedFees()
width: stack.width
2022-02-09 09:43:23 +00:00
property var estimateGas: Backpressure.debounce(gasSelector, 600, function() {
2021-08-09 22:23:52 +00:00
let estimatedGas = root.estimateGasFunction(selectFromAccount.selectedAccount, uuid);
if (estimatedGas !== undefined) {
gasSelector.selectedGasLimit = estimatedGas
}
2021-08-09 22:23:52 +00:00
return estimatedGas;
})
}
GasValidator {
id: gasValidator
anchors.top: gasSelector.bottom
selectedAccount: selectFromAccount.selectedAccount
selectedAsset: root.asset
selectedAmount: parseFloat(root.assetPrice)
selectedGasEthValue: gasSelector.selectedGasEthValue
}
}
TransactionFormGroup {
2021-11-19 09:42:41 +00:00
id: group2
2020-09-18 06:33:20 +00:00
//% "Authorize %1 %2"
headerText: qsTrId("authorize--1--2").arg(Utils.stripTrailingZeros(root.assetPrice)).arg(root.asset.symbol)
2020-09-18 06:33:20 +00:00
//% "Sign with password"
footerText: qsTrId("sign-with-password")
TransactionPreview {
id: pvwTransaction
width: stack.width
fromAccount: selectFromAccount.selectedAccount
gas: {
"value": gasSelector.selectedGasEthValue,
"symbol": "ETH",
"fiatValue": gasSelector.selectedGasFiatValue
}
toAccount: selectRecipient.selectedRecipient
asset: root.asset
amount: {
const fiatValue = root.stickersStore.getFiatValue(root.assetPrice || 0, root.asset.symbol, currency)
return { "value": root.assetPrice, "fiatValue": fiatValue }
}
currency: root.stickersStore.getCurrentCurrency()
}
}
TransactionFormGroup {
2021-11-19 09:42:41 +00:00
id: group3
2020-09-18 06:33:20 +00:00
//% "Send %1 %2"
headerText: qsTrId("send--1--2").arg(Utils.stripTrailingZeros(root.assetPrice)).arg(root.asset.symbol)
2020-09-18 06:33:20 +00:00
//% "Sign with password"
footerText: qsTrId("sign-with-password")
TransactionSigner {
id: transactionSigner
width: stack.width
signingPhrase: root.stickersStore.getSigningPhrase()
}
}
}
footer: Item {
width: parent.width
height: btnNext.height
StatusRoundButton {
id: btnBack
anchors.left: parent.left
icon.name: "arrow-right"
icon.width: 20
icon.height: 16
icon.rotation: 180
visible: stack.currentGroup.showBackBtn
2021-11-19 09:42:41 +00:00
enabled: {
stack.currentGroup.isValid || stack.isLastGroup
}
onClicked: {
if (typeof stack.currentGroup.onBackClicked === "function") {
return stack.currentGroup.onBackClicked()
}
stack.back()
}
}
2021-07-05 12:34:56 +00:00
Component {
id: transactionSettingsConfirmationPopupComponent
TransactionSettingsConfirmationPopup {
}
}
StatusButton {
id: btnNext
anchors.right: parent.right
2021-02-18 16:36:05 +00:00
//% "Next"
text: qsTrId("next")
2020-09-17 09:08:31 +00:00
enabled: stack.currentGroup.isValid && !stack.currentGroup.isPending
loading: stack.currentGroup.isPending
onClicked: {
2021-11-19 09:42:41 +00:00
const validity = stack.currentGroup.validate()
2022-02-09 09:43:23 +00:00
if (validity.isValid && !validity.isPending) {
if (stack.isLastGroup) {
return root.sendTransaction()
}
2021-07-05 12:34:56 +00:00
2022-02-28 12:30:36 +00:00
if(gasSelector.isEIP1559Enabled && stack.currentGroup === group2 && gasSelector.advancedMode){
2021-07-05 12:34:56 +00:00
if(gasSelector.showPriceLimitWarning || gasSelector.showTipLimitWarning){
Global.openPopup(transactionSettingsConfirmationPopupComponent, {
currentBaseFee: gasSelector.suggestedFees.baseFee,
2021-07-05 12:34:56 +00:00
currentMinimumTip: gasSelector.perGasTipLimitFloor,
currentAverageTip: gasSelector.perGasTipLimitAverage,
tipLimit: gasSelector.selectedTipLimit,
suggestedTipLimit: gasSelector.perGasTipLimitFloor, // TODO:
priceLimit: gasSelector.selectedOverallLimit,
suggestedPriceLimit: gasSelector.suggestedFees.baseFee + gasSelector.perGasTipLimitFloor,
2021-07-05 12:34:56 +00:00
showPriceLimitWarning: gasSelector.showPriceLimitWarning,
showTipLimitWarning: gasSelector.showTipLimitWarning,
onConfirm: function(){
stack.next();
}
})
return
}
}
stack.next()
}
}
}
}
}
/*##^##
Designer {
D{i:0;autoSize:true;height:480;width:640}
}
##^##*/