forked from Joystream/community-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneral.ts
More file actions
76 lines (66 loc) · 2.42 KB
/
general.ts
File metadata and controls
76 lines (66 loc) · 2.42 KB
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
import { WsProvider, ApiPromise } from "@polkadot/api";
import { types } from "@joystream/types";
import { Vec } from "@polkadot/types";
import {
Balance,
EventRecord,
Extrinsic,
SignedBlock,
} from "@polkadot/types/interfaces";
async function main() {
// Initialise the provider to connect to the local node
const provider = new WsProvider("ws://127.0.0.1:9944");
//If you want to play around on our staging network, go ahead and connect to this staging network instead.
//const provider = new WsProvider('wss://testnet-rpc-2-singapore.joystream.org');
// Create the API and wait until ready
const api = await ApiPromise.create({ provider, types });
// get finalized head of chain, as number and hash:
const finalizedHeadNumber = await api.derive.chain.bestNumberFinalized();
const finalizedHeadHash = await api.rpc.chain.getFinalizedHead();
// get head of chain, as hash or number:
const headNumber = await api.derive.chain.bestNumber();
const headHash = await api.rpc.chain.getBlockHash(headNumber);
console.log(
`Finalized head of chain
- as number: ${finalizedHeadNumber}
- with hash: ${finalizedHeadHash}`
);
console.log(
`Head of chain
- as number: ${headNumber}
- with hash: ${headHash}`
);
// get current issuance
const issuance = (await api.query.balances.totalIssuance()) as Balance;
console.log(`current issuance is: ${issuance.toNumber()}tJOY`);
// get events in newest block:
const events = (await api.query.system.events()) as Vec<EventRecord>;
for (let { event } of events) {
const section = event.section;
const method = event.method;
const data = event.data;
console.log("section", section);
console.log("method", method);
console.log("data", data.toHuman());
console.log("");
}
// get extrinsics in finalized head block:
const getLatestBlock = (await api.rpc.chain.getBlock(
finalizedHeadHash
)) as SignedBlock;
const extrinsics = getLatestBlock.block.extrinsics as Vec<Extrinsic>;
for (let i = 0; i < extrinsics.length; i++) {
const section = extrinsics[i].method.section;
const method = extrinsics[i].method.method;
console.log("section", section);
console.log("method", method);
console.log("");
// get signer of extrinsics if applicable
const signer = extrinsics[i].signer;
if (!signer.isEmpty) {
console.log("signer", signer);
}
}
await api.disconnect();
}
main();