Skip to content

Commit d3f1cbb

Browse files
alcercujaybuidl
authored andcommitted
refactor(subgraph): fix code smells
1 parent 146d46b commit d3f1cbb

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

subgraph/src/datapoint.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -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+
4753
export function updateStakedPNK(delta: BigInt, timestamp: BigInt): void {
4854
updateDataPoint(delta, timestamp, "stakedPNK");
4955
}

subgraph/src/entities/User.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { User } from "../../generated/schema";
22
import { ZERO } from "../utils";
33

44
export function ensureUser(id: string): User {
5-
let user = User.load(id);
5+
const user = User.load(id);
66

77
if (user) {
88
return user;

0 commit comments

Comments
 (0)