Skip to content

chore: scripts to populate the policy registry #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -160,3 +160,33 @@ This must be done for each network separately.
```bash
yarn hardhat --network <arbitrumGoerli|arbitrumRinkeby|arbitrum|goerli|rinkeby|mainnet> 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
```
4 changes: 4 additions & 0 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"author": "Kleros",
"license": "MIT",
"packageManager": "yarn@3.1.0",
"engines": {
"node": ">=16.0.0"
},
"volta": {
"node": "16.13.0"
},
Expand Down Expand Up @@ -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",
Expand Down
42 changes: 42 additions & 0 deletions contracts/scripts/getPoliciesV1.ts
Original file line number Diff line number Diff line change
@@ -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<Policy> => {
return fetch(url).then((response) => response.json());
};

const fetchPolicyUri = (court: number): Promise<string> => {
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);
});
44 changes: 44 additions & 0 deletions contracts/scripts/populatePolicyRegistry.ts
Original file line number Diff line number Diff line change
@@ -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);
});
3 changes: 2 additions & 1 deletion contracts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"outDir": "dist",
"declaration": true,
"sourceMap": true,
"noImplicitAny": false
"noImplicitAny": false,
"resolveJsonModule": true
},
"include": [
"./src",
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down