1- import {
2- PNK ,
3- RandomizerRNG ,
4- BlockHashRNG ,
5- SortitionModule ,
6- KlerosCore ,
7- KlerosCoreNeo ,
8- SortitionModuleNeo ,
9- DisputeKitClassic ,
10- ChainlinkRNG ,
11- } from "../typechain-types" ;
1+ import hre from "hardhat" ;
2+ import { toBigInt , BigNumberish , getNumber , BytesLike } from "ethers" ;
3+ import { SortitionModule , SortitionModuleNeo } from "../typechain-types" ;
124import env from "./utils/env" ;
135import loggerFactory from "./utils/logger" ;
14- import { toBigInt , BigNumberish , getNumber } from "ethers" ;
15- import hre = require( "hardhat" ) ;
6+ import { Cores , getContracts as getContractsForCoreType } from "./utils/contracts" ;
167
178let request : < T > ( url : string , query : string ) => Promise < T > ; // Workaround graphql-request ESM import
189const { ethers } = hre ;
@@ -44,29 +35,12 @@ const loggerOptions = env.optionalNoDefault("LOGTAIL_TOKEN_KEEPER_BOT")
4435const logger = loggerFactory . createLogger ( loggerOptions ) ;
4536
4637const getContracts = async ( ) => {
47- let core : KlerosCore | KlerosCoreNeo ;
48- let sortition : SortitionModule | SortitionModuleNeo ;
49- let disputeKitClassic : DisputeKitClassic ;
5038 const coreType = Cores [ CORE_TYPE . toUpperCase ( ) as keyof typeof Cores ] ;
51- switch ( coreType ) {
52- case Cores . NEO :
53- core = ( await ethers . getContract ( "KlerosCoreNeo" ) ) as KlerosCoreNeo ;
54- sortition = ( await ethers . getContract ( "SortitionModuleNeo" ) ) as SortitionModuleNeo ;
55- disputeKitClassic = ( await ethers . getContract ( "DisputeKitClassicNeo" ) ) as DisputeKitClassic ;
56- break ;
57- case Cores . BASE :
58- core = ( await ethers . getContract ( "KlerosCore" ) ) as KlerosCore ;
59- sortition = ( await ethers . getContract ( "SortitionModule" ) ) as SortitionModule ;
60- disputeKitClassic = ( await ethers . getContract ( "DisputeKitClassic" ) ) as DisputeKitClassic ;
61- break ;
62- default :
63- throw new Error ( "Invalid core type, must be one of base, neo" ) ;
39+ if ( coreType === Cores . UNIVERSITY ) {
40+ throw new Error ( "University is not supported yet" ) ;
6441 }
65- const chainlinkRng = await ethers . getContractOrNull < ChainlinkRNG > ( "ChainlinkRNG" ) ;
66- const randomizerRng = await ethers . getContractOrNull < RandomizerRNG > ( "RandomizerRNG" ) ;
67- const blockHashRNG = await ethers . getContractOrNull < BlockHashRNG > ( "BlockHashRNG" ) ;
68- const pnk = ( await ethers . getContract ( "PNK" ) ) as PNK ;
69- return { core, sortition, chainlinkRng, randomizerRng, blockHashRNG, disputeKitClassic, pnk } ;
42+ const contracts = await getContractsForCoreType ( hre , coreType ) ;
43+ return { ...contracts , sortition : contracts . sortition as SortitionModule | SortitionModuleNeo } ;
7044} ;
7145
7246type Contribution = {
@@ -87,18 +61,14 @@ type Dispute = {
8761} ;
8862
8963type CustomError = {
64+ data : BytesLike ;
9065 reason : string ;
9166 code : string ;
9267 errorArgs : any [ ] ;
9368 errorName : string ;
9469 errorSignature : string ;
9570} ;
9671
97- enum Cores {
98- BASE ,
99- NEO ,
100- }
101-
10272enum Phase {
10373 STAKING = "staking" ,
10474 GENERATING = "generating" ,
@@ -185,7 +155,7 @@ const handleError = (e: any) => {
185155const isRngReady = async ( ) => {
186156 const { chainlinkRng, randomizerRng, blockHashRNG, sortition } = await getContracts ( ) ;
187157 const currentRng = await sortition . rng ( ) ;
188- if ( currentRng === chainlinkRng ?. target ) {
158+ if ( currentRng === chainlinkRng ?. target && chainlinkRng !== null ) {
189159 const requestID = await chainlinkRng . lastRequestId ( ) ;
190160 const n = await chainlinkRng . randomNumbers ( requestID ) ;
191161 if ( Number ( n ) === 0 ) {
@@ -195,7 +165,7 @@ const isRngReady = async () => {
195165 logger . info ( `ChainlinkRNG is ready: ${ n . toString ( ) } ` ) ;
196166 return true ;
197167 }
198- } else if ( currentRng === randomizerRng ?. target ) {
168+ } else if ( currentRng === randomizerRng ?. target && randomizerRng !== null ) {
199169 const requestID = await randomizerRng . lastRequestId ( ) ;
200170 const n = await randomizerRng . randomNumbers ( requestID ) ;
201171 if ( Number ( n ) === 0 ) {
@@ -205,7 +175,7 @@ const isRngReady = async () => {
205175 logger . info ( `RandomizerRNG is ready: ${ n . toString ( ) } ` ) ;
206176 return true ;
207177 }
208- } else if ( currentRng === blockHashRNG ?. target ) {
178+ } else if ( currentRng === blockHashRNG ?. target && blockHashRNG !== null ) {
209179 const requestBlock = await sortition . randomNumberRequestBlock ( ) ;
210180 const lookahead = await sortition . rngLookahead ( ) ;
211181 const n = await blockHashRNG . receiveRandomness . staticCall ( requestBlock + lookahead ) ;
@@ -229,7 +199,8 @@ const passPhase = async () => {
229199 await sortition . passPhase . staticCall ( ) ;
230200 } catch ( e ) {
231201 const error = e as CustomError ;
232- logger . info ( `passPhase: not ready yet because of ${ error ?. reason ?? error ?. errorName ?? error ?. code } ` ) ;
202+ const errorDescription = sortition . interface . parseError ( error . data ) ?. signature ;
203+ logger . info ( `passPhase: not ready yet because of ${ errorDescription } ` ) ;
233204 return success ;
234205 }
235206 const before = getNumber ( await sortition . phase ( ) ) ;
@@ -254,11 +225,8 @@ const passPeriod = async (dispute: { id: string }) => {
254225 await core . passPeriod . staticCall ( dispute . id ) ;
255226 } catch ( e ) {
256227 const error = e as CustomError ;
257- logger . info (
258- `passPeriod: not ready yet for dispute ${ dispute . id } because of error ${
259- error ?. reason ?? error ?. errorName ?? error ?. code
260- } `
261- ) ;
228+ const errorDescription = core . interface . parseError ( error . data ) ?. signature ;
229+ logger . info ( `passPeriod: not ready yet for dispute ${ dispute . id } because of error ${ errorDescription } ` ) ;
262230 return success ;
263231 }
264232 const before = ( await core . disputes ( dispute . id ) ) . period ;
@@ -567,7 +535,7 @@ async function main() {
567535 }
568536 if ( await isPhaseDrawing ( ) ) {
569537 let maxDrawingTimePassed = await hasMaxDrawingTimePassed ( ) ;
570- for ( dispute of disputesWithoutJurors ) {
538+ for ( const dispute of disputesWithoutJurors ) {
571539 if ( maxDrawingTimePassed ) {
572540 logger . info ( "Max drawing time passed" ) ;
573541 break ;
0 commit comments