forked from balancer/bal-mining-scripts
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpublish.js
65 lines (57 loc) · 1.72 KB
/
publish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const fleek = require('@fleekhq/fleek-storage-js');
const requireContext = require('require-context');
const NAMESPACE = 'balancer-claim';
const SNAPSHOT_KEY = `${NAMESPACE}/snapshot`;
const config = {
apiKey: process.env.FLEEK_API_KEY,
apiSecret: process.env.FLEEK_API_SECRET
};
async function getSnapshot() {
const input = config;
input.key = SNAPSHOT_KEY;
input.getOptions = ['data'];
const result = await fleek.get(input);
return JSON.parse(result.data.toString());
}
async function uploadJson(key, body) {
const input = config;
input.key = key;
input.data = JSON.stringify(body);
const result = await fleek.upload(input);
return {
key,
ipfsHash: result.hashV0
};
}
const requireFile = requireContext(`${__dirname}/reports`, true, /_totals.json$/);
const files = Object.fromEntries(requireFile.keys().map(
(fileName) => [fileName.replace('/_totals.json', ''), requireFile(fileName)]
));
(async () => {
const snapshot = await getSnapshot();
console.log('Last snapshot', snapshot);
const promises = [];
Object.entries(files).forEach(([week, file]) => {
if (!snapshot[week]) {
console.log(`Publish week ${week}`);
const key = `${NAMESPACE}/reports/${week}`;
promises.push(uploadJson(key, file));
}
});
if (promises.length === 0) {
console.log('Already updated');
return;
}
try {
await Promise.all(promises).then(result => {
result.forEach(upload => {
const week = upload.key.replace(`${NAMESPACE}/reports/`, '');
snapshot[week] = upload.ipfsHash;
})
});
const snapshotUpload = await uploadJson(SNAPSHOT_KEY, snapshot);
console.log('Successfully published', snapshotUpload);
} catch (e) {
console.error(e);
}
})();