@@ -23,27 +23,33 @@ function updateDataPoint(
2323 const newCounter = new Entity ( ) ;
2424 const counter = store . get ( "Counter" , "0" ) ;
2525 for ( let i = 0 ; i < VARIABLES . length ; i ++ ) {
26- const targetVar = VARIABLES [ i ] ;
27- if ( targetVar === variable ) {
28- newCounter . set (
29- targetVar ,
30- ! counter
31- ? Value . fromBigInt ( delta )
32- : Value . fromBigInt ( counter . get ( targetVar ) ! . toBigInt ( ) . plus ( delta ) )
33- ) ;
34- } else {
35- newCounter . set (
36- targetVar ,
37- ! counter ? Value . fromBigInt ( ZERO ) : counter . get ( VARIABLES [ i ] ) !
38- ) ;
39- }
26+ const currentVar = VARIABLES [ i ] ;
27+ newCounter . set (
28+ currentVar ,
29+ getNewValue ( currentVar , variable , delta , counter )
30+ ) ;
4031 }
4132 const dayID = timestamp . toI32 ( ) / 86400 ;
4233 const dayStartTimestamp = dayID * 86400 ;
4334 store . set ( "Counter" , dayStartTimestamp . toString ( ) , newCounter ) ;
4435 store . set ( "Counter" , "0" , newCounter ) ;
4536}
4637
38+ function getNewValue (
39+ currentVar : string ,
40+ targetVar : string ,
41+ delta : BigInt ,
42+ counter : Entity | null
43+ ) : Value {
44+ if ( currentVar === targetVar ) {
45+ return ! counter
46+ ? Value . fromBigInt ( delta )
47+ : Value . fromBigInt ( counter . get ( currentVar ) ! . toBigInt ( ) . plus ( delta ) ) ;
48+ } else {
49+ return ! counter ? Value . fromBigInt ( ZERO ) : counter . get ( currentVar ) ! ;
50+ }
51+ }
52+
4753export function updateStakedPNK ( delta : BigInt , timestamp : BigInt ) : void {
4854 updateDataPoint ( delta , timestamp , "stakedPNK" ) ;
4955}
0 commit comments