-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
46 lines (40 loc) · 1.53 KB
/
index.ts
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
import {CronJob} from "cron";
import {createHash, getBpjson, getProducerJson, setProducerJson, transact} from "./src/utils";
import {BPJSON_ENDPOINT, CRON_TIME, PRODUCER_ACCOUNT_NAME, PRODUCERJSON, rpc, TIMEZONE} from "./src/config";
async function main() {
// Fetch bp.json from website
const bpjson = await getBpjson(BPJSON_ENDPOINT);
if (bpjson.producer_account_name !== PRODUCER_ACCOUNT_NAME) {
throw new Error("[PRODUCER_ACCOUNT_NAME] does not match bp.json");
}
// Check if producer account exists
try {
await rpc.get_account(PRODUCER_ACCOUNT_NAME)
} catch (e) {
throw new Error(`[PRODUCER_ACCOUNT_NAME => ${PRODUCER_ACCOUNT_NAME}] does not exist`);
}
// Check if on-chain is different from website
const producerjson = await getProducerJson(PRODUCER_ACCOUNT_NAME);
let hasChanged = true;
if (producerjson) {
console.log("comparing hashes", createHash(bpjson), createHash(producerjson));
if (createHash(bpjson) === createHash(producerjson)) {
hasChanged = false;
console.log(`[${PRODUCERJSON}] & [bp.json] hashes are the same, nothing to update`);
}
} else {
console.log('first time publishing');
}
if (hasChanged) {
// Push actions
const action = setProducerJson(PRODUCER_ACCOUNT_NAME, bpjson);
await transact([action]);
}
}
new CronJob(CRON_TIME, async () => {
main().catch(e => {
console.error(e.message);
process.exit(1);
});
}, () => {
}, true, TIMEZONE);