@@ -2,7 +2,7 @@ import { DisputeKitClassic, KlerosCore, PNK, RandomizerRNG, BlockHashRNG, Sortit
2
2
import request from "graphql-request" ;
3
3
import env from "./utils/env" ;
4
4
import loggerFactory from "./utils/logger" ;
5
- import { BigNumber } from "ethers" ;
5
+ import { toBigInt , BigNumberish , getNumber } from "ethers" ;
6
6
import hre = require( "hardhat" ) ;
7
7
8
8
const { ethers } = hre ;
@@ -153,8 +153,8 @@ const handleError = (e: any) => {
153
153
const isRngReady = async ( ) => {
154
154
const { randomizerRng, blockHashRNG, sortition } = await getContracts ( ) ;
155
155
const currentRng = await sortition . rng ( ) ;
156
- if ( currentRng === randomizerRng . address ) {
157
- const requesterID = await randomizerRng . requesterToID ( sortition . address ) ;
156
+ if ( currentRng === randomizerRng . target ) {
157
+ const requesterID = await randomizerRng . requesterToID ( sortition . target ) ;
158
158
const n = await randomizerRng . randomNumbers ( requesterID ) ;
159
159
if ( Number ( n ) === 0 ) {
160
160
logger . info ( "RandomizerRNG is NOT ready yet" ) ;
@@ -163,10 +163,10 @@ const isRngReady = async () => {
163
163
logger . info ( `RandomizerRNG is ready: ${ n . toString ( ) } ` ) ;
164
164
return true ;
165
165
}
166
- } else if ( currentRng === blockHashRNG . address ) {
166
+ } else if ( currentRng === blockHashRNG . target ) {
167
167
const requestBlock = await sortition . randomNumberRequestBlock ( ) ;
168
168
const lookahead = await sortition . rngLookahead ( ) ;
169
- const n = await blockHashRNG . callStatic . receiveRandomness ( requestBlock . add ( lookahead ) ) ;
169
+ const n = await blockHashRNG . receiveRandomness . staticCall ( requestBlock + lookahead ) ;
170
170
if ( Number ( n ) === 0 ) {
171
171
logger . info ( "BlockHashRNG is NOT ready yet" ) ;
172
172
return false ;
@@ -184,21 +184,21 @@ const passPhase = async () => {
184
184
const { sortition } = await getContracts ( ) ;
185
185
let success = false ;
186
186
try {
187
- await sortition . callStatic . passPhase ( ) ;
187
+ await sortition . passPhase . staticCall ( ) ;
188
188
} catch ( e ) {
189
189
const error = e as CustomError ;
190
190
logger . info ( `passPhase: not ready yet because of ${ error ?. reason ?? error ?. errorName ?? error ?. code } ` ) ;
191
191
return success ;
192
192
}
193
- const before = await sortition . phase ( ) ;
193
+ const before = getNumber ( await sortition . phase ( ) ) ;
194
194
try {
195
- const gas = ( await sortition . estimateGas . passPhase ( ) ) . mul ( 150 ) . div ( 100 ) ; // 50% extra gas
195
+ const gas = ( ( await sortition . passPhase . estimateGas ( ) ) * toBigInt ( 150 ) ) / toBigInt ( 100 ) ; // 50% extra gas
196
196
const tx = await ( await sortition . passPhase ( { gasLimit : gas } ) ) . wait ( ) ;
197
- logger . info ( `passPhase txID: ${ tx ?. transactionHash } ` ) ;
197
+ logger . info ( `passPhase txID: ${ tx ?. hash } ` ) ;
198
198
} catch ( e ) {
199
199
handleError ( e ) ;
200
200
} finally {
201
- const after = await sortition . phase ( ) ;
201
+ const after = getNumber ( await sortition . phase ( ) ) ;
202
202
logger . info ( `passPhase: ${ PHASES [ before ] } -> ${ PHASES [ after ] } ` ) ;
203
203
success = before !== after ; // true if successful
204
204
}
@@ -209,7 +209,7 @@ const passPeriod = async (dispute: { id: string }) => {
209
209
const { core } = await getContracts ( ) ;
210
210
let success = false ;
211
211
try {
212
- await core . callStatic . passPeriod ( dispute . id ) ;
212
+ await core . passPeriod . staticCall ( dispute . id ) ;
213
213
} catch ( e ) {
214
214
const error = e as CustomError ;
215
215
logger . info (
@@ -221,9 +221,9 @@ const passPeriod = async (dispute: { id: string }) => {
221
221
}
222
222
const before = ( await core . disputes ( dispute . id ) ) . period ;
223
223
try {
224
- const gas = ( await core . estimateGas . passPeriod ( dispute . id ) ) . mul ( 150 ) . div ( 100 ) ; // 50% extra gas
224
+ const gas = ( ( await core . passPeriod . estimateGas ( dispute . id ) ) * toBigInt ( 150 ) ) / toBigInt ( 100 ) ; // 50% extra gas
225
225
const tx = await ( await core . passPeriod ( dispute . id , { gasLimit : gas } ) ) . wait ( ) ;
226
- logger . info ( `passPeriod txID: ${ tx ?. transactionHash } ` ) ;
226
+ logger . info ( `passPeriod txID: ${ tx ?. hash } ` ) ;
227
227
} catch ( e ) {
228
228
handleError ( e ) ;
229
229
} finally {
@@ -238,14 +238,14 @@ const drawJurors = async (dispute: { id: string; currentRoundIndex: string }, it
238
238
const { core } = await getContracts ( ) ;
239
239
let success = false ;
240
240
try {
241
- await core . callStatic . draw ( dispute . id , iterations , HIGH_GAS_LIMIT ) ;
241
+ await core . draw . staticCall ( dispute . id , iterations , HIGH_GAS_LIMIT ) ;
242
242
} catch ( e ) {
243
243
logger . error ( `Draw: will fail for ${ dispute . id } , skipping` ) ;
244
244
return success ;
245
245
}
246
246
try {
247
247
const tx = await ( await core . draw ( dispute . id , iterations , HIGH_GAS_LIMIT ) ) . wait ( ) ;
248
- logger . info ( `Draw txID: ${ tx ?. transactionHash } ` ) ;
248
+ logger . info ( `Draw txID: ${ tx ?. hash } ` ) ;
249
249
success = true ;
250
250
} catch ( e ) {
251
251
handleError ( e ) ;
@@ -260,14 +260,14 @@ const executeRepartitions = async (dispute: { id: string; currentRoundIndex: str
260
260
const { core } = await getContracts ( ) ;
261
261
let success = false ;
262
262
try {
263
- await core . callStatic . execute ( dispute . id , dispute . currentRoundIndex , iterations , HIGH_GAS_LIMIT ) ;
263
+ await core . execute . staticCall ( dispute . id , dispute . currentRoundIndex , iterations , HIGH_GAS_LIMIT ) ;
264
264
} catch ( e ) {
265
265
logger . error ( `Execute: will fail for ${ dispute . id } , skipping` ) ;
266
266
return success ;
267
267
}
268
268
try {
269
269
const tx = await ( await core . execute ( dispute . id , dispute . currentRoundIndex , iterations , HIGH_GAS_LIMIT ) ) . wait ( ) ;
270
- logger . info ( `Execute txID: ${ tx ?. transactionHash } ` ) ;
270
+ logger . info ( `Execute txID: ${ tx ?. hash } ` ) ;
271
271
success = true ;
272
272
} catch ( e ) {
273
273
handleError ( e ) ;
@@ -279,15 +279,15 @@ const executeRuling = async (dispute: { id: string }) => {
279
279
const { core } = await getContracts ( ) ;
280
280
let success = false ;
281
281
try {
282
- await core . callStatic . executeRuling ( dispute . id ) ;
282
+ await core . executeRuling . staticCall ( dispute . id ) ;
283
283
} catch ( e ) {
284
284
logger . error ( `ExecuteRuling: will fail for ${ dispute . id } , skipping` ) ;
285
285
return success ;
286
286
}
287
287
try {
288
- const gas = ( await core . estimateGas . executeRuling ( dispute . id ) ) . mul ( 150 ) . div ( 100 ) ; // 50% extra gas
288
+ const gas = ( ( await core . executeRuling . estimateGas ( dispute . id ) ) * toBigInt ( 150 ) ) / toBigInt ( 100 ) ; // 50% extra gas
289
289
const tx = await ( await core . executeRuling ( dispute . id , { gasLimit : gas } ) ) . wait ( ) ;
290
- logger . info ( `ExecuteRuling txID: ${ tx ?. transactionHash } ` ) ;
290
+ logger . info ( `ExecuteRuling txID: ${ tx ?. hash } ` ) ;
291
291
success = true ;
292
292
} catch ( e ) {
293
293
handleError ( e ) ;
@@ -302,9 +302,9 @@ const withdrawAppealContribution = async (
302
302
) : Promise < boolean > => {
303
303
const { disputeKitClassic : kit } = await getContracts ( ) ;
304
304
let success = false ;
305
- let amountWithdrawn = BigNumber . from ( 0 ) ;
305
+ let amountWithdrawn = toBigInt ( 0 ) ;
306
306
try {
307
- amountWithdrawn = await kit . callStatic . withdrawFeesAndRewards (
307
+ amountWithdrawn = await kit . withdrawFeesAndRewards . staticCall (
308
308
disputeId ,
309
309
contribution . contributor . id ,
310
310
roundId ,
@@ -316,7 +316,7 @@ const withdrawAppealContribution = async (
316
316
) ;
317
317
return success ;
318
318
}
319
- if ( amountWithdrawn . isZero ( ) ) {
319
+ if ( amountWithdrawn === toBigInt ( 0 ) ) {
320
320
logger . debug (
321
321
`WithdrawFeesAndRewards: no fees or rewards to withdraw for dispute #${ disputeId } , round #${ roundId } , choice ${ contribution . choice } and beneficiary ${ contribution . contributor . id } , skipping`
322
322
) ;
@@ -326,17 +326,21 @@ const withdrawAppealContribution = async (
326
326
logger . info (
327
327
`WithdrawFeesAndRewards: appeal contribution for dispute #${ disputeId } , round #${ roundId } , choice ${ contribution . choice } and beneficiary ${ contribution . contributor . id } `
328
328
) ;
329
- const gas = (
330
- await kit . estimateGas . withdrawFeesAndRewards ( disputeId , contribution . contributor . id , roundId , contribution . choice )
331
- )
332
- . mul ( 150 )
333
- . div ( 100 ) ; // 50% extra gas
329
+ const gas =
330
+ ( ( await kit . withdrawFeesAndRewards . estimateGas (
331
+ disputeId ,
332
+ contribution . contributor . id ,
333
+ roundId ,
334
+ contribution . choice
335
+ ) ) *
336
+ toBigInt ( 150 ) ) /
337
+ toBigInt ( 100 ) ; // 50% extra gas
334
338
const tx = await (
335
339
await kit . withdrawFeesAndRewards ( disputeId , contribution . contributor . id , roundId , contribution . choice , {
336
340
gasLimit : gas ,
337
341
} )
338
342
) . wait ( ) ;
339
- logger . info ( `WithdrawFeesAndRewards txID: ${ tx ?. transactionHash } ` ) ;
343
+ logger . info ( `WithdrawFeesAndRewards txID: ${ tx ?. hash } ` ) ;
340
344
success = true ;
341
345
} catch ( e ) {
342
346
handleError ( e ) ;
@@ -348,30 +352,30 @@ const executeDelayedStakes = async () => {
348
352
const { sortition } = await getContracts ( ) ;
349
353
350
354
// delayedStakes = 1 + delayedStakeWriteIndex - delayedStakeReadIndex
351
- const delayedStakesRemaining = BigNumber . from ( 1 )
352
- . add ( await sortition . delayedStakeWriteIndex ( ) )
353
- . sub ( await sortition . delayedStakeReadIndex ( ) ) ;
355
+ const delayedStakesRemaining =
356
+ toBigInt ( 1 ) + ( await sortition . delayedStakeWriteIndex ( ) ) - ( await sortition . delayedStakeReadIndex ( ) ) ;
354
357
355
- const delayedStakes = delayedStakesRemaining . lt ( MAX_DELAYED_STAKES_ITERATIONS )
356
- ? delayedStakesRemaining
357
- : BigNumber . from ( MAX_DELAYED_STAKES_ITERATIONS ) ;
358
+ const delayedStakes =
359
+ delayedStakesRemaining < MAX_DELAYED_STAKES_ITERATIONS
360
+ ? delayedStakesRemaining
361
+ : toBigInt ( MAX_DELAYED_STAKES_ITERATIONS ) ;
358
362
359
- if ( delayedStakes . eq ( 0 ) ) {
363
+ if ( delayedStakes === toBigInt ( 0 ) ) {
360
364
logger . info ( "No delayed stakes to execute" ) ;
361
365
return true ;
362
366
}
363
367
logger . info ( `Executing ${ delayedStakes } delayed stakes, ${ delayedStakesRemaining } remaining` ) ;
364
368
let success = false ;
365
369
try {
366
- await sortition . callStatic . executeDelayedStakes ( delayedStakes ) ;
370
+ await sortition . executeDelayedStakes . staticCall ( delayedStakes ) ;
367
371
} catch ( e ) {
368
372
logger . error ( `executeDelayedStakes: will fail because of ${ JSON . stringify ( e ) } ` ) ;
369
373
return success ;
370
374
}
371
375
try {
372
- const gas = ( await sortition . estimateGas . executeDelayedStakes ( delayedStakes ) ) . mul ( 150 ) . div ( 100 ) ; // 50% extra gas
376
+ const gas = ( ( await sortition . executeDelayedStakes . estimateGas ( delayedStakes ) ) * toBigInt ( 150 ) ) / toBigInt ( 100 ) ; // 50% extra gas
373
377
const tx = await ( await sortition . executeDelayedStakes ( delayedStakes , { gasLimit : gas } ) ) . wait ( ) ;
374
- logger . info ( `executeDelayedStakes txID: ${ tx ?. transactionHash } ` ) ;
378
+ logger . info ( `executeDelayedStakes txID: ${ tx ?. hash } ` ) ;
375
379
} catch ( e ) {
376
380
handleError ( e ) ;
377
381
}
@@ -381,22 +385,22 @@ const executeDelayedStakes = async () => {
381
385
const getMissingJurors = async ( dispute : { id : string ; currentRoundIndex : string } ) => {
382
386
const { core } = await getContracts ( ) ;
383
387
const { nbVotes, drawnJurors } = await core . getRoundInfo ( dispute . id , dispute . currentRoundIndex ) ;
384
- return nbVotes . sub ( drawnJurors . length ) ;
388
+ return nbVotes - toBigInt ( drawnJurors . length ) ;
385
389
} ;
386
390
387
391
const isDisputeFullyDrawn = async ( dispute : { id : string ; currentRoundIndex : string } ) : Promise < boolean > => {
388
- return ( await getMissingJurors ( dispute ) ) . eq ( 0 ) ;
392
+ return ( await getMissingJurors ( dispute ) ) === toBigInt ( 0 ) ;
389
393
} ;
390
394
391
395
const getNumberOfMissingRepartitions = async (
392
396
dispute : { id : string ; currentRoundIndex : string } ,
393
- coherentCount : BigNumber
397
+ coherentCount : BigNumberish
394
398
) => {
395
399
const { core } = await getContracts ( ) ;
396
400
const { repartitions, drawnJurors } = await core . getRoundInfo ( dispute . id , dispute . currentRoundIndex ) ;
397
- return coherentCount . eq ( 0 )
398
- ? drawnJurors . length - repartitions . toNumber ( )
399
- : 2 * drawnJurors . length - repartitions . toNumber ( ) ;
401
+ return coherentCount === toBigInt ( 0 )
402
+ ? drawnJurors . length - getNumber ( repartitions )
403
+ : 2 * drawnJurors . length - getNumber ( repartitions ) ;
400
404
} ;
401
405
402
406
const filterDisputesToSkip = ( disputes : Dispute [ ] ) => {
@@ -436,43 +440,44 @@ async function main() {
436
440
437
441
const getBlockTime = async ( ) => {
438
442
return await ethers . provider . getBlock ( "latest" ) . then ( ( block ) => {
439
- return BigNumber . from ( block . timestamp ) ;
443
+ if ( block ?. timestamp === undefined ) return 0 ;
444
+ return block ?. timestamp ;
440
445
} ) ;
441
446
} ;
442
447
443
448
const hasMinStakingTimePassed = async ( ) : Promise < boolean > => {
444
449
const minStakingTime = await sortition . minStakingTime ( ) ;
445
450
const blockTime = await getBlockTime ( ) ;
446
451
return await sortition . lastPhaseChange ( ) . then ( ( lastPhaseChange ) => {
447
- return blockTime . sub ( lastPhaseChange ) . gt ( minStakingTime ) ;
452
+ return toBigInt ( blockTime ) - lastPhaseChange > minStakingTime ;
448
453
} ) ;
449
454
} ;
450
455
451
456
const hasMaxDrawingTimePassed = async ( ) : Promise < boolean > => {
452
457
const maxDrawingTime = await sortition . maxDrawingTime ( ) ;
453
458
const blockTime = await getBlockTime ( ) ;
454
459
return await sortition . lastPhaseChange ( ) . then ( ( lastPhaseChange ) => {
455
- return blockTime . sub ( lastPhaseChange ) . gt ( maxDrawingTime ) ;
460
+ return toBigInt ( blockTime ) - lastPhaseChange > maxDrawingTime ;
456
461
} ) ;
457
462
} ;
458
463
459
464
const isPhaseStaking = async ( ) : Promise < boolean > => {
460
- return PHASES [ await sortition . phase ( ) ] === Phase . STAKING ;
465
+ return PHASES [ getNumber ( await sortition . phase ( ) ) ] === Phase . STAKING ;
461
466
} ;
462
467
463
468
const isPhaseGenerating = async ( ) : Promise < boolean > => {
464
- return PHASES [ await sortition . phase ( ) ] === Phase . GENERATING ;
469
+ return PHASES [ getNumber ( await sortition . phase ( ) ) ] === Phase . GENERATING ;
465
470
} ;
466
471
467
472
const isPhaseDrawing = async ( ) : Promise < boolean > => {
468
- return PHASES [ await sortition . phase ( ) ] === Phase . DRAWING ;
473
+ return PHASES [ getNumber ( await sortition . phase ( ) ) ] === Phase . DRAWING ;
469
474
} ;
470
475
471
476
logger . info ( "Starting up" ) ;
472
477
473
478
await sendHeartbeat ( ) ;
474
479
475
- logger . info ( `Current phase: ${ PHASES [ await sortition . phase ( ) ] } ` ) ;
480
+ logger . info ( `Current phase: ${ PHASES [ getNumber ( await sortition . phase ( ) ) ] } ` ) ;
476
481
477
482
// Retrieve the disputes which are in a non-final period
478
483
let disputes = await getNonFinalDisputes ( ) . catch ( ( e ) => handleError ( e ) ) ;
@@ -485,7 +490,7 @@ async function main() {
485
490
486
491
// Just a sanity check
487
492
const numberOfDisputesWithoutJurors = await sortition . disputesWithoutJurors ( ) ;
488
- if ( ! numberOfDisputesWithoutJurors . eq ( disputesWithoutJurors . length ) ) {
493
+ if ( ! ( numberOfDisputesWithoutJurors === toBigInt ( disputesWithoutJurors . length ) ) ) {
489
494
logger . error ( "Discrepancy between SortitionModule.disputesWithoutJurors and KlerosCore.disputes" ) ;
490
495
logger . error ( `KlerosCore.disputes without jurors = ${ disputesWithoutJurors . length } ` ) ;
491
496
logger . error ( `SortitionModule.disputesWithoutJurors = ${ numberOfDisputesWithoutJurors } ` ) ;
@@ -523,12 +528,12 @@ async function main() {
523
528
break ;
524
529
}
525
530
let numberOfMissingJurors = await getMissingJurors ( dispute ) ;
526
- if ( numberOfMissingJurors . gt ( MAX_JURORS_PER_DISPUTE ) ) {
531
+ if ( numberOfMissingJurors > MAX_JURORS_PER_DISPUTE ) {
527
532
logger . warn ( `Skipping dispute #${ dispute . id } with too many jurors to draw` ) ;
528
533
continue ;
529
534
}
530
535
do {
531
- const drawIterations = Math . min ( MAX_DRAW_ITERATIONS , numberOfMissingJurors . toNumber ( ) ) ;
536
+ const drawIterations = Math . min ( MAX_DRAW_ITERATIONS , getNumber ( numberOfMissingJurors ) ) ;
532
537
logger . info (
533
538
`Drawing ${ drawIterations } out of ${ numberOfMissingJurors } jurors needed for dispute #${ dispute . id } `
534
539
) ;
@@ -539,7 +544,7 @@ async function main() {
539
544
await delay ( ITERATIONS_COOLDOWN_PERIOD ) ; // To avoid spiking the gas price
540
545
maxDrawingTimePassed = await hasMaxDrawingTimePassed ( ) ;
541
546
numberOfMissingJurors = await getMissingJurors ( dispute ) ;
542
- } while ( ! numberOfMissingJurors . eq ( 0 ) && ! maxDrawingTimePassed ) ;
547
+ } while ( ! ( numberOfMissingJurors === toBigInt ( 0 ) ) && ! maxDrawingTimePassed ) ;
543
548
}
544
549
// At this point, either all disputes are fully drawn or max drawing time has passed
545
550
}
@@ -554,7 +559,7 @@ async function main() {
554
559
555
560
await sendHeartbeat ( ) ;
556
561
557
- logger . info ( `Current phase: ${ PHASES [ await sortition . phase ( ) ] } ` ) ;
562
+ logger . info ( `Current phase: ${ PHASES [ getNumber ( await sortition . phase ( ) ) ] } ` ) ;
558
563
559
564
for ( var dispute of disputes ) {
560
565
// ----------------------------------------------- //
@@ -583,7 +588,7 @@ async function main() {
583
588
584
589
for ( var dispute of unprocessedDisputesInExecution ) {
585
590
const { period } = await core . disputes ( dispute . id ) ;
586
- if ( period !== 4 ) {
591
+ if ( period !== toBigInt ( 4 ) ) {
587
592
logger . info ( `Skipping dispute #${ dispute . id } because it is not in the execution period` ) ;
588
593
continue ;
589
594
}
@@ -636,14 +641,14 @@ async function main() {
636
641
contributions = [ ...new Set ( contributions ) ] ;
637
642
for ( let contribution of contributions ) {
638
643
// Could be improved by pinpointing exactly which round requires a withdrawal, just try all of them for now.
639
- for ( let round = BigNumber . from ( dispute . currentRoundIndex ) ; round . gte ( 0 ) ; round = round . sub ( 1 ) ) {
644
+ for ( let round = toBigInt ( dispute . currentRoundIndex ) ; round >= 0 ; round = round - toBigInt ( 1 ) ) {
640
645
await withdrawAppealContribution ( dispute . id , round . toString ( ) , contribution ) ;
641
646
await delay ( ITERATIONS_COOLDOWN_PERIOD ) ; // To avoid spiking the gas price
642
647
}
643
648
}
644
649
}
645
650
646
- logger . info ( `Current phase: ${ PHASES [ await sortition . phase ( ) ] } ` ) ;
651
+ logger . info ( `Current phase: ${ PHASES [ getNumber ( await sortition . phase ( ) ) ] } ` ) ;
647
652
648
653
// ----------------------------------------------- //
649
654
// EXECUTE DELAYED STAKES //
0 commit comments