Skip to content

Commit e1a6d1d

Browse files
alcercujaybuidl
authored andcommitted
fix(subgraph): improve datapoint updates
1 parent beedffa commit e1a6d1d

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

subgraph/src/datapoint.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,28 @@ function updateDataPoint(
2020
timestamp: BigInt,
2121
variable: string
2222
): void {
23-
let counter = store.get("Counter", "0");
24-
if (!counter) {
25-
counter = new Entity();
26-
for (let i = 0; i < VARIABLES.length; i++) {
27-
counter.set(VARIABLES[i], Value.fromBigInt(ZERO));
23+
const newCounter = new Entity();
24+
const counter = store.get("Counter", "0");
25+
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+
);
2839
}
2940
}
3041
const dayID = timestamp.toI32() / 86400;
3142
const dayStartTimestamp = dayID * 86400;
32-
const newValue = counter.get(variable)!.toBigInt().plus(delta);
33-
counter.set(variable, Value.fromBigInt(newValue));
34-
store.set("Counter", dayStartTimestamp.toString(), counter);
35-
store.set("Counter", "0", counter);
43+
store.set("Counter", dayStartTimestamp.toString(), newCounter);
44+
store.set("Counter", "0", newCounter);
3645
}
3746

3847
export function updateStakedPNK(delta: BigInt, timestamp: BigInt): void {

0 commit comments

Comments
 (0)