diff --git a/contracts/README.md b/contracts/README.md index e8178e29c..f93e90d22 100644 --- a/contracts/README.md +++ b/contracts/README.md @@ -47,7 +47,7 @@ Refresh the list of deployed contracts by running `./scripts/generateDeployments - [PolicyRegistry](https://testnet.arbiscan.io/address/0x76262035D1b280cC0b08024177b837893bcAd3DA) - [SortitionSumTreeFactory](https://testnet.arbiscan.io/address/0x48ce286978C74c288eA6Bc9a536BcC899DF8D177) -## Contributing +## Getting Started ### Install the Dependencies @@ -160,3 +160,33 @@ This must be done for each network separately. ```bash yarn hardhat --network etherscan-verify ``` + +## Ad-hoc procedures + +### Populating the policy registry + +#### 1/ Export the registry data from V1 + +```bash +yarn hardhat run scripts/getPoliciesV1.ts --network mainnet | tee policies.v1.json +``` + +#### 2/ Import the data to V2 - Local Network + +Shell 1: + +```bash +yarn hardhat node --tags Arbitration +``` + +Shell 2: + +```bash +yarn hardhat run scripts/populatePolicyRegistry.ts --network localhost +``` + +#### 3/ Import the data to V2 - Public Testnet + +```bash +yarn hardhat run scripts/populatePolicyRegistry.ts --network arbitrumRinkeby +``` diff --git a/contracts/package.json b/contracts/package.json index 82998220c..53dbd505d 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -7,6 +7,9 @@ "author": "Kleros", "license": "MIT", "packageManager": "yarn@3.1.0", + "engines": { + "node": ">=16.0.0" + }, "volta": { "node": "16.13.0" }, @@ -49,6 +52,7 @@ "hardhat-watcher": "^2.3.0", "json-schema": "^0.4.0", "mocha": "^10.0.0", + "node-fetch": "^3.2.10", "npm-run-all": "^4.1.5", "shelljs": "^0.8.5", "solhint": "^3.3.7", diff --git a/contracts/scripts/getPoliciesV1.ts b/contracts/scripts/getPoliciesV1.ts new file mode 100644 index 000000000..9b881629d --- /dev/null +++ b/contracts/scripts/getPoliciesV1.ts @@ -0,0 +1,42 @@ +import { ethers } from "hardhat"; +import fetch from "node-fetch"; + +interface Policy { + court: number; + uri: string; + name: string; + description: string; + summary: string; + requiredSkills: string; +} + +async function main() { + const policyRegistryV1 = await ethers.getContractAt("PolicyRegistry", "0xCf1f07713d5193FaE5c1653C9f61953D048BECe4"); + + const fetchPolicy = (url: string): Promise => { + return fetch(url).then((response) => response.json()); + }; + + const fetchPolicyUri = (court: number): Promise => { + return policyRegistryV1.policies(court); + }; + + const policies: Policy[] = []; + for (let court = 0; ; ++court) { + const uri = await fetchPolicyUri(court); + if (!uri) break; + + const policy = await fetchPolicy("https://ipfs.kleros.io" + uri); + policy.court = court; + policy.uri = uri; + policies.push(policy); + } + console.log(JSON.stringify(policies, null, "\t")); +} + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + }); diff --git a/contracts/scripts/populatePolicyRegistry.ts b/contracts/scripts/populatePolicyRegistry.ts new file mode 100644 index 000000000..0ce15ab48 --- /dev/null +++ b/contracts/scripts/populatePolicyRegistry.ts @@ -0,0 +1,44 @@ +import { deployments, getNamedAccounts, getChainId, ethers } from "hardhat"; +import { PolicyRegistry } from "../typechain-types"; +import policiesV1 from "../policies.v1.json"; + +enum HomeChains { + ARBITRUM_ONE = 42161, + ARBITRUM_RINKEBY = 421611, + ARBITRUM_GOERLI = 421613, + HARDHAT = 31337, +} + +async function main() { + // fallback to hardhat node signers on local network + const deployer = (await getNamedAccounts()).deployer ?? (await ethers.getSigners())[0].address; + + const chainId = Number(await getChainId()); + if (!HomeChains[chainId]) { + console.error(`Aborting: script is not compatible with ${chainId}`); + return; + } else { + console.log("deploying to %s with deployer %s", HomeChains[chainId], deployer); + } + + // WARNING: skip the Forking court at id 0, so the v1 courts are shifted by 1 + const policiesV2 = policiesV1.map((policy) => ({ ...policy, court: policy.court + 1 })); + + const policyRegistryDeployment = await deployments.get("PolicyRegistry"); + const policyRegistry = (await ethers.getContractAt( + "PolicyRegistry", + policyRegistryDeployment.address + )) as PolicyRegistry; + + for await (const policy of policiesV2) { + console.log("Populating policy for %s Court (%d): %s", policy.name, policy.court, policy.uri); + await policyRegistry.setPolicy(policy.court, policy.uri); + } +} + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + }); diff --git a/contracts/tsconfig.json b/contracts/tsconfig.json index dffa3bb53..0399f604c 100644 --- a/contracts/tsconfig.json +++ b/contracts/tsconfig.json @@ -7,7 +7,8 @@ "outDir": "dist", "declaration": true, "sourceMap": true, - "noImplicitAny": false + "noImplicitAny": false, + "resolveJsonModule": true }, "include": [ "./src", diff --git a/yarn.lock b/yarn.lock index d0e6b0d82..7e49d970c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2865,6 +2865,7 @@ __metadata: hardhat-watcher: ^2.3.0 json-schema: ^0.4.0 mocha: ^10.0.0 + node-fetch: ^3.2.10 npm-run-all: ^4.1.5 shelljs: ^0.8.5 solhint: ^3.3.7