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/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); 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"