-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathdatapoint.ts
94 lines (80 loc) · 2.95 KB
/
datapoint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { BigInt, Entity, Value, store } from "@graphprotocol/graph-ts";
import { Counter } from "../generated/schema";
import { ZERO } from "./utils";
export function getDelta(previousValue: BigInt, newValue: BigInt): BigInt {
return newValue.minus(previousValue);
}
const VARIABLES = [
"stakedPNK",
"redistributedPNK",
"paidETH",
"activeJurors",
"cases",
"casesVoting",
"casesRuled",
"casesAppealing",
"totalLeaderboardJurors",
];
function updateDataPoint(delta: BigInt, timestamp: BigInt, variable: string): void {
checkFirstDayActivity();
const newCounter = new Entity();
const counter = store.get("Counter", "0");
for (let i = 0; i < VARIABLES.length; i++) {
const currentVar = VARIABLES[i];
newCounter.set(currentVar, getNewValue(currentVar, variable, delta, counter));
}
const dayID = timestamp.toI32() / 86400;
const dayStartTimestamp = dayID * 86400;
store.set("Counter", dayStartTimestamp.toString(), newCounter);
store.set("Counter", "0", newCounter);
}
function checkFirstDayActivity(): void {
let counter = Counter.load("1691452800");
if (!counter) {
counter = new Counter("1691452800");
counter.stakedPNK = ZERO;
counter.redistributedPNK = ZERO;
counter.paidETH = ZERO;
counter.activeJurors = ZERO;
counter.cases = ZERO;
counter.casesVoting = ZERO;
counter.casesRuled = ZERO;
counter.casesAppealing = ZERO;
counter.totalLeaderboardJurors = ZERO;
counter.save();
}
}
function getNewValue(currentVar: string, targetVar: string, delta: BigInt, counter: Entity | null): Value {
if (currentVar === targetVar) {
return !counter ? Value.fromBigInt(delta) : Value.fromBigInt(counter.get(currentVar)!.toBigInt().plus(delta));
} else {
return !counter ? Value.fromBigInt(ZERO) : counter.get(currentVar)!;
}
}
export function updateStakedPNK(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "stakedPNK");
}
export function updateRedistributedPNK(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "redistributedPNK");
}
export function updatePaidETH(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "paidETH");
}
export function updateActiveJurors(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "activeJurors");
}
export function updateCases(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "cases");
}
export function updateCasesVoting(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "casesVoting");
}
export function updateCasesRuled(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "casesRuled");
}
export function updateCasesAppealing(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "casesAppealing");
}
export function updateTotalLeaderboardJurors(delta: BigInt, timestamp: BigInt): void {
updateDataPoint(delta, timestamp, "totalLeaderboardJurors");
}