Skip to content

Commit b1420c6

Browse files
committed
chore: add-sortition-module-as-dataSource
1 parent 17502d9 commit b1420c6

File tree

6 files changed

+256
-50
lines changed

6 files changed

+256
-50
lines changed

subgraph/src/KlerosCore.ts

+9-23
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import {
88
CourtModified,
99
Draw as DrawEvent,
1010
NewPeriod,
11-
StakeSet,
1211
TokenAndETHShift as TokenAndETHShiftEvent,
1312
CourtJump,
1413
Ruling,
15-
StakeDelayedNotTransferred,
1614
AcceptedFeeToken,
1715
} from "../generated/KlerosCore/KlerosCore";
1816
import { ZERO, ONE } from "./utils";
@@ -22,7 +20,7 @@ import { createDisputeFromEvent } from "./entities/Dispute";
2220
import { createRoundFromRoundInfo } from "./entities/Round";
2321
import { updateCases, updateCasesAppealing, updateCasesRuled, updateCasesVoting } from "./datapoint";
2422
import { addUserActiveDispute, ensureUser } from "./entities/User";
25-
import { updateJurorDelayedStake, updateJurorStake } from "./entities/JurorTokensPerCourt";
23+
import { updateJurorStake } from "./entities/JurorTokensPerCourt";
2624
import { createDrawFromEvent } from "./entities/Draw";
2725
import { updateTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
2826
import { updateArbitrableCases } from "./entities/Arbitrable";
@@ -31,6 +29,7 @@ import { BigInt } from "@graphprotocol/graph-ts";
3129
import { updatePenalty } from "./entities/Penalty";
3230
import { ensureFeeToken } from "./entities/FeeToken";
3331
import { getAndIncrementPeriodCounter } from "./entities/PeriodIndexCounter";
32+
import { SortitionModule } from "../generated/SortitionModule/SortitionModule";
3433

3534
function getPeriodName(index: i32): string {
3635
const periodArray = ["evidence", "commit", "vote", "appeal", "execution"];
@@ -179,27 +178,12 @@ export function handleDraw(event: DrawEvent): void {
179178
const disputeID = event.params._disputeID.toString();
180179
const dispute = Dispute.load(disputeID);
181180
if (!dispute) return;
182-
const contract = KlerosCore.bind(event.address);
183-
const jurorAddress = event.params._address.toHexString();
184-
updateJurorStake(jurorAddress, dispute.court, contract, event.block.timestamp);
185-
addUserActiveDispute(jurorAddress, disputeID);
186-
}
181+
const klerosCore = KlerosCore.bind(event.address);
182+
const sortitionModule = SortitionModule.bind(klerosCore.sortitionModule());
187183

188-
export function handleStakeSet(event: StakeSet): void {
189184
const jurorAddress = event.params._address.toHexString();
190-
ensureUser(jurorAddress);
191-
const courtID = event.params._courtID.toString();
192-
193-
updateJurorStake(jurorAddress, courtID.toString(), KlerosCore.bind(event.address), event.block.timestamp);
194-
195-
// Check if the transaction the event comes from is executeDelayedStakes
196-
if (event.transaction.input.toHexString().substring(0, 10) === "0x35975f4a") {
197-
updateJurorDelayedStake(jurorAddress, courtID, ZERO.minus(event.params._amount));
198-
}
199-
}
200-
201-
export function handleStakeDelayedNotTransferred(event: StakeDelayedNotTransferred): void {
202-
updateJurorDelayedStake(event.params._address.toHexString(), event.params._courtID.toString(), event.params._amount);
185+
updateJurorStake(jurorAddress, dispute.court, sortitionModule, event.block.timestamp);
186+
addUserActiveDispute(jurorAddress, disputeID);
203187
}
204188

205189
export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
@@ -211,7 +195,9 @@ export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
211195
if (!dispute) return;
212196
const court = Court.load(dispute.court);
213197
if (!court) return;
214-
updateJurorStake(jurorAddress, court.id, KlerosCore.bind(event.address), event.block.timestamp);
198+
const klerosCore = KlerosCore.bind(event.address);
199+
const sortitionModule = SortitionModule.bind(klerosCore.sortitionModule());
200+
updateJurorStake(jurorAddress, court.id, sortitionModule, event.block.timestamp);
215201
}
216202

217203
export function handleAcceptedFeeToken(event: AcceptedFeeToken): void {

subgraph/src/SortitionModule.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {
2+
SortitionModule,
3+
StakeDelayedAlreadyTransferred,
4+
StakeDelayedAlreadyTransferredWithdrawn,
5+
StakeDelayedNotTransferred,
6+
StakeLocked,
7+
StakeSet,
8+
} from "../generated/SortitionModule/SortitionModule";
9+
10+
import { updateJurorDelayedStake, updateJurorStake } from "./entities/JurorTokensPerCourt";
11+
import { ensureUser } from "./entities/User";
12+
import { ZERO } from "./utils";
13+
14+
export function handleStakeDelayedAlreadyTransferred(event: StakeDelayedAlreadyTransferred): void {
15+
const jurorAddress = event.params._address.toHexString();
16+
ensureUser(jurorAddress);
17+
const courtID = event.params._courtID.toString();
18+
19+
updateJurorStake(jurorAddress, courtID.toString(), SortitionModule.bind(event.address), event.block.timestamp);
20+
21+
//stake is updated instantly so no delayed amount, set delay amount to zero
22+
updateJurorDelayedStake(jurorAddress, courtID, ZERO);
23+
}
24+
25+
export function handleStakeDelayedAlreadyTransferredWithdrawn(event: StakeDelayedAlreadyTransferredWithdrawn): void {
26+
const jurorAddress = event.params._address.toHexString();
27+
ensureUser(jurorAddress);
28+
const courtID = event.params._courtID.toString();
29+
30+
updateJurorStake(jurorAddress, courtID.toString(), SortitionModule.bind(event.address), event.block.timestamp);
31+
32+
updateJurorDelayedStake(jurorAddress, courtID, ZERO);
33+
}
34+
35+
export function handleStakeDelayedNotTransferred(event: StakeDelayedNotTransferred): void {
36+
updateJurorDelayedStake(event.params._address.toHexString(), event.params._courtID.toString(), event.params._amount);
37+
}
38+
39+
export function handleStakeSet(event: StakeSet): void {
40+
const jurorAddress = event.params._address.toHexString();
41+
ensureUser(jurorAddress);
42+
const courtID = event.params._courtID.toString();
43+
44+
updateJurorStake(jurorAddress, courtID.toString(), SortitionModule.bind(event.address), event.block.timestamp);
45+
//stake is updated instantly so no delayed amount, set delay amount to zero
46+
updateJurorDelayedStake(jurorAddress, courtID, ZERO);
47+
}
48+
export function handleStakeLocked(event: StakeLocked): void {}

subgraph/src/entities/JurorTokensPerCourt.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { BigInt, Address } from "@graphprotocol/graph-ts";
2-
import { KlerosCore } from "../../generated/KlerosCore/KlerosCore";
32
import { Court, JurorTokensPerCourt } from "../../generated/schema";
43
import { updateActiveJurors, getDelta, updateStakedPNK } from "../datapoint";
54
import { ensureUser } from "./User";
65
import { ONE, ZERO } from "../utils";
6+
import { SortitionModule } from "../../generated/SortitionModule/SortitionModule";
77

88
export function ensureJurorTokensPerCourt(jurorAddress: string, courtID: string): JurorTokensPerCourt {
99
const id = `${jurorAddress}-${courtID}`;
@@ -30,7 +30,12 @@ export function createJurorTokensPerCourt(jurorAddress: string, courtID: string)
3030
return jurorTokens;
3131
}
3232

33-
export function updateJurorStake(jurorAddress: string, courtID: string, contract: KlerosCore, timestamp: BigInt): void {
33+
export function updateJurorStake(
34+
jurorAddress: string,
35+
courtID: string,
36+
contract: SortitionModule,
37+
timestamp: BigInt
38+
): void {
3439
const juror = ensureUser(jurorAddress);
3540
const court = Court.load(courtID);
3641
if (!court) return;
@@ -59,9 +64,12 @@ export function updateJurorDelayedStake(jurorAddress: string, courtID: string, a
5964
const court = Court.load(courtID);
6065
if (!court) return;
6166
const jurorTokens = ensureJurorTokensPerCourt(jurorAddress, courtID);
62-
jurorTokens.delayed = jurorTokens.delayed.plus(amount);
63-
juror.totalDelayed = juror.totalDelayed.plus(amount);
64-
court.delayedStake = court.stake.plus(amount);
67+
let lastDelayedAmount = jurorTokens.delayed;
68+
69+
jurorTokens.delayed = amount;
70+
//since we need to track only the latest delay amount now, subtract the previous amount and add the new amount
71+
juror.totalDelayed = juror.totalDelayed.plus(amount).minus(lastDelayedAmount);
72+
court.delayedStake = court.stake.plus(amount).minus(lastDelayedAmount);
6573
jurorTokens.save();
6674
juror.save();
6775
court.save();

subgraph/subgraph.yaml

+46-22
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ schema:
44
dataSources:
55
- kind: ethereum
66
name: KlerosCore
7-
network: arbitrum-goerli
7+
network: mainnet
88
source:
9-
address: "0xB88643fd1e4351dAF9eA7292db126207FDE42e45"
9+
address: "0x59b670e9fA9D0A427751Af201D676719a970857b"
1010
abi: KlerosCore
11-
startBlock: 49141544
11+
startBlock: 22
1212
mapping:
1313
kind: ethereum/events
1414
apiVersion: 0.0.6
@@ -26,9 +26,9 @@ dataSources:
2626
- Counter
2727
abis:
2828
- name: DisputeKitClassic
29-
file: ../contracts/deployments/arbitrumGoerliDevnet/DisputeKitClassic.json
29+
file: ../contracts/deployments/localhost/DisputeKitClassic.json
3030
- name: KlerosCore
31-
file: ../contracts/deployments/arbitrumGoerliDevnet/KlerosCore.json
31+
file: ../contracts/deployments/localhost/KlerosCore.json
3232
eventHandlers:
3333
- event: AppealDecision(indexed uint256,indexed address)
3434
handler: handleAppealDecision
@@ -46,10 +46,6 @@ dataSources:
4646
handler: handleDisputeKitCreated
4747
- event: DisputeKitEnabled(indexed uint96,indexed uint256,indexed bool)
4848
handler: handleDisputeKitEnabled
49-
- event: StakeSet(indexed address,uint256,uint256)
50-
handler: handleStakeSet
51-
- event: StakeDelayedNotTransferred(indexed address,uint256,uint256)
52-
handler: handleStakeDelayedNotTransferred
5349
- event: TokenAndETHShift(indexed address,indexed uint256,indexed uint256,uint256,int256,int256,address)
5450
handler: handleTokenAndETHShift
5551
- event: Ruling(indexed address,indexed uint256,uint256)
@@ -61,11 +57,11 @@ dataSources:
6157
file: ./src/KlerosCore.ts
6258
- kind: ethereum
6359
name: PolicyRegistry
64-
network: arbitrum-goerli
60+
network: mainnet
6561
source:
66-
address: "0x37FFaF5506BB16327B4a32191Bb39d739fCE55a3"
62+
address: "0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e"
6763
abi: PolicyRegistry
68-
startBlock: 48886711
64+
startBlock: 12
6965
mapping:
7066
kind: ethereum/events
7167
apiVersion: 0.0.6
@@ -74,18 +70,18 @@ dataSources:
7470
- Court
7571
abis:
7672
- name: PolicyRegistry
77-
file: ../contracts/deployments/arbitrumGoerliDevnet/PolicyRegistry.json
73+
file: ../contracts/deployments/localhost/PolicyRegistry.json
7874
eventHandlers:
7975
- event: PolicyUpdate(indexed uint256,string,string)
8076
handler: handlePolicyUpdate
8177
file: ./src/PolicyRegistry.ts
8278
- kind: ethereum
8379
name: DisputeKitClassic
84-
network: arbitrum-goerli
80+
network: mainnet
8581
source:
86-
address: "0x67f3b472F345119692d575E59190400E371946f6"
82+
address: "0x9A9f2CCfdE556A7E9Ff0848998Aa4a0CFD8863AE"
8783
abi: DisputeKitClassic
88-
startBlock: 49141470
84+
startBlock: 18
8985
mapping:
9086
kind: ethereum/events
9187
apiVersion: 0.0.6
@@ -97,9 +93,9 @@ dataSources:
9793
- ClassicContribution
9894
abis:
9995
- name: DisputeKitClassic
100-
file: ../contracts/deployments/arbitrumGoerliDevnet/DisputeKitClassic.json
96+
file: ../contracts/deployments/localhost/DisputeKitClassic.json
10197
- name: KlerosCore
102-
file: ../contracts/deployments/arbitrumGoerliDevnet/KlerosCore.json
98+
file: ../contracts/deployments/localhost/KlerosCore.json
10399
eventHandlers:
104100
- event: DisputeCreation(indexed uint256,uint256,bytes)
105101
handler: handleDisputeCreation
@@ -116,11 +112,11 @@ dataSources:
116112
file: ./src/DisputeKitClassic.ts
117113
- kind: ethereum
118114
name: EvidenceModule
119-
network: arbitrum-goerli
115+
network: mainnet
120116
source:
121-
address: "0xF679E4a92AE843fd5cD0717A7417C3A773Dfd55F"
117+
address: "0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82"
122118
abi: EvidenceModule
123-
startBlock: 49137992
119+
startBlock: 14
124120
mapping:
125121
kind: ethereum/events
126122
apiVersion: 0.0.6
@@ -130,8 +126,36 @@ dataSources:
130126
- ClassicEvidence
131127
abis:
132128
- name: EvidenceModule
133-
file: ../contracts/deployments/arbitrumGoerliDevnet/EvidenceModule.json
129+
file: ../contracts/deployments/localhost/EvidenceModule.json
134130
eventHandlers:
135131
- event: Evidence(indexed uint256,indexed address,string)
136132
handler: handleEvidenceEvent
137133
file: ./src/EvidenceModule.ts
134+
- kind: ethereum
135+
name: SortitionModule
136+
network: mainnet
137+
source:
138+
address: "0x3Aa5ebB10DC797CAC828524e59A333d0A371443c"
139+
abi: SortitionModule
140+
startBlock: 20
141+
mapping:
142+
kind: ethereum/events
143+
apiVersion: 0.0.6
144+
language: wasm/assemblyscript
145+
entities:
146+
- JurorTokensPerCourt
147+
abis:
148+
- name: SortitionModule
149+
file: ../contracts/deployments/localhost/SortitionModule.json
150+
eventHandlers:
151+
- event: StakeDelayedAlreadyTransferred(indexed address,uint256,uint256)
152+
handler: handleStakeDelayedAlreadyTransferred
153+
- event: StakeDelayedAlreadyTransferredWithdrawn(indexed address,indexed uint96,uint256)
154+
handler: handleStakeDelayedAlreadyTransferredWithdrawn
155+
- event: StakeDelayedNotTransferred(indexed address,uint256,uint256)
156+
handler: handleStakeDelayedNotTransferred
157+
- event: StakeLocked(indexed address,uint256,bool)
158+
handler: handleStakeLocked
159+
- event: StakeSet(indexed address,uint256,uint256)
160+
handler: handleStakeSet
161+
file: ./src/SortitionModule.ts

0 commit comments

Comments
 (0)