chore(Wallet.RootStore): Moved backend independent methods to a stateless WalletUtils singleton

fixes: #14804
This commit is contained in:
RomanChornii 2024-06-21 00:13:15 +03:00
parent afde836517
commit 3076dd2f57
16 changed files with 120 additions and 99 deletions

View File

@ -80,7 +80,7 @@ QtObject {
} }
function copyToClipboard(value) { function copyToClipboard(value) {
globalUtils.copyToClipboard(value) Utils.copyToClipboard(value)
} }
function saveProfileIdentityChanges(displayName, bio, imageInfo) { function saveProfileIdentityChanges(displayName, bio, imageInfo) {

View File

@ -162,8 +162,27 @@ QtObject {
return networksModuleInst.getNetworkShortNames(chainIds) return networksModuleInst.getNetworkShortNames(chainIds)
} }
function copyToClipboard(textToCopy) { function processPreferredSharingNetworkToggle(preferredSharingNetworks, toggledNetwork) {
globalUtils.copyToClipboard(textToCopy) let prefChains = preferredSharingNetworks
if(prefChains.length === root.flatNetworks.count) {
prefChains = [toggledNetwork.chainId.toString()]
}
else if(!prefChains.includes(toggledNetwork.chainId.toString())) {
prefChains.push(toggledNetwork.chainId.toString())
}
else {
if(prefChains.length === 1) {
prefChains = getAllNetworksChainIds()
}
else {
for(var i = 0; i < prefChains.length;i++) {
if(prefChains[i] === toggledNetwork.chainId.toString()) {
prefChains.splice(i, 1)
}
}
}
}
return prefChains
} }
function getNetworkData(combinedNetwork) { function getNetworkData(combinedNetwork) {

View File

@ -148,7 +148,7 @@ StatusDialog {
CopyToClipBoardButton { CopyToClipBoardButton {
id: copyToClipboardButton id: copyToClipboardButton
onCopyClicked: root.walletStore.copyToClipboard(textToCopy) onCopyClicked: Utils.copyToClipboard(textToCopy)
onPressed: function() { onPressed: function() {
let copiedText = "Total" + '\t' + d.totalFilteredCalls + " of " + d.totalCalls + '\n' + '\n' let copiedText = "Total" + '\t' + d.totalFilteredCalls + " of " + d.totalCalls + '\n' + '\n'
for (let i = 0; i < resultsListView.model.count; i++) { for (let i = 0; i < resultsListView.model.count; i++) {

View File

@ -196,7 +196,7 @@ ColumnLayout {
copyButtonEnabled: true copyButtonEnabled: true
title: qsTr("Derivation Path") title: qsTr("Derivation Path")
subTitle: !!root.account? Utils.getPathForDisplay(root.account.path) : "" subTitle: !!root.account? Utils.getPathForDisplay(root.account.path) : ""
onCopyClicked: root.walletStore.copyToClipboard(!!root.account? root.account.path : "") onCopyClicked: Utils.copyToClipboard(!!root.account? root.account.path : "")
visible: !!subTitle && !d.privateKeyAccount && !d.watchOnlyAccount visible: !!subTitle && !d.privateKeyAccount && !d.watchOnlyAccount
} }
Separator { Separator {
@ -332,7 +332,7 @@ ColumnLayout {
areTestNetworksEnabled: root.walletStore.areTestNetworksEnabled areTestNetworksEnabled: root.walletStore.areTestNetworksEnabled
isGoerliEnabled: root.walletStore.isGoerliEnabled isGoerliEnabled: root.walletStore.isGoerliEnabled
preferredSharedNetworkNamesArray: d.preferredSharingNetworkShortNames.split(":").filter(Boolean) preferredSharedNetworkNamesArray: d.preferredSharingNetworkShortNames.split(":").filter(Boolean)
onCopyToClipboard: root.walletStore.copyToClipboard(address) onCopyToClipboard: Utils.copyToClipboard(address)
} }
WalletKeypairAccountMenu { WalletKeypairAccountMenu {

View File

@ -7,9 +7,36 @@ import StatusQ.Core.Theme 0.1
import StatusQ.Core.Utils 0.1 as StatusQUtils import StatusQ.Core.Utils 0.1 as StatusQUtils
import "stores" as WalletStores import AppLayouts.Wallet.stores 1.0 as WalletStores
QtObject { QtObject {
property QtObject _d: QtObject {
id: d
property var chainColors: ({})
function initChainColors(model) {
for (let i = 0; i < model.count; i++) {
const item = SQUtils.ModelUtils.get(model, i)
chainColors[item.shortName] = item.chainColor
}
}
function colorForChainShortName(chainShortName) {
return d.chainColors[chainShortName]
}
readonly property Connections walletRootStoreConnections: Connections {
target: WalletStores.RootStore
function onFlatNetworksChanged() {
d.initChainColors(WalletStores.RootStore.flatNetworks)
}
}
}
function colorizedChainPrefix(prefix) { function colorizedChainPrefix(prefix) {
if (!prefix) if (!prefix)
return "" return ""
@ -21,7 +48,7 @@ QtObject {
for (let i in prefixes) { for (let i in prefixes) {
const pref = prefixes[i] const pref = prefixes[i]
let col = WalletStores.RootStore.colorForChainShortName(pref) let col = d.colorForChainShortName(pref)
if (!col) if (!col)
col = defaultColor col = defaultColor
@ -73,4 +100,51 @@ QtObject {
return value - Math.max(0.0001, Math.min(0.01, value * 0.1)) return value - Math.max(0.0001, Math.min(0.01, value * 0.1))
} }
function getAssetForSendTx(tx) {
if (tx.isNFT) {
return {
uid: tx.tokenID,
chainId: tx.chainId,
name: tx.nftName,
imageUrl: tx.nftImageUrl,
collectionUid: "",
collectionName: ""
}
} else {
return tx.symbol
}
}
function isTxRepeatable(tx) {
if (!tx || tx.txType !== Constants.TransactionType.Send)
return false
let res = root.lookupAddressObject(tx.sender)
if (!res || res.type !== RootStore.LookupType.Account || res.object.walletType == Constants.watchWalletType)
return false
if (tx.isNFT) {
// TODO #12275: check if account owns enough NFT
} else {
// TODO #12275: Check if account owns enough tokens
}
return true
}
function getExplorerNameForNetwork(networkShortName) {
if (networkShortName === Constants.networkShortChainNames.arbitrum) {
return qsTr("Arbiscan Explorer")
}
if (networkShortName === Constants.networkShortChainNames.optimism) {
return qsTr("Optimism Explorer")
}
return qsTr("Etherscan Explorer")
}
function getTwitterLink(twitterHandle) {
const prefix = Constants.socialLinkPrefixesByType[Constants.socialLinkType.twitter]
return prefix + twitterHandle
}
} }

View File

@ -178,7 +178,7 @@ StatusListItem {
timeout: 1500 timeout: 1500
autoDismissMenu: true autoDismissMenu: true
onTriggered: { onTriggered: {
store.copyToClipboard(d.visibleAddress) Utils.copyToClipboard(d.visibleAddress)
} }
} }

View File

@ -34,7 +34,7 @@ StatusModal {
property bool switchingAccounsEnabled: true property bool switchingAccounsEnabled: true
property bool changingPreferredChainsEnabled: true property bool changingPreferredChainsEnabled: true
property string qrImageSource: store.getQrCode(d.visibleAddress) property string qrImageSource: Utils.getQrCode(d.visibleAddress)
property var getNetworkShortNames: function(chainIDsString) { property var getNetworkShortNames: function(chainIDsString) {
return store.getNetworkShortNames(chainIDsString) return store.getNetworkShortNames(chainIDsString)
} }

View File

@ -227,7 +227,7 @@ StatusModal {
anchors.top: addressText.top anchors.top: addressText.top
icon.name: "copy" icon.name: "copy"
type: StatusRoundButton.Type.Tertiary type: StatusRoundButton.Type.Tertiary
onClicked: WalletStore.RootStore.copyToClipboard(d.visibleAddress) onClicked: Utils.copyToClipboard(d.visibleAddress)
} }
} }

View File

@ -277,7 +277,7 @@ StatusMenu {
} }
} }
icon.name: "copy" icon.name: "copy"
onTriggered: RootStore.copyToClipboard(d.selectedAddress) onTriggered: Utils.copyToClipboard(d.selectedAddress)
} }
StatusAction { StatusAction {
id: showQrAction id: showQrAction

View File

@ -130,15 +130,6 @@ QtObject {
tokensList: walletAssetsStore.groupedAccountAssetsModel tokensList: walletAssetsStore.groupedAccountAssetsModel
} }
property var chainColors: ({})
function initChainColors(model) {
for (let i = 0; i < model.count; i++) {
const item = SQUtils.ModelUtils.get(model, i)
chainColors[item.shortName] = item.chainColor
}
}
readonly property Connections walletSectionConnections: Connections { readonly property Connections walletSectionConnections: Connections {
target: root.walletSectionInst target: root.walletSectionInst
function onWalletAccountRemoved(address) { function onWalletAccountRemoved(address) {
@ -153,9 +144,6 @@ QtObject {
} }
} }
function colorForChainShortName(chainShortName) {
return d.chainColors[chainShortName]
}
property var flatNetworks: networksModule.flatNetworks property var flatNetworks: networksModule.flatNetworks
property SortFilterProxyModel filteredFlatModel: SortFilterProxyModel { property SortFilterProxyModel filteredFlatModel: SortFilterProxyModel {
@ -163,10 +151,6 @@ QtObject {
filters: ValueFilter { roleName: "isTest"; value: root.areTestNetworksEnabled } filters: ValueFilter { roleName: "isTest"; value: root.areTestNetworksEnabled }
} }
onFlatNetworksChanged: {
d.initChainColors(flatNetworks)
}
property var cryptoRampServicesModel: walletSectionBuySellCrypto.model property var cryptoRampServicesModel: walletSectionBuySellCrypto.model
function resetCurrentViewedHolding(type) { function resetCurrentViewedHolding(type) {
@ -228,10 +212,6 @@ QtObject {
walletSection.updateCurrency(newCurrency) walletSection.updateCurrency(newCurrency)
} }
function getQrCode(address) {
return globalUtils.qrCode(address)
}
function getNameForWalletAddress(address) { function getNameForWalletAddress(address) {
return walletSectionAccounts.getNameByAddress(address) return walletSectionAccounts.getNameByAddress(address)
} }
@ -316,7 +296,7 @@ QtObject {
if (acc) { if (acc) {
res = {type: RootStore.LookupType.Account, object: acc} res = {type: RootStore.LookupType.Account, object: acc}
} else { } else {
let sa = SQUtils.ModelUtils.getByKey(walletSectionSavedAddresses.model, "address", address) let sa = SQUtils.ModelUtils.getByKey(walletSectionSavedAddressesInst.model, "address", address)
if (sa) { if (sa) {
res = {type: RootStore.LookupType.SavedAddress, object: sa} res = {type: RootStore.LookupType.SavedAddress, object: sa}
} }
@ -325,38 +305,6 @@ QtObject {
return res return res
} }
function getAssetForSendTx(tx) {
if (tx.isNFT) {
return {
uid: tx.tokenID,
chainId: tx.chainId,
name: tx.nftName,
imageUrl: tx.nftImageUrl,
collectionUid: "",
collectionName: ""
}
} else {
return tx.symbol
}
}
function isTxRepeatable(tx) {
if (!tx || tx.txType !== Constants.TransactionType.Send)
return false
let res = root.lookupAddressObject(tx.sender)
if (!res || res.type !== RootStore.LookupType.Account || res.object.walletType == Constants.watchWalletType)
return false
if (tx.isNFT) {
// TODO #12275: check if account owns enough NFT
} else {
// TODO #12275: Check if account owns enough tokens
}
return true
}
function isOwnedAccount(address) { function isOwnedAccount(address) {
return walletSectionAccounts.isOwnedAccount(address) return walletSectionAccounts.isOwnedAccount(address)
} }
@ -391,10 +339,6 @@ QtObject {
networksModule.toggleNetwork(chainId) networksModule.toggleNetwork(chainId)
} }
function copyToClipboard(text) {
globalUtils.copyToClipboard(text)
}
function runAddAccountPopup() { function runAddAccountPopup() {
walletSection.runAddAccountPopup(false) walletSection.runAddAccountPopup(false)
} }
@ -411,10 +355,6 @@ QtObject {
walletSectionSend.switchReceiveAccountByAddress(address) walletSectionSend.switchReceiveAccountByAddress(address)
} }
function toggleWatchOnlyAccounts() {
walletSection.toggleWatchOnlyAccounts()
}
function getAllNetworksChainIds() { function getAllNetworksChainIds() {
let result = [] let result = []
let chainIdsArray = SQUtils.ModelUtils.modelToFlatArray(root.filteredFlatModel, "chainId") let chainIdsArray = SQUtils.ModelUtils.modelToFlatArray(root.filteredFlatModel, "chainId")
@ -494,16 +434,6 @@ QtObject {
} }
} }
function getExplorerNameForNetwork(networkShortName) {
if (networkShortName === Constants.networkShortChainNames.arbitrum) {
return qsTr("Arbiscan Explorer")
}
if (networkShortName === Constants.networkShortChainNames.optimism) {
return qsTr("Optimism Explorer")
}
return qsTr("Etherscan Explorer")
}
function getOpenSeaNetworkName(networkShortName) { function getOpenSeaNetworkName(networkShortName) {
let networkName = Constants.openseaExplorerLinks.ethereum let networkName = Constants.openseaExplorerLinks.ethereum
if (networkShortName === Constants.networkShortChainNames.mainnet) { if (networkShortName === Constants.networkShortChainNames.mainnet) {
@ -553,8 +483,4 @@ QtObject {
return "%1/assets/%2/%3/%4".arg(baseLink).arg(networkName).arg(contractAddress).arg(tokenId) return "%1/assets/%2/%3/%4".arg(baseLink).arg(networkName).arg(contractAddress).arg(tokenId)
} }
function getTwitterLink(twitterHandle) {
const prefix = Constants.socialLinkPrefixesByType[Constants.socialLinkType.twitter]
return prefix + twitterHandle
}
} }

View File

@ -27,7 +27,7 @@ StatusMenu {
icon.name: "copy" icon.name: "copy"
timeout: 1500 timeout: 1500
enabled: !!root.account enabled: !!root.account
onTriggered: RootStore.copyToClipboard(root.account.address?? "") onTriggered: Utils.copyToClipboard(root.account.address?? "")
} }
StatusMenuSeparator { StatusMenuSeparator {

View File

@ -770,10 +770,10 @@ Item {
if (!d.isTransactionValid || root.overview.isWatchOnlyAccount) if (!d.isTransactionValid || root.overview.isWatchOnlyAccount)
return false return false
return WalletStores.RootStore.isTxRepeatable(tx) return WalletUtils.isTxRepeatable(tx)
} }
onClicked: { onClicked: {
let asset = WalletStores.RootStore.getAssetForSendTx(tx) let asset = WalletUtils.getAssetForSendTx(tx)
let req = Helpers.lookupAddressesForSendModal(tx.sender, tx.recipient, asset, tx.isNFT, tx.amount) let req = Helpers.lookupAddressesForSendModal(tx.sender, tx.recipient, asset, tx.isNFT, tx.amount)
root.sendModal.preSelectedAccount = req.preSelectedAccount root.sendModal.preSelectedAccount = req.preSelectedAccount
@ -795,7 +795,7 @@ Item {
icon.width: 20 icon.width: 20
icon.height: 20 icon.height: 20
size: StatusButton.Small size: StatusButton.Small
onClicked: RootStore.copyToClipboard(transactionHeader.getDetailsString(d.details)) onClicked: Utils.copyToClipboard(transactionHeader.getDetailsString(d.details))
} }
} }
} }

View File

@ -16,6 +16,7 @@ import shared.controls 1.0
import shared.views 1.0 import shared.views 1.0
import shared.popups 1.0 import shared.popups 1.0
import AppLayouts.Wallet 1.0
import "../../stores" import "../../stores"
import "../../controls" import "../../controls"
@ -96,7 +97,7 @@ Item {
networkShortName: !!collectible ? collectible.networkShortName : "" networkShortName: !!collectible ? collectible.networkShortName : ""
networkColor: !!collectible ?collectible.networkColor : "" networkColor: !!collectible ?collectible.networkColor : ""
networkIconURL: !!collectible ? collectible.networkIconUrl : "" networkIconURL: !!collectible ? collectible.networkIconUrl : ""
networkExplorerName: !!collectible ? root.walletRootStore.getExplorerNameForNetwork(collectible.networkShortName): "" networkExplorerName: !!collectible ? WalletUtils.getExplorerNameForNetwork(collectible.networkShortName): ""
collectibleLinkEnabled: Utils.getUrlStatus(d.collectibleLink) collectibleLinkEnabled: Utils.getUrlStatus(d.collectibleLink)
collectionLinkEnabled: (!!communityDetails && communityDetails.name) || Utils.getUrlStatus(d.collectionLink) collectionLinkEnabled: (!!communityDetails && communityDetails.name) || Utils.getUrlStatus(d.collectionLink)
explorerLinkEnabled: Utils.getUrlStatus(d.blockExplorerLink) explorerLinkEnabled: Utils.getUrlStatus(d.blockExplorerLink)
@ -338,7 +339,7 @@ Item {
primaryText: qsTr("Twitter") primaryText: qsTr("Twitter")
secondaryText: !!collectible ? collectible.twitterHandle : "" secondaryText: !!collectible ? collectible.twitterHandle : ""
visible: !!collectible && collectible.twitterHandle visible: !!collectible && collectible.twitterHandle
onClicked: Global.openLinkWithConfirmation(root.walletRootStore.getTwitterLink(collectible.twitterHandle), Constants.socialLinkPrefixesByType[Constants.socialLinkType.twitter]) onClicked: Global.openLinkWithConfirmation(WalletUtils.getTwitterLink(collectible.twitterHandle), Constants.socialLinkPrefixesByType[Constants.socialLinkType.twitter])
} }
} }
} }

View File

@ -37,10 +37,6 @@ QtObject {
property var flatNetworks: networksModule.flatNetworks property var flatNetworks: networksModule.flatNetworks
function hex2Dec(value) {
return globalUtils.hex2Dec(value)
}
readonly property var formationChars: (["*", "`", "~"]) readonly property var formationChars: (["*", "`", "~"])
function getSelectedTextWithFormationChars(messageInputField) { function getSelectedTextWithFormationChars(messageInputField) {
let i = 1 let i = 1

View File

@ -19,6 +19,7 @@ import "../popups/send"
import "../stores" import "../stores"
import "../controls" import "../controls"
import AppLayouts.Wallet 1.0
import AppLayouts.Wallet.stores 1.0 as WalletStores import AppLayouts.Wallet.stores 1.0 as WalletStores
import AppLayouts.Wallet.popups 1.0 import AppLayouts.Wallet.popups 1.0
import AppLayouts.Wallet.controls 1.0 import AppLayouts.Wallet.controls 1.0
@ -310,13 +311,13 @@ ColumnLayout {
enabled: { enabled: {
if (!overview.isWatchOnlyAccount && !tx) if (!overview.isWatchOnlyAccount && !tx)
return false return false
return WalletStores.RootStore.isTxRepeatable(tx) return WalletUtils.isTxRepeatable(tx)
} }
onTriggered: { onTriggered: {
if (!tx) if (!tx)
return return
let asset = WalletStores.RootStore.getAssetForSendTx(tx) let asset = WalletUtils.getAssetForSendTx(tx)
let req = Helpers.lookupAddressesForSendModal(tx.sender, tx.recipient, asset, tx.isNFT, tx.amount) let req = Helpers.lookupAddressesForSendModal(tx.sender, tx.recipient, asset, tx.isNFT, tx.amount)

View File

@ -963,4 +963,8 @@ QtObject {
function getUrlStatus(url) { function getUrlStatus(url) {
return globalUtilsInst.isValidURL(url) return globalUtilsInst.isValidURL(url)
} }
function getQrCode(address) {
return globalUtilsInst.qrCode(address)
}
} }