Skip to content

Commit 5b1ce6a

Browse files
authored
Merge pull request #941 from kleros/feat(subgraph)/delayedStakes
Feat(subgraph): Add delayedStakes logic to subgraph
2 parents 7abd641 + 477c5a8 commit 5b1ce6a

File tree

5 files changed

+37
-55
lines changed

5 files changed

+37
-55
lines changed

subgraph/schema.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type User @entity {
6161
id: ID! # address
6262
tokens: [JurorTokensPerCourt!]! @derivedFrom(field: "juror")
6363
totalStake: BigInt!
64+
totalDelayed: BigInt!
6465
shifts: [TokenAndETHShift!]! @derivedFrom(field: "juror")
6566
draws: [Draw!]! @derivedFrom(field: "juror")
6667
activeDisputes: BigInt!
@@ -104,6 +105,7 @@ type JurorTokensPerCourt @entity {
104105
court: Court!
105106
staked: BigInt!
106107
locked: BigInt!
108+
delayed: BigInt!
107109
}
108110

109111
type Court @entity {
@@ -124,6 +126,7 @@ type Court @entity {
124126
stakedJurors: [JurorTokensPerCourt!]! @derivedFrom(field: "court")
125127
numberStakedJurors: BigInt!
126128
stake: BigInt!
129+
delayedStake: BigInt!
127130
paidETH: BigInt!
128131
paidPNK: BigInt!
129132
}

subgraph/src/KlerosCore.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,9 @@ import { createCourtFromEvent, getFeeForJuror } from "./entities/Court";
1818
import { createDisputeKitFromEvent, filterSupportedDisputeKits } from "./entities/DisputeKit";
1919
import { createDisputeFromEvent } from "./entities/Dispute";
2020
import { createRoundFromRoundInfo } from "./entities/Round";
21-
import {
22-
updateCases,
23-
updatePaidETH,
24-
updateStakedPNK,
25-
updateRedistributedPNK,
26-
updateCasesRuled,
27-
updateCasesVoting,
28-
getDelta,
29-
} from "./datapoint";
21+
import { updateCases, updatePaidETH, updateRedistributedPNK, updateCasesRuled, updateCasesVoting } from "./datapoint";
3022
import { addUserActiveDispute, ensureUser, resolveUserDispute } from "./entities/User";
31-
import { ensureJurorTokensPerCourt, updateJurorStake } from "./entities/JurorTokensPerCourt";
23+
import { updateJurorDelayedStake, updateJurorStake } from "./entities/JurorTokensPerCourt";
3224
import { createDrawFromEvent } from "./entities/Draw";
3325
import { createTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
3426
import { updateArbitrableCases } from "./entities/Arbitrable";
@@ -144,17 +136,18 @@ export function handleDraw(event: DrawEvent): void {
144136
export function handleStakeSet(event: StakeSet): void {
145137
const jurorAddress = event.params._address.toHexString();
146138
ensureUser(jurorAddress);
147-
const courtID = event.params._courtID;
139+
const courtID = event.params._courtID.toString();
148140

149141
updateJurorStake(jurorAddress, courtID.toString(), KlerosCore.bind(event.address), event.block.timestamp);
142+
143+
// Check if the transaction the event comes from is executeDelayedStakes
144+
if (event.transaction.input.toHexString().substring(0, 10) === "0x35975f4a") {
145+
updateJurorDelayedStake(jurorAddress, courtID, ZERO.minus(event.params._amount));
146+
}
150147
}
151148

152149
export function handleStakeDelayed(event: StakeDelayed): void {
153-
const jurorAddress = event.params._address.toHexString();
154-
ensureUser(jurorAddress);
155-
const courtID = event.params._courtID;
156-
157-
// TODO: Update stake delayed
150+
updateJurorDelayedStake(event.params._address.toString(), event.params._courtID.toString(), event.params._amount);
158151
}
159152

160153
export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {

subgraph/src/entities/Court.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ export function createCourtFromEvent(event: CourtCreated): void {
1212
court.feeForJuror = event.params._feeForJuror;
1313
court.jurorsForCourtJump = event.params._jurorsForCourtJump;
1414
court.timesPerPeriod = event.params._timesPerPeriod;
15-
court.supportedDisputeKits = event.params._supportedDisputeKits.map<string>(
16-
(value) => value.toString()
17-
);
15+
court.supportedDisputeKits = event.params._supportedDisputeKits.map<string>((value) => value.toString());
1816
court.numberDisputes = ZERO;
1917
court.numberStakedJurors = ZERO;
2018
court.stake = ZERO;
19+
court.delayedStake = ZERO;
2120
court.paidETH = ZERO;
2221
court.paidPNK = ZERO;
2322
court.save();

subgraph/src/entities/JurorTokensPerCourt.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,37 @@ import { updateActiveJurors, getDelta, updateStakedPNK } from "../datapoint";
55
import { ensureUser } from "./User";
66
import { ONE, ZERO } from "../utils";
77

8-
export function ensureJurorTokensPerCourt(
9-
jurorAddress: string,
10-
courtID: string
11-
): JurorTokensPerCourt {
8+
export function ensureJurorTokensPerCourt(jurorAddress: string, courtID: string): JurorTokensPerCourt {
129
const id = `${jurorAddress}-${courtID}`;
13-
let jurorTokens = JurorTokensPerCourt.load(id);
10+
const jurorTokens = JurorTokensPerCourt.load(id);
1411

1512
if (jurorTokens) {
1613
return jurorTokens;
1714
}
1815

19-
jurorTokens = new JurorTokensPerCourt(id);
20-
jurorTokens.juror = jurorAddress;
21-
jurorTokens.court = courtID;
22-
jurorTokens.staked = ZERO;
23-
jurorTokens.locked = ZERO;
24-
jurorTokens.save();
25-
26-
return jurorTokens;
16+
return createJurorTokensPerCourt(jurorAddress, courtID);
2717
}
2818

29-
export function createJurorTokensPerCourt(
30-
jurorAddress: string,
31-
courtID: string
32-
): JurorTokensPerCourt {
19+
export function createJurorTokensPerCourt(jurorAddress: string, courtID: string): JurorTokensPerCourt {
3320
const id = `${jurorAddress}-${courtID}`;
3421

3522
const jurorTokens = new JurorTokensPerCourt(id);
3623
jurorTokens.juror = jurorAddress;
3724
jurorTokens.court = courtID;
3825
jurorTokens.staked = ZERO;
3926
jurorTokens.locked = ZERO;
27+
jurorTokens.delayed = ZERO;
4028
jurorTokens.save();
4129

4230
return jurorTokens;
4331
}
4432

45-
export function updateJurorStake(
46-
jurorAddress: string,
47-
courtID: string,
48-
contract: KlerosCore,
49-
timestamp: BigInt
50-
): void {
33+
export function updateJurorStake(jurorAddress: string, courtID: string, contract: KlerosCore, timestamp: BigInt): void {
5134
const juror = ensureUser(jurorAddress);
5235
const court = Court.load(courtID);
5336
if (!court) return;
5437
const jurorTokens = ensureJurorTokensPerCourt(jurorAddress, courtID);
55-
const jurorBalance = contract.getJurorBalance(
56-
Address.fromString(jurorAddress),
57-
BigInt.fromString(courtID)
58-
);
38+
const jurorBalance = contract.getJurorBalance(Address.fromString(jurorAddress), BigInt.fromString(courtID));
5939
const previousStake = jurorTokens.staked;
6040
const previousTotalStake = juror.totalStake;
6141
jurorTokens.staked = jurorBalance.value0;
@@ -67,16 +47,26 @@ export function updateJurorStake(
6747
court.stake = court.stake.plus(stakeDelta);
6848
updateStakedPNK(stakeDelta, timestamp);
6949
const activeJurorsDelta = getActivityDelta(previousTotalStake, newTotalStake);
70-
const stakedJurorsDelta = getActivityDelta(
71-
previousStake,
72-
jurorBalance.value0
73-
);
50+
const stakedJurorsDelta = getActivityDelta(previousStake, jurorBalance.value0);
7451
court.numberStakedJurors = court.numberStakedJurors.plus(stakedJurorsDelta);
7552
updateActiveJurors(activeJurorsDelta, timestamp);
7653
juror.save();
7754
court.save();
7855
}
7956

57+
export function updateJurorDelayedStake(jurorAddress: string, courtID: string, amount: BigInt): void {
58+
const juror = ensureUser(jurorAddress);
59+
const court = Court.load(courtID);
60+
if (!court) return;
61+
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);
65+
jurorTokens.save();
66+
juror.save();
67+
court.save();
68+
}
69+
8070
function getActivityDelta(previousStake: BigInt, newStake: BigInt): BigInt {
8171
if (previousStake.gt(ZERO)) {
8272
return newStake.gt(ZERO) ? ZERO : BigInt.fromI32(-1);

subgraph/src/entities/User.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function ensureUser(id: string): User {
1515
export function createUserFromAddress(id: string): User {
1616
const user = new User(id);
1717
user.totalStake = ZERO;
18+
user.totalDelayed = ZERO;
1819
user.activeDisputes = ZERO;
1920
user.disputes = [];
2021
user.resolvedDisputes = [];
@@ -36,11 +37,7 @@ export function addUserActiveDispute(id: string, disputeID: string): void {
3637
user.save();
3738
}
3839

39-
export function resolveUserDispute(
40-
id: string,
41-
tokenAmount: BigInt,
42-
disputeID: string
43-
): void {
40+
export function resolveUserDispute(id: string, tokenAmount: BigInt, disputeID: string): void {
4441
const user = ensureUser(id);
4542
if (user.resolvedDisputes.includes(disputeID)) {
4643
return;

0 commit comments

Comments
 (0)