generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind-referendum.ts
43 lines (35 loc) · 1.64 KB
/
find-referendum.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
import { ApiPromise, WsProvider } from "@polkadot/api";
import { PROVIDER_URL } from "./constants";
import { ParseRFCResult } from "./parse-RFC";
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment */
export const findReferendum = async (opts: {
parseRFCResult: ParseRFCResult;
blockHash: string;
}): Promise<null | { approved: boolean }> => {
const api = new ApiPromise({ provider: new WsProvider(PROVIDER_URL) });
await api.isReadyOrError;
const apiAt = await api.at(opts.blockHash);
// The `referendumInfoFor()` function exposes more data at blocks before the referendum got approved.
const apiAtPrev = await api.at((await api.rpc.chain.getHeader(opts.blockHash)).parentHash);
const events = (await apiAt.query.system.events()) as unknown as any[];
for (const event of events) {
if (event.event.section === "fellowshipReferenda" && event.event.method === "Confirmed") {
const [referendumIndex] = event.event.data;
const info = (await apiAtPrev.query.fellowshipReferenda.referendumInfoFor(referendumIndex)).toJSON() as Record<
string,
any
>;
const proposalHash = info?.ongoing?.proposal?.lookup?.hash; // TODO: handle inlined proposal as well
if (proposalHash === api.tx.system.remark(opts.parseRFCResult.approveRemarkText).method.hash.toHex()) {
await api.disconnect();
return { approved: true };
}
if (proposalHash === api.tx.system.remark(opts.parseRFCResult.rejectRemarkText).method.hash.toHex()) {
await api.disconnect();
return { approved: false };
}
}
}
await api.disconnect();
return null;
};