2020-08-13 09:27:53 +02:00
import QtQuick 2.13
import QtQuick . Controls 2.13
import QtQuick . Layouts 1.13
import "../imports"
2020-09-17 17:04:38 +02:00
import "./status"
2020-08-13 09:27:53 +02:00
import "./"
Item {
id: root
2021-05-21 16:19:03 -04:00
width: parent . width
height: Style . current . smallPadding + prioritytext . height +
( advancedMode ? advancedModeItemGroup.height : selectorButtons . height )
2020-08-13 09:27:53 +02:00
property double slowestGasPrice: 0
property double fastestGasPrice: 100
property double stepSize: ( ( root . fastestGasPrice - root . slowestGasPrice ) / 10 ) . toFixed ( 1 )
property var getGasEthValue: function ( ) { }
property var getFiatValue: function ( ) { }
property string defaultCurrency: "USD"
property alias selectedGasPrice: inputGasPrice . text
property alias selectedGasLimit: inputGasLimit . text
2020-09-01 13:49:05 +10:00
property double selectedGasEthValue
property double selectedGasFiatValue
2020-09-14 14:12:47 +02:00
//% "Must be greater than 0"
property string greaterThan0ErrorMessage: qsTrId ( "must-be-greater-than-0" )
2020-08-20 14:45:29 +10:00
//% "This needs to be a number"
property string invalidInputErrorMessage: qsTrId ( "this-needs-to-be-a-number" )
2020-09-14 14:12:47 +02:00
//% "Please enter an amount"
property string noInputErrorMessage: qsTrId ( "please-enter-an-amount" )
2020-08-20 14:45:29 +10:00
property bool isValid: true
2020-12-14 16:50:47 +11:00
readonly property string uuid: Utils . uuid ( )
2020-08-13 09:27:53 +02:00
2021-05-21 16:19:03 -04:00
property bool advancedMode: false
2020-08-13 09:27:53 +02:00
function updateGasEthValue ( ) {
2020-08-13 18:24:51 +10:00
// causes error on application load without this null check
if ( ! inputGasPrice || ! inputGasLimit ) {
return
}
2020-08-13 09:27:53 +02:00
let ethValue = root . getGasEthValue ( inputGasPrice . text , inputGasLimit . text )
let fiatValue = root . getFiatValue ( ethValue , "ETH" , root . defaultCurrency )
2021-05-21 16:19:03 -04:00
2020-09-01 13:49:05 +10:00
selectedGasEthValue = ethValue
selectedGasFiatValue = fiatValue
2020-08-13 09:27:53 +02:00
}
2021-05-25 10:58:53 -04:00
Component.onCompleted: updateGasEthValue ( )
2021-05-21 16:19:03 -04:00
function validate ( ) {
// causes error on application load without a null check
if ( ! inputGasLimit || ! inputGasPrice ) {
return
}
inputGasLimit . validationError = ""
inputGasPrice . validationError = ""
const noInputLimit = inputGasLimit . text === ""
const noInputPrice = inputGasPrice . text === ""
if ( noInputLimit ) {
inputGasLimit . validationError = root . noInputErrorMessage
}
if ( noInputPrice ) {
inputGasPrice . validationError = root . noInputErrorMessage
}
if ( isNaN ( inputGasLimit . text ) ) {
inputGasLimit . validationError = invalidInputErrorMessage
}
if ( isNaN ( inputGasPrice . text ) ) {
inputGasPrice . validationError = invalidInputErrorMessage
}
let inputLimit = parseFloat ( inputGasLimit . text || "0.00" )
let inputPrice = parseFloat ( inputGasPrice . text || "0.00" )
2021-07-05 12:59:50 +02:00
if ( inputLimit <= 0 ) {
2021-05-21 16:19:03 -04:00
inputGasLimit . validationError = root . greaterThan0ErrorMessage
}
2021-07-05 12:59:50 +02:00
if ( inputPrice <= 0 ) {
2021-05-21 16:19:03 -04:00
inputGasPrice . validationError = root . greaterThan0ErrorMessage
}
const isValid = inputGasLimit . validationError === "" && inputGasPrice . validationError === ""
return isValid
}
2020-08-13 09:27:53 +02:00
StyledText {
2021-05-21 16:19:03 -04:00
id: prioritytext
2020-08-13 09:27:53 +02:00
anchors.top: parent . top
anchors.left: parent . left
2021-05-21 16:19:03 -04:00
text: qsTr ( "Priority" )
2020-08-13 09:27:53 +02:00
font.weight: Font . Medium
font.pixelSize: 13
color: Style . current . textColor
}
2021-05-21 16:19:03 -04:00
StatusButton {
id: buttonAdvanced
anchors.verticalCenter: prioritytext . verticalCenter
2020-08-13 09:27:53 +02:00
anchors.right: parent . right
2021-05-21 16:19:03 -04:00
text: advancedMode ? qsTr ( "Use suggestions" ) : qsTr ( "Use custom" )
flat: true
2020-08-13 09:27:53 +02:00
font.pixelSize: 13
2021-05-21 16:19:03 -04:00
onClicked: advancedMode = ! advancedMode
2020-08-13 09:27:53 +02:00
}
2021-05-21 16:19:03 -04:00
Row {
id: selectorButtons
visible: ! advancedMode
anchors.top: prioritytext . bottom
anchors.topMargin: Style . current . halfPadding
spacing: 11
2020-08-13 09:27:53 +02:00
2021-05-21 16:19:03 -04:00
ButtonGroup {
id: gasGroup
onClicked: updateGasEthValue ( )
2020-08-13 09:27:53 +02:00
}
2021-05-21 16:19:03 -04:00
GasSelectorButton {
buttonGroup: gasGroup
text: qsTr ( "Low" )
price: slowestGasPrice
gasLimit: inputGasLimit ? inputGasLimit.text : ""
getGasEthValue: root . getGasEthValue
getFiatValue: root . getFiatValue
defaultCurrency: root . defaultCurrency
onChecked: inputGasPrice . text = price
2020-08-13 09:27:53 +02:00
}
2021-05-21 16:19:03 -04:00
GasSelectorButton {
2021-05-25 10:58:53 -04:00
id: optimalGasButton
2021-05-21 16:19:03 -04:00
buttonGroup: gasGroup
checkedByDefault: true
text: qsTr ( "Optimal" )
2021-06-22 13:17:37 -04:00
price: {
const price = ( fastestGasPrice + slowestGasPrice ) / 2
// Setting the gas price field here because the binding didn't work
inputGasPrice . text = price
return price
}
2021-05-21 16:19:03 -04:00
gasLimit: inputGasLimit ? inputGasLimit.text : ""
getGasEthValue: root . getGasEthValue
getFiatValue: root . getFiatValue
defaultCurrency: root . defaultCurrency
onChecked: inputGasPrice . text = price
2020-08-13 09:27:53 +02:00
}
2021-05-21 16:19:03 -04:00
GasSelectorButton {
buttonGroup: gasGroup
text: qsTr ( "High" )
price: fastestGasPrice
gasLimit: inputGasLimit ? inputGasLimit.text : ""
getGasEthValue: root . getGasEthValue
getFiatValue: root . getFiatValue
defaultCurrency: root . defaultCurrency
onChecked: inputGasPrice . text = price
2020-08-13 09:27:53 +02:00
}
}
2021-05-21 16:19:03 -04:00
Item {
id: advancedModeItemGroup
anchors.top: prioritytext . bottom
anchors.topMargin: 14
visible: root . advancedMode
width: parent . width
height: childrenRect . height
2020-08-20 14:45:29 +10:00
2021-05-21 16:19:03 -04:00
Input {
id: inputGasLimit
label: qsTr ( "Gas amount limit" )
text: "21000"
customHeight: 56
anchors.top: parent . top
anchors.left: parent . left
anchors.right: inputGasPrice . left
anchors.rightMargin: Style . current . padding
placeholderText: "21000"
validationErrorAlignment: TextEdit . AlignRight
validationErrorTopMargin: 8
onTextChanged: {
if ( root . validate ( ) ) {
root . updateGasEthValue ( )
}
2020-08-20 14:45:29 +10:00
}
}
2020-08-13 09:27:53 +02:00
Input {
2021-05-21 16:19:03 -04:00
id: inputGasPrice
label: qsTr ( "Per-gas overall limit" )
anchors.top: parent . top
anchors.left: undefined
anchors.right: parent . right
width: 130
customHeight: 56
placeholderText: "20"
onTextChanged: {
if ( root . validate ( ) ) {
root . updateGasEthValue ( )
}
}
2020-08-13 09:27:53 +02:00
}
2021-05-21 16:19:03 -04:00
StyledText {
color: Style . current . secondaryText
2020-08-26 11:52:26 -04:00
//% "Gwei"
text: qsTrId ( "gwei" )
2020-08-13 09:27:53 +02:00
anchors.top: parent . top
anchors.topMargin: 42
2021-05-21 16:19:03 -04:00
anchors.right: inputGasPrice . right
2020-08-13 09:27:53 +02:00
anchors.rightMargin: Style . current . padding
font.pixelSize: 15
}
StyledText {
2021-05-21 16:19:03 -04:00
id: maxPriorityFeeText
text: qsTr ( "Maximum priority fee: %1 ETH" ) . arg ( selectedGasEthValue )
anchors.top: inputGasLimit . bottom
anchors.topMargin: 19
2020-08-13 09:27:53 +02:00
font.pixelSize: 13
2021-05-21 16:19:03 -04:00
}
StyledText {
id: maxPriorityFeeFiatText
text: ` $ { selectedGasFiatValue } $ { root . defaultCurrency } `
anchors.verticalCenter: maxPriorityFeeText . verticalCenter
anchors.left: maxPriorityFeeText . right
anchors.leftMargin: 6
2020-08-13 09:27:53 +02:00
color: Style . current . secondaryText
2021-05-21 16:19:03 -04:00
anchors.topMargin: 19
font.pixelSize: 13
2020-08-13 09:27:53 +02:00
}
2021-05-21 16:19:03 -04:00
StyledText {
id: maxPriorityFeeDetailsText
text: qsTr ( "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." )
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 09:27:53 +02:00
}
}
}