@@ -14,6 +14,7 @@ import {
14
14
StorageData ,
15
15
TrailingAvgData ,
16
16
CandleData ,
17
+ ReducedIndexData ,
17
18
} from './types' ;
18
19
import { MoreThan } from 'typeorm' ;
19
20
import { TIME_INTERVALS } from 'src/fixtures' ;
@@ -558,13 +559,14 @@ export class ResourcePerformance {
558
559
559
560
if ( existingIndex === - 1 ) {
560
561
// Create a new placeholder
561
- resourceStore . data . push ( {
562
+ const datapoint : ReducedCandleData = {
562
563
t : item . timestamp ,
563
564
o : price . toString ( ) ,
564
565
h : price . toString ( ) ,
565
566
l : price . toString ( ) ,
566
567
c : price . toString ( ) ,
567
- } ) ;
568
+ } ;
569
+ resourceStore . data . push ( datapoint ) ;
568
570
569
571
resourceStore . metadata . push ( {
570
572
st : itemStartTime ,
@@ -713,13 +715,12 @@ export class ResourcePerformance {
713
715
714
716
if ( ! isLastStoredItem ) {
715
717
// Create a new placeholder
716
- piStore . data . push ( {
718
+ const datapoint : ReducedIndexData = {
717
719
t : itemStartTime ,
718
- o : '0' ,
719
- h : '0' ,
720
- l : '0' ,
720
+ v : '0' ,
721
721
c : '0' ,
722
- } ) ;
722
+ } ;
723
+ piStore . data . push ( datapoint ) ;
723
724
724
725
piStore . metadata . push ( {
725
726
st : item . timestamp ,
@@ -764,15 +765,12 @@ export class ResourcePerformance {
764
765
// Update the placeholder with final values
765
766
piStore . data [ currentPlaceholderIndex ] = {
766
767
t : piStore . data [ currentPlaceholderIndex ] . t ,
767
- o : avgPrice . toString ( ) ,
768
- h : avgPrice . toString ( ) ,
769
- l : avgPrice . toString ( ) ,
770
- c : avgPrice . toString ( ) ,
768
+ v : avgPrice . toString ( ) , // value
769
+ c : fixedUsed . toString ( ) , // cumulative
771
770
} ;
772
771
773
772
piStore . metadata [ currentPlaceholderIndex ] = {
774
- st :
775
- piStore . metadata [ currentPlaceholderIndex ] . st ,
773
+ st : piStore . metadata [ currentPlaceholderIndex ] . st ,
776
774
et : item . timestamp ,
777
775
u : ripd . used . toString ( ) ,
778
776
f : ripd . feePaid . toString ( ) ,
@@ -799,10 +797,8 @@ export class ResourcePerformance {
799
797
// Create a new placeholder
800
798
piStore . data . push ( {
801
799
t : itemStartTime ,
802
- o : avgPrice . toString ( ) ,
803
- h : avgPrice . toString ( ) ,
804
- l : avgPrice . toString ( ) ,
805
- c : avgPrice . toString ( ) ,
800
+ v : avgPrice . toString ( ) ,
801
+ c : fixedUsed . toString ( ) ,
806
802
} ) ;
807
803
808
804
piStore . metadata . push ( {
@@ -962,8 +958,7 @@ export class ResourcePerformance {
962
958
} ;
963
959
964
960
ptStore . metadata [ currentPlaceholderIndex ] = {
965
- st :
966
- ptStore . metadata [ currentPlaceholderIndex ] . st ,
961
+ st : ptStore . metadata [ currentPlaceholderIndex ] . st ,
967
962
et : item . timestamp ,
968
963
u : rtpd . used . toString ( ) ,
969
964
f : rtpd . feePaid . toString ( ) ,
@@ -1072,18 +1067,17 @@ export class ResourcePerformance {
1072
1067
const pmStore = this . persistentStorage [ interval ] . marketStore [ epoch . id ] ;
1073
1068
1074
1069
// Create a placeholder in the store
1075
- const itemStartTime = this . startOfCurrentInterval (
1076
- item . t ,
1077
- interval
1078
- ) ;
1070
+ const itemStartTime = this . startOfCurrentInterval ( item . t , interval ) ;
1079
1071
1080
1072
// Check if we already have an item for this interval
1081
1073
const lastStoreIndex =
1082
1074
pmStore . data . length > 0 ? pmStore . data . length - 1 : undefined ;
1083
1075
1084
1076
// Get cached data from the latest stored item
1085
1077
if ( lastStoreIndex !== undefined ) {
1086
- const previousData = pmStore . data [ lastStoreIndex ] ;
1078
+ const previousData = pmStore . data [
1079
+ lastStoreIndex
1080
+ ] as ReducedCandleData ;
1087
1081
rmpd . open = BigInt ( previousData . o ) ;
1088
1082
rmpd . high = BigInt ( previousData . h ) ;
1089
1083
rmpd . low = BigInt ( previousData . l ) ;
@@ -1131,15 +1125,11 @@ export class ResourcePerformance {
1131
1125
rmpd . close = itemValueBn ;
1132
1126
1133
1127
// Create a placeholder for the next interval
1134
- const itemStartTime = this . startOfCurrentInterval (
1135
- item . t ,
1136
- interval
1137
- ) ;
1128
+ const itemStartTime = this . startOfCurrentInterval ( item . t , interval ) ;
1138
1129
1139
1130
// Check if we already have an item for this interval
1140
1131
const existingIndex = pmStore . data . findIndex (
1141
- ( d ) =>
1142
- d . t >= itemStartTime && d . t < rmpd . nextTimestamp
1132
+ ( d ) => d . t >= itemStartTime && d . t < rmpd . nextTimestamp
1143
1133
) ;
1144
1134
1145
1135
if ( existingIndex === - 1 && ! isEndOfEpoch ) {
@@ -1172,14 +1162,18 @@ export class ResourcePerformance {
1172
1162
throw new Error ( `Epoch not found for ${ chainId } -${ address } -${ epoch } ` ) ;
1173
1163
}
1174
1164
1175
- return theEpoch . id ;
1165
+ return {
1166
+ id : theEpoch . id ,
1167
+ isCumulative : theEpoch . market . isCumulative ,
1168
+ } ;
1176
1169
}
1177
1170
1178
1171
getResourcePrices ( from : number , to : number , interval : number ) {
1179
1172
this . checkInterval ( interval ) ;
1180
1173
1181
1174
return this . getPricesFromArray (
1182
- this . persistentStorage [ interval ] . resourceStore . data ,
1175
+ this . persistentStorage [ interval ] . resourceStore
1176
+ . data as ReducedCandleData [ ] ,
1183
1177
from ,
1184
1178
to ,
1185
1179
interval
@@ -1195,17 +1189,27 @@ export class ResourcePerformance {
1195
1189
epoch : string
1196
1190
) {
1197
1191
this . checkInterval ( interval ) ;
1198
- const epochId = this . getEpochId ( chainId , address , epoch ) ;
1192
+ const { id : epochId , isCumulative } = this . getEpochId (
1193
+ chainId ,
1194
+ address ,
1195
+ epoch
1196
+ ) ;
1199
1197
if ( ! this . persistentStorage [ interval ] . indexStore [ epochId ] ) {
1200
1198
return [ ] ;
1201
1199
}
1202
- return this . getPricesFromArray (
1203
- this . persistentStorage [ interval ] . indexStore [ epochId ] . data ,
1204
- from ,
1205
- to ,
1206
- interval ,
1207
- false
1208
- ) ;
1200
+
1201
+ const indexDatapoints = (
1202
+ this . persistentStorage [ interval ] . indexStore [ epochId ]
1203
+ . data as ReducedIndexData [ ]
1204
+ ) . map ( ( d ) => ( {
1205
+ t : d . t ,
1206
+ o : isCumulative ? d . c : d . v ,
1207
+ h : isCumulative ? d . c : d . v ,
1208
+ l : isCumulative ? d . c : d . v ,
1209
+ c : isCumulative ? d . c : d . v ,
1210
+ } ) ) ;
1211
+
1212
+ return this . getPricesFromArray ( indexDatapoints , from , to , interval , false ) ;
1209
1213
}
1210
1214
1211
1215
getTrailingAvgPrices (
@@ -1218,7 +1222,7 @@ export class ResourcePerformance {
1218
1222
return this . getPricesFromArray (
1219
1223
this . persistentStorage [ interval ] . trailingAvgStore [
1220
1224
trailingAvgTime . toString ( )
1221
- ] . data ,
1225
+ ] . data as ReducedCandleData [ ] ,
1222
1226
from ,
1223
1227
to ,
1224
1228
interval
@@ -1234,13 +1238,14 @@ export class ResourcePerformance {
1234
1238
epoch : string
1235
1239
) {
1236
1240
this . checkInterval ( interval ) ;
1237
- const epochId = this . getEpochId ( chainId , address , epoch ) ;
1241
+ const { id : epochId } = this . getEpochId ( chainId , address , epoch ) ;
1238
1242
if ( ! this . persistentStorage [ interval ] . marketStore [ epochId ] ) {
1239
1243
return [ ] ;
1240
1244
}
1241
1245
1242
1246
const prices = await this . getPricesFromArray (
1243
- this . persistentStorage [ interval ] . marketStore [ epochId ] . data ,
1247
+ this . persistentStorage [ interval ] . marketStore [ epochId ]
1248
+ . data as ReducedCandleData [ ] ,
1244
1249
from ,
1245
1250
to ,
1246
1251
interval ,
@@ -1326,9 +1331,7 @@ export class ResourcePerformance {
1326
1331
1327
1332
// TODO: Use pointer to find the start and end indices
1328
1333
// Since prices are ordered by timestamp, we can find the start and end indices
1329
- let startIndex = prices . findIndex (
1330
- ( price ) => price . t >= timeWindow . from
1331
- ) ;
1334
+ let startIndex = prices . findIndex ( ( price ) => price . t >= timeWindow . from ) ;
1332
1335
if ( startIndex === - 1 ) startIndex = prices . length ;
1333
1336
1334
1337
let endIndex = prices . length ;
0 commit comments