Skip to content

Commit d11339d

Browse files
committed
fix: keeperBot handling of custom errors, use the getContracts() utility
1 parent ae5179c commit d11339d

File tree

2 files changed

+19
-55
lines changed

2 files changed

+19
-55
lines changed

contracts/scripts/keeperBot.ts

+17-49
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
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";
124
import env from "./utils/env";
135
import 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

178
let request: <T>(url: string, query: string) => Promise<T>; // Workaround graphql-request ESM import
189
const { ethers } = hre;
@@ -44,29 +35,12 @@ const loggerOptions = env.optionalNoDefault("LOGTAIL_TOKEN_KEEPER_BOT")
4435
const logger = loggerFactory.createLogger(loggerOptions);
4536

4637
const 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

7246
type Contribution = {
@@ -87,18 +61,14 @@ type Dispute = {
8761
};
8862

8963
type 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-
10272
enum Phase {
10373
STAKING = "staking",
10474
GENERATING = "generating",
@@ -185,7 +155,7 @@ const handleError = (e: any) => {
185155
const 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;

contracts/scripts/populatePolicyRegistry.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ task("populate:policy-registry", "Populates the policy registry for each court")
3636
types.int
3737
)
3838
.setAction(async (taskArgs, hre) => {
39-
const { deployments, getNamedAccounts, getChainId, ethers, network } = hre;
39+
const { getNamedAccounts, getChainId, ethers, network } = hre;
4040

4141
// fallback to hardhat node signers on local network
4242
const deployer = (await getNamedAccounts()).deployer ?? (await ethers.getSigners())[0].address;
@@ -102,11 +102,7 @@ task("populate:policy-registry", "Populates the policy registry for each court")
102102
console.log(`Keeping only the first ${end - start} courts, starting from ${start}`);
103103
policiesV2 = policiesV2.slice(start, end);
104104

105-
const policyRegistryDeployment = await deployments.get("PolicyRegistry");
106-
const policyRegistry = (await ethers.getContractAt(
107-
"PolicyRegistry",
108-
policyRegistryDeployment.address
109-
)) as PolicyRegistry;
105+
const policyRegistry = await ethers.getContract<PolicyRegistry>("PolicyRegistry");
110106

111107
for await (const policy of policiesV2) {
112108
console.log("Populating policy for %s Court (%d): %s", policy.name, policy.court, policy.uri);

0 commit comments

Comments
 (0)