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

268 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 int chainId
property var estimateGasFunction: (function(userAddress, uuid) { return 0; })
property var onSendTransaction: (function(userAddress, gasLimit, gasPrice, tipLimit, overallLimit, password, eip1559Enabled){ return ""; })
property var onSuccess: (function(){})
property var asyncGasEstimateTarget
Component.onCompleted: {
gasSelector.estimateGas();
}
2021-07-05 12:34:56 +00:00
height: 540
title: qsTr("Authorize %1 %2").arg(Utils.stripTrailingZeros(assetPrice)).arg(asset.symbol)
property MessageDialog sendingError: MessageDialog {
id: sendingError
title: qsTr("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,
gasSelector.suggestedFees.eip1559Enabled ? "" : gasSelector.selectedGasPrice,
2021-07-05 12:34:56 +00:00
gasSelector.selectedTipLimit,
gasSelector.selectedOverallLimit,
transactionSigner.enteredPassword,
gasSelector.suggestedFees.eip1559Enabled);
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)){
transactionSigner.validationError = qsTr("Wrong password")
return
}
sendingError.text = response.result
return sendingError.open()
}
onSuccess();
root.close();
}
onOpened: {
gasSelector.suggestedFees = root.store.suggestedFees(root.chainId)
gasSelector.checkOptimal()
}
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
headerText: qsTr("Authorize %1 %2").arg(Utils.stripTrailingZeros(root.assetPrice)).arg(root.asset.symbol)
footerText: qsTr("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
label: qsTr("Choose account")
showBalanceForAssetSymbol: root.asset.symbol
minRequiredAssetBalance: root.assetPrice
chainId: root.chainId
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
getGasEthValue: root.stickersStore.getGasEthValue
getFiatValue: root.stickersStore.getFiatValue
defaultCurrency: root.stickersStore.getCurrentCurrency()
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
anchors.horizontalCenter: parent.horizontalCenter
selectedAccount: selectFromAccount.selectedAccount
selectedAsset: root.asset
selectedAmount: parseFloat(root.assetPrice)
selectedGasEthValue: gasSelector.selectedGasEthValue
}
}
TransactionFormGroup {
2021-11-19 09:42:41 +00:00
id: group2
headerText: qsTr("Authorize %1 %2").arg(Utils.stripTrailingZeros(root.assetPrice)).arg(root.asset.symbol)
footerText: qsTr("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
headerText: qsTr("Send %1 %2").arg(Utils.stripTrailingZeros(root.assetPrice)).arg(root.asset.symbol)
footerText: qsTr("Sign with password")
TransactionSigner {
id: transactionSigner
width: stack.width
signingPhrase: root.stickersStore.getSigningPhrase()
}
}
}
footer: Item {
width: parent.width
height: btnNext.height
StatusBackButton {
id: btnBack
anchors.left: parent.left
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
text: qsTr("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
if(gasSelector.suggestedFees.eip1559Enabled && 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}
}
##^##*/