fix(@desktop/wallet): avoid storing CurrencyAmount objects in QML

This commit is contained in:
Dario Gabriel Lipicar 2023-02-17 09:56:31 -03:00 committed by dlipicar
parent f51dba06df
commit ed1a0d04a0
17 changed files with 175 additions and 161 deletions

View File

@ -32,6 +32,8 @@ QtObject:
derivedfrom: string
relatedAccounts: compact_model.Model
ens: string
tmpChainID: int # shouldn't be used anywhere except in prepareCurrencyAmount/getPreparedCurrencyAmount procs
tmpSymbol: string # shouldn't be used anywhere except in prepareCurrencyAmount/getPreparedCurrencyAmount procs
proc setup(self: View) =
self.QObject.setup
@ -242,5 +244,13 @@ QtObject:
#proc getTokenBalanceOnChain*(self: View, chainId: int, tokenSymbol: string): QVariant {.slot.} =
# return newQVariant(self.assets.getTokenBalanceOnChain(chainId, tokenSymbol))
proc getTokenBalanceOnChainAsJson*(self: View, chainId: int, tokenSymbol: string): string {.slot.} =
return $self.assets.getTokenBalanceOnChain(chainId, tokenSymbol).toJsonNode()
# As a workaround, we do it in two steps: First call prepareTokenBalanceOnChain, then getPreparedTokenBalanceOnChain
proc prepareTokenBalanceOnChain*(self: View, chainId: int, tokenSymbol: string) {.slot.} =
self.tmpChainId = chainId
self.tmpSymbol = tokenSymbol
proc getPreparedTokenBalanceOnChain*(self: View): QVariant {.slot.} =
let currencyAmount = self.assets.getTokenBalanceOnChain(self.tmpChainId, self.tmpSymbol)
self.tmpChainId = 0
self.tmpSymbol = "ERROR"
return newQVariant(currencyAmount)

View File

@ -12,6 +12,8 @@ QtObject:
signingPhrase: string
isMnemonicBackedUp: bool
tokensLoading: bool
tmpAmount: float # shouldn't be used anywhere except in prepareCurrencyAmount/getPreparedCurrencyAmount procs
tmpSymbol: string # shouldn't be used anywhere except in prepareCurrencyAmount/getPreparedCurrencyAmount procs
proc setup(self: View) =
self.QObject.setup
@ -90,8 +92,16 @@ QtObject:
# proc getCurrencyAmount*(self: View, amount: float, symbol: string): QVariant {.slot.} =
# return newQVariant(self.delegate.getCurrencyAmount(amount, symbol))
proc getCurrencyAmountAsJson*(self: View, amount: float, symbol: string): string {.slot.} =
return $self.delegate.getCurrencyAmount(amount, symbol).toJsonNode()
# As a workaround, we do it in two steps: First call prepareCurrencyAmount, then getPreparedCurrencyAmount
proc prepareCurrencyAmount*(self: View, amount: float, symbol: string) {.slot.} =
self.tmpAmount = amount
self.tmpSymbol = symbol
proc getPreparedCurrencyAmount*(self: View): QVariant {.slot.} =
let currencyAmount = self.delegate.getCurrencyAmount(self.tmpAmount, self.tmpSymbol)
self.tmpAmount = 0
self.tmpSymbol = "ERROR"
return newQVariant(currencyAmount)
proc setTokensLoading*(self: View, loading: bool) =
self.tokensLoading = loading

View File

@ -59,8 +59,9 @@ Item {
modelData: transaction
isIncoming: d.isIncoming
cryptoValue: root.isTransactionValid ? transaction.value : undefined
fiatValue: root.isTransactionValid ? RootStore.getFiatValue(cryptoValue.amount, symbol, RootStore.currentCurrency): undefined
currentCurrency: RootStore.currentCurrency
cryptoValue: root.isTransactionValid ? transaction.value.amount: 0.0
fiatValue: root.isTransactionValid ? RootStore.getFiatValue(cryptoValue, symbol, currentCurrency): 0.0
networkIcon: root.isTransactionValid ? RootStore.getNetworkIcon(transaction.chainId): ""
networkColor: root.isTransactionValid ? RootStore.getNetworkColor(transaction.chainId): ""
networkName: root.isTransactionValid ? RootStore.getNetworkShortName(transaction.chainId): ""
@ -68,8 +69,8 @@ Item {
transferStatus: root.isTransactionValid ? RootStore.hex2Dec(transaction.txStatus): ""
shortTimeStamp: root.isTransactionValid ? LocaleUtils.formatTime(transaction.timestamp * 1000, Locale.ShortFormat): ""
savedAddressName: root.isTransactionValid ? RootStore.getNameForSavedWalletAddress(transaction.to): ""
title: d.isIncoming ? qsTr("Received %1 from %2").arg(LocaleUtils.currencyAmountToLocaleString(cryptoValue)).arg(d.from) :
qsTr("Sent %1 to %2").arg(LocaleUtils.currencyAmountToLocaleString(cryptoValue)).arg(d.to)
title: d.isIncoming ? qsTr("Received %1 from %2").arg(RootStore.formatCurrencyAmount(cryptoValue, symbol)).arg(d.from) :
qsTr("Sent %1 to %2").arg(RootStore.formatCurrencyAmount(cryptoValue, symbol)).arg(d.to)
sensor.enabled: false
color: Theme.palette.statusListItem.backgroundColor
state: "big"
@ -137,8 +138,9 @@ Item {
width: parent.width
modelData: transaction
isIncoming: d.isIncoming
cryptoValue: root.isTransactionValid ? transaction.value: undefined
fiatValue: root.isTransactionValid ? RootStore.getFiatValue(cryptoValue.amount, symbol, RootStore.currentCurrency): undefined
currentCurrency: RootStore.currentCurrency
cryptoValue: root.isTransactionValid ? transaction.value.amount: 0.0
fiatValue: root.isTransactionValid ? RootStore.getFiatValue(cryptoValue, symbol, currentCurrency): 0.0
networkIcon: root.isTransactionValid ? RootStore.getNetworkIcon(transaction.chainId) : ""
networkColor: root.isTransactionValid ? RootStore.getNetworkColor(transaction.chainId): ""
networkName: root.isTransactionValid ? RootStore.getNetworkShortName(transaction.chainId): ""
@ -146,8 +148,8 @@ Item {
transferStatus: root.isTransactionValid ? RootStore.hex2Dec(transaction.txStatus): ""
shortTimeStamp: root.isTransactionValid ? LocaleUtils.formatTime(transaction.timestamp * 1000, Locale.ShortFormat): ""
savedAddressName: root.isTransactionValid ? RootStore.getNameForSavedWalletAddress(transaction.to): ""
title: d.isIncoming ? qsTr("Received %1 from %2").arg(LocaleUtils.currencyAmountToLocaleString(cryptoValue)).arg(d.from) :
qsTr("Sent %1 to %2").arg(LocaleUtils.currencyAmountToLocaleString(cryptoValue)).arg(d.to)
title: d.isIncoming ? qsTr("Received %1 from %2").arg(RootStore.formatCurrencyAmount(cryptoValue, symbol)).arg(d.from) :
qsTr("Sent %1 to %2").arg(RootStore.formatCurrencyAmount(cryptoValue, symbol)).arg(d.to)
sensor.enabled: false
color: Theme.palette.statusListItem.backgroundColor
border.width: 1

View File

@ -20,7 +20,7 @@ Item {
property var bestRoutes: []
property var getGasEthValue: function () {}
property var getFiatValue: function () {}
property var getCurrencyAmount: function () {}
property var formatCurrencyAmount: function () {}
width: parent.width
height: visible ? advancedGasSelector.height + Style.current.halfPadding : 0
@ -48,20 +48,20 @@ Item {
statusListItemIcon.active: true
statusListItemIcon.opacity: modelData.isFirstSimpleTx
title: qsTr("%1 transaction fee").arg(modelData.fromNetwork.chainName)
subTitle: LocaleUtils.currencyAmountToLocaleString(totalGasAmountEth)
property var totalGasAmountEth: {
subTitle: root.formatCurrencyAmount(totalGasAmountEth, "ETH")
property double totalGasAmountEth: {
let maxFees = modelData.gasFees.maxFeePerGasM
let gasPrice = modelData.gasFees.eip1559Enabled ? maxFees : modelData.gasFees.gasPrice
return root.getGasEthValue(gasPrice , modelData.gasAmount)
}
property var totalGasAmountFiat: root.getFiatValue(totalGasAmountEth.amount, "ETH", root.currentCurrency)
property double totalGasAmountFiat: root.getFiatValue(totalGasAmountEth, "ETH", root.currentCurrency)
statusListItemSubTitle.width: listItem.width/2 - Style.current.smallPadding
statusListItemSubTitle.elide: Text.ElideMiddle
statusListItemSubTitle.wrapMode: Text.NoWrap
components: [
StatusBaseText {
Layout.alignment: Qt.AlignRight
text: LocaleUtils.currencyAmountToLocaleString(totalGasAmountFiat)
text: root.formatCurrencyAmount(totalGasAmountFiat, root.currentCurrency)
font.pixelSize: 15
color: Theme.palette.baseColor1
width: listItem.width/2 - Style.current.padding
@ -83,9 +83,10 @@ Item {
statusListItemIcon.active: true
statusListItemIcon.opacity: modelData.isFirstSimpleTx
title: qsTr("Approve %1 %2 Bridge").arg(modelData.fromNetwork.chainName).arg(root.selectedTokenSymbol)
property var approvalGasFees: root.getCurrencyAmount(modelData.approvalGasFees, "ETH")
property var approvalGasFeesFiat: root.getFiatValue(approvalGasFees.amount, "ETH", root.currentCurrency)
subTitle: LocaleUtils.currencyAmountToLocaleString(approvalGasFees)
property double approvalGasFees: modelData.approvalGasFees
property string approvalGasFeesSymbol: "ETH"
property double approvalGasFeesFiat: root.getFiatValue(approvalGasFees, approvalGasFeesSymbol, root.currentCurrency)
subTitle: root.formatCurrencyAmount(approvalGasFees, approvalGasFeesSymbol)
statusListItemSubTitle.width: listItem.width/2 - Style.current.smallPadding
statusListItemSubTitle.elide: Text.ElideMiddle
statusListItemSubTitle.wrapMode: Text.NoWrap
@ -93,7 +94,7 @@ Item {
components: [
StatusBaseText {
Layout.alignment: Qt.AlignRight
text: LocaleUtils.currencyAmountToLocaleString(approvalGasFeesFiat)
text: root.formatCurrencyAmount(approvalGasFeesFiat, root.currentCurrency)
font.pixelSize: 15
color: Theme.palette.baseColor1
width: listItem.width/2 - Style.current.padding
@ -116,16 +117,16 @@ Item {
statusListItemIcon.active: true
statusListItemIcon.opacity: modelData.isFirstBridgeTx
title: qsTr("%1 -> %2 bridge").arg(modelData.fromNetwork.chainName).arg(modelData.toNetwork.chainName)
property var tokenFees: root.getCurrencyAmount(modelData.tokenFees, root.selectedTokenSymbol)
property var tokenFeesFiat: root.getFiatValue(tokenFees.amount, root.selectedTokenSymbol, root.currentCurrency)
subTitle: LocaleUtils.currencyAmountToLocaleString(tokenFees)
property double tokenFees: modelData.tokenFees
property double tokenFeesFiat: root.getFiatValue(tokenFees, root.selectedTokenSymbol, root.currentCurrency)
subTitle: root.formatCurrencyAmount(tokenFees, root.selectedTokenSymbol)
visible: modelData.bridgeName !== "Simple"
statusListItemSubTitle.width: 100
statusListItemSubTitle.elide: Text.ElideMiddle
components: [
StatusBaseText {
Layout.alignment: Qt.AlignRight
text: LocaleUtils.currencyAmountToLocaleString(tokenFeesFiat)
text: root.formatCurrencyAmount(tokenFeesFiat, root.currentCurrency)
font.pixelSize: 15
color: Theme.palette.baseColor1
width: listItem2.width/2 - Style.current.padding

View File

@ -8,6 +8,7 @@ import StatusQ.Controls 0.1
import utils 1.0
import shared 1.0
import shared.stores 1.0
StatusListItem {
id: root
@ -18,9 +19,10 @@ StatusListItem {
property var modelData
property string symbol
property bool isIncoming
property string currentCurrency
property int transferStatus
property var cryptoValue
property var fiatValue
property double cryptoValue
property double fiatValue
property string networkIcon
property string networkColor
property string networkName
@ -73,8 +75,8 @@ StatusListItem {
}
StatusTextWithLoadingState {
id: cryptoValueText
text: LocaleUtils.currencyAmountToLocaleString(cryptoValue)
customColor: Theme.palette.directColor1
text: RootStore.formatCurrencyAmount(cryptoValue, root.symbol)
color: Theme.palette.directColor1
loading: root.loading
}
@ -82,7 +84,7 @@ StatusListItem {
StatusTextWithLoadingState {
id: fiatValueText
Layout.alignment: Qt.AlignRight
text: LocaleUtils.currencyAmountToLocaleString(fiatValue)
text: RootStore.formatCurrencyAmount(fiatValue, root.currentCurrency)
font.pixelSize: 15
customColor: Theme.palette.baseColor1
loading: root.loading

View File

@ -60,8 +60,8 @@ StatusDialog {
popup.store.authenticateAndTransfer(
popup.selectedAccount.address,
recipientAddress,
assetSelector.selectedAsset.symbol,
amountToSendInput.cryptoValueToSend.amount,
d.selectedSymbol,
amountToSendInput.cryptoValueToSend,
d.uuid,
JSON.stringify(popup.bestRoutes)
)
@ -70,7 +70,7 @@ StatusDialog {
property var recalculateRoutesAndFees: Backpressure.debounce(popup, 600, function() {
if(!!popup.selectedAccount && !!assetSelector.selectedAsset && d.recipientReady && amountToSendInput.input.valid) {
popup.isLoading = true
let amount = Math.round(amountToSendInput.cryptoValueToSend.amount * Math.pow(10, assetSelector.selectedAsset.decimals))
let amount = Math.round(amountToSendInput.cryptoValueToSend * Math.pow(10, assetSelector.selectedAsset.decimals))
popup.store.suggestedRoutes(popup.selectedAccount.address, amount.toString(16), assetSelector.selectedAsset.symbol,
store.disabledChainIdsFromList, store.disabledChainIdsToList,
store.preferredChainIds, popup.sendType, store.lockedInAmounts)
@ -82,9 +82,11 @@ StatusDialog {
readonly property int errorType: !amountToSendInput.input.valid ? Constants.SendAmountExceedsBalance :
(networkSelector.bestRoutes && networkSelector.bestRoutes.length <= 0 && !!amountToSendInput.input.text && recipientReady && !popup.isLoading) ?
Constants.NoRoute : Constants.NoError
readonly property var maxFiatBalance: !!assetSelector.selectedAsset ? (amountToSendInput.inputIsFiat ?
assetSelector.selectedAsset.totalCurrencyBalance :
assetSelector.selectedAsset.totalBalance): undefined
readonly property double maxFiatBalance: !!assetSelector.selectedAsset ? assetSelector.selectedAsset.totalCurrencyBalance.amount : 0
readonly property double maxCryptoBalance: !!assetSelector.selectedAsset ? assetSelector.selectedAsset.totalBalance.amount : 0
readonly property double maxInputBalance: amountToSendInput.inputIsFiat ? maxFiatBalance : maxCryptoBalance
readonly property string selectedSymbol: !!assetSelector.selectedAsset ? assetSelector.selectedAsset.symbol : ""
readonly property string inputSymbol: amountToSendInput.inputIsFiat ? popup.store.currentCurrency : selectedSymbol
readonly property bool errorMode: popup.isLoading || !recipientReady ? false : errorType !== Constants.NoError || networkSelector.errorMode || isNaN(amountToSendInput.input.text)
readonly property bool recipientReady: (isAddressValid || isENSValid) && !recipientSelector.isPending
property bool isAddressValid: Utils.isValidAddress(popup.addressText)
@ -96,9 +98,9 @@ StatusDialog {
readonly property string uuid: Utils.uuid()
property bool isPendingTx: false
property string totalTimeEstimate
property var totalFeesInEth
property var totalFeesInFiat
property var totalAmountToReceive
property double totalFeesInEth
property double totalFeesInFiat
property double totalAmountToReceive
property Timer waitTimer: Timer {
interval: 1500
@ -252,9 +254,9 @@ StatusDialog {
Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
Layout.preferredHeight: 22
title: {
if (!d.maxFiatBalance || d.maxFiatBalance.amount <= 0)
if (d.maxInputBalance <= 0)
return qsTr("No balances active")
const balance = LocaleUtils.currencyAmountToLocaleString(d.maxFiatBalance)
const balance = popup.currencyStore.formatCurrencyAmount(d.maxInputBalance, d.inputSymbol)
return qsTr("Max: %1").arg(balance)
}
closeButtonVisible: false
@ -269,21 +271,16 @@ StatusDialog {
Layout.fillWidth:true
isBridgeTx: popup.isBridgeTx
interactive: popup.interactive
selectedAsset: assetSelector.selectedAsset
maxFiatBalance: d.maxFiatBalance
selectedSymbol: d.selectedSymbol
maxInputBalance: d.maxInputBalance
currentCurrency: popup.store.currentCurrency
getFiatValue: function(cryptoValue) {
return selectedAsset ? popup.currencyStore.getFiatValue(cryptoValue, selectedAsset.symbol, currentCurrency) : undefined
return selectedSymbol ? popup.currencyStore.getFiatValue(cryptoValue, selectedSymbol, currentCurrency) : 0.0
}
getCryptoValue: function(fiatValue) {
return selectedAsset ? popup.currencyStore.getCryptoValue(fiatValue, selectedAsset.symbol, currentCurrency) : undefined
}
getFiatCurrencyAmount: function(fiatValue) {
return popup.currencyStore.getCurrencyAmount(fiatValue, currentCurrency)
}
getCryptoCurrencyAmount: function(cryptoValue) {
return selectedAsset ? popup.currencyStore.getCurrencyAmount(cryptoValue, selectedAsset.symbol) : undefined
return selectedSymbol ? popup.currencyStore.getCryptoValue(fiatValue, selectedSymbol, currentCurrency) : 0.0
}
formatCurrencyAmount: popup.currencyStore.formatCurrencyAmount
onReCalculateSuggestedRoute: popup.recalculateRoutesAndFees()
}
@ -297,14 +294,15 @@ StatusDialog {
visible: popup.bestRoutes !== undefined && popup.bestRoutes.length > 0 && !!amountToSendInput.input.text && amountToSendInput.input.valid
store: popup.store
isLoading: popup.isLoading
selectedAsset: assetSelector.selectedAsset
selectedSymbol: d.selectedSymbol
isBridgeTx: popup.isBridgeTx
cryptoValueToReceive: d.totalAmountToReceive
inputIsFiat: amountToSendInput.inputIsFiat
currentCurrency: popup.store.currentCurrency
getFiatValue: function(cryptoValue) {
return popup.currencyStore.getFiatValue(cryptoValue, selectedAsset.symbol, currentCurrency)
return popup.currencyStore.getFiatValue(cryptoValue, selectedSymbol, currentCurrency)
}
formatCurrencyAmount: popup.currencyStore.formatCurrencyAmount
}
}
TokenListView {
@ -465,7 +463,7 @@ StatusDialog {
anchors.leftMargin: Style.current.bigPadding
anchors.rightMargin: Style.current.bigPadding
visible: d.recipientReady && !!assetSelector.selectedAsset && networkSelector.advancedOrCustomMode && !!amountToSendInput.input.text
selectedTokenSymbol: assetSelector.selectedAsset ? assetSelector.selectedAsset.symbol: ""
selectedTokenSymbol: d.selectedSymbol
isLoading: popup.isLoading
bestRoutes: popup.bestRoutes
store: popup.store
@ -479,10 +477,10 @@ StatusDialog {
footer: SendModalFooter {
nextButtonText: popup.isBridgeTx ? qsTr("Bridge") : qsTr("Send")
maxFiatFees: popup.isLoading ? "..." : LocaleUtils.currencyAmountToLocaleString(d.totalFeesInFiat)
maxFiatFees: popup.isLoading ? "..." : popup.currencyStore.formatCurrencyAmount(d.totalFeesInFiat, popup.store.currentCurrency)
totalTimeEstimate: popup.isLoading? "..." : d.totalTimeEstimate
pending: d.isPendingTx || popup.isLoading
visible: d.recipientReady && !!amountToSendInput.cryptoValueToSend && amountToSendInput.cryptoValueToSend.amount > 0 && !d.errorMode && !!amountToSendInput.input.text && amountToSendInput.input.valid
visible: d.recipientReady && amountToSendInput.cryptoValueToSend > 0 && !d.errorMode && !!amountToSendInput.input.text && amountToSendInput.input.valid
onNextButtonClicked: popup.sendTransaction()
}
@ -502,11 +500,10 @@ StatusDialog {
popup.bestRoutes = response.suggestedRoutes.best
let gasTimeEstimate = response.suggestedRoutes.gasTimeEstimate
d.totalTimeEstimate = popup.store.getLabelForEstimatedTxTime(gasTimeEstimate.totalTime)
d.totalFeesInEth = popup.currencyStore.getCurrencyAmount(gasTimeEstimate.totalFeesInEth, "ETH")
let totalFeesInFiat = popup.currencyStore.getFiatValue( gasTimeEstimate.totalFeesInEth, "ETH", popup.store.currentCurrency).amount +
popup.currencyStore.getFiatValue(gasTimeEstimate.totalTokenFees, fees.selectedTokenSymbol, popup.store.currentCurrency).amount
d.totalFeesInFiat = popup.currencyStore.getCurrencyAmount(totalFeesInFiat, popup.store.currentCurrency)
d.totalAmountToReceive = popup.currencyStore.getCurrencyAmount(popup.store.getWei2Eth(response.suggestedRoutes.amountToReceive, assetSelector.selectedAsset.decimals), fees.selectedTokenSymbol)
d.totalFeesInEth = gasTimeEstimate.totalFeesInEth
d.totalFeesInFiat = popup.currencyStore.getFiatValue( gasTimeEstimate.totalFeesInEth, "ETH", popup.store.currentCurrency) +
popup.currencyStore.getFiatValue(gasTimeEstimate.totalTokenFees, fees.selectedTokenSymbol, popup.store.currentCurrency)
d.totalAmountToReceive = popup.store.getWei2Eth(response.suggestedRoutes.amountToReceive, assetSelector.selectedAsset.decimals)
networkSelector.toNetworksList = response.suggestedRoutes.toNetworks
popup.isLoading = false
}

View File

@ -972,34 +972,31 @@ QtObject {
walletSection.updateCurrency(shortName)
}
// The object returned by this sometimes becomes null when used as part of a binding expression.
// Will probably be solved when moving to C++, for now avoid storing the result of this function and use
// formatCurrencyAmount at the visualization point instead, or move functionality over to the NIM side.
function getCurrencyAmount(amount, symbol) {
if (isNaN(amount)) {
amount = 0
}
let obj = JSON.parse((walletSection.getCurrencyAmountAsJson(amount, symbol)))
if (obj.error) {
console.error("Error parsing currency amount json object, amount: ", amount, ", symbol ", symbol, " error: ", obj.error)
return {amount: nan, symbol: symbol}
}
return obj
walletSection.prepareCurrencyAmount(amount, symbol)
return walletSection.getPreparedCurrencyAmount()
}
function getFiatValue(balance, cryptoSymbol, fiatSymbol) {
var amount = profileSectionStore.profileSectionModuleInst.ensUsernamesModule.getFiatValue(balance, cryptoSymbol, fiatSymbol)
return getCurrencyAmount(parseFloat(amount), fiatSymbol)
function formatCurrencyAmount(amount, symbol, options = null, locale = null) {
var currencyAmount = getCurrencyAmount(amount, symbol)
return LocaleUtils.currencyAmountToLocaleString(currencyAmount, options, locale)
}
function getCryptoValue(balance, cryptoSymbol, fiatSymbol) {
var amount = profileSectionStore.profileSectionModuleInst.ensUsernamesModule.getCryptoValue(balance, cryptoSymbol, fiatSymbol)
return getCurrencyAmount(parseFloat(amount), cryptoSymbol)
function getFiatValue(cryptoAmount, cryptoSymbol, fiatSymbol) {
var amount = profileSectionStore.profileSectionModuleInst.ensUsernamesModule.getFiatValue(cryptoAmount, cryptoSymbol, fiatSymbol)
return parseFloat(amount)
}
function getCryptoValue(fiatAmount, cryptoSymbol, fiatSymbol) {
var amount = profileSectionStore.profileSectionModuleInst.ensUsernamesModule.getCryptoValue(fiatAmount, cryptoSymbol, fiatSymbol)
return parseFloat(amount)
}
function getGasEthValue(gweiValue, gasLimit) {
var amount = profileSectionStore.profileSectionModuleInst.ensUsernamesModule.getGasEthValue(gweiValue, gasLimit)
return getCurrencyAmount(parseFloat(amount), "ETH")
}
function formatCurrencyAmount(currencyAmount) {
return LocaleUtils.currencyAmountToLocaleString(currencyAmount)
return parseFloat(amount)
}
}

View File

@ -221,8 +221,8 @@ QtObject {
return currencyStore.getGasEthValue(gweiValue, gasLimit)
}
function formatCurrencyAmount(currencyAmount) {
return currencyStore.formatCurrencyAmount(currencyAmount)
function formatCurrencyAmount(amount, symbol, options = null, locale = null) {
return currencyStore.formatCurrencyAmount(amount, symbol, options, locale)
}
function getHistoricalDataForToken(symbol, currency) {

View File

@ -256,6 +256,7 @@ QtObject {
return undefined
}
return JSON.parse(selectedAccount.getTokenBalanceOnChainAsJson(chainId, tokenSymbol))
selectedAccount.prepareTokenBalanceOnChain(chainId, tokenSymbol)
return selectedAccount.getPreparedTokenBalanceOnChain()
}
}

View File

@ -11,26 +11,27 @@ ColumnLayout {
id: root
property var store
property var selectedAsset
property string selectedSymbol
property bool isLoading: false
property var cryptoValueToReceive
property double cryptoValueToReceive
property bool isBridgeTx: false
property bool inputIsFiat: false
property string currentCurrency
property var getFiatValue: function(cryptoValue) {}
property var formatCurrencyAmount: function() {}
QtObject {
id: d
readonly property string fiatValue: {
if(!root.selectedAsset || !cryptoValueToReceive)
if(!root.selectedSymbol || !cryptoValueToReceive)
return LocaleUtils.numberToLocaleString(0, 2)
let fiatValue = root.getFiatValue(cryptoValueToReceive.amount, root.selectedAsset.symbol, RootStore.currentCurrency)
return LocaleUtils.currencyAmountToLocaleString(fiatValue)
let fiatValue = root.getFiatValue(cryptoValueToReceive, root.selectedSymbol, root.currentCurrency)
return root.formatCurrencyAmount(fiatValue, root.currentCurrency)
}
readonly property string cryptoValue: {
if(!root.selectedAsset || !cryptoValueToReceive)
if(!root.selectedSymbol || !cryptoValueToReceive)
return LocaleUtils.numberToLocaleString(0, 2)
return LocaleUtils.currencyAmountToLocaleString(cryptoValueToReceive)
return root.formatCurrencyAmount(cryptoValueToReceive, root.selectedSymbol)
}
}

View File

@ -13,19 +13,20 @@ ColumnLayout {
id: root
property alias input: topAmountToSendInput
readonly property double inputNumber: topAmountToSendInput.text ? LocaleUtils.numberFromLocaleString(topAmountToSendInput.text) : 0
property var selectedAsset
property string selectedSymbol
property bool isBridgeTx: false
property bool interactive: false
property var maxFiatBalance
property double maxInputBalance
property bool inputIsFiat: false
property var cryptoValueToSend
property double cryptoValueToSend
Binding {
target: root
property: "cryptoValueToSend"
value: {
const value = !inputIsFiat ? getCryptoCurrencyAmount(LocaleUtils.numberFromLocaleString(topAmountToSendInput.text)) : getCryptoValue(fiatValueToSend ? fiatValueToSend.amount : 0.0)
return root.selectedAsset, value
const value = !inputIsFiat ? inputNumber : getCryptoValue(fiatValueToSend)
return root.selectedSymbol, value
}
delayed: true
}
@ -34,16 +35,15 @@ ColumnLayout {
target: root
property: "fiatValueToSend"
value: {
const value = inputIsFiat ? getFiatCurrencyAmount(LocaleUtils.numberFromLocaleString(topAmountToSendInput.text)) : getFiatValue(cryptoValueToSend ? cryptoValueToSend.amount : 0.0)
return root.selectedAsset, value
const value = inputIsFiat ? inputNumber : getFiatValue(cryptoValueToSend)
return root.selectedSymbol, value
}
delayed: true
}
property string currentCurrency
property var getFiatValue: function(cryptoValue) {}
property var getCryptoValue: function(fiatValue) {}
property var getFiatCurrencyAmount: function(fiatValue) {}
property var getCryptoCurrencyAmount: function(cryptoValue) {}
property var formatCurrencyAmount: function() {}
signal reCalculateSuggestedRoute()
@ -54,17 +54,10 @@ ColumnLayout {
interval: 1000
onTriggered: reCalculateSuggestedRoute()
}
function formatValue(value) {
if (!value) {
return zeroString
}
return LocaleUtils.currencyAmountToLocaleString(value)
}
}
onMaxFiatBalanceChanged: {
floatValidator.top = maxFiatBalance ? maxFiatBalance.amount : 0.0
onMaxInputBalanceChanged: {
floatValidator.top = maxInputBalance
input.validate()
}
@ -78,7 +71,8 @@ ColumnLayout {
}
RowLayout {
id: topItem
property var topAmountToSend: !inputIsFiat ? cryptoValueToSend : fiatValueToSend
property double topAmountToSend: !inputIsFiat ? cryptoValueToSend : fiatValueToSend
property string topAmountSymbol: !inputIsFiat ? root.selectedSymbol : root.currentCurrency
Layout.alignment: Qt.AlignLeft
AmountInputWithCursor {
id: topAmountToSendInput
@ -92,7 +86,7 @@ ColumnLayout {
StatusFloatValidator {
id: floatValidator
bottom: 0
top: root.maxFiatBalance.amount
top: root.maxInputBalance
errorMessage: ""
}
]
@ -112,7 +106,8 @@ ColumnLayout {
}
Item {
id: bottomItem
property var bottomAmountToSend: inputIsFiat ? cryptoValueToSend : fiatValueToSend
property double bottomAmountToSend: inputIsFiat ? cryptoValueToSend : fiatValueToSend
property string bottomAmountSymbol: inputIsFiat ? selectedSymbol : currentCurrency
Layout.alignment: Qt.AlignLeft | Qt.AlignBottom
Layout.preferredWidth: txtBottom.width
Layout.preferredHeight: txtBottom.height
@ -120,7 +115,7 @@ ColumnLayout {
id: txtBottom
anchors.top: parent.top
anchors.left: parent.left
text: d.formatValue(bottomItem.bottomAmountToSend)
text: root.formatCurrencyAmount(bottomItem.bottomAmountToSend, bottomItem.bottomAmountSymbol)
font.pixelSize: 13
color: Theme.palette.directColor5
}
@ -130,7 +125,7 @@ ColumnLayout {
onClicked: {
topAmountToSendInput.validate()
if(!!topAmountToSendInput.text) {
topAmountToSendInput.text = LocaleUtils.currencyAmountToLocaleString(bottomItem.bottomAmountToSend, {onlyAmount: true})
topAmountToSendInput.text = root.formatCurrencyAmount(bottomItem.bottomAmountToSend, bottomItem.bottomAmountSymbol, {onlyAmount: true})
}
inputIsFiat = !inputIsFiat
d.waitTimer.restart()

View File

@ -19,7 +19,7 @@ Rectangle {
property var bestRoutes
property var store
property var currencyStore: store.currencyStore
property var selectedTokenSymbol
property string selectedTokenSymbol
property int errorType: Constants.NoError
radius: 13
@ -60,7 +60,7 @@ Rectangle {
anchors.right: parent.right
anchors.rightMargin: Style.current.padding
id: totalFeesAdvanced
text: root.isLoading ? "..." : LocaleUtils.currencyAmountToLocaleString(root.gasFiatAmount)
text: root.isLoading ? "..." : root.currencyStore.formatCurrencyAmount(root.gasFiatAmount, root.currencyStore.currentCurrency)
font.pixelSize: 15
color: Theme.palette.directColor1
visible: !!root.bestRoutes && root.bestRoutes !== undefined && root.bestRoutes.length > 0
@ -71,7 +71,7 @@ Rectangle {
width: parent.width
getGasEthValue: root.currencyStore.getGasEthValue
getFiatValue: root.currencyStore.getFiatValue
getCurrencyAmount: root.currencyStore.getCurrencyAmount
formatCurrencyAmount: root.currencyStore.formatCurrencyAmount
currentCurrency: root.currencyStore.currentCurrency
visible: root.errorType === Constants.NoError && !root.isLoading
bestRoutes: root.bestRoutes

View File

@ -101,8 +101,9 @@ ColumnLayout {
TransactionDelegate {
property bool modelDataValid: !!modelData
isIncoming: modelDataValid ? modelData.to === account.address: false
cryptoValue: modelDataValid ? modelData.value : undefined
fiatValue: modelDataValid && !!cryptoValue ? RootStore.getFiatValue(cryptoValue.amount, symbol, RootStore.currentCurrency) : undefined
currentCurrency: RootStore.currentCurrency
cryptoValue: modelDataValid ? modelData.value.amount : 0.0
fiatValue: modelDataValid ? RootStore.getFiatValue(cryptoValue, symbol, currentCurrency): 0.0
networkIcon: modelDataValid ? RootStore.getNetworkIcon(modelData.chainId) : ""
networkColor: modelDataValid ? RootStore.getNetworkColor(modelData.chainId) : ""
networkName: modelDataValid ? RootStore.getNetworkShortName(modelData.chainId) : ""

View File

@ -1,4 +1,4 @@
import QtQuick 2.13
import QtQuick 2.13
import QtQuick.Layouts 1.13
import utils 1.0
@ -16,20 +16,21 @@ Item {
id: root
property var store
readonly property var currencyStore: store.currencyStore
property var bestRoutes
property var selectedAccount
property string ensAddressOrEmpty: ""
property var selectedAsset
readonly property string selectedSymbol: !!selectedAsset ? selectedAsset.symbol : ""
property var allNetworks
property bool customMode: false
property var amountToSend
property var requiredGasInEth
property bool errorMode: !root.amountToSend || d.customAmountToSend > root.amountToSend.amount
property double amountToSend
property double requiredGasInEth
property bool errorMode: d.customAmountToSend > root.amountToSend
property bool interactive: true
property bool showPreferredChains: false
property var weiToEth: function(wei) {}
property var reCalculateSuggestedRoute: function() {}
property var getCryptoCurrencyAmount: function(cryptoValue) {}
property int errorType: Constants.NoError
property bool isLoading
@ -52,7 +53,7 @@ Item {
d.customAmountToSend = 0
for(var i = 0; i<fromNetworksRepeater.count; i++) {
if(fromNetworksRepeater.itemAt(i).locked) {
let amountEntered = fromNetworksRepeater.itemAt(i).advancedInputCurrencyAmount.amount
let amountEntered = fromNetworksRepeater.itemAt(i).advancedInputCurrencyAmount
d.customAmountToSend += isNaN(amountEntered) ? 0 : amountEntered
}
}
@ -94,27 +95,27 @@ Item {
property double amountToSend: 0
property int routeOnNetwork: 0
property bool selectedAssetValid: selectedAccount && selectedAccount !== undefined && selectedAsset !== undefined
property var tokenBalanceOnChain: selectedAssetValid ? root.store.getTokenBalanceOnChain(selectedAccount, model.chainId, selectedAsset.symbol) : undefined
property bool hasGas: selectedAssetValid && requiredGasInEth ? selectedAccount.hasGas(model.chainId, model.nativeCurrencySymbol, requiredGasInEth.amount) : false
property var advancedInputCurrencyAmount: selectedAssetValid ? root.getCryptoCurrencyAmount(LocaleUtils.numberFromLocaleString(advancedInputText)) : undefined
property double tokenBalanceOnChain: selectedAssetValid ? root.store.getTokenBalanceOnChain(selectedAccount, model.chainId, root.selectedSymbol).amount : 0.0
property bool hasGas: selectedAssetValid && requiredGasInEth !== undefined ? selectedAccount.hasGas(model.chainId, model.nativeCurrencySymbol, requiredGasInEth) : false
property double advancedInputCurrencyAmount: selectedAssetValid && advancedInput.valid ? LocaleUtils.numberFromLocaleString(advancedInputText) : 0.0
primaryText: model.chainName
secondaryText: !tokenBalanceOnChain || (tokenBalanceOnChain.amount === 0 && root.amountToSend && root.amountToSend.amount !== 0) ?
qsTr("No Balance") : !hasGas ? qsTr("No Gas") : advancedInputCurrencyAmount ? LocaleUtils.currencyAmountToLocaleString(advancedInputCurrencyAmount) : "N/A"
tertiaryText: root.errorMode && advancedInputCurrencyAmount && advancedInputCurrencyAmount.amount !== 0 && advancedInput.valid ? qsTr("EXCEEDS SEND AMOUNT"): qsTr("BALANCE: ") + LocaleUtils.currencyAmountToLocaleString(tokenBalanceOnChain)
secondaryText: (tokenBalanceOnChain == 0 && root.amountToSend > 0) ?
qsTr("No Balance") : !hasGas ? qsTr("No Gas") : root.currencyStore.formatCurrencyAmount(advancedInputCurrencyAmount, root.selectedSymbol)
tertiaryText: root.errorMode && advancedInputCurrencyAmount > 0 ? qsTr("EXCEEDS SEND AMOUNT"): qsTr("BALANCE: ") + root.currencyStore.formatCurrencyAmount(tokenBalanceOnChain, root.selectedSymbol)
locked: store.lockedInAmounts.findIndex(lockedItem => lockedItem !== undefined && lockedItem.chainID === model.chainId) !== -1
preCalculatedAdvancedText: {
let index = store.lockedInAmounts.findIndex(lockedItem => lockedItem!== undefined && lockedItem.chainID === model.chainId)
if(locked && index !== -1) {
let amount = root.weiToEth(parseInt(store.lockedInAmounts[index].value, 16))
return LocaleUtils.numberToLocaleString(amount.amount)
return LocaleUtils.numberToLocaleString(amount)
}
else return LocaleUtils.numberToLocaleString(fromNetwork.amountToSend)
}
maxAdvancedValue: tokenBalanceOnChain ? tokenBalanceOnChain.amount : 0.0
state: !tokenBalanceOnChain || tokenBalanceOnChain.amount === 0 || !hasGas ?
maxAdvancedValue: tokenBalanceOnChain
state: tokenBalanceOnChain === 0 || !hasGas ?
"unavailable" :
(root.errorMode || !advancedInput.valid) && (advancedInputCurrencyAmount.amount !== 0) ? "error" : "default"
(root.errorMode || !advancedInput.valid) && advancedInputCurrencyAmount > 0 ? "error" : "default"
cardIcon.source: Style.svg(model.iconUrl)
disabledText: qsTr("Disabled")
disableText: qsTr("Disable")
@ -134,9 +135,9 @@ Item {
}
onCardLocked: {
store.addLockedInAmount(model.chainId, advancedInputCurrencyAmount.amount, root.selectedAsset.decimals, isLocked)
store.addLockedInAmount(model.chainId, advancedInputCurrencyAmount, root.selectedAsset.decimals, isLocked)
d.calculateCustomAmounts()
if(!locked || (d.customAmountToSend <= root.amountToSend.amount && advancedInput.valid))
if(!locked || (d.customAmountToSend <= root.amountToSend && advancedInput.valid))
root.reCalculateSuggestedRoute()
}
}
@ -145,7 +146,7 @@ Item {
BalanceExceeded {
Layout.fillWidth: true
Layout.alignment: Qt.AlignVCenter
amountToSend: root.amountToSend ? root.amountToSend.amount : 0.0
amountToSend: root.amountToSend
errorType: root.errorType
visible: root.errorType === Constants.NoRoute
}
@ -187,11 +188,10 @@ Item {
property int routeOnNetwork: 0
property int bentLine: 0
property double amountToReceive: 0
property var currencyAmountToReceive: root.getCryptoCurrencyAmount(amountToReceive)
property var preferredChains: store.preferredChainIds
property bool preferred: store.preferredChainIds.includes(model.chainId)
primaryText: model.chainName
secondaryText: LocaleUtils.currencyAmountToLocaleString(currencyAmountToReceive)
secondaryText: root.currencyStore.formatCurrencyAmount(amountToReceive, root.selectedSymbol)
tertiaryText: state === "unpreferred" ? qsTr("UNPREFERRED") : ""
state: !preferred ? "unpreferred" : "default"
opacity: preferred || showPreferredChains ? 1 : 0
@ -276,10 +276,10 @@ Item {
yOffsetFrom = toN.objectName === fromN.objectName && toN.routeOnNetwork !== 0 ? toN.routeOnNetwork * 16 : 0
yOffsetTo = toN.routeOnNetwork * 16
xOffset = (fromN.y - toN.y > 0 ? -1 : 1) * toN.bentLine * 16
let amountToSend = weiToEth(bestRoutes[i].amountIn)
let amountToReceive = weiToEth(bestRoutes[i].amountOut)
fromN.amountToSend = amountToSend.amount
toN.amountToReceive += amountToReceive.amount
let amountToSend = root.weiToEth(bestRoutes[i].amountIn)
let amountToReceive = root.weiToEth(bestRoutes[i].amountOut)
fromN.amountToSend = amountToSend
toN.amountToReceive += amountToReceive
fromN.routeOnNetwork += 1
toN.routeOnNetwork += 1
toN.bentLine = toN.objectName !== fromN.objectName

View File

@ -1,4 +1,4 @@
import QtQuick 2.13
import QtQuick 2.13
import QtQuick.Layouts 1.13
import utils 1.0
@ -87,8 +87,9 @@ Item {
errorType: root.errorType
toNetworksList: root.toNetworksList
weiToEth: function(wei) {
return root.currencyStore.getCurrencyAmount(parseFloat(store.getWei2Eth(wei, selectedAsset.decimals)), selectedAsset.symbol)
return parseFloat(store.getWei2Eth(wei, selectedAsset.decimals))
}
formatCurrencyAmount: root.currencyStore.formatCurrencyAmount
reCalculateSuggestedRoute: function() {
root.reCalculateSuggestedRoute()
}
@ -118,10 +119,7 @@ Item {
isBridgeTx: root.isBridgeTx
errorType: root.errorType
weiToEth: function(wei) {
return root.currencyStore.getCurrencyAmount(parseFloat(store.getWei2Eth(wei, selectedAsset.decimals)), selectedAsset.symbol)
}
getCryptoCurrencyAmount: function(cryptoValue) {
return selectedAsset ? root.currencyStore.getCurrencyAmount(parseFloat(cryptoValue), selectedAsset.symbol) : undefined
return parseFloat(store.getWei2Eth(wei, selectedAsset.decimals))
}
}
}

View File

@ -1,4 +1,4 @@
import QtQuick 2.13
import QtQuick 2.13
import QtQuick.Layouts 1.13
import utils 1.0
@ -17,15 +17,14 @@ ColumnLayout {
property var store
property var selectedAccount
property string ensAddressOrEmpty: ""
property var amountToSend
property var requiredGasInEth
property double amountToSend
property double requiredGasInEth
property bool customMode: false
property var selectedAsset
property var bestRoutes
property bool isLoading: false
property bool errorMode: networksLoader.item ? networksLoader.item.errorMode : false
property var weiToEth: function(wei) {}
property var getCryptoCurrencyAmount: function(cryptoValue) {}
property bool interactive: true
property bool isBridgeTx: false
property bool showUnpreferredNetworks: preferredToggleButton.checked
@ -96,7 +95,6 @@ ColumnLayout {
showPreferredChains: preferredToggleButton.checked
bestRoutes: root.bestRoutes
weiToEth: root.weiToEth
getCryptoCurrencyAmount: root.getCryptoCurrencyAmount
interactive: root.interactive
errorType: root.errorType
isLoading: root.isLoading

View File

@ -1,4 +1,4 @@
import QtQuick 2.13
import QtQuick 2.13
import QtQuick.Controls 2.13
import QtQuick.Layouts 1.13
@ -17,13 +17,14 @@ RowLayout {
property var store
property var bestRoutes
property var amountToSend
property double amountToSend
property bool isLoading: false
property bool isBridgeTx: false
property var selectedAsset
property var selectedAccount
property var toNetworksList: []
property var weiToEth: function(wei) {}
property var formatCurrencyAmount: function () {}
property var reCalculateSuggestedRoute: function() {}
property bool errorMode: false
property int errorType: Constants.NoError
@ -80,7 +81,7 @@ RowLayout {
Layout.fillWidth: true
Layout.alignment: Qt.AlignHCenter
Layout.topMargin: Style.current.smallPadding
amountToSend: root.amountToSend ? root.amountToSend.amount : 0.0
amountToSend: root.amountToSend
errorType: root.errorType
isLoading: root.isLoading && !root.isBridgeTx
}
@ -106,7 +107,7 @@ RowLayout {
else {
amountOut = root.weiToEth(parseInt(store.lockedInAmounts[index].value, 16))
}
return LocaleUtils.currencyAmountToLocaleString(amountOut)
return root.formatCurrencyAmount(amountOut, selectedAsset.symbol)
}
statusListItemSubTitle.color: root.errorMode ? Theme.palette.dangerColor1 : Theme.palette.primaryColor1
asset.width: 32
@ -130,8 +131,8 @@ RowLayout {
implicitWidth: 410
title: chainName
property bool tokenBalanceOnChainValid: selectedAccount && selectedAccount !== undefined && selectedAsset !== undefined
property var tokenBalanceOnChain: tokenBalanceOnChainValid ? root.store.getTokenBalanceOnChain(selectedAccount, chainId, selectedAsset.symbol) : undefined
subTitle: tokenBalanceOnChain ? LocaleUtils.currencyAmountToLocaleString(tokenBalanceOnChain) : "N/A"
property double tokenBalanceOnChain: tokenBalanceOnChainValid ? root.store.getTokenBalanceOnChain(selectedAccount, chainId, selectedAsset.symbol).amount : 0.0
subTitle: tokenBalanceOnChainValid ? root.formatCurrencyAmount(tokenBalanceOnChain, selectedAsset.symbol) : "N/A"
statusListItemSubTitle.color: Theme.palette.primaryColor1
asset.width: 32
asset.height: 32