Skip to content

Commit 1ecd467

Browse files
tractorssjaybuidl
authored andcommitted
chore: add-sortition-module-as-dataSource
1 parent 658ff0f commit 1ecd467

File tree

6 files changed

+238
-33
lines changed

6 files changed

+238
-33
lines changed

subgraph/core/src/KlerosCore.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { createDisputeFromEvent } from "./entities/Dispute";
2020
import { createRoundFromRoundInfo } from "./entities/Round";
2121
import { updateCases, updateCasesAppealing, updateCasesRuled, updateCasesVoting } from "./datapoint";
2222
import { addUserActiveDispute, ensureUser } from "./entities/User";
23-
import { updateJurorDelayedStake, updateJurorStake } from "./entities/JurorTokensPerCourt";
23+
import { updateJurorStake } from "./entities/JurorTokensPerCourt";
2424
import { createDrawFromEvent } from "./entities/Draw";
2525
import { updateTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
2626
import { updateArbitrableCases } from "./entities/Arbitrable";
@@ -29,6 +29,7 @@ import { BigInt } from "@graphprotocol/graph-ts";
2929
import { updatePenalty } from "./entities/Penalty";
3030
import { ensureFeeToken } from "./entities/FeeToken";
3131
import { getAndIncrementPeriodCounter } from "./entities/PeriodIndexCounter";
32+
import { SortitionModule } from "../generated/SortitionModule/SortitionModule";
3233

3334
function getPeriodName(index: i32): string {
3435
const periodArray = ["evidence", "commit", "vote", "appeal", "execution"];
@@ -177,31 +178,14 @@ export function handleDraw(event: DrawEvent): void {
177178
const disputeID = event.params._disputeID.toString();
178179
const dispute = Dispute.load(disputeID);
179180
if (!dispute) return;
180-
const contract = KlerosCore.bind(event.address);
181+
const klerosCore = KlerosCore.bind(event.address);
182+
const sortitionModule = SortitionModule.bind(klerosCore.sortitionModule());
183+
181184
const jurorAddress = event.params._address.toHexString();
182-
updateJurorStake(jurorAddress, dispute.court, contract, event.block.timestamp);
185+
updateJurorStake(jurorAddress, dispute.court, sortitionModule, event.block.timestamp);
183186
addUserActiveDispute(jurorAddress, disputeID);
184187
}
185188

186-
// TODO: index the sortition module and handle these events there
187-
// export function handleStakeSet(event: StakeSet): void {
188-
// const jurorAddress = event.params._address.toHexString();
189-
// ensureUser(jurorAddress);
190-
// const courtID = event.params._courtID.toString();
191-
192-
// updateJurorStake(jurorAddress, courtID.toString(), KlerosCore.bind(event.address), event.block.timestamp);
193-
194-
// // Check if the transaction the event comes from is executeDelayedStakes
195-
// if (event.transaction.input.toHexString().substring(0, 10) === "0x35975f4a") {
196-
// updateJurorDelayedStake(jurorAddress, courtID, ZERO.minus(event.params._amount));
197-
// }
198-
// }
199-
200-
// TODO: index the sortition module and handle these events there
201-
// export function handleStakeDelayedNotTransferred(event: StakeDelayedNotTransferred): void {
202-
// updateJurorDelayedStake(event.params._address.toHexString(), event.params._courtID.toString(), event.params._amount);
203-
// }
204-
205189
export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
206190
updatePenalty(event);
207191
updateTokenAndEthShiftFromEvent(event);
@@ -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 {
Lines changed: 48 additions & 0 deletions
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/core/src/entities/JurorTokensPerCourt.ts

Lines changed: 13 additions & 5 deletions
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;
@@ -61,9 +66,12 @@ export function updateJurorDelayedStake(jurorAddress: string, courtID: string, a
6166
const court = Court.load(courtID);
6267
if (!court) return;
6368
const jurorTokens = ensureJurorTokensPerCourt(jurorAddress, courtID);
64-
jurorTokens.delayed = jurorTokens.delayed.plus(amount);
65-
juror.totalDelayed = juror.totalDelayed.plus(amount);
66-
court.delayedStake = court.stake.plus(amount);
69+
let lastDelayedAmount = jurorTokens.delayed;
70+
71+
jurorTokens.delayed = amount;
72+
//since we need to track only the latest delay amount now, subtract the previous amount and add the new amount
73+
juror.totalDelayed = juror.totalDelayed.plus(amount).minus(lastDelayedAmount);
74+
court.delayedStake = court.stake.plus(amount).minus(lastDelayedAmount);
6775
jurorTokens.save();
6876
juror.save();
6977
court.save();

subgraph/core/subgraph.yaml

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ dataSources:
4646
handler: handleDisputeKitCreated
4747
- event: DisputeKitEnabled(indexed uint96,indexed uint256,indexed bool)
4848
handler: handleDisputeKitEnabled
49-
# TODO: index the sortition module and handle these events there
50-
# - event: StakeSet(indexed address,uint256,uint256)
51-
# handler: handleStakeSet
52-
# - event: StakeDelayedNotTransferred(indexed address,uint256,uint256)
53-
# handler: handleStakeDelayedNotTransferred
5449
- event: TokenAndETHShift(indexed address,indexed uint256,indexed uint256,uint256,int256,int256,address)
5550
handler: handleTokenAndETHShift
5651
- event: Ruling(indexed address,indexed uint256,uint256)
@@ -136,3 +131,31 @@ dataSources:
136131
- event: Evidence(indexed uint256,indexed address,string)
137132
handler: handleEvidenceEvent
138133
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
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { newMockEvent } from "matchstick-as";
2+
import { ethereum, BigInt, Address } from "@graphprotocol/graph-ts";
3+
import {
4+
StakeDelayedAlreadyTransferred,
5+
StakeDelayedAlreadyTransferredWithdrawn,
6+
StakeDelayedNotTransferred,
7+
StakeLocked,
8+
StakeSet,
9+
} from "../generated/SortitionModule/SortitionModule";
10+
11+
export function createStakeDelayedAlreadyTransferredEvent(
12+
_address: Address,
13+
_courtID: BigInt,
14+
_amount: BigInt
15+
): StakeDelayedAlreadyTransferred {
16+
let stakeDelayedAlreadyTransferredEvent: StakeDelayedAlreadyTransferred = newMockEvent();
17+
18+
stakeDelayedAlreadyTransferredEvent.parameters = new Array();
19+
20+
stakeDelayedAlreadyTransferredEvent.parameters.push(
21+
new ethereum.EventParam("_address", ethereum.Value.fromAddress(_address))
22+
);
23+
stakeDelayedAlreadyTransferredEvent.parameters.push(
24+
new ethereum.EventParam("_courtID", ethereum.Value.fromUnsignedBigInt(_courtID))
25+
);
26+
stakeDelayedAlreadyTransferredEvent.parameters.push(
27+
new ethereum.EventParam("_amount", ethereum.Value.fromUnsignedBigInt(_amount))
28+
);
29+
30+
return stakeDelayedAlreadyTransferredEvent;
31+
}
32+
33+
export function createStakeDelayedAlreadyTransferredWithdrawnEvent(
34+
_address: Address,
35+
_courtID: BigInt,
36+
_amount: BigInt
37+
): StakeDelayedAlreadyTransferredWithdrawn {
38+
let stakeDelayedAlreadyTransferredWithdrawnEvent = newMockEvent();
39+
40+
stakeDelayedAlreadyTransferredWithdrawnEvent.parameters = new Array();
41+
42+
stakeDelayedAlreadyTransferredWithdrawnEvent.parameters.push(
43+
new ethereum.EventParam("_address", ethereum.Value.fromAddress(_address))
44+
);
45+
stakeDelayedAlreadyTransferredWithdrawnEvent.parameters.push(
46+
new ethereum.EventParam("_courtID", ethereum.Value.fromUnsignedBigInt(_courtID))
47+
);
48+
stakeDelayedAlreadyTransferredWithdrawnEvent.parameters.push(
49+
new ethereum.EventParam("_amount", ethereum.Value.fromUnsignedBigInt(_amount))
50+
);
51+
52+
return stakeDelayedAlreadyTransferredWithdrawnEvent;
53+
}
54+
55+
export function createStakeDelayedNotTransferredEvent(
56+
_address: Address,
57+
_courtID: BigInt,
58+
_amount: BigInt
59+
): StakeDelayedNotTransferred {
60+
let stakeDelayedNotTransferredEvent = newMockEvent();
61+
62+
stakeDelayedNotTransferredEvent.parameters = new Array();
63+
64+
stakeDelayedNotTransferredEvent.parameters.push(
65+
new ethereum.EventParam("_address", ethereum.Value.fromAddress(_address))
66+
);
67+
stakeDelayedNotTransferredEvent.parameters.push(
68+
new ethereum.EventParam("_courtID", ethereum.Value.fromUnsignedBigInt(_courtID))
69+
);
70+
stakeDelayedNotTransferredEvent.parameters.push(
71+
new ethereum.EventParam("_amount", ethereum.Value.fromUnsignedBigInt(_amount))
72+
);
73+
74+
return stakeDelayedNotTransferredEvent;
75+
}
76+
77+
export function createStakeLockedEvent(_address: Address, _relativeAmount: BigInt, _unlock: boolean): StakeLocked {
78+
let stakeLockedEvent = newMockEvent();
79+
80+
stakeLockedEvent.parameters = new Array();
81+
82+
stakeLockedEvent.parameters.push(new ethereum.EventParam("_address", ethereum.Value.fromAddress(_address)));
83+
stakeLockedEvent.parameters.push(
84+
new ethereum.EventParam("_relativeAmount", ethereum.Value.fromUnsignedBigInt(_relativeAmount))
85+
);
86+
stakeLockedEvent.parameters.push(new ethereum.EventParam("_unlock", ethereum.Value.fromBoolean(_unlock)));
87+
88+
return stakeLockedEvent;
89+
}
90+
91+
export function createStakeSetEvent(_address: Address, _courtID: BigInt, _amount: BigInt): StakeSet {
92+
let stakeSetEvent = newMockEvent();
93+
94+
stakeSetEvent.parameters = new Array();
95+
96+
stakeSetEvent.parameters.push(new ethereum.EventParam("_address", ethereum.Value.fromAddress(_address)));
97+
stakeSetEvent.parameters.push(new ethereum.EventParam("_courtID", ethereum.Value.fromUnsignedBigInt(_courtID)));
98+
stakeSetEvent.parameters.push(new ethereum.EventParam("_amount", ethereum.Value.fromUnsignedBigInt(_amount)));
99+
100+
return stakeSetEvent;
101+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { assert, describe, test, clearStore, beforeAll, afterAll } from "matchstick-as/assembly/index";
2+
import { BigInt, Address } from "@graphprotocol/graph-ts";
3+
import { handleStakeSet } from "../src/SortitionModule";
4+
import { createStakeSetEvent } from "./sortition-module-utils";
5+
6+
// Tests structure (matchstick-as >=0.5.0)
7+
// https://thegraph.com/docs/en/developer/matchstick/#tests-structure-0-5-0
8+
9+
describe("Describe event", () => {
10+
beforeAll(() => {
11+
let courtId = BigInt.fromI32(1);
12+
let amount = BigInt.fromI32(1000);
13+
let jurorAddress = Address.fromString("0x922911F4f80a569a4425fa083456239838F7F003");
14+
let newStakeSetEvent = createStakeSetEvent(jurorAddress, courtId, amount);
15+
handleStakeSet(newStakeSetEvent);
16+
});
17+
18+
afterAll(() => {
19+
clearStore();
20+
});
21+
22+
// For more test scenarios, see:
23+
// https://thegraph.com/docs/en/developer/matchstick/#write-a-unit-test
24+
25+
test("Initialized created and stored", () => {
26+
assert.entityCount("Initialized", 1);
27+
28+
// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default address used in newMockEvent() function
29+
// assert.fieldEquals(
30+
// "Initialized",
31+
// "0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
32+
// "version",
33+
// "234"
34+
// )
35+
36+
// More assert options:
37+
// https://thegraph.com/docs/en/developer/matchstick/#asserts
38+
});
39+
});

0 commit comments

Comments
 (0)