2020-08-13 07:27:53 +00:00
import QtQuick 2.13
import QtQuick . Controls 2.13
import QtQuick . Layouts 1.13
2021-09-28 15:04:06 +00:00
import utils 1.0
2021-10-28 20:23:30 +00:00
import shared . panels 1.0
import shared . controls 1.0
import shared . controls . chat 1.0
2021-10-21 15:07:13 +00:00
import StatusQ . Controls 0.1
2020-08-13 07:27:53 +00:00
Item {
id: root
2021-05-21 20:19:03 +00:00
width: parent . width
2022-03-18 14:47:51 +00:00
height: visible ? Style . current . smallPadding + prioritytext . height +
( advancedMode ? advancedModeItemGroup.height : selectorButtons . height ) : 0
2021-05-21 20:19:03 +00:00
2021-07-05 12:34:56 +00:00
2022-05-19 08:53:57 +00:00
property var suggestedFees: ( {
eip1559Enabled: true
} )
2021-07-05 12:34:56 +00:00
property var getGasGweiValue: function ( ) { }
2020-08-13 07:27:53 +00:00
property var getGasEthValue: function ( ) { }
property var getFiatValue: function ( ) { }
property string defaultCurrency: "USD"
property alias selectedGasPrice: inputGasPrice . text
property alias selectedGasLimit: inputGasLimit . text
2021-07-05 12:34:56 +00:00
property string defaultGasLimit: "0"
2022-03-18 14:47:51 +00:00
property string maxFiatFees: selectedGasFiatValue + root . defaultCurrency . toUpperCase ( )
2021-07-05 12:34:56 +00:00
property alias selectedTipLimit: inputPerGasTipLimit . text
property alias selectedOverallLimit: inputGasPrice . text
2020-09-01 03:49:05 +00:00
property double selectedGasEthValue
property double selectedGasFiatValue
2020-09-14 12:12:47 +00:00
//% "Must be greater than 0"
property string greaterThan0ErrorMessage: qsTrId ( "must-be-greater-than-0" )
2020-08-20 04:45:29 +00:00
//% "This needs to be a number"
property string invalidInputErrorMessage: qsTrId ( "this-needs-to-be-a-number" )
2020-09-14 12:12:47 +00:00
//% "Please enter an amount"
property string noInputErrorMessage: qsTrId ( "please-enter-an-amount" )
2020-08-20 04:45:29 +00:00
property bool isValid: true
2020-12-14 05:50:47 +00:00
readonly property string uuid: Utils . uuid ( )
2020-08-13 07:27:53 +00:00
2022-03-23 08:32:25 +00:00
property bool advancedMode: false
2021-07-05 12:34:56 +00:00
// TODO: change these values false once EIP1559 suggestions are revised
property double perGasTipLimitFloor: 1 // Matches status-react minimum-priority-fee
2022-03-23 08:32:25 +00:00
property double perGasTipLimitAverage: formatDec ( root . suggestedFees . maxPriorityFeePerGas , 2 ) // 1.5 // Matches status-react average-priority-fee
2021-07-05 12:34:56 +00:00
property bool showPriceLimitWarning : false
property bool showTipLimitWarning : false
function formatDec ( num , dec ) {
return Math . round ( ( num + Number . EPSILON ) * Math . pow ( 10 , dec ) ) / Math . pow ( 10 , dec )
}
2021-05-21 20:19:03 +00:00
2020-08-13 07:27:53 +00:00
function updateGasEthValue ( ) {
2020-08-13 08:24:51 +00:00
// causes error on application load without this null check
if ( ! inputGasPrice || ! inputGasLimit ) {
return
2022-05-19 08:53:57 +00:00
2020-08-13 08:24:51 +00:00
}
2020-08-13 07:27:53 +00:00
let ethValue = root . getGasEthValue ( inputGasPrice . text , inputGasLimit . text )
2022-05-19 08:53:57 +00:00
2020-08-13 07:27:53 +00:00
let fiatValue = root . getFiatValue ( ethValue , "ETH" , root . defaultCurrency )
2020-09-01 03:49:05 +00:00
selectedGasEthValue = ethValue
selectedGasFiatValue = fiatValue
2020-08-13 07:27:53 +00:00
}
2021-07-05 12:34:56 +00:00
function appendError ( accum , error , nonBlocking = false ) {
return accum + ` < span class = "${nonBlocking ? " non - blocking " : " "}" > $ { error } . < / s p a n > `
}
function checkLimits ( ) {
2022-05-19 08:53:57 +00:00
if ( ! root . suggestedFees . eip1559Enabled ) return ;
2021-07-05 12:34:56 +00:00
let inputTipLimit = parseFloat ( inputPerGasTipLimit . text || "0.00" )
let inputOverallLimit = parseFloat ( inputGasPrice . text || "0.00" )
let gasLimit = parseInt ( inputGasLimit . text , 10 )
errorsText . text = "" ;
showPriceLimitWarning = false
showTipLimitWarning = false
let errorMsg = "" ;
2022-02-09 09:43:23 +00:00
2021-07-05 12:34:56 +00:00
if ( gasLimit < 21000 ) {
errorMsg = appendError ( errorMsg , qsTr ( "Min 21000 units" ) )
} else if ( gasLimit < parseInt ( defaultGasLimit ) ) {
errorMsg = appendError ( errorMsg , qsTr ( "Not enough gas" ) . arg ( perGasTipLimitAverage ) , true )
}
// Per-gas tip limit rules
if ( inputTipLimit < perGasTipLimitFloor ) {
errorMsg = appendError ( errorMsg , qsTr ( "Miners will currently not process transactions with a tip below %1 Gwei, the average is %2 Gwei" ) . arg ( perGasTipLimitFloor ) . arg ( perGasTipLimitAverage ) )
showTipLimitWarning = true
} else if ( inputTipLimit < perGasTipLimitAverage ) {
errorMsg = appendError ( errorMsg , qsTr ( "The average miner tip is %1 Gwei" ) . arg ( perGasTipLimitAverage ) , true )
}
errorsText . text = ` < style type = "text/css" > span { color: "#ff0000" } span . non - blocking { color: "#FE8F59" } < / s t y l e > $ { e r r o r M s g } `
}
2022-05-19 08:53:57 +00:00
function checkOptimal ( ) {
if ( ! optimalGasButton . gasRadioBtn . checked ) {
optimalGasButton . gasRadioBtn . toggle ( )
}
2021-07-05 12:34:56 +00:00
}
2021-05-25 14:58:53 +00:00
2021-05-21 20:19:03 +00:00
function validate ( ) {
// causes error on application load without a null check
2021-07-05 12:34:56 +00:00
if ( ! inputGasLimit || ! inputGasPrice || ! inputPerGasTipLimit ) {
2021-05-21 20:19:03 +00:00
return
}
2021-07-05 12:34:56 +00:00
2021-05-21 20:19:03 +00:00
inputGasLimit . validationError = ""
inputGasPrice . validationError = ""
2021-07-05 12:34:56 +00:00
inputPerGasTipLimit . validationError = ""
2021-05-21 20:19:03 +00:00
const noInputLimit = inputGasLimit . text === ""
const noInputPrice = inputGasPrice . text === ""
2021-07-05 12:34:56 +00:00
const noPerGasTip = inputPerGasTipLimit . text === ""
2021-05-21 20:19:03 +00:00
if ( noInputLimit ) {
inputGasLimit . validationError = root . noInputErrorMessage
}
2021-07-05 12:34:56 +00:00
2021-05-21 20:19:03 +00:00
if ( noInputPrice ) {
inputGasPrice . validationError = root . noInputErrorMessage
}
2021-07-05 12:34:56 +00:00
2022-05-19 08:53:57 +00:00
if ( root . suggestedFees . eip1559Enabled && noPerGasTip ) {
2021-07-05 12:34:56 +00:00
inputPerGasTipLimit . validationError = root . noInputErrorMessage
}
2021-05-21 20:19:03 +00:00
if ( isNaN ( inputGasLimit . text ) ) {
inputGasLimit . validationError = invalidInputErrorMessage
}
if ( isNaN ( inputGasPrice . text ) ) {
inputGasPrice . validationError = invalidInputErrorMessage
}
2021-07-05 12:34:56 +00:00
2022-05-19 08:53:57 +00:00
if ( root . suggestedFees . eip1559Enabled && isNaN ( inputPerGasTipLimit . text ) ) {
2021-07-05 12:34:56 +00:00
inputPerGasTipLimit . validationError = invalidInputErrorMessage
}
2021-05-21 20:19:03 +00:00
let inputLimit = parseFloat ( inputGasLimit . text || "0.00" )
let inputPrice = parseFloat ( inputGasPrice . text || "0.00" )
2021-07-05 12:34:56 +00:00
let inputTipLimit = parseFloat ( inputPerGasTipLimit . text || "0.00" )
if ( inputLimit <= 0.00 ) {
2021-05-21 20:19:03 +00:00
inputGasLimit . validationError = root . greaterThan0ErrorMessage
}
2021-07-05 12:34:56 +00:00
if ( inputPrice <= 0.00 ) {
2021-05-21 20:19:03 +00:00
inputGasPrice . validationError = root . greaterThan0ErrorMessage
}
2022-05-19 08:53:57 +00:00
if ( root . suggestedFees . eip1559Enabled && inputTipLimit <= 0.00 ) {
2021-07-05 12:34:56 +00:00
inputPerGasTipLimit . validationError = root . greaterThan0ErrorMessage
}
2022-05-19 08:53:57 +00:00
const isInputValid = inputGasLimit . validationError === "" && inputGasPrice . validationError === "" && ( ! root . suggestedFees . eip1559Enabled || ( root . suggestedFees . eip1559Enabled && inputPerGasTipLimit . validationError === "" ) )
2021-07-05 12:34:56 +00:00
return isInputValid
2021-05-21 20:19:03 +00:00
}
2020-08-13 07:27:53 +00:00
StyledText {
2021-05-21 20:19:03 +00:00
id: prioritytext
2020-08-13 07:27:53 +00:00
anchors.top: parent . top
anchors.left: parent . left
2022-05-19 08:53:57 +00:00
text: root . suggestedFees . eip1559Enabled ? qsTr ( "Priority" ) : qsTr ( "Gas Price" )
2020-08-13 07:27:53 +00:00
font.weight: Font . Medium
font.pixelSize: 13
color: Style . current . textColor
}
2021-07-05 12:34:56 +00:00
StyledText {
id: baseFeeText
2022-05-19 08:53:57 +00:00
visible: root . suggestedFees . eip1559Enabled && advancedMode
2021-07-05 12:34:56 +00:00
anchors.top: parent . top
anchors.left: prioritytext . right
anchors.leftMargin: Style . current . smallPadding
2022-03-23 08:32:25 +00:00
text: qsTr ( "Current base fee: %1 %2" ) . arg ( root . suggestedFees . baseFee ) . arg ( "Gwei" )
2021-07-05 12:34:56 +00:00
font.weight: Font . Medium
font.pixelSize: 13
color: Style . current . secondaryText
}
2021-10-21 15:07:13 +00:00
StatusFlatButton {
2021-05-21 20:19:03 +00:00
id: buttonAdvanced
anchors.verticalCenter: prioritytext . verticalCenter
2020-08-13 07:27:53 +00:00
anchors.right: parent . right
2022-05-19 08:53:57 +00:00
visible: root . suggestedFees . eip1559Enabled
2021-07-22 15:03:59 +00:00
text: advancedMode ?
2021-07-19 21:57:57 +00:00
//% "Use suggestions"
2021-07-22 15:03:59 +00:00
qsTrId ( "use-suggestions" ) :
2021-07-19 21:57:57 +00:00
//% "Use custom"
qsTrId ( "use-custom" )
2020-08-13 07:27:53 +00:00
font.pixelSize: 13
2021-05-21 20:19:03 +00:00
onClicked: advancedMode = ! advancedMode
2020-08-13 07:27:53 +00:00
}
2021-05-21 20:19:03 +00:00
Row {
id: selectorButtons
2022-05-19 08:53:57 +00:00
visible: root . suggestedFees . eip1559Enabled && ! advancedMode
2021-05-21 20:19:03 +00:00
anchors.top: prioritytext . bottom
anchors.topMargin: Style . current . halfPadding
spacing: 11
2020-08-13 07:27:53 +00:00
2021-05-21 20:19:03 +00:00
ButtonGroup {
id: gasGroup
onClicked: updateGasEthValue ( )
2020-08-13 07:27:53 +00:00
}
2021-05-21 20:19:03 +00:00
GasSelectorButton {
2022-05-19 08:53:57 +00:00
id: lowGasButton
2021-05-21 20:19:03 +00:00
buttonGroup: gasGroup
2021-07-05 12:34:56 +00:00
text: qsTr ( "Low" )
price: {
2022-05-19 08:53:57 +00:00
if ( ! root . suggestedFees . eip1559Enabled ) return root . suggestedFees . gasPrice ;
2022-03-23 08:32:25 +00:00
return formatDec ( root . suggestedFees . maxFeePerGasL , 6 )
2021-07-05 12:34:56 +00:00
}
2021-05-21 20:19:03 +00:00
gasLimit: inputGasLimit ? inputGasLimit.text : ""
getGasEthValue: root . getGasEthValue
getFiatValue: root . getFiatValue
defaultCurrency: root . defaultCurrency
2021-07-05 12:34:56 +00:00
onChecked: {
2022-05-19 08:53:57 +00:00
if ( root . suggestedFees . eip1559Enabled ) {
2022-03-23 08:32:25 +00:00
inputPerGasTipLimit . text = formatDec ( root . suggestedFees . maxPriorityFeePerGas , 2 ) ;
inputGasPrice . text = formatDec ( root . suggestedFees . maxFeePerGasL , 2 ) ;
2021-07-05 12:34:56 +00:00
} else {
inputGasPrice . text = price
}
root . updateGasEthValue ( )
root . checkLimits ( )
}
2020-08-13 07:27:53 +00:00
}
2021-05-21 20:19:03 +00:00
GasSelectorButton {
2021-05-25 14:58:53 +00:00
id: optimalGasButton
2021-05-21 20:19:03 +00:00
buttonGroup: gasGroup
2021-07-16 20:22:50 +00:00
//% "Optimal"
text: qsTrId ( "optimal" )
2021-06-22 17:17:37 +00:00
price: {
2022-05-19 08:53:57 +00:00
if ( ! root . suggestedFees . eip1559Enabled ) {
2021-07-05 12:34:56 +00:00
// Setting the gas price field here because the binding didn't work
2022-05-19 08:53:57 +00:00
inputGasPrice . text = root . suggestedFees . gasPrice
return root . suggestedFees . gasPrice
2021-07-05 12:34:56 +00:00
}
2022-03-23 08:32:25 +00:00
return formatDec ( root . suggestedFees . maxFeePerGasM , 6 )
2021-06-22 17:17:37 +00:00
}
2021-05-21 20:19:03 +00:00
gasLimit: inputGasLimit ? inputGasLimit.text : ""
getGasEthValue: root . getGasEthValue
getFiatValue: root . getFiatValue
defaultCurrency: root . defaultCurrency
2021-07-05 12:34:56 +00:00
onChecked: {
2022-05-19 08:53:57 +00:00
if ( root . suggestedFees . eip1559Enabled ) {
2022-03-23 08:32:25 +00:00
inputPerGasTipLimit . text = formatDec ( root . suggestedFees . maxPriorityFeePerGas , 2 ) ;
inputGasPrice . text = formatDec ( root . suggestedFees . maxFeePerGasM , 2 ) ;
2021-07-05 12:34:56 +00:00
} else {
2022-05-19 08:53:57 +00:00
inputGasPrice . text = root . suggestedFees . gasPrice
2021-07-05 12:34:56 +00:00
}
root . updateGasEthValue ( )
root . checkLimits ( )
}
2020-08-13 07:27:53 +00:00
}
2021-05-21 20:19:03 +00:00
GasSelectorButton {
2022-05-19 08:53:57 +00:00
id: highGasButton
2021-05-21 20:19:03 +00:00
buttonGroup: gasGroup
2021-07-05 12:34:56 +00:00
text: qsTr ( "High" )
price: {
2022-05-19 08:53:57 +00:00
if ( ! root . suggestedFees . eip1559Enabled ) return root . suggestedFees . gasPrice ;
2022-03-23 08:32:25 +00:00
return formatDec ( root . suggestedFees . maxFeePerGasH , 6 ) ;
2021-07-05 12:34:56 +00:00
}
2021-05-21 20:19:03 +00:00
gasLimit: inputGasLimit ? inputGasLimit.text : ""
getGasEthValue: root . getGasEthValue
getFiatValue: root . getFiatValue
defaultCurrency: root . defaultCurrency
2021-07-05 12:34:56 +00:00
onChecked: {
2022-05-19 08:53:57 +00:00
if ( root . suggestedFees . eip1559Enabled ) {
2022-03-23 08:32:25 +00:00
inputPerGasTipLimit . text = formatDec ( root . suggestedFees . maxPriorityFeePerGas , 2 ) ;
inputGasPrice . text = formatDec ( root . suggestedFees . maxFeePerGasH , 2 ) ;
2021-07-05 12:34:56 +00:00
} else {
inputGasPrice . text = price
}
root . updateGasEthValue ( )
root . checkLimits ( )
}
2020-08-13 07:27:53 +00:00
}
}
2021-05-21 20:19:03 +00:00
Item {
id: advancedModeItemGroup
anchors.top: prioritytext . bottom
anchors.topMargin: 14
2022-05-19 08:53:57 +00:00
visible: ! root . suggestedFees . eip1559Enabled || root . advancedMode
2021-05-21 20:19:03 +00:00
width: parent . width
height: childrenRect . height
2020-08-20 04:45:29 +00:00
2021-05-21 20:19:03 +00:00
Input {
id: inputGasLimit
2021-07-16 20:22:50 +00:00
//% "Gas amount limit"
label: qsTrId ( "gas-amount-limit" )
2021-05-21 20:19:03 +00:00
text: "21000"
2021-07-05 12:34:56 +00:00
inputLabel.color: Style . current . secondaryText
2021-05-21 20:19:03 +00:00
customHeight: 56
anchors.top: parent . top
anchors.left: parent . left
2022-05-19 08:53:57 +00:00
anchors.right: root . suggestedFees . eip1559Enabled ? inputPerGasTipLimit.left : inputGasPrice . left
2021-05-21 20:19:03 +00:00
anchors.rightMargin: Style . current . padding
placeholderText: "21000"
2021-07-27 08:59:28 +00:00
validator: IntValidator {
bottom: 1
}
2021-05-21 20:19:03 +00:00
validationErrorAlignment: TextEdit . AlignRight
validationErrorTopMargin: 8
onTextChanged: {
if ( root . validate ( ) ) {
root . updateGasEthValue ( )
2021-07-05 12:34:56 +00:00
root . checkLimits ( )
}
}
}
Input {
id: inputPerGasTipLimit
label: qsTr ( "Per-gas tip limit" )
inputLabel.color: Style . current . secondaryText
anchors.top: parent . top
anchors.right: inputGasPrice . left
anchors.rightMargin: Style . current . padding
anchors.left: undefined
2022-05-19 08:53:57 +00:00
visible: root . suggestedFees . eip1559Enabled
2021-07-05 12:34:56 +00:00
width: 125
customHeight: 56
2022-03-23 08:32:25 +00:00
text: formatDec ( root . suggestedFees . maxPriorityFeePerGas , 2 ) ;
2021-07-05 12:34:56 +00:00
placeholderText: "20"
onTextChanged: {
if ( root . validate ( ) ) {
root . updateGasEthValue ( )
root . checkLimits ( )
2021-05-21 20:19:03 +00:00
}
2020-08-20 04:45:29 +00:00
}
}
2020-08-13 07:27:53 +00:00
2021-07-05 12:34:56 +00:00
StyledText {
color: Style . current . secondaryText
//% "Gwei"
text: qsTrId ( "gwei" )
2022-05-19 08:53:57 +00:00
visible: root . suggestedFees . eip1559Enabled
2021-07-05 12:34:56 +00:00
anchors.top: parent . top
anchors.topMargin: 42
anchors.right: inputPerGasTipLimit . right
anchors.rightMargin: Style . current . padding
font.pixelSize: 15
}
2020-08-13 07:27:53 +00:00
Input {
2021-05-21 20:19:03 +00:00
id: inputGasPrice
2021-07-16 20:22:50 +00:00
//% "Per-gas overall limit"
label: qsTrId ( "per-gas-overall-limit" )
2021-07-05 12:34:56 +00:00
inputLabel.color: Style . current . secondaryText
2021-05-21 20:19:03 +00:00
anchors.top: parent . top
anchors.left: undefined
anchors.right: parent . right
2021-07-05 12:34:56 +00:00
width: 125
2021-05-21 20:19:03 +00:00
customHeight: 56
placeholderText: "20"
onTextChanged: {
if ( root . validate ( ) ) {
root . updateGasEthValue ( )
2021-07-05 12:34:56 +00:00
root . checkLimits ( )
2021-05-21 20:19:03 +00:00
}
}
2020-08-13 07:27:53 +00:00
}
2021-05-21 20:19:03 +00:00
StyledText {
color: Style . current . secondaryText
2020-08-26 15:52:26 +00:00
//% "Gwei"
text: qsTrId ( "gwei" )
2020-08-13 07:27:53 +00:00
anchors.top: parent . top
anchors.topMargin: 42
2021-05-21 20:19:03 +00:00
anchors.right: inputGasPrice . right
2020-08-13 07:27:53 +00:00
anchors.rightMargin: Style . current . padding
font.pixelSize: 15
}
2021-07-05 12:34:56 +00:00
StyledText {
id: errorsText
text: ""
width: parent . width - Style . current . padding
visible: text != ""
height: visible ? undefined : 0
anchors.top: inputGasLimit . bottom
anchors.topMargin: Style . current . smallPadding + 5
font.pixelSize: 13
textFormat: Text . RichText
color: Style . current . secondaryText
wrapMode: Text . WordWrap
}
2020-08-13 07:27:53 +00:00
StyledText {
2021-05-21 20:19:03 +00:00
id: maxPriorityFeeText
2021-07-05 12:34:56 +00:00
anchors.left: parent . left
2022-05-19 08:53:57 +00:00
visible: root . suggestedFees . eip1559Enabled
2021-07-16 20:22:50 +00:00
//% "Maximum priority fee: %1 ETH"
2021-07-05 12:34:56 +00:00
text: {
2022-02-09 09:43:23 +00:00
let v = selectedGasEthValue > 0.00009 ? selectedGasEthValue :
2021-07-05 12:34:56 +00:00
( selectedGasEthValue < 0.000001 ? "0.000000..." : selectedGasEthValue . toFixed ( 6 ) )
return qsTrId ( "maximum-priority-fee---1-eth" ) . arg ( v )
}
anchors.top: errorsText . bottom
anchors.topMargin: Style . current . smallPadding + 5
2020-08-13 07:27:53 +00:00
font.pixelSize: 13
2021-07-05 12:34:56 +00:00
color: Style . current . textColor
2021-05-21 20:19:03 +00:00
}
StyledText {
id: maxPriorityFeeFiatText
2022-03-18 14:47:51 +00:00
text: root . maxFiatFees
2022-05-19 08:53:57 +00:00
visible: root . suggestedFees . eip1559Enabled
2021-05-21 20:19:03 +00:00
anchors.verticalCenter: maxPriorityFeeText . verticalCenter
anchors.left: maxPriorityFeeText . right
anchors.leftMargin: 6
2020-08-13 07:27:53 +00:00
color: Style . current . secondaryText
2021-05-21 20:19:03 +00:00
anchors.topMargin: 19
font.pixelSize: 13
2020-08-13 07:27:53 +00:00
}
2021-05-21 20:19:03 +00:00
StyledText {
id: maxPriorityFeeDetailsText
2021-07-16 20:22:50 +00:00
//% "Maximum overall price for the transaction. If the block base fee exceeds this, it will be included in a following block with a lower base fee."
text: qsTrId ( "maximum-overall-price-for-the-transaction--if-the-block-base-fee-exceeds-this--it-will-be-included-in-a-following-block-with-a-lower-base-fee-" )
2022-05-19 08:53:57 +00:00
visible: root . suggestedFees . eip1559Enabled
2021-05-21 20:19:03 +00:00
width: parent . width
anchors.top: maxPriorityFeeText . bottom
anchors.topMargin: Style . current . smallPadding
font.pixelSize: 13
color: Style . current . secondaryText
wrapMode: Text . WordWrap
2020-08-13 07:27:53 +00:00
}
}
}