1
1
import { Address , BigInt } from "@graphprotocol/graph-ts" ;
2
+ import { ZERO } from "./utils" ;
2
3
import {
3
4
KlerosCore ,
4
5
AppealDecision ,
@@ -51,6 +52,11 @@ export function handleSubcourtCreated(event: SubcourtCreated): void {
51
52
event . params . _supportedDisputeKits . map < string > ( ( disputeKitID : BigInt ) =>
52
53
disputeKitID . toString ( )
53
54
) ;
55
+ subcourt . numberDisputes = ZERO ;
56
+ subcourt . numberStakedJurors = ZERO ;
57
+ subcourt . stake = ZERO ;
58
+ subcourt . paidETH = ZERO ;
59
+ subcourt . paidPNK = ZERO ;
54
60
subcourt . save ( ) ;
55
61
}
56
62
@@ -59,11 +65,11 @@ export function handleSubcourtModified(event: SubcourtModified): void {
59
65
if ( court ) {
60
66
const contract = KlerosCore . bind ( event . address ) ;
61
67
const courtContractState = contract . courts ( event . params . _subcourtID ) ;
62
- court . hiddenVotes = courtContractState . getHiddenVotes ( ) ;
63
- court . minStake = courtContractState . getMinStake ( ) ;
64
- court . alpha = courtContractState . getAlpha ( ) ;
65
- court . feeForJuror = courtContractState . getFeeForJuror ( ) ;
66
- court . jurorsForCourtJump = courtContractState . getJurorsForCourtJump ( ) ;
68
+ court . hiddenVotes = courtContractState . value1 ;
69
+ court . minStake = courtContractState . value2 ;
70
+ court . alpha = courtContractState . value3 ;
71
+ court . feeForJuror = courtContractState . value4 ;
72
+ court . jurorsForCourtJump = courtContractState . value5 ;
67
73
court . timesPerPeriod = contract . getTimesPerPeriod ( event . params . _subcourtID ) ;
68
74
court . save ( ) ;
69
75
}
@@ -122,7 +128,7 @@ export function handleAppealDecision(event: AppealDecision): void {
122
128
round . dispute = disputeID . toString ( ) ;
123
129
round . tokensAtStakePerJuror = roundInfo . value0 ;
124
130
round . totalFeesForJurors = roundInfo . value1 ;
125
- round . nbVotes = roundInfo . value1 . div ( subcourtStorage . getFeeForJuror ( ) ) ;
131
+ round . nbVotes = roundInfo . value1 . div ( subcourtStorage . value4 ) ;
126
132
round . totalVoted = BigInt . fromI32 ( 0 ) ;
127
133
round . repartitions = roundInfo . value2 ;
128
134
round . penalties = roundInfo . value3 ;
@@ -139,6 +145,7 @@ export function handleDisputeCreation(event: DisputeCreation): void {
139
145
const dispute = new Dispute ( disputeID . toString ( ) ) ;
140
146
const disputeStorage = contract . disputes ( disputeID ) ;
141
147
const subcourtID = disputeStorage . value0 ;
148
+ const subcourt = Court . load ( subcourtID . toString ( ) ) ;
142
149
dispute . arbitrated = event . params . _arbitrable ;
143
150
dispute . subcourtID = subcourtID . toString ( ) ;
144
151
dispute . period = "Evidence" ;
@@ -147,15 +154,17 @@ export function handleDisputeCreation(event: DisputeCreation): void {
147
154
dispute . currentRound = 0 ;
148
155
const roundInfo = contract . getRoundInfo ( disputeID , BigInt . fromString ( "0" ) ) ;
149
156
const round = new Round ( `${ disputeID . toString ( ) } -0` ) ;
150
- const subcourtStorage = contract . courts ( subcourtID ) ;
151
157
round . dispute = disputeID . toString ( ) ;
152
158
round . tokensAtStakePerJuror = roundInfo . value0 ;
153
159
round . totalFeesForJurors = roundInfo . value1 ;
154
- round . nbVotes = roundInfo . value1 . div ( subcourtStorage . getFeeForJuror ( ) ) ;
160
+ round . nbVotes = subcourt ? roundInfo . value1 . div ( subcourt . feeForJuror ) : ZERO ;
155
161
round . totalVoted = BigInt . fromI32 ( 0 ) ;
156
162
round . repartitions = roundInfo . value2 ;
157
163
round . penalties = roundInfo . value3 ;
158
164
round . disputeKitID = roundInfo . value5 . toString ( ) ;
165
+ if ( subcourt ) {
166
+ subcourt . numberDisputes = subcourt . numberDisputes . plus ( BigInt . fromI32 ( 1 ) ) ;
167
+ }
159
168
dispute . save ( ) ;
160
169
round . save ( ) ;
161
170
updateCases ( BigInt . fromI32 ( 1 ) , event . block . timestamp ) ;
@@ -170,6 +179,51 @@ export function handleNewPeriod(event: NewPeriod): void {
170
179
}
171
180
}
172
181
182
+ function updateJurorStake (
183
+ jurorAddress : string ,
184
+ subcourtID : string ,
185
+ contract : KlerosCore ,
186
+ timestamp : BigInt
187
+ ) : void {
188
+ const juror = Juror . load ( jurorAddress ) ;
189
+ const subcourt = Court . load ( subcourtID ) ;
190
+ const jurorTokens = JurorTokensPerSubcourt . load (
191
+ `${ jurorAddress } -${ subcourtID } `
192
+ ) ;
193
+ if ( juror && subcourt && jurorTokens ) {
194
+ const jurorBalance = contract . getJurorBalance (
195
+ Address . fromString ( jurorAddress ) ,
196
+ BigInt . fromString ( subcourtID )
197
+ ) ;
198
+ const previousStake = jurorTokens . staked ;
199
+ jurorTokens . staked = jurorBalance . value0 ;
200
+ jurorTokens . locked = jurorBalance . value1 ;
201
+ jurorTokens . save ( ) ;
202
+ const stakeDelta = jurorTokens . staked . minus ( previousStake ) ;
203
+ const previousTotalStake = juror . totalStake ;
204
+ juror . totalStake = juror . totalStake . plus ( stakeDelta ) ;
205
+ subcourt . stake = subcourt . stake . plus ( stakeDelta ) ;
206
+ let activeJurorsDelta : BigInt ;
207
+ let numberStakedJurorsDelta : BigInt ;
208
+ if ( previousTotalStake . equals ( ZERO ) ) {
209
+ activeJurorsDelta = BigInt . fromI32 ( 1 ) ;
210
+ numberStakedJurorsDelta = BigInt . fromI32 ( 1 ) ;
211
+ } else if ( previousStake . equals ( ZERO ) ) {
212
+ activeJurorsDelta = ZERO ;
213
+ numberStakedJurorsDelta = BigInt . fromI32 ( 1 ) ;
214
+ } else {
215
+ activeJurorsDelta = ZERO ;
216
+ numberStakedJurorsDelta = ZERO ;
217
+ }
218
+ subcourt . numberStakedJurors = subcourt . numberStakedJurors . plus (
219
+ numberStakedJurorsDelta
220
+ ) ;
221
+ updateActiveJurors ( activeJurorsDelta , timestamp ) ;
222
+ juror . save ( ) ;
223
+ subcourt . save ( ) ;
224
+ }
225
+ }
226
+
173
227
export function handleDraw ( event : DrawEvent ) : void {
174
228
const disputeID = event . params . _disputeID ;
175
229
const currentRound = event . params . _roundID ;
@@ -184,29 +238,16 @@ export function handleDraw(event: DrawEvent): void {
184
238
draw . save ( ) ;
185
239
const dispute = Dispute . load ( disputeID . toString ( ) ) ;
186
240
if ( dispute ) {
187
- updateJurorBalance (
241
+ const contract = KlerosCore . bind ( event . address ) ;
242
+ updateJurorStake (
188
243
drawnAddress . toHexString ( ) ,
189
244
dispute . subcourtID . toString ( ) ,
190
- event
245
+ contract ,
246
+ event . block . timestamp
191
247
) ;
192
248
}
193
249
}
194
250
195
- function updateJurorBalance (
196
- address : string ,
197
- subcourt : string ,
198
- event : DrawEvent
199
- ) : void {
200
- const jurorTokens = new JurorTokensPerSubcourt ( `${ address } -${ subcourt } ` ) ;
201
- const contract = KlerosCore . bind ( event . address ) ;
202
- const jurorBalance = contract . getJurorBalance (
203
- Address . fromString ( address ) ,
204
- BigInt . fromString ( subcourt )
205
- ) ;
206
- jurorTokens . locked = jurorBalance . value1 ;
207
- jurorTokens . save ( ) ;
208
- }
209
-
210
251
export function handleStakeSet ( event : StakeSet ) : void {
211
252
const jurorAddress = event . params . _address . toHexString ( ) ;
212
253
let juror = Juror . load ( jurorAddress ) ;
@@ -216,19 +257,25 @@ export function handleStakeSet(event: StakeSet): void {
216
257
}
217
258
juror . save ( ) ;
218
259
const subcourtID = event . params . _subcourtID ;
219
- const amountStaked = event . params . _newTotalStake ;
220
260
const jurorTokensID = `${ jurorAddress } -${ subcourtID . toString ( ) } ` ;
221
261
let jurorTokens = JurorTokensPerSubcourt . load ( jurorTokensID ) ;
222
262
let previousStake : BigInt ;
223
263
if ( ! jurorTokens ) {
224
264
jurorTokens = new JurorTokensPerSubcourt ( jurorTokensID ) ;
225
265
jurorTokens . juror = jurorAddress ;
226
266
jurorTokens . subcourt = subcourtID . toString ( ) ;
227
- jurorTokens . locked = BigInt . fromI32 ( 0 ) ;
228
- previousStake = BigInt . fromI32 ( 0 ) ;
267
+ jurorTokens . staked = ZERO ;
268
+ jurorTokens . locked = ZERO ;
269
+ jurorTokens . save ( ) ;
270
+ previousStake = ZERO ;
229
271
} else previousStake = jurorTokens . staked ;
230
- jurorTokens . staked = amountStaked ;
231
- jurorTokens . save ( ) ;
272
+ updateJurorStake (
273
+ jurorAddress ,
274
+ subcourtID . toString ( ) ,
275
+ KlerosCore . bind ( event . address ) ,
276
+ event . block . timestamp
277
+ ) ;
278
+ const amountStaked = event . params . _newTotalStake ;
232
279
updateStakedPNK ( getDelta ( previousStake , amountStaked ) , event . block . timestamp ) ;
233
280
}
234
281
@@ -248,4 +295,19 @@ export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
248
295
shift . tokenAmount = tokenAmount ;
249
296
shift . ethAmount = ethAmount ;
250
297
shift . save ( ) ;
298
+ const dispute = Dispute . load ( disputeID . toString ( ) ) ;
299
+ if ( dispute ) {
300
+ const subcourt = Court . load ( dispute . subcourtID . toString ( ) ) ;
301
+ if ( subcourt ) {
302
+ updateJurorStake (
303
+ jurorAddress ,
304
+ subcourt . id ,
305
+ KlerosCore . bind ( event . address ) ,
306
+ event . block . timestamp
307
+ ) ;
308
+ subcourt . paidETH = subcourt . paidETH . plus ( ethAmount ) ;
309
+ subcourt . paidPNK = subcourt . paidETH . plus ( tokenAmount ) ;
310
+ subcourt . save ( ) ;
311
+ }
312
+ }
251
313
}
0 commit comments