general.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { WsProvider, ApiPromise } from "@polkadot/api";
  2. import { types } from "@joystream/types";
  3. import { Vec } from "@polkadot/types";
  4. import { Balance, EventRecord, Extrinsic, SignedBlock } from "@polkadot/types/interfaces";
  5. async function main() {
  6. // Initialise the provider to connect to the local node
  7. const provider = new WsProvider('ws://127.0.0.1:9944');
  8. //If you want to play around on our staging network, go ahead and connect to this staging network instead.
  9. //const provider = new WsProvider('wss://testnet-rpc-2-singapore.joystream.org');
  10. // Create the API and wait until ready
  11. const api = await ApiPromise.create({ provider, types })
  12. // get finalized head of chain, as number and hash:
  13. const finalizedHeadNumber = await api.derive.chain.bestNumberFinalized()
  14. const finalizedHeadHash = await api.rpc.chain.getFinalizedHead()
  15. // get head of chain, as hash or number:
  16. const headNumber = await api.derive.chain.bestNumber()
  17. const headHash = await api.rpc.chain.getBlockHash(headNumber)
  18. console.log(
  19. `Finalized head of chain
  20. - as number: ${finalizedHeadNumber}
  21. - with hash: ${finalizedHeadHash}`
  22. )
  23. console.log(
  24. `Head of chain
  25. - as number: ${headNumber}
  26. - with hash: ${headHash}`
  27. )
  28. // get current issuance
  29. const issuance = await api.query.balances.totalIssuance() as Balance
  30. console.log(`current issuance is: ${issuance.toNumber()}tJOY`)
  31. // get events in newest block:
  32. const events = await api.query.system.events() as Vec<EventRecord>;
  33. for (let { event } of events) {
  34. const section = event.section
  35. const method = event.method
  36. const data = event.data
  37. console.log("section",section)
  38. console.log("method",method)
  39. console.log("data",data.toHuman())
  40. console.log("")
  41. }
  42. // get extrinsics in finalized head block:
  43. const getLatestBlock = await api.rpc.chain.getBlock(finalizedHeadHash) as SignedBlock
  44. const extrinsics = getLatestBlock.block.extrinsics as Vec<Extrinsic>
  45. for (let i=0; i<extrinsics.length; i++) {
  46. const section = extrinsics[i].method.section
  47. const method = extrinsics[i].method.method
  48. console.log("section",section)
  49. console.log("method",method)
  50. console.log("")
  51. // get signer of extrinsics if applicable
  52. const signer = extrinsics[i].signer
  53. if (!signer.isEmpty) {
  54. console.log("signer",signer)
  55. }
  56. }
  57. api.disconnect()
  58. }
  59. main()