From afe1cd59c09be3b6d5d3915b85050b7f2fece756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Tue, 24 Sep 2024 12:57:09 +0200 Subject: [PATCH] shared/RootStore: remove duplicated methods of CurrenciesStore --- .../Wallet/views/TransactionDetailView.qml | 50 ++++++++-------- .../shared/controls/TransactionDelegate.qml | 60 +++++++++---------- ui/imports/shared/stores/CurrenciesStore.qml | 6 ++ ui/imports/shared/stores/RootStore.qml | 26 -------- 4 files changed, 61 insertions(+), 81 deletions(-) diff --git a/ui/app/AppLayouts/Wallet/views/TransactionDetailView.qml b/ui/app/AppLayouts/Wallet/views/TransactionDetailView.qml index 350025c844..2dc7a09a34 100644 --- a/ui/app/AppLayouts/Wallet/views/TransactionDetailView.qml +++ b/ui/app/AppLayouts/Wallet/views/TransactionDetailView.qml @@ -86,27 +86,27 @@ Item { readonly property string fiatValueFormatted: { if (!d.isTransactionValid || transactionHeader.isMultiTransaction || !symbol) return "" - return root.rootStore.formatCurrencyAmount(transactionHeader.fiatValue, root.rootStore.currentCurrency) + return root.rootStore.currencyStore.formatCurrencyAmount(transactionHeader.fiatValue, root.rootStore.currentCurrency) } readonly property string cryptoValueFormatted: { if (!d.isTransactionValid || transactionHeader.isMultiTransaction) return "" - const formatted = root.rootStore.formatCurrencyAmount(transaction.amount, transaction.symbol) + const formatted = root.rootStore.currencyStore.formatCurrencyAmount(transaction.amount, transaction.symbol) return symbol || (!d.isDetailsValid || !d.details.contract) ? formatted : "%1 (%2)".arg(formatted).arg(Utils.compactAddress(transaction.tokenAddress, 4)) } readonly property string outFiatValueFormatted: { if (!d.isTransactionValid || !transactionHeader.isMultiTransaction || !outSymbol) return "" - return root.rootStore.formatCurrencyAmount(transactionHeader.outFiatValue, root.rootStore.currentCurrency) + return root.rootStore.currencyStore.formatCurrencyAmount(transactionHeader.outFiatValue, root.rootStore.currentCurrency) } readonly property string outCryptoValueFormatted: { if (!d.isTransactionValid || !transactionHeader.isMultiTransaction) return "" - const formatted = root.rootStore.formatCurrencyAmount(transaction.outAmount, transaction.outSymbol) + const formatted = root.rootStore.currencyStore.formatCurrencyAmount(transaction.outAmount, transaction.outSymbol) return outSymbol || !transaction.tokenOutAddress ? formatted : "%1 (%2)".arg(formatted).arg(Utils.compactAddress(transaction.tokenOutAddress, 4)) } - readonly property real feeEthValue: d.details ? root.rootStore.getFeeEthValue(d.details.totalFees) : 0 - readonly property real feeFiatValue: root.rootStore.getFiatValue(d.feeEthValue, Constants.ethToken) + readonly property real feeEthValue: d.details ? root.rootStore.currencyStore.getFeeEthValue(d.details.totalFees) : 0 + readonly property real feeFiatValue: root.rootStore.currencyStore.getFiatValue(d.feeEthValue, Constants.ethToken) readonly property int transactionType: d.isTransactionValid ? WalletStores.RootStore.transactionType(transaction) : Constants.TransactionType.Send readonly property bool isBridge: d.transactionType === Constants.TransactionType.Bridge @@ -649,20 +649,20 @@ Item { return "" const type = d.transactionType if (type === Constants.TransactionType.Swap) { - return root.rootStore.formatCurrencyAmount(transactionHeader.inCryptoValue, d.inSymbol) + return root.rootStore.currencyStore.formatCurrencyAmount(transactionHeader.inCryptoValue, d.inSymbol) } else if (type === Constants.TransactionType.Bridge) { // Reduce crypto value by fee value - const valueInCrypto = root.rootStore.getCryptoValue(transactionHeader.outFiatValue - d.feeFiatValue, d.inSymbol) - return root.rootStore.formatCurrencyAmount(valueInCrypto, d.inSymbol) + const valueInCrypto = root.rootStore.currencyStore.getCryptoValue(transactionHeader.outFiatValue - d.feeFiatValue, d.inSymbol) + return root.rootStore.currencyStore.formatCurrencyAmount(valueInCrypto, d.inSymbol) } return "" } tertiaryTitle: { const type = d.transactionType if (type === Constants.TransactionType.Swap) { - return root.rootStore.formatCurrencyAmount(transactionHeader.inFiatValue, root.rootStore.currentCurrency) + return root.rootStore.currencyStore.formatCurrencyAmount(transactionHeader.inFiatValue, root.rootStore.currentCurrency) } else if (type === Constants.TransactionType.Bridge) { - return root.rootStore.formatCurrencyAmount(transactionHeader.outFiatValue - d.feeFiatValue, root.rootStore.currentCurrency) + return root.rootStore.currencyStore.formatCurrencyAmount(transactionHeader.outFiatValue - d.feeFiatValue, root.rootStore.currentCurrency) } return "" } @@ -676,8 +676,8 @@ Item { if (!d.isTransactionValid || transactionHeader.isNFT || !d.isDetailsValid) return "" if (!d.symbol) { - const maxFeeEth = root.rootStore.getFeeEthValue(d.details.maxTotalFees) - return root.rootStore.formatCurrencyAmount(maxFeeEth, Constants.ethToken) + const maxFeeEth = root.rootStore.currencyStore.getFeeEthValue(d.details.maxTotalFees) + return root.rootStore.currencyStore.formatCurrencyAmount(maxFeeEth, Constants.ethToken) } switch(d.transactionType) { @@ -694,12 +694,12 @@ Item { return "" let fiatValue if (!d.symbol) { - const maxFeeEth = root.rootStore.getFeeEthValue(d.details.maxTotalFees) - fiatValue = root.rootStore.getFiatValue(maxFeeEth, Constants.ethToken) + const maxFeeEth = root.rootStore.currencyStore.getFeeEthValue(d.details.maxTotalFees) + fiatValue = root.rootStore.currencyStore.getFiatValue(maxFeeEth, Constants.ethToken) } else { fiatValue = d.feeFiatValue } - return root.rootStore.formatCurrencyAmount(fiatValue, root.rootStore.currentCurrency) + return root.rootStore.currencyStore.formatCurrencyAmount(fiatValue, root.rootStore.currentCurrency) } visible: !!subTitle } @@ -723,30 +723,30 @@ Item { if (fieldIsHidden) return "" if (showMaxFee) { - const maxFeeEth = root.rootStore.getFeeEthValue(d.details.maxTotalFees) - return root.rootStore.formatCurrencyAmount(maxFeeEth, Constants.ethToken) + const maxFeeEth = root.rootStore.currencyStore.getFeeEthValue(d.details.maxTotalFees) + return root.rootStore.currencyStore.formatCurrencyAmount(maxFeeEth, Constants.ethToken) } else if (showFee) { - return root.rootStore.formatCurrencyAmount(d.feeEthValue, Constants.ethToken) + return root.rootStore.currencyStore.formatCurrencyAmount(d.feeEthValue, Constants.ethToken) } else if (showValue) { return d.cryptoValueFormatted } const cryptoValue = transactionHeader.isMultiTransaction ? d.outCryptoValueFormatted : d.cryptoValueFormatted - return "%1 + %2".arg(cryptoValue).arg(root.rootStore.formatCurrencyAmount(d.feeEthValue, Constants.ethToken)) + return "%1 + %2".arg(cryptoValue).arg(root.rootStore.currencyStore.formatCurrencyAmount(d.feeEthValue, Constants.ethToken)) } tertiaryTitle: { if (fieldIsHidden) return "" if (showMaxFee) { - const maxFeeEth = root.rootStore.getFeeEthValue(d.details.maxTotalFees) - const maxFeeFiat = root.rootStore.getFiatValue(d.feeEthValue, Constants.ethToken) - return root.rootStore.formatCurrencyAmount(maxFeeFiat, root.rootStore.currentCurrency) + const maxFeeEth = root.rootStore.currencyStore.getFeeEthValue(d.details.maxTotalFees) + const maxFeeFiat = root.rootStore.currencyStore.getFiatValue(d.feeEthValue, Constants.ethToken) + return root.rootStore.currencyStore.formatCurrencyAmount(maxFeeFiat, root.rootStore.currentCurrency) } else if (showFee) { - return root.rootStore.formatCurrencyAmount(d.feeFiatValue, root.rootStore.currentCurrency) + return root.rootStore.currencyStore.formatCurrencyAmount(d.feeFiatValue, root.rootStore.currentCurrency) } else if (showValue) { return d.fiatValueFormatted } const fiatValue = transactionHeader.isMultiTransaction ? transactionHeader.outFiatValue : transactionHeader.fiatValue - return root.rootStore.formatCurrencyAmount(fiatValue + d.feeFiatValue, root.rootStore.currentCurrency) + return root.rootStore.currencyStore.formatCurrencyAmount(fiatValue + d.feeFiatValue, root.rootStore.currentCurrency) } visible: !!subTitle highlighted: true diff --git a/ui/imports/shared/controls/TransactionDelegate.qml b/ui/imports/shared/controls/TransactionDelegate.qml index e7866e3e30..a34e339fdd 100644 --- a/ui/imports/shared/controls/TransactionDelegate.qml +++ b/ui/imports/shared/controls/TransactionDelegate.qml @@ -58,11 +58,11 @@ StatusListItem { readonly property bool isMultiTransaction: isModelDataValid && modelData.isMultiTransaction readonly property string currentCurrency: rootStore.currentCurrency readonly property double cryptoValue: isModelDataValid ? modelData.amount : 0.0 - readonly property double fiatValue: isModelDataValid && !isMultiTransaction ? rootStore.getFiatValue(cryptoValue, modelData.symbol) : 0.0 + readonly property double fiatValue: isModelDataValid && !isMultiTransaction ? rootStore.currencyStore.getFiatValue(cryptoValue, modelData.symbol) : 0.0 readonly property double inCryptoValue: isModelDataValid ? modelData.inAmount : 0.0 - readonly property double inFiatValue: isModelDataValid && isMultiTransaction ? rootStore.getFiatValue(inCryptoValue, modelData.inSymbol): 0.0 + readonly property double inFiatValue: isModelDataValid && isMultiTransaction ? rootStore.currencyStore.getFiatValue(inCryptoValue, modelData.inSymbol): 0.0 readonly property double outCryptoValue: isModelDataValid ? modelData.outAmount : 0.0 - readonly property double outFiatValue: isModelDataValid && isMultiTransaction ? rootStore.getFiatValue(outCryptoValue, modelData.outSymbol): 0.0 + readonly property double outFiatValue: isModelDataValid && isMultiTransaction ? rootStore.currencyStore.getFiatValue(outCryptoValue, modelData.outSymbol): 0.0 readonly property string networkColor: isModelDataValid ? SQUtils.ModelUtils.getByKey(rootStore.flatNetworks, "chainId", modelData.chainId, "chainColor") : "" readonly property string networkName: isModelDataValid ? SQUtils.ModelUtils.getByKey(rootStore.flatNetworks, "chainId", modelData.chainId, "chainName") : "" readonly property string networkNameIn: isMultiTransaction ? SQUtils.ModelUtils.getByKey(rootStore.flatNetworks, "chainId", modelData.chainIdIn, "chainName") : "" @@ -105,26 +105,26 @@ StatusListItem { value += (modelData.nftName ? modelData.nftName : "#" + modelData.tokenID) return value } else if (!modelData.symbol && !!modelData.tokenAddress) { - return "%1 (%2)".arg(root.rootStore.formatCurrencyAmount(cryptoValue, "")).arg(Utils.compactAddress(modelData.tokenAddress, 4)) + return "%1 (%2)".arg(root.rootStore.currencyStore.formatCurrencyAmount(cryptoValue, "")).arg(Utils.compactAddress(modelData.tokenAddress, 4)) } - return root.rootStore.formatCurrencyAmount(cryptoValue, modelData.symbol) + return root.rootStore.currencyStore.formatCurrencyAmount(cryptoValue, modelData.symbol) } readonly property string inTransactionValue: { if (!isModelDataValid || !isMultiTransaction) { return qsTr("N/A") } else if (!modelData.inSymbol && !!modelData.tokenInAddress) { - return "%1 (%2)".arg(root.rootStore.formatCurrencyAmount(inCryptoValue, "")).arg(Utils.compactAddress(modelData.tokenInAddress, 4)) + return "%1 (%2)".arg(root.rootStore.currencyStore.formatCurrencyAmount(inCryptoValue, "")).arg(Utils.compactAddress(modelData.tokenInAddress, 4)) } - return rootStore.formatCurrencyAmount(inCryptoValue, modelData.inSymbol) + return rootStore.currencyStore.formatCurrencyAmount(inCryptoValue, modelData.inSymbol) } readonly property string outTransactionValue: { if (!isModelDataValid || !isMultiTransaction) { return qsTr("N/A") } else if (!modelData.outSymbol && !!modelData.tokenOutAddress) { - return "%1 (%2)".arg(root.rootStore.formatCurrencyAmount(outCryptoValue, "")).arg(Utils.compactAddress(modelData.tokenOutAddress, 4)) + return "%1 (%2)".arg(root.rootStore.currencyStore.formatCurrencyAmount(outCryptoValue, "")).arg(Utils.compactAddress(modelData.tokenOutAddress, 4)) } - return rootStore.formatCurrencyAmount(outCryptoValue, modelData.outSymbol) + return rootStore.currencyStore.formatCurrencyAmount(outCryptoValue, modelData.outSymbol) } readonly property string tokenImage: { @@ -235,7 +235,7 @@ StatusListItem { const endl = "\n" const endl2 = endl + endl const type = d.txType - const feeEthValue = rootStore.getGasEthValue(detailsObj.totalFees.amount, 1) + const feeEthValue = rootStore.currencyStore.getGasEthValue(detailsObj.totalFees.amount, 1) // TITLE switch (type) { @@ -465,8 +465,8 @@ StatusListItem { } // VALUES - const fiatTransactionValue = rootStore.formatCurrencyAmount(isMultiTransaction ? root.outFiatValue : root.fiatValue, root.currentCurrency) - const feeFiatValue = rootStore.getFiatValue(feeEthValue, Constants.ethToken) + const fiatTransactionValue = rootStore.currencyStore.formatCurrencyAmount(isMultiTransaction ? root.outFiatValue : root.fiatValue, root.currentCurrency) + const feeFiatValue = rootStore.currencyStore.getFiatValue(feeEthValue, Constants.ethToken) let valuesString = "" if (!root.isNFT) { switch(type) { @@ -481,14 +481,14 @@ StatusListItem { break } if (type === Constants.TransactionType.Swap) { - const crypto = rootStore.formatCurrencyAmount(root.inCryptoValue, modelData.inSymbol) - const fiat = rootStore.formatCurrencyAmount(root.inCryptoValue, modelData.inSymbol) + const crypto = rootStore.currencyStore.formatCurrencyAmount(root.inCryptoValue, modelData.inSymbol) + const fiat = rootStore.currencyStore.formatCurrencyAmount(root.inCryptoValue, modelData.inSymbol) valuesString += qsTr("Amount received %1 (%2)").arg(crypto).arg(fiat) + endl2 } else if (type === Constants.TransactionType.Bridge) { // Reduce crypto value by fee value - const valueInCrypto = rootStore.getCryptoValue(root.fiatValue - feeFiatValue, modelData.inSymbol) - const crypto = rootStore.formatCurrencyAmount(valueInCrypto, modelData.inSymbol) - const fiat = rootStore.formatCurrencyAmount(root.fiatValue - feeFiatValue, root.currentCurrency) + const valueInCrypto = rootStore.currencyStore.getCryptoValue(root.fiatValue - feeFiatValue, modelData.inSymbol) + const crypto = rootStore.currencyStore.formatCurrencyAmount(valueInCrypto, modelData.inSymbol) + const fiat = rootStore.currencyStore.formatCurrencyAmount(root.fiatValue - feeFiatValue, root.currentCurrency) valuesString += qsTr("Amount received %1 (%2)").arg(crypto).arg(fiat) + endl2 } switch(type) { @@ -496,7 +496,7 @@ StatusListItem { case Constants.TransactionType.Swap: case Constants.TransactionType.Bridge: const feeValue = LocaleUtils.currencyAmountToLocaleString(detailsObj.totalFees) - const feeFiat = rootStore.formatCurrencyAmount(feeFiatValue, root.currentCurrency) + const feeFiat = rootStore.currencyStore.formatCurrencyAmount(feeFiatValue, root.currentCurrency) valuesString += qsTr("Fees %1 (%2)").arg(feeValue).arg(feeFiat) + endl2 break default: @@ -506,25 +506,25 @@ StatusListItem { if (!root.isNFT || type !== Constants.TransactionType.Receive) { if (type === Constants.TransactionType.Destroy || root.isNFT) { - const feeCrypto = rootStore.formatCurrencyAmount(feeEthValue, "ETH") - const feeFiat = rootStore.formatCurrencyAmount(feeFiatValue, root.currentCurrency) + const feeCrypto = rootStore.currencyStore.formatCurrencyAmount(feeEthValue, "ETH") + const feeFiat = rootStore.currencyStore.formatCurrencyAmount(feeFiatValue, root.currentCurrency) valuesString += qsTr("Fees %1 (%2)").arg(feeCrypto).arg(feeFiat) + endl2 } else if (type === Constants.TransactionType.Receive || (type === Constants.TransactionType.Buy && networkLayer === 1)) { valuesString += qsTr("Total %1 (%2)").arg(root.transactionValue).arg(fiatTransactionValue) + endl2 } else if (type === Constants.TransactionType.ContractDeployment) { const isPending = root.transactionStatus === Constants.TransactionStatus.Pending if (isPending) { - const maxFeeEthValue = rootStore.getFeeEthValue(detailsObj.maxTotalFees.amount) - const maxFeeCrypto = rootStore.formatCurrencyAmount(maxFeeEthValue, "ETH") - const maxFeeFiat = rootStore.formatCurrencyAmount(maxFeeCrypto, root.currentCurrency) + const maxFeeEthValue = rootStore.currencyStore.getFeeEthValue(detailsObj.maxTotalFees.amount) + const maxFeeCrypto = rootStore.currencyStore.formatCurrencyAmount(maxFeeEthValue, "ETH") + const maxFeeFiat = rootStore.currencyStore.formatCurrencyAmount(maxFeeCrypto, root.currentCurrency) valuesString += qsTr("Estimated max fee %1 (%2)").arg(maxFeeCrypto).arg(maxFeeFiat) + endl2 } else { - const feeCrypto = rootStore.formatCurrencyAmount(feeEthValue, "ETH") - const feeFiat = rootStore.formatCurrencyAmount(feeFiatValue, root.currentCurrency) + const feeCrypto = rootStore.currencyStore.formatCurrencyAmount(feeEthValue, "ETH") + const feeFiat = rootStore.currencyStore.formatCurrencyAmount(feeFiatValue, root.currentCurrency) valuesString += qsTr("Fees %1 (%2)").arg(feeCrypto).arg(feeFiat) + endl2 } } else { - const feeEth = rootStore.formatCurrencyAmount(feeEthValue, "ETH") + const feeEth = rootStore.currencyStore.formatCurrencyAmount(feeEthValue, "ETH") const txValue = isMultiTransaction ? root.inTransactionValue : root.transactionValue valuesString += qsTr("Total %1 + %2 (%3)").arg(txValue).arg(feeEth).arg(fiatTransactionValue) + endl2 } @@ -841,12 +841,12 @@ StatusListItem { case Constants.TransactionType.Send: case Constants.TransactionType.Sell: case Constants.TransactionType.Buy: - return "−" + root.rootStore.formatCurrencyAmount(root.fiatValue, root.currentCurrency) + return "−" + root.rootStore.currencyStore.formatCurrencyAmount(root.fiatValue, root.currentCurrency) case Constants.TransactionType.Receive: - return "+" + root.rootStore.formatCurrencyAmount(root.fiatValue, root.currentCurrency) + return "+" + root.rootStore.currencyStore.formatCurrencyAmount(root.fiatValue, root.currentCurrency) case Constants.TransactionType.Swap: - return "-%1 / +%2".arg(root.rootStore.formatCurrencyAmount(root.outFiatValue, root.currentCurrency)) - .arg(root.rootStore.formatCurrencyAmount(root.inFiatValue, root.currentCurrency)) + return "-%1 / +%2".arg(root.rootStore.currencyStore.formatCurrencyAmount(root.outFiatValue, root.currentCurrency)) + .arg(root.rootStore.currencyStore.formatCurrencyAmount(root.inFiatValue, root.currentCurrency)) case Constants.TransactionType.Bridge: case Constants.TransactionType.Approve: default: diff --git a/ui/imports/shared/stores/CurrenciesStore.qml b/ui/imports/shared/stores/CurrenciesStore.qml index 5cea43ecd0..2e4f37efa5 100644 --- a/ui/imports/shared/stores/CurrenciesStore.qml +++ b/ui/imports/shared/stores/CurrenciesStore.qml @@ -67,6 +67,12 @@ QtObject { return parseFloat(amount) } + function getFeeEthValue(feeCurrency) { + if (!feeCurrency || feeCurrency.symbol !== "Gwei") + return 0 + return getGasEthValue(feeCurrency.amount / Math.pow(10, feeCurrency.displayDecimals), 1) + } + function getCurrentCurrencyAmount(amount) { return getCurrencyAmount(amount, currentCurrency) } diff --git a/ui/imports/shared/stores/RootStore.qml b/ui/imports/shared/stores/RootStore.qml index 76b9d3166f..6434f1cc56 100644 --- a/ui/imports/shared/stores/RootStore.qml +++ b/ui/imports/shared/stores/RootStore.qml @@ -64,32 +64,6 @@ QtObject { walletSectionInst.activityController.updateFilter() } - function getCurrencyAmount(amount, symbol) { - return currencyStore.getCurrencyAmount(amount, symbol) - } - - function getFiatValue(balance, cryptoSymbol) { - return currencyStore.getFiatValue(balance, cryptoSymbol) - } - - function getCryptoValue(balance, cryptoSymbol) { - return currencyStore.getCryptoValue(balance, cryptoSymbol) - } - - function getGasEthValue(gweiValue, gasLimit) { - return currencyStore.getGasEthValue(gweiValue, gasLimit) - } - - function getFeeEthValue(feeCurrency) { - if (!feeCurrency || feeCurrency.symbol !== "Gwei") - return 0 - return currencyStore.getGasEthValue(feeCurrency.amount / Math.pow(10, feeCurrency.displayDecimals), 1) - } - - function formatCurrencyAmount(amount, symbol, options = null, locale = null) { - return currencyStore.formatCurrencyAmount(amount, symbol, options, locale) - } - function getHistoricalDataForToken(symbol, currency) { if (Global.appIsReady) walletSectionAllTokens.getHistoricalDataForToken(symbol,currency)