|
| 1 | +import { deployments, getNamedAccounts, getChainId, ethers } from "hardhat"; |
| 2 | +import { PolicyRegistry } from "../typechain-types"; |
| 3 | +import fetch from "node-fetch"; |
| 4 | +import FormData from "form-data"; |
| 5 | +import fs from "fs"; |
| 6 | +const path = require("path"); |
| 7 | + |
| 8 | +enum HomeChains { |
| 9 | + ARBITRUM_ONE = 42161, |
| 10 | + ARBITRUM_RINKEBY = 421611, |
| 11 | + ARBITRUM_GOERLI = 421613, |
| 12 | + HARDHAT = 31337, |
| 13 | +} |
| 14 | +async function main(filePath: string) { |
| 15 | + let courtsV1; |
| 16 | + fs.readFile(filePath, "utf8", (err, jsonString) => { |
| 17 | + if (err) { |
| 18 | + console.log("File read failed:", err); |
| 19 | + return; |
| 20 | + } |
| 21 | + const json = JSON.parse(jsonString); |
| 22 | + courtsV1 = json.map((courtDetails) => ({ |
| 23 | + ...courtDetails, |
| 24 | + name: courtDetails.name, |
| 25 | + description: courtDetails.description, |
| 26 | + summary: courtDetails.summary, |
| 27 | + court: courtDetails.court, |
| 28 | + uri: courtDetails.uri, |
| 29 | + })); |
| 30 | + }); |
| 31 | + |
| 32 | + // fallback to hardhat node signers on local network |
| 33 | + // const governor = (await getNamedAccounts()).governor ?? (await ethers.getSigners())[0].address; |
| 34 | + const governor = (await ethers.getSigners())[0]; |
| 35 | + |
| 36 | + const chainId = Number(await getChainId()); |
| 37 | + if (!HomeChains[chainId]) { |
| 38 | + console.error(`Aborting: script is not compatible with ${chainId}`); |
| 39 | + return; |
| 40 | + } else { |
| 41 | + console.log("deploying to %s with deployer %s", HomeChains[chainId], governor); |
| 42 | + } |
| 43 | + |
| 44 | + //--------uncomment once configuration is set in deployments------ |
| 45 | + // const policyRegistryDeployment = await deployments.get("PolicyRegistry"); |
| 46 | + const policyRegistry = (await ethers.getContractAt( |
| 47 | + "PolicyRegistry", |
| 48 | + "0xAF0F49Fe110b48bd512F00d51D141F023c9a9106" // arbitrumgoerli contract address |
| 49 | + // policyRegistryDeployment.address |
| 50 | + )) as PolicyRegistry; |
| 51 | + for (const courtObject of courtsV1) { |
| 52 | + var courtV2 = courtObject.court + 1; |
| 53 | + var filename = courtObject.name.replace(" ", "-").concat(".json"); |
| 54 | + const formData = await constructData(courtObject, filename); |
| 55 | + let response = await uploadFormDataToIPFS(formData); |
| 56 | + if (response && response.statusCode === 200) { |
| 57 | + try { |
| 58 | + console.log(courtV2, courtObject.name); |
| 59 | + const data = await JSON.parse(response.body); |
| 60 | + const cid = "/ipfs/" + data["cid"]; |
| 61 | + console.log(cid, "cid"); |
| 62 | + await policyRegistry.connect(governor).setPolicy(courtV2, courtObject.name, cid); |
| 63 | + } catch (error) { |
| 64 | + console.log(error); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +const constructData = async (courtObj: any, filename: string) => { |
| 71 | + const formData = new FormData(); |
| 72 | + fs.writeFileSync( |
| 73 | + "./scripts/courtObjects/" + filename.toString(), |
| 74 | + JSON.stringify({ name: courtObj.name, description: courtObj.description, summary: courtObj.summary }, null, "\t") |
| 75 | + ); |
| 76 | + const RelativePath = path.join(__dirname, "./courtObjects/" + filename.toString()); |
| 77 | + formData.append("data", fs.createReadStream(RelativePath)); |
| 78 | + formData.append("filename", filename); |
| 79 | + return formData; |
| 80 | +}; |
| 81 | + |
| 82 | +const uploadFormDataToIPFS = async (data: FormData) => { |
| 83 | + const ESTUARY_URL = process.env.ESTUARY_URL; |
| 84 | + let auth = `Bearer ${process.env.ESTUARY_KEY}`; |
| 85 | + try { |
| 86 | + const response = await fetch(ESTUARY_URL, { |
| 87 | + method: "POST", |
| 88 | + headers: { |
| 89 | + Accept: "application/json", |
| 90 | + Authorization: auth, |
| 91 | + }, |
| 92 | + body: data, |
| 93 | + }); |
| 94 | + let parseResponse = await response.json(); |
| 95 | + console.log(parseResponse, "parse response"); |
| 96 | + return { |
| 97 | + statusCode: response.status, |
| 98 | + body: JSON.stringify(parseResponse), |
| 99 | + }; |
| 100 | + } catch (error) { |
| 101 | + console.log(error); |
| 102 | + } |
| 103 | +}; |
| 104 | +main("./config/policies.v1.mainnet.json") |
| 105 | + .then(() => process.exit(0)) |
| 106 | + .catch((error) => { |
| 107 | + console.error(error); |
| 108 | + process.exit(1); |
| 109 | + }); |
0 commit comments