2022-10-17 10:17:25 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
|
|
|
|
import utils 1.0
|
|
|
|
|
|
|
|
import shared.stores 1.0
|
|
|
|
import "../../../app/AppLayouts/Profile/stores"
|
|
|
|
|
|
|
|
QtObject {
|
|
|
|
id: root
|
|
|
|
|
2022-12-29 16:44:51 +00:00
|
|
|
property CurrenciesStore currencyStore: CurrenciesStore {}
|
2022-10-17 10:17:25 +00:00
|
|
|
property ProfileSectionStore profileSectionStore: ProfileSectionStore {}
|
|
|
|
property var contactStore: profileSectionStore.contactsStore
|
|
|
|
|
|
|
|
property var mainModuleInst: mainModule
|
|
|
|
property var walletSectionTransactionsInst: walletSectionTransactions
|
|
|
|
|
2022-12-29 16:44:51 +00:00
|
|
|
property var locale: Qt.locale(localAppSettings.language)
|
2022-10-17 10:17:25 +00:00
|
|
|
property string currentCurrency: walletSection.currentCurrency
|
|
|
|
property var allNetworks: networksModule.all
|
|
|
|
property var accounts: walletSectionAccounts.model
|
|
|
|
property var currentAccount: walletSectionCurrent
|
|
|
|
property string signingPhrase: walletSection.signingPhrase
|
|
|
|
property var savedAddressesModel: walletSectionSavedAddresses.model
|
|
|
|
property var disabledChainIdsFromList: []
|
|
|
|
property var disabledChainIdsToList: []
|
|
|
|
|
|
|
|
function addRemoveDisabledFromChain(chainID, isDisabled) {
|
|
|
|
if(isDisabled) {
|
2022-12-09 09:10:05 +00:00
|
|
|
if(!root.disabledChainIdsFromList.includes(chainID))
|
|
|
|
disabledChainIdsFromList.push(chainID)
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-11-23 17:58:22 +00:00
|
|
|
for(var i = 0; i < disabledChainIdsFromList.length;i++) {
|
|
|
|
if(disabledChainIdsFromList[i] === chainID) {
|
|
|
|
disabledChainIdsFromList.splice(i, 1)
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addRemoveDisabledToChain(chainID, isDisabled) {
|
|
|
|
if(isDisabled) {
|
2022-12-09 09:10:05 +00:00
|
|
|
if(!root.disabledChainIdsToList.includes(chainID))
|
|
|
|
root.disabledChainIdsToList.push(chainID)
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-11-23 17:58:22 +00:00
|
|
|
for(var i = 0; i < root.disabledChainIdsToList.length;i++) {
|
|
|
|
if(root.disabledChainIdsToList[i] === chainID) {
|
|
|
|
root.disabledChainIdsToList.splice(i, 1)
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 15:56:02 +00:00
|
|
|
function getEtherscanLink(chainID) {
|
|
|
|
return networksModule.all.getBlockExplorerURL(chainID)
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function copyToClipboard(text) {
|
|
|
|
globalUtils.copyToClipboard(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFiatValue(balance, cryptoSymbol, fiatSymbol) {
|
|
|
|
return profileSectionStore.ensUsernamesStore.getFiatValue(balance, cryptoSymbol, fiatSymbol)
|
|
|
|
}
|
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
function getCryptoValue(balance, cryptoSymbol, fiatSymbol) {
|
|
|
|
return profileSectionStore.ensUsernamesStore.getCryptoValue(balance, cryptoSymbol, fiatSymbol)
|
|
|
|
}
|
|
|
|
|
2022-10-17 10:17:25 +00:00
|
|
|
function getGasEthValue(gweiValue, gasLimit) {
|
|
|
|
return profileSectionStore.ensUsernamesStore.getGasEthValue(gweiValue, gasLimit)
|
|
|
|
}
|
|
|
|
|
2022-11-30 12:59:21 +00:00
|
|
|
function authenticateAndTransfer(from, to, tokenSymbol, amount, uuid, selectedRoutes) {
|
|
|
|
walletSectionTransactions.authenticateAndTransfer(from, to, tokenSymbol, amount, uuid, selectedRoutes)
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function suggestedFees(chainId) {
|
|
|
|
return JSON.parse(walletSectionTransactions.suggestedFees(chainId))
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEstimatedTime(chainId, maxFeePerGas) {
|
|
|
|
return walletSectionTransactions.getEstimatedTime(chainId, maxFeePerGas)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getChainIdForChat() {
|
|
|
|
return walletSectionTransactions.getChainIdForChat()
|
|
|
|
}
|
|
|
|
|
|
|
|
function getChainIdForBrowser() {
|
|
|
|
return walletSectionTransactions.getChainIdForBrowser()
|
|
|
|
}
|
|
|
|
|
2022-11-25 09:13:02 +00:00
|
|
|
function suggestedRoutes(account, amount, token, disabledFromChainIDs, disabledToChainIDs, preferredChainIds, sendType, lockedInAmounts) {
|
|
|
|
walletSectionTransactions.suggestedRoutes(account, amount, token, disabledFromChainIDs, disabledToChainIDs, preferredChainIds, sendType, JSON.stringify(lockedInAmounts))
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hex2Eth(value) {
|
|
|
|
return globalUtils.hex2Eth(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
function switchAccount(newIndex) {
|
|
|
|
if(Constants.isCppApp)
|
|
|
|
walletSectionAccounts.switchAccount(newIndex)
|
|
|
|
else
|
|
|
|
walletSection.switchAccount(newIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveENS(value) {
|
|
|
|
mainModuleInst.resolveENS(value, "")
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:22:03 +00:00
|
|
|
function getWei2Eth(wei, decimals) {
|
|
|
|
return globalUtils.wei2Eth(wei, decimals)
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:22:03 +00:00
|
|
|
function getEth2Wei(eth, decimals) {
|
|
|
|
return globalUtils.eth2Wei(eth, decimals)
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function plainText(text) {
|
|
|
|
return globalUtils.plainText(text)
|
|
|
|
}
|
2022-12-01 15:48:44 +00:00
|
|
|
|
2022-11-25 09:13:02 +00:00
|
|
|
function setDefaultPreferredDisabledChains() {
|
|
|
|
let mainnetChainId = getMainnetChainId()
|
|
|
|
preferredChainIds.push(mainnetChainId)
|
|
|
|
addUnpreferredChainsToDisabledChains()
|
|
|
|
}
|
2022-12-01 15:48:44 +00:00
|
|
|
|
2022-11-25 09:13:02 +00:00
|
|
|
function resetTxStoreProperties() {
|
|
|
|
disabledChainIdsFromList = []
|
|
|
|
disabledChainIdsToList = []
|
|
|
|
preferredChainIds = []
|
|
|
|
lockedInAmounts = []
|
2022-12-01 15:48:44 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 09:13:02 +00:00
|
|
|
property var preferredChainIds: []
|
|
|
|
|
2022-12-01 15:48:44 +00:00
|
|
|
function getMainnetChainId() {
|
|
|
|
return networksModule.getMainnetChainId()
|
|
|
|
}
|
|
|
|
|
2022-11-25 09:13:02 +00:00
|
|
|
function addPreferredChains(preferredchains, showUnpreferredNetworks) {
|
|
|
|
for(const chain of preferredchains) {
|
|
|
|
if(!preferredChainIds.includes(chain)) {
|
|
|
|
preferredChainIds.push(chain)
|
|
|
|
// remove from disabled accounts as it was added as preferred
|
|
|
|
addRemoveDisabledToChain(chain, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// here we are trying to remove chains that are not preferred from the list and
|
|
|
|
// also disable them incase the showUnpreferredNetworks toggle is turned off
|
|
|
|
for(var i = 0; i < preferredChainIds.length; i++) {
|
|
|
|
if(!preferredchains.includes(preferredChainIds[i])) {
|
|
|
|
if(!showUnpreferredNetworks)
|
|
|
|
addRemoveDisabledToChain(preferredChainIds[i], true)
|
|
|
|
preferredChainIds.splice(i, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 15:48:44 +00:00
|
|
|
function addUnpreferredChainsToDisabledChains() {
|
|
|
|
for(var i = 0; i < allNetworks.count; i++) {
|
|
|
|
let chainId = allNetworks.rowData(i, "chainId") * 1
|
2022-11-25 09:13:02 +00:00
|
|
|
if(!preferredChainIds.includes(chainId)) {
|
2022-12-01 15:48:44 +00:00
|
|
|
addRemoveDisabledToChain(chainId, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 12:59:21 +00:00
|
|
|
|
2022-11-25 09:13:02 +00:00
|
|
|
function splitAndFormatAddressPrefix(text, isBridgeTx, showUnpreferredNetworks) {
|
|
|
|
let address = ""
|
|
|
|
let tempPreferredChains = []
|
|
|
|
let chainFound = false
|
|
|
|
let splitWords = plainText(text).split(':')
|
|
|
|
let editedText = ""
|
|
|
|
|
|
|
|
for(var i=0; i<splitWords.length; i++) {
|
|
|
|
const word = splitWords[i]
|
|
|
|
if(word.startsWith("0x")) {
|
|
|
|
address = word
|
|
|
|
editedText += word
|
|
|
|
} else {
|
|
|
|
let chainColor = allNetworks.getNetworkColor(word)
|
|
|
|
if(!!chainColor) {
|
|
|
|
chainFound = true
|
|
|
|
if(!isBridgeTx)
|
|
|
|
tempPreferredChains.push(allNetworks.getNetworkChainId(word))
|
|
|
|
editedText += `<span style='color: %1'>%2</span>`.arg(chainColor).arg(word)+':'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!chainFound && !isBridgeTx)
|
|
|
|
addPreferredChains([getMainnetChainId()], showUnpreferredNetworks)
|
|
|
|
else
|
|
|
|
addPreferredChains(tempPreferredChains, showUnpreferredNetworks)
|
|
|
|
|
|
|
|
editedText +="</a></p>"
|
|
|
|
return {
|
|
|
|
formattedText: editedText,
|
|
|
|
address: address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 12:59:21 +00:00
|
|
|
enum EstimatedTime {
|
|
|
|
Unknown = 0,
|
|
|
|
LessThanOneMin,
|
|
|
|
LessThanThreeMins,
|
|
|
|
LessThanFiveMins,
|
|
|
|
MoreThanFiveMins
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLabelForEstimatedTxTime(estimatedFlag) {
|
|
|
|
switch(estimatedFlag) {
|
|
|
|
case TransactionStore.EstimatedTime.Unknown:
|
|
|
|
return qsTr("~ Unknown")
|
|
|
|
case TransactionStore.EstimatedTime.LessThanOneMin :
|
|
|
|
return qsTr("< 1 minute")
|
|
|
|
case TransactionStore.EstimatedTime.LessThanThreeMins :
|
|
|
|
return qsTr("< 3 minutes")
|
|
|
|
case TransactionStore.EstimatedTime.LessThanFiveMins:
|
|
|
|
return qsTr("< 5 minutes")
|
|
|
|
default:
|
|
|
|
return qsTr("> 5 minutes")
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 09:13:02 +00:00
|
|
|
|
|
|
|
property var lockedInAmounts: []
|
|
|
|
|
|
|
|
function addLockedInAmount(chainID, value, decimals, locked) {
|
|
|
|
let amount = Number.fromLocaleString(Qt.locale(), value) * Math.pow(10, decimals)
|
|
|
|
let index = lockedInAmounts.findIndex(lockedItem => lockedItem !== undefined && lockedItem.chainID === chainID)
|
|
|
|
if(index === -1) {
|
|
|
|
lockedInAmounts.push({"chainID": chainID, "value": amount.toString(16)})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(locked) {
|
|
|
|
lockedInAmounts[index].value = amount.toString(16)
|
|
|
|
} else {
|
|
|
|
lockedInAmounts.splice(index,1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 16:44:51 +00:00
|
|
|
|
|
|
|
function getTokenBalanceOnChain(selectedAccount, chainId: int, tokenSymbol: string) {
|
|
|
|
if (!selectedAccount) {
|
|
|
|
console.warn("selectedAccount invalid")
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsonObj = selectedAccount.getTokenBalanceOnChainAsJson(chainId, tokenSymbol)
|
|
|
|
if (jsonObj === "") {
|
|
|
|
console.warn("failed to get balance, returned json is empty")
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const obj = JSON.parse(jsonObj)
|
|
|
|
if (obj.error) {
|
|
|
|
console.warn("failed to get balance, json parse error: ", obj.error)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
2022-10-17 10:17:25 +00:00
|
|
|
}
|