forked from balancer/bal-mining-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.js
91 lines (80 loc) · 2.45 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require('dotenv').config();
const fleek = require('@fleekhq/fleek-storage-js');
const requireContext = require('require-context');
const network = process.env.NETWORK || 'mainnet';
const config = {
apiKey: process.env.FLEEK_API_KEY,
apiSecret: process.env.FLEEK_API_SECRET,
bucket: 'balancer-team-bucket',
};
const networkStr = network === 'kovan' ? '-kovan' : '';
const NAMESPACE = `balancer-claim${networkStr}`;
const SNAPSHOT_KEY = `${NAMESPACE}/snapshot`;
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 offsetMainnet = 20;
const requireFile = requireContext(
`${__dirname}/reports${networkStr}`,
true,
/_totals.json$/
);
const files = Object.fromEntries(
requireFile
.keys()
.map((fileName) => [
fileName.replace('/_totals.json', ''),
requireFile(fileName),
])
.filter(
(file) => network === 'mainnet' && parseInt(file[0]) > offsetMainnet
)
.map((file) =>
network === 'mainnet'
? [(parseInt(file[0]) - offsetMainnet).toString(), file[1]]
: file
)
);
console.log(Object.keys(files));
(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);
}
})();