status-desktop/ui/imports/shared/stores/TokenBalanceHistoryStore.qml
Stefan d0389a6305 feat(Wallet) show balance cache for chain native token
Quick integration of fetching balance in the current chart view.

The proper implementation requires refactoring the QML views to separate
price chart, that depends only on the token and chain, from balance that
depends on token, chain and address.

Closes: #7662
2022-11-16 13:07:46 +01:00

89 lines
3.3 KiB
QML

import QtQuick 2.13
import utils 1.0
ChartStoreBase {
id: root
/*required*/ property string address: ""
/// \arg timeRange: of type ChartStoreBase.TimeRange
function setData(timeRange, timeRangeData, balanceData) {
switch(timeRange) {
case ChartStoreBase.TimeRange.Weekly:
root.weeklyData = balanceData
root.weeklyTimeRange = timeRangeData
root.weeklyMaxTicks = 0
break;
case ChartStoreBase.TimeRange.Monthly:
root.monthlyData = balanceData
root.monthlyTimeRange = timeRangeData
root.monthlyMaxTicks = 0
break;
case ChartStoreBase.TimeRange.HalfYearly:
root.halfYearlyData = balanceData
root.halfYearlyTimeRange = timeRangeData
root.halfYearlyMaxTicks = 0
break;
case ChartStoreBase.TimeRange.Yearly:
root.yearlyData = balanceData
root.yearlyTimeRange = timeRangeData
root.yearlyMaxTicks = 0
break;
case ChartStoreBase.TimeRange.All:
root.allData = balanceData
root.allTimeRange = timeRangeData
root.allTimeRangeTicks = 0
break;
default:
console.warn("Invalid or unsupported time range")
break;
}
root.newDataReady(timeRange)
}
/// \arg timeRange: of type ChartStoreBase.TimeRange
function resetData(timeRange) {
root.setData(timeRange, [], [])
}
Connections {
target: walletSectionAllTokens
onTokenBalanceHistoryDataReady: (balanceHistory) => {
// chainId, address, symbol, timeInterval
let response = JSON.parse(balanceHistory)
if (response === null) {
console.warn("error parsing balance history json message data")
root.resetRequestTime()
return
}
if(typeof response.historicalData === "undefined" || response.historicalData === null || response.historicalData.length == 0) {
console.warn("error no data in balance history. Must be an error from status-go")
root.resetRequestTime()
return
} else if(response.address !== root.address) {
// Ignore data for other addresses. Will be handled by other instances of this store
return
}
root.resetData(response.timeInterval)
var tmpTimeRange = []
var tmpDataValues = []
for(let i = 0; i < response.historicalData.length; i++) {
let dataEntry = response.historicalData[i]
let dateString = response.timeInterval == ChartStoreBase.TimeRange.Weekly || response.timeInterval == ChartStoreBase.TimeRange.Monthly
? Utils.getDayMonth(dataEntry.time * 1000, RootStore.accountSensitiveSettings.is24hTimeFormat)
: Utils.getMonthYear(dataEntry.time * 1000)
tmpTimeRange.push(dateString)
tmpDataValues.push(parseFloat(globalUtils.wei2Eth(dataEntry.value, 18)))
}
root.setData(response.timeInterval, tmpTimeRange, tmpDataValues)
root.updateRequestTime(response.timeInterval)
}
}
}