From 1b5a16adcf3d70e9a03000ca177c7585d115a4a8 Mon Sep 17 00:00:00 2001 From: jaybuidl Date: Tue, 9 Jan 2024 14:40:09 +0000 Subject: [PATCH 1/2] chore: viem experiment --- contracts/.npmignore | 5 ++++ contracts/package.json | 3 +- contracts/scripts/viem-test.ts | 25 ---------------- contracts/scripts/viemTest.ts | 53 ++++++++++++++++++++++++++++++++++ yarn.lock | 16 ++++++++++ 5 files changed, 76 insertions(+), 26 deletions(-) delete mode 100644 contracts/scripts/viem-test.ts create mode 100644 contracts/scripts/viemTest.ts diff --git a/contracts/.npmignore b/contracts/.npmignore index 382ffca6e..ac1207574 100644 --- a/contracts/.npmignore +++ b/contracts/.npmignore @@ -1,2 +1,7 @@ # NOP, just force npm to disregard .gitignore # https://docs.npmjs.com/cli/v9/using-npm/developers#keeping-files-out-of-your-package + +.env* +.flaskenv* +!.env.project +!.env.vault \ No newline at end of file diff --git a/contracts/package.json b/contracts/package.json index 36dc89378..4b09fb6df 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -36,7 +36,7 @@ "export:devnet": "yarn hardhat export --export deployments/arbitrumSepoliaDevnet.ts --network arbitrumSepoliaDevnet", "export:testnet": "yarn hardhat export --export deployments/arbitrumSepolia.ts --network arbitrumSepolia", "export:mainnet": "yarn hardhat export --export deployments/arbitrum.ts --network arbitrum", - "viem:test": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch ts-node ./scripts/viem-test.ts", + "viem:test": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch ts-node ./scripts/viemTest.ts", "bot:keeper": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch hardhat run ./scripts/keeperBot.ts", "bot:relayer-from-chiado": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch hardhat run ./scripts/disputeRelayerBotFromChiado.ts", "bot:relayer-from-sepolia": "NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-fetch hardhat run ./scripts/disputeRelayerBotFromSepolia.ts", @@ -70,6 +70,7 @@ "@types/mocha": "^10.0.6", "@types/node": "^16.18.68", "@wagmi/cli": "^1.5.2", + "abitype": "^0.10.3", "chai": "^4.3.10", "dotenv": "^16.3.1", "ethereumjs-util": "^7.1.5", diff --git a/contracts/scripts/viem-test.ts b/contracts/scripts/viem-test.ts deleted file mode 100644 index b019d3ebb..000000000 --- a/contracts/scripts/viem-test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { createPublicClient, http, getContract } from "viem"; -import { arbitrumSepolia } from "viem/chains"; -import { disputeKitClassicConfig } from "../deployments/devnet.viem"; - -const main = async () => { - const client = createPublicClient({ - chain: arbitrumSepolia, - transport: http(), - }); - - const disputeKit = getContract({ - address: disputeKitClassicConfig.address[arbitrumSepolia.id], - abi: disputeKitClassicConfig.abi, - publicClient: client, - }); - - await disputeKit.read.governor().then(console.log); -}; - -main() - .then(() => process.exit(0)) - .catch((error) => { - console.error(error); - process.exit(1); - }); diff --git a/contracts/scripts/viemTest.ts b/contracts/scripts/viemTest.ts new file mode 100644 index 000000000..17c19d783 --- /dev/null +++ b/contracts/scripts/viemTest.ts @@ -0,0 +1,53 @@ +import { createPublicClient, http, getContract } from "viem"; +import { arbitrumSepolia } from "viem/chains"; +import { disputeKitClassicConfig } from "../deployments/devnet.viem"; +import { AbiFunction, AbiParametersToPrimitiveTypes, ExtractAbiFunction, FormatAbiItem } from "abitype"; + +const main = async () => { + const client = createPublicClient({ + chain: arbitrumSepolia, + transport: http(), + }); + + const disputeKit = getContract({ + address: disputeKitClassicConfig.address[arbitrumSepolia.id], + abi: disputeKitClassicConfig.abi, + publicClient: client, + }); + + await disputeKit.read.governor().then(console.log); + + // -------------------------------------------------- + + // Working around the "unknown tuple types" issue + // https://viem.sh/docs/faq.html#why-are-contract-function-args-with-fully-named-inputs-represented-as-unnamed-tuple-types-instead-of-object-types + + // Not human-readable + type DelayedStakesFunction = ExtractAbiFunction; + type Result = AbiParametersToPrimitiveTypes; + // -> readonly [bigint, boolean, `0x${string}`] + // Ideally we would get an object instead of a tuple + + // Human-readable + type FormattedFunction = FormatAbiItem; + // -> "function disputes(uint256) view returns (uint256 numberOfChoices, bool jumped, bytes extraData)" + + const getFunctionReturnParameterNames = (abi: AbiFunction[], name: string): string[] => { + const f = abi.filter((abi: AbiFunction) => abi.type === "function" && abi.name === name)[0]; // WARNING: overloaded functions confusion + return f.outputs.map((item) => item.name).filter(String) as string[]; + }; + + const createObject = (keys: string[], values: any[]) => Object.fromEntries(keys.map((k, i) => [k, values[i]])); + + const disputes = await disputeKit.read.disputes([BigInt(0)]); + const disputeParamNames = getFunctionReturnParameterNames(disputeKit.abi as unknown as AbiFunction[], "disputes"); // such type hack + const disputeObject = createObject(disputeParamNames, [...disputes]); + console.log("disputes: %O", disputeObject); +}; + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + }); diff --git a/yarn.lock b/yarn.lock index 00e4603b0..2c64cea3b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5468,6 +5468,7 @@ __metadata: "@types/mocha": ^10.0.6 "@types/node": ^16.18.68 "@wagmi/cli": ^1.5.2 + abitype: ^0.10.3 chai: ^4.3.10 dotenv: ^16.3.1 ethereumjs-util: ^7.1.5 @@ -11336,6 +11337,21 @@ __metadata: languageName: node linkType: hard +"abitype@npm:^0.10.3": + version: 0.10.3 + resolution: "abitype@npm:0.10.3" + peerDependencies: + typescript: ">=5.0.4" + zod: ^3 >=3.22.0 + peerDependenciesMeta: + typescript: + optional: true + zod: + optional: true + checksum: 3dccd692c2cf26bfa29926ef8cf5e9649d496327f77e693f953647e86ff15d70ff9d26a7296c27313ef34ca208faeaafc8cd4054dfc1bd075089e78a2d124e7d + languageName: node + linkType: hard + "abort-controller@npm:^3.0.0": version: 3.0.0 resolution: "abort-controller@npm:3.0.0" From 8184b87fe4127c870b4549a7f575e1f210885343 Mon Sep 17 00:00:00 2001 From: jaybuidl Date: Tue, 9 Jan 2024 16:34:15 +0000 Subject: [PATCH 2/2] chore: enabling hidden votes in the devnet court, script tweaks --- contracts/README.md | 6 +-- contracts/config/courts.v1.gnosischain.json | 36 ++++++++-------- contracts/config/courts.v1.mainnet.json | 48 ++++++++++----------- contracts/config/courts.v2.devnet.json | 6 +-- contracts/config/courts.v2.testnet.json | 6 +-- contracts/scripts/getCourtsV1.ts | 2 +- contracts/scripts/populateCourts.ts | 44 +++++++++++-------- 7 files changed, 77 insertions(+), 71 deletions(-) diff --git a/contracts/README.md b/contracts/README.md index 72c3c4a5a..da781dddd 100644 --- a/contracts/README.md +++ b/contracts/README.md @@ -214,6 +214,8 @@ yarn sourcify --network ({ id: courtId, - parent: result.parent.toString(), + parent: result.parent.toNumber(), hiddenVotes: result.hiddenVotes, minStake: result.minStake.toString(), alpha: result.alpha.toString(), diff --git a/contracts/scripts/populateCourts.ts b/contracts/scripts/populateCourts.ts index 38b8bb114..46d9b7b00 100644 --- a/contracts/scripts/populateCourts.ts +++ b/contracts/scripts/populateCourts.ts @@ -1,6 +1,6 @@ import { deployments, getNamedAccounts, getChainId, ethers, network } from "hardhat"; import { KlerosCore } from "../typechain-types"; -import { BigNumber } from "ethers"; +import { BigNumber, BigNumberish } from "ethers"; import courtsV1Mainnet from "../config/courts.v1.mainnet.json"; import courtsV1GnosisChain from "../config/courts.v1.gnosischain.json"; import courtsV2ArbitrumTestnet from "../config/courts.v2.testnet.json"; @@ -20,8 +20,20 @@ enum Sources { V2_TESTNET, } +type Court = { + id: number; + parent: number; + hiddenVotes: boolean; + minStake: BigNumberish; + alpha: BigNumberish; + feeForJuror: BigNumberish; + jurorsForCourtJump: BigNumberish; + timesPerPeriod: BigNumberish[]; + supportedDisputeKits?: BigNumberish[]; +}; + const from = isDevnet(network) ? Sources.V2_DEVNET : Sources.V2_TESTNET; -const TESTING_PARAMETERS = false; +const V1_DEV_PARAMETERS = false; // rename to V1_DEV_PARAMETERS const ETH_USD = BigNumber.from(1800); const DISPUTE_KIT_CLASSIC = BigNumber.from(1); const TEN_THOUSAND_GWEI = BigNumber.from(10).pow(13); @@ -40,54 +52,50 @@ async function main() { const truncateWei = (x: BigNumber) => x.div(TEN_THOUSAND_GWEI).mul(TEN_THOUSAND_GWEI); - const parametersUsdToEth = (court) => ({ + const parametersUsdToEth = (court: Court): Court => ({ ...court, minStake: truncateWei(BigNumber.from(court.minStake).div(ETH_USD)), feeForJuror: truncateWei(BigNumber.from(court.feeForJuror).div(ETH_USD)), }); - // TODO: rename this to Devnet instead of Testing - const parametersProductionToTesting = (court) => ({ + const parametersProductionToDev = (court: Court): Court => ({ ...court, - hiddenVotes: false, minStake: truncateWei(BigNumber.from(court.minStake).div(10000)), feeForJuror: truncateWei(ethers.utils.parseEther("0.00001")), timesPerPeriod: [120, 120, 120, 240], }); // WARNING: skip the Forking court at id 0, so the v1 courts are shifted by 1 - const parametersV1ToV2 = (court) => ({ + const parametersV1ToV2 = (court: Court): Court => ({ ...court, - id: BigNumber.from(court.id).add(1), - parent: BigNumber.from(court.parent).add(1), + id: court.id++, + parent: court.parent++, }); let courtsV2; switch (+from) { case Sources.V1_MAINNET: { - let courtsV1 = courtsV1Mainnet; - courtsV1 = TESTING_PARAMETERS ? courtsV1.map(parametersProductionToTesting) : courtsV1; + let courtsV1: Court[] = courtsV1Mainnet; + courtsV1 = V1_DEV_PARAMETERS ? courtsV1.map(parametersProductionToDev) : courtsV1; courtsV2 = courtsV1.map(parametersV1ToV2); break; } case Sources.V1_GNOSIS: { - let courtsV1 = courtsV1GnosisChain.map(parametersUsdToEth); - courtsV1 = TESTING_PARAMETERS ? courtsV1.map(parametersProductionToTesting) : courtsV1; + let courtsV1: Court[] = courtsV1GnosisChain.map(parametersUsdToEth); + courtsV1 = V1_DEV_PARAMETERS ? courtsV1.map(parametersProductionToDev) : courtsV1; courtsV2 = courtsV1.map(parametersV1ToV2); break; } case Sources.V2_DEVNET: { - courtsV2 = courtsV2ArbitrumDevnet.map(parametersProductionToTesting); + courtsV2 = courtsV2ArbitrumDevnet.map(parametersProductionToDev); break; } case Sources.V2_TESTNET: { - courtsV2 = TESTING_PARAMETERS - ? courtsV2ArbitrumTestnet.map(parametersProductionToTesting) - : courtsV2ArbitrumTestnet; + courtsV2 = courtsV2ArbitrumTestnet; break; } default: - return; + throw new Error("Unknown source"); } console.log("courtsV2 = %O", courtsV2);