Skip to content

Commit fb912bb

Browse files
committed
fix(in Progress): issues #589 #590 additionally added rabbitmq upload to the filebase exchange
1 parent a87edbd commit fb912bb

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

contracts/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
"typescript": "^4.6.4"
7070
},
7171
"dependencies": {
72-
"@kleros/vea-contracts": "^0.1.12"
72+
"@kleros/vea-contracts": "^0.1.12",
73+
"aws-sdk": "^2.1329.0",
74+
"uuid": "^9.0.0"
7375
}
7476
}

contracts/scripts/policyUpdate.ts

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { deployments, getNamedAccounts, getChainId, ethers } from "hardhat";
2+
import { PolicyRegistry } from "../typechain-types";
3+
import fs from "fs";
4+
const AWS = require("aws-sdk");
5+
const { v4: uuidv4 } = require("uuid");
6+
const id = uuidv4();
7+
const currentDate = new Date();
8+
const formattedDate = `${currentDate.getFullYear()}/${(currentDate.getMonth() + 1)
9+
.toString()
10+
.padStart(2, "0")}/${currentDate.getDate().toString().padStart(2, "0")}`;
11+
const S3_PATH = formattedDate + "/" + id + "/";
12+
13+
const s3 = new AWS.S3({
14+
endpoint: "https://s3.filebase.com",
15+
region: "us-east-1",
16+
signatureVersion: "v4",
17+
accessKeyId: process.env.FILEBASE_ACCESS_KEY,
18+
secretAccessKey: process.env.FILEBASE_SECRET_KEY,
19+
});
20+
enum HomeChains {
21+
ARBITRUM_ONE = 42161,
22+
ARBITRUM_RINKEBY = 421611,
23+
ARBITRUM_GOERLI = 421613,
24+
HARDHAT = 31337,
25+
}
26+
async function main(filePath: string) {
27+
let courtsV1;
28+
fs.readFile(filePath, "utf8", (err, jsonString) => {
29+
if (err) {
30+
console.log("File read failed:", err);
31+
return;
32+
}
33+
const json = JSON.parse(jsonString);
34+
courtsV1 = json.map((courtDetails) => ({
35+
...courtDetails,
36+
name: courtDetails.name,
37+
description: courtDetails.description,
38+
summary: courtDetails.summary,
39+
court: courtDetails.court,
40+
uri: courtDetails.uri,
41+
}));
42+
});
43+
44+
// fallback to hardhat node signers on local network
45+
// const governor = (await getNamedAccounts()).governor ?? (await ethers.getSigners())[0].address;
46+
const governor = (await ethers.getSigners())[0];
47+
const chainId = Number(await getChainId());
48+
if (!HomeChains[chainId]) {
49+
console.error(`Aborting: script is not compatible with ${chainId}`);
50+
return;
51+
} else {
52+
console.log("deploying to %s with deployer %s", HomeChains[chainId], governor);
53+
}
54+
55+
//--------uncomment once configuration is set in deployments------
56+
// const policyRegistryDeployment = await deployments.get("PolicyRegistry");
57+
const policyRegistry = (await ethers.getContractAt(
58+
"PolicyRegistry",
59+
"0xAF0F49Fe110b48bd512F00d51D141F023c9a9106" // arbitrumgoerli contract address
60+
// policyRegistryDeployment.address
61+
)) as PolicyRegistry;
62+
for (const courtObject of courtsV1) {
63+
var courtV2 = courtObject.court + 1;
64+
var filename = courtObject.name.replace(" ", "-").concat(".json");
65+
const data = { name: courtObject.name, description: courtObject.description, summary: courtObject.summary };
66+
let response = await uploadFormDataToIPFS(data, filename);
67+
console.log(response);
68+
69+
if (response && response.statusCode === 200) {
70+
try {
71+
console.log(courtV2, courtObject.name);
72+
const data = await JSON.parse(response.body);
73+
const cid = "/ipfs/" + data.message.Metadata.cid;
74+
console.log(cid, "cid");
75+
await policyRegistry.connect(governor).setPolicy(courtV2, courtObject.name, cid);
76+
} catch (error) {
77+
console.log(error);
78+
}
79+
}
80+
}
81+
}
82+
83+
const uploadFormDataToIPFS = async (data, filename) => {
84+
try {
85+
const params = {
86+
Bucket: process.env.FILEBASE_BUCKET_NAME,
87+
Key: S3_PATH + filename,
88+
ContentType: "application/json",
89+
Body: Buffer.from(JSON.stringify(data)),
90+
};
91+
const request = await s3.upload(params).promise();
92+
93+
const head_params = {
94+
Bucket: process.env.FILEBASE_BUCKET_NAME,
95+
Key: request.key,
96+
};
97+
const head = await s3.headObject(head_params).promise();
98+
99+
return {
100+
statusCode: 200,
101+
body: JSON.stringify({ message: head }),
102+
};
103+
} catch (error) {
104+
console.log(error);
105+
106+
return {
107+
statusCode: 500,
108+
body: JSON.stringify({ message: error }),
109+
};
110+
}
111+
};
112+
main("./config/policies.v1.mainnet.json")
113+
.then(() => process.exit(0))
114+
.catch((error) => {
115+
console.error(error);
116+
process.exit(1);
117+
});

0 commit comments

Comments
 (0)