2024-10-17 11:57:38 +00:00
|
|
|
import QtQml 2.15
|
2023-01-12 22:39:46 +00:00
|
|
|
|
2022-10-28 17:17:16 +00:00
|
|
|
import utils 1.0
|
|
|
|
|
2024-10-17 12:41:42 +00:00
|
|
|
ChartDataBase {
|
2022-10-28 17:17:16 +00:00
|
|
|
id: root
|
|
|
|
|
2022-11-15 21:48:59 +00:00
|
|
|
readonly property alias address: d.address
|
|
|
|
readonly property alias tokenSymbol: d.tokenSymbol
|
|
|
|
readonly property alias currencySymbol: d.currencySymbol
|
|
|
|
|
2024-10-17 11:57:38 +00:00
|
|
|
readonly property QtObject _d: QtObject {
|
2022-11-15 21:48:59 +00:00
|
|
|
id: d
|
|
|
|
|
|
|
|
// Data identity received from backend
|
|
|
|
property var chainIds: []
|
|
|
|
property string address
|
|
|
|
property string tokenSymbol
|
|
|
|
property string currencySymbol
|
|
|
|
}
|
|
|
|
|
2024-03-28 11:54:39 +00:00
|
|
|
function hasData(address, tokenSymbol, currencySymbol, timeRangeEnum) {
|
|
|
|
return address === d.address && tokenSymbol === d.tokenSymbol && currencySymbol === d.currencySymbol
|
2022-11-15 21:48:59 +00:00
|
|
|
&& root.dataRange[root.timeRangeEnumToTimeIndex(timeRangeEnum)][root.timeRangeEnumToStr(timeRangeEnum)].length > 0
|
|
|
|
}
|
2022-10-28 17:17:16 +00:00
|
|
|
|
2024-10-17 12:41:42 +00:00
|
|
|
/// \arg timeRange: of type ChartDataBase.TimeRange
|
2024-03-28 11:54:39 +00:00
|
|
|
function setData(address, tokenSymbol, currencySymbol, timeRange, timeRangeData, balanceData) {
|
2022-10-28 17:17:16 +00:00
|
|
|
switch(timeRange) {
|
2024-10-17 12:41:42 +00:00
|
|
|
case ChartDataBase.TimeRange.Weekly:
|
2022-10-28 17:17:16 +00:00
|
|
|
root.weeklyData = balanceData
|
|
|
|
root.weeklyMaxTicks = 0
|
|
|
|
break;
|
2024-10-17 12:41:42 +00:00
|
|
|
case ChartDataBase.TimeRange.Monthly:
|
2022-10-28 17:17:16 +00:00
|
|
|
root.monthlyData = balanceData
|
|
|
|
root.monthlyMaxTicks = 0
|
|
|
|
break;
|
2024-10-17 12:41:42 +00:00
|
|
|
case ChartDataBase.TimeRange.HalfYearly:
|
2022-10-28 17:17:16 +00:00
|
|
|
root.halfYearlyData = balanceData
|
|
|
|
root.halfYearlyMaxTicks = 0
|
|
|
|
break;
|
2024-10-17 12:41:42 +00:00
|
|
|
case ChartDataBase.TimeRange.Yearly:
|
2022-10-28 17:17:16 +00:00
|
|
|
root.yearlyData = balanceData
|
|
|
|
root.yearlyMaxTicks = 0
|
|
|
|
break;
|
2024-10-17 12:41:42 +00:00
|
|
|
case ChartDataBase.TimeRange.All:
|
2022-10-28 17:17:16 +00:00
|
|
|
root.allData = balanceData
|
|
|
|
root.allTimeRangeTicks = 0
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn("Invalid or unsupported time range")
|
2022-11-15 21:48:59 +00:00
|
|
|
return
|
2022-10-28 17:17:16 +00:00
|
|
|
}
|
2022-11-15 21:48:59 +00:00
|
|
|
|
|
|
|
d.address = address
|
|
|
|
d.tokenSymbol = tokenSymbol
|
|
|
|
d.currencySymbol = currencySymbol
|
|
|
|
|
|
|
|
root.newDataReady(address, tokenSymbol, currencySymbol, timeRange)
|
2022-10-28 17:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 11:54:39 +00:00
|
|
|
function resetAllData(address, tokenSymbol, currencySymbol) {
|
2024-10-17 12:41:42 +00:00
|
|
|
for (let tR = ChartDataBase.TimeRange.Weekly; tR <= ChartDataBase.TimeRange.All; tR++) {
|
2024-03-28 11:54:39 +00:00
|
|
|
root.setData(address, tokenSymbol, currencySymbol, tR, [], [])
|
2022-11-15 21:48:59 +00:00
|
|
|
}
|
2022-10-28 17:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 11:57:38 +00:00
|
|
|
readonly property Connections _c: Connections {
|
2022-10-28 17:17:16 +00:00
|
|
|
target: walletSectionAllTokens
|
2022-11-15 21:48:59 +00:00
|
|
|
|
|
|
|
function onTokenBalanceHistoryDataReady(balanceHistoryJson: string) {
|
|
|
|
// chainIds, address, tokenSymbol, currencySymbol, timeInterval
|
|
|
|
let response = JSON.parse(balanceHistoryJson)
|
|
|
|
if(typeof response.error !== "undefined") {
|
|
|
|
console.warn("error in balance history: " + response.error)
|
2022-10-28 17:17:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-28 11:54:39 +00:00
|
|
|
if (response.addresses.length > 1) {
|
2023-11-29 11:19:59 +00:00
|
|
|
response.address = ""
|
2024-03-28 11:54:39 +00:00
|
|
|
} else {
|
|
|
|
response.address = response.addresses[0]
|
2023-11-29 11:19:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 11:54:39 +00:00
|
|
|
if(d.address != response.address || d.tokenSymbol != response.tokenSymbol || d.currencySymbol != response.currencySymbol) {
|
|
|
|
root.resetAllData(response.address, response.tokenSymbol, response.currencySymbol)
|
2022-11-15 21:48:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 17:17:16 +00:00
|
|
|
if(typeof response.historicalData === "undefined" || response.historicalData === null || response.historicalData.length == 0) {
|
2022-11-15 21:48:59 +00:00
|
|
|
console.info("no data in balance history")
|
2022-10-28 17:17:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmpDataValues = []
|
|
|
|
for(let i = 0; i < response.historicalData.length; i++) {
|
|
|
|
let dataEntry = response.historicalData[i]
|
2023-09-15 17:27:00 +00:00
|
|
|
tmpDataValues.push({ x: new Date(dataEntry.time * 1000), y: dataEntry.value })
|
2022-10-28 17:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 11:54:39 +00:00
|
|
|
root.setData(response.address, response.tokenSymbol, response.currencySymbol, response.timeInterval, [], tmpDataValues)
|
2022-10-28 17:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-12 22:39:46 +00:00
|
|
|
}
|