2020-06-17 19:18:31 +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-05 20:50:22 +00:00
|
|
|
|
|
|
|
import "../../../../shared"
|
|
|
|
import "../../../../shared/status"
|
|
|
|
import "../stores"
|
2020-06-11 19:52:54 +00:00
|
|
|
|
|
|
|
ModalPopup {
|
|
|
|
id: popup
|
|
|
|
|
2020-10-30 19:22:54 +00:00
|
|
|
property bool editable: true
|
2020-06-11 19:52:54 +00:00
|
|
|
property int marginBetweenInputs: 35
|
2020-11-02 16:46:04 +00:00
|
|
|
property string validationError: ""
|
2020-06-11 19:52:54 +00:00
|
|
|
|
2020-10-30 19:22:54 +00:00
|
|
|
title: editable ?
|
|
|
|
//% "Add custom token"
|
|
|
|
qsTrId("add-custom-token")
|
|
|
|
: nameInput.text
|
|
|
|
|
|
|
|
height: editable ? 450 : 380
|
|
|
|
|
2020-06-11 19:52:54 +00:00
|
|
|
onOpened: {
|
2020-11-02 18:18:13 +00:00
|
|
|
addressInput.forceActiveFocus(Qt.MouseFocusReason)
|
|
|
|
}
|
|
|
|
|
|
|
|
function openEditable(){
|
2020-10-30 19:22:54 +00:00
|
|
|
addressInput.text = "";
|
|
|
|
nameInput.text = "";
|
|
|
|
symbolInput.text = "";
|
|
|
|
decimalsInput.text = "";
|
2020-11-02 18:18:13 +00:00
|
|
|
editable = true;
|
|
|
|
open();
|
2020-06-11 19:52:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 18:18:13 +00:00
|
|
|
function openWithData(address, name, symbol, decimals){
|
2020-10-30 19:22:54 +00:00
|
|
|
addressInput.text = address;
|
|
|
|
nameInput.text = name;
|
|
|
|
symbolInput.text = symbol;
|
|
|
|
decimalsInput.text = decimals;
|
|
|
|
editable = false;
|
2020-11-02 18:18:13 +00:00
|
|
|
open();
|
2020-10-30 19:22:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:46:04 +00:00
|
|
|
|
|
|
|
function validate() {
|
|
|
|
if (addressInput.text !== "" && !Utils.isAddress(addressInput.text)) {
|
2021-02-18 16:36:05 +00:00
|
|
|
//% "This needs to be a valid address"
|
|
|
|
validationError = qsTrId("this-needs-to-be-a-valid-address");
|
2020-11-02 16:46:04 +00:00
|
|
|
}
|
|
|
|
return validationError === ""
|
|
|
|
}
|
|
|
|
|
|
|
|
property var getTokenDetails: Backpressure.debounce(popup, 500, function (tokenAddress){
|
2021-10-05 20:50:22 +00:00
|
|
|
RootStore.customTokenList.getTokenDetails(tokenAddress)
|
2020-11-02 16:46:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function onKeyReleased(){
|
|
|
|
validationError = "";
|
|
|
|
if (!validate() || addressInput.text === "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Qt.callLater(getTokenDetails, addressInput.text)
|
|
|
|
}
|
|
|
|
|
|
|
|
Item {
|
|
|
|
Connections {
|
2021-10-05 20:50:22 +00:00
|
|
|
target: RootStore.customTokenList
|
2020-11-02 16:46:04 +00:00
|
|
|
onTokenDetailsWereResolved: {
|
|
|
|
const jsonObj = JSON.parse(tokenDetails)
|
2021-03-25 06:19:05 +00:00
|
|
|
if (jsonObj.error) {
|
|
|
|
validationError = jsonObj.error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (jsonObj.name === "" && jsonObj.symbol === "" && jsonObj.decimals === "") {
|
2021-02-18 16:36:05 +00:00
|
|
|
//% "Invalid ERC20 address"
|
|
|
|
validationError = qsTrId("invalid-erc20-address")
|
2020-11-02 16:46:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-25 06:19:05 +00:00
|
|
|
if (addressInput.text.toLowerCase() === jsonObj.address.toLowerCase()) {
|
2020-11-02 16:46:04 +00:00
|
|
|
symbolInput.text = jsonObj.symbol;
|
|
|
|
decimalsInput.text = jsonObj.decimals;
|
|
|
|
nameInput.text = jsonObj.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:52:54 +00:00
|
|
|
Input {
|
|
|
|
id: addressInput
|
2020-10-30 19:22:54 +00:00
|
|
|
readOnly: !editable
|
2020-11-02 16:46:04 +00:00
|
|
|
textField.maximumLength: 42
|
2020-07-06 20:39:55 +00:00
|
|
|
//% "Enter contract address..."
|
|
|
|
placeholderText: qsTrId("enter-contract-address...")
|
|
|
|
//% "Contract address"
|
|
|
|
label: qsTrId("contract-address")
|
2020-11-02 16:46:04 +00:00
|
|
|
validationError: popup.validationError
|
|
|
|
Keys.onReleased: onKeyReleased()
|
2020-06-11 19:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Input {
|
|
|
|
id: nameInput
|
2020-10-30 19:22:54 +00:00
|
|
|
readOnly: !editable
|
2020-06-11 19:52:54 +00:00
|
|
|
anchors.top: addressInput.bottom
|
|
|
|
anchors.topMargin: marginBetweenInputs
|
2020-07-06 20:39:55 +00:00
|
|
|
//% "The name of your token..."
|
|
|
|
placeholderText: qsTrId("the-name-of-your-token...")
|
|
|
|
//% "Name"
|
|
|
|
label: qsTrId("name")
|
2020-06-11 19:52:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 18:18:13 +00:00
|
|
|
Input {
|
|
|
|
id: symbolInput
|
|
|
|
readOnly: !editable
|
|
|
|
//% "ABC"
|
|
|
|
placeholderText: qsTrId("abc")
|
|
|
|
//% "Symbol"
|
|
|
|
label: qsTrId("symbol")
|
2020-06-11 19:52:54 +00:00
|
|
|
anchors.top: nameInput.bottom
|
|
|
|
anchors.topMargin: marginBetweenInputs
|
2020-10-30 19:22:54 +00:00
|
|
|
anchors.left: parent.left
|
2020-11-02 18:18:13 +00:00
|
|
|
anchors.right: undefined
|
|
|
|
width: parent.width / 2 - 20
|
2020-06-11 19:52:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 18:18:13 +00:00
|
|
|
Input {
|
|
|
|
id: decimalsInput
|
|
|
|
readOnly: !editable
|
|
|
|
placeholderText: "18"
|
|
|
|
//% "Decimals"
|
|
|
|
label: qsTrId("decimals")
|
|
|
|
text: "18"
|
2020-10-30 19:22:54 +00:00
|
|
|
anchors.top: nameInput.bottom
|
2020-06-11 19:52:54 +00:00
|
|
|
anchors.topMargin: marginBetweenInputs
|
2020-10-30 19:22:54 +00:00
|
|
|
anchors.right: parent.right
|
2020-11-02 18:18:13 +00:00
|
|
|
anchors.left: undefined
|
|
|
|
width: parent.width / 2 - 20
|
2020-06-11 19:52:54 +00:00
|
|
|
}
|
2020-11-02 18:18:13 +00:00
|
|
|
|
2020-06-11 19:52:54 +00:00
|
|
|
footer: Item {
|
2021-01-13 19:15:52 +00:00
|
|
|
width: parent.width
|
|
|
|
height: addBtn.height
|
2020-10-30 19:22:54 +00:00
|
|
|
visible: editable
|
2021-01-13 19:15:52 +00:00
|
|
|
|
2021-01-28 11:04:10 +00:00
|
|
|
StatusButton {
|
2020-06-11 19:52:54 +00:00
|
|
|
id: addBtn
|
|
|
|
anchors.top: parent.top
|
|
|
|
anchors.right: parent.right
|
2020-07-02 15:14:31 +00:00
|
|
|
anchors.rightMargin: Style.current.padding
|
2020-07-06 20:39:55 +00:00
|
|
|
//% "Add"
|
2021-01-28 11:04:10 +00:00
|
|
|
text: qsTrId("add")
|
2020-06-11 19:52:54 +00:00
|
|
|
|
2021-01-28 11:04:10 +00:00
|
|
|
enabled: validationError === "" && addressInput.text !== "" && nameInput.text !== "" && symbolInput.text !== "" && decimalsInput.text !== ""
|
2020-06-11 19:52:54 +00:00
|
|
|
|
|
|
|
onClicked : {
|
2021-10-05 20:50:22 +00:00
|
|
|
const error = RootStore.addCustomToken(addressInput.text, nameInput.text, symbolInput.text, decimalsInput.text);
|
2020-06-11 19:52:54 +00:00
|
|
|
|
|
|
|
if (error) {
|
2020-06-30 20:01:37 +00:00
|
|
|
errorSound.play()
|
2020-06-11 19:52:54 +00:00
|
|
|
changeError.text = error
|
|
|
|
changeError.open()
|
|
|
|
return
|
|
|
|
}
|
2020-09-08 15:52:09 +00:00
|
|
|
|
2021-10-05 20:50:22 +00:00
|
|
|
RootStore.loadCustomTokens()
|
2020-06-11 19:52:54 +00:00
|
|
|
popup.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|