-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPollCuratorsFromMetadata.ts
60 lines (51 loc) · 1.51 KB
/
getPollCuratorsFromMetadata.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import "@polkadot/api-augment/kusama";
import { ApiPromise } from "@polkadot/api";
import { hexToString } from "@polkadot/util";
import { Codec } from "@polkadot/types/types";
import { Event, H256 } from "@polkadot/types/interfaces";
import { u32 } from "@polkadot/types/primitive";
type PollMetadata = {
pollId: number;
curators: string[];
};
export async function getPollCuratorsFromMetadata(
api: ApiPromise,
{ data }: Event
) {
const { index, hash_: hash } = data as unknown as {
index: u32;
hash_: H256;
};
const length = await getPreimageLenght(api, hash);
try {
const pollMetadata = decodePollMetadata(
await api.query.preimage.preimageFor([hash, length])
);
if (index.toNumber() === pollMetadata.pollId) {
console.log(
`
Curators for ${pollMetadata.pollId} are: %j
`,
pollMetadata.curators
);
}
} catch (error) {
console.error("Error decoding poll metadata", error);
}
}
export async function getPreimageLenght(api: ApiPromise, hash: H256) {
const requestStatus = (
await api.query.preimage.requestStatusFor(hash)
).unwrapOrDefault();
switch (true) {
case requestStatus.isRequested:
return requestStatus.asRequested.maybeLen.unwrapOrDefault();
case requestStatus.isUnrequested:
default:
return requestStatus.asUnrequested.len;
}
}
export function decodePollMetadata(preimage: Codec): PollMetadata {
const preimageAsHex = hexToString(preimage.toString());
return JSON.parse(preimageAsHex) as PollMetadata;
}