fix(qml): fix etherscan links for all networks

Also refactor code for reusability and readability.

Closes: #13986
This commit is contained in:
Stefan 2024-03-15 18:43:12 +02:00 committed by Stefan Dunca
parent f12c6dfa40
commit 0958860bc9
8 changed files with 49 additions and 38 deletions

View File

@ -51,11 +51,11 @@ StatusMenu {
} }
StatusAction { StatusAction {
text: Utils.getActionNameForDisplayingAddressOnNetwork(Constants.networkShortChainNames.arbiscan) text: Utils.getActionNameForDisplayingAddressOnNetwork(Constants.networkShortChainNames.arbitrum)
enabled: root.preferredSharedNetworkNamesArray.includes(Constants.networkShortChainNames.arbiscan) enabled: root.preferredSharedNetworkNamesArray.includes(Constants.networkShortChainNames.arbitrum)
icon.name: "link" icon.name: "link"
onTriggered: { onTriggered: {
let link = Utils.getUrlForAddressOnNetwork(Constants.networkShortChainNames.arbiscan, root.areTestNetworksEnabled, root.isGoerliEnabled, root.selectedAccount.address?? "") let link = Utils.getUrlForAddressOnNetwork(Constants.networkShortChainNames.arbitrum, root.areTestNetworksEnabled, root.isGoerliEnabled, root.selectedAccount.address?? "")
Global.openLink(link) Global.openLink(link)
} }
} }

View File

@ -241,11 +241,11 @@ StatusListItem {
} }
StatusAction { StatusAction {
text: Utils.getActionNameForDisplayingAddressOnNetwork(Constants.networkShortChainNames.arbiscan) text: Utils.getActionNameForDisplayingAddressOnNetwork(Constants.networkShortChainNames.arbitrum)
enabled: d.preferredSharedNetworkNamesArray.includes(Constants.networkShortChainNames.arbiscan) enabled: d.preferredSharedNetworkNamesArray.includes(Constants.networkShortChainNames.arbitrum)
icon.name: "link" icon.name: "link"
onTriggered: { onTriggered: {
let link = Utils.getUrlForAddressOnNetwork(Constants.networkShortChainNames.arbiscan, root.areTestNetworksEnabled, root.isGoerliEnabled, d.visibleAddress ? d.visibleAddress : root.ens) let link = Utils.getUrlForAddressOnNetwork(Constants.networkShortChainNames.arbitrum, root.areTestNetworksEnabled, root.isGoerliEnabled, d.visibleAddress ? d.visibleAddress : root.ens)
Global.openLink(link) Global.openLink(link)
} }
} }

View File

@ -107,9 +107,9 @@ ColumnLayout {
let link = Constants.networkExplorerLinks.etherscan let link = Constants.networkExplorerLinks.etherscan
if (areTestNetworksEnabled) { if (areTestNetworksEnabled) {
if (root.isGoerliEnabled) { if (root.isGoerliEnabled) {
link = Constants.networkExplorerLinks.sepoliaEtherscan
} else {
link = Constants.networkExplorerLinks.goerliEtherscan link = Constants.networkExplorerLinks.goerliEtherscan
} else {
link = Constants.networkExplorerLinks.sepoliaEtherscan
} }
} }
Global.openLink("%1/nft/%2/%3".arg(link).arg(root.tokenAddress).arg(root.tokenId)) Global.openLink("%1/nft/%2/%3".arg(link).arg(root.tokenAddress).arg(root.tokenId))

View File

@ -50,6 +50,7 @@ StatusMenu {
id: d id: d
property string selectedAddress: "" property string selectedAddress: ""
property string cleanSelectedAddress: d.selectedAddress.split(":").pop()
property string addressName: "" property string addressName: ""
property string addressEns: "" property string addressEns: ""
@ -60,6 +61,10 @@ StatusMenu {
property int addressType: TransactionAddressMenu.AddressType.Address property int addressType: TransactionAddressMenu.AddressType.Address
readonly property QtObject exp: Constants.networkExplorerLinks
readonly property QtObject chains: Constants.networkShortChainNames
readonly property bool isAddress: d.addressType !== TransactionAddressMenu.Tx
function getViewText(target) { function getViewText(target) {
switch(d.addressType) { switch(d.addressType) {
case TransactionAddressMenu.AddressType.Contract: case TransactionAddressMenu.AddressType.Contract:
@ -82,10 +87,10 @@ StatusMenu {
function refreshShowOnActionsVisiblity(shortChainNameList) { function refreshShowOnActionsVisiblity(shortChainNameList) {
for (let i = 0 ; i < shortChainNameList.length ; i++) { for (let i = 0 ; i < shortChainNameList.length ; i++) {
switch(shortChainNameList[i].toLowerCase()) { switch(shortChainNameList[i].toLowerCase()) {
case Constants.networkShortChainNames.arbiscan.toLowerCase(): case d.chains.arbitrum:
showOnArbiscanAction.enabled = true showOnArbiscanAction.enabled = true
break break
case Constants.networkShortChainNames.optimism.toLowerCase(): case d.chains.optimism:
showOnOptimismAction.enabled = true showOnOptimismAction.enabled = true
break break
default: default:
@ -152,8 +157,8 @@ StatusMenu {
} }
showOnEtherscanAction.enabled = true showOnEtherscanAction.enabled = true
showOnArbiscanAction.enabled = address.includes(Constants.networkShortChainNames.arbiscan + ":") showOnArbiscanAction.enabled = address.includes(d.chains.arbitrum + ":")
showOnOptimismAction.enabled = address.includes(Constants.networkShortChainNames.optimism + ":") showOnOptimismAction.enabled = address.includes(d.chains.optimism + ":")
d.refreshShowOnActionsVisiblity(chainShortNameList) d.refreshShowOnActionsVisiblity(chainShortNameList)
saveAddressAction.enabled = d.addressName.length === 0 saveAddressAction.enabled = d.addressName.length === 0
editAddressAction.enabled = !isWalletAccount && !isContact && d.addressName.length > 0 editAddressAction.enabled = !isWalletAccount && !isContact && d.addressName.length > 0
@ -210,16 +215,9 @@ StatusMenu {
text: d.getViewText(qsTr("Etherscan")) text: d.getViewText(qsTr("Etherscan"))
icon.name: "link" icon.name: "link"
onTriggered: { onTriggered: {
const type = d.addressType === TransactionAddressMenu.Tx ? Constants.networkExplorerLinks.txPath : Constants.networkExplorerLinks.addressPath let url = Utils.getEtherscanUrl(d.chains.mainnet, root.areTestNetworksEnabled, !root.isGoerliEnabled,
let link = Constants.networkExplorerLinks.etherscan d.cleanSelectedAddress, d.isAddress)
if (areTestNetworksEnabled) { Global.openLink(url)
if (root.isGoerliEnabled) {
link = Constants.networkExplorerLinks.sepoliaEtherscan
} else {
link = Constants.networkExplorerLinks.goerliEtherscan
}
}
Global.openLink("%1/%2/%3".arg(link).arg(type).arg(d.selectedAddress))
} }
} }
StatusAction { StatusAction {
@ -228,9 +226,9 @@ StatusMenu {
text: d.getViewText(qsTr("Arbiscan")) text: d.getViewText(qsTr("Arbiscan"))
icon.name: "link" icon.name: "link"
onTriggered: { onTriggered: {
const type = d.addressType === TransactionAddressMenu.Tx ? Constants.networkExplorerLinks.txPath : Constants.networkExplorerLinks.addressPath let url = Utils.getEtherscanUrl(d.chains.arbitrum, root.areTestNetworksEnabled, !root.isGoerliEnabled,
const link = areTestNetworksEnabled ? Constants.networkExplorerLinks.goerliArbiscan : Constants.networkExplorerLinks.arbiscan d.cleanSelectedAddress, d.isAddress)
Global.openLink("%1/%2/%3".arg(link).arg(type).arg(d.selectedAddress)) Global.openLink(url)
} }
} }
StatusAction { StatusAction {
@ -239,9 +237,9 @@ StatusMenu {
text: d.getViewText(qsTr("Optimism Explorer")) text: d.getViewText(qsTr("Optimism Explorer"))
icon.name: "link" icon.name: "link"
onTriggered: { onTriggered: {
const type = d.addressType === TransactionAddressMenu.Tx ? Constants.networkExplorerLinks.txPath : Constants.networkExplorerLinks.addressPath let url = Utils.getEtherscanUrl(d.chains.optimism, root.areTestNetworksEnabled, !root.isGoerliEnabled,
const link = areTestNetworksEnabled ? Constants.networkExplorerLinks.goerliOptimism : Constants.networkExplorerLinks.optimism d.cleanSelectedAddress, d.isAddress)
Global.openLink("%1/%2/%3".arg(link).arg(type).arg(d.selectedAddress)) Global.openLink(url)
} }
} }
StatusSuccessAction { StatusSuccessAction {

View File

@ -492,16 +492,16 @@ QtObject {
let link = Constants.networkExplorerLinks.etherscan let link = Constants.networkExplorerLinks.etherscan
if (root.areTestNetworksEnabled) { if (root.areTestNetworksEnabled) {
if (root.isGoerliEnabled) { if (root.isGoerliEnabled) {
link = Constants.networkExplorerLinks.sepoliaEtherscan
} else {
link = Constants.networkExplorerLinks.goerliEtherscan link = Constants.networkExplorerLinks.goerliEtherscan
} else {
link = Constants.networkExplorerLinks.sepoliaEtherscan
} }
} }
return link return link
} }
function getExplorerNameForNetwork(networkShortName) { function getExplorerNameForNetwork(networkShortName) {
if (networkShortName === Constants.networkShortChainNames.arbiscan) { if (networkShortName === Constants.networkShortChainNames.arbitrum) {
return qsTr("Arbiscan Explorer") return qsTr("Arbiscan Explorer")
} }
if (networkShortName === Constants.networkShortChainNames.optimism) { if (networkShortName === Constants.networkShortChainNames.optimism) {

View File

@ -299,7 +299,7 @@ Item {
width: parent.width width: parent.width
title: d.transactionType === Constants.TransactionType.Swap || d.transactionType === Constants.TransactionType.Bridge ? title: d.transactionType === Constants.TransactionType.Swap || d.transactionType === Constants.TransactionType.Bridge ?
qsTr("In") : qsTr("From") qsTr("In") : qsTr("From")
addresses: d.isTransactionValid && d.reEvaluateSender? [d.transaction.sender] : [] addresses: d.isTransactionValid && d.reEvaluateSender? [d.transaction.from] : []
contactsStore: root.contactsStore contactsStore: root.contactsStore
rootStore: WalletStores.RootStore rootStore: WalletStores.RootStore
onButtonClicked: { onButtonClicked: {
@ -342,7 +342,7 @@ Item {
TransactionAddressTile { TransactionAddressTile {
width: parent.width width: parent.width
title: qsTr("To") title: qsTr("To")
addresses: d.isTransactionValid && visible && d.reEvaluateRecipient? [d.transaction.recipient] : [] addresses: d.isTransactionValid && visible && d.reEvaluateRecipient? [d.transaction.to] : []
contactsStore: root.contactsStore contactsStore: root.contactsStore
rootStore: WalletStores.RootStore rootStore: WalletStores.RootStore
onButtonClicked: addressMenu.openReceiverMenu(this, addresses[0], [d.networkShortName]) onButtonClicked: addressMenu.openReceiverMenu(this, addresses[0], [d.networkShortName])

View File

@ -895,7 +895,7 @@ QtObject {
readonly property QtObject networkShortChainNames: QtObject { readonly property QtObject networkShortChainNames: QtObject {
readonly property string mainnet: "eth" readonly property string mainnet: "eth"
readonly property string arbiscan: "arb" readonly property string arbitrum: "arb"
readonly property string optimism: "opt" readonly property string optimism: "opt"
} }

View File

@ -911,7 +911,7 @@ QtObject {
} }
function getActionNameForDisplayingAddressOnNetwork(networkShortName) { function getActionNameForDisplayingAddressOnNetwork(networkShortName) {
if (networkShortName === Constants.networkShortChainNames.arbiscan) { if (networkShortName === Constants.networkShortChainNames.arbitrum) {
return qsTr("View on Arbiscan") return qsTr("View on Arbiscan")
} }
if (networkShortName === Constants.networkShortChainNames.optimism) { if (networkShortName === Constants.networkShortChainNames.optimism) {
@ -921,7 +921,10 @@ QtObject {
return qsTr("View on Etherscan") return qsTr("View on Etherscan")
} }
function getUrlForAddressOnNetwork(networkShortName, testnetMode, sepoliaEnabled, address) { function getEtherscanUrl(networkShortName, testnetMode, sepoliaEnabled, addressOrTx, isAddressNotTx) {
const type = isAddressNotTx
? Constants.networkExplorerLinks.addressPath
: Constants.networkExplorerLinks.txPath
let link = Constants.networkExplorerLinks.etherscan let link = Constants.networkExplorerLinks.etherscan
if (testnetMode) { if (testnetMode) {
if (sepoliaEnabled) { if (sepoliaEnabled) {
@ -931,7 +934,7 @@ QtObject {
} }
} }
if (networkShortName === Constants.networkShortChainNames.arbiscan) { if (networkShortName === Constants.networkShortChainNames.arbitrum) {
link = Constants.networkExplorerLinks.arbiscan link = Constants.networkExplorerLinks.arbiscan
if (testnetMode) { if (testnetMode) {
if (sepoliaEnabled) { if (sepoliaEnabled) {
@ -951,7 +954,17 @@ QtObject {
} }
} }
return "%1/%2/%3".arg(link).arg(Constants.networkExplorerLinks.addressPath).arg(address) return "%1/%2/%3".arg(link).arg(type).arg(addressOrTx)
}
// Etherscan URL for an address
function getUrlForAddressOnNetwork(networkShortName, testnetMode, sepoliaEnabled, address) {
return getEtherscanUrl(networkShortName, testnetMode, sepoliaEnabled, address, true /* is address */)
}
// Etherscan URL for a transaction
function getUrlForTxOnNetwork(networkShortName, testnetMode, sepoliaEnabled, tx) {
return getEtherscanUrl(networkShortName, testnetMode, sepoliaEnabled, tx, false /* is TX */)
} }
// Leave this function at the bottom of the file as QT Creator messes up the code color after this // Leave this function at the bottom of the file as QT Creator messes up the code color after this