get-events-and-extrinsics.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { WsProvider, ApiPromise } from "@polkadot/api";
  2. import { types } from "@joystream/types";
  3. import { Vec } from "@polkadot/types";
  4. import { 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 all extrinsic and event types in a range of blocks (only works for last 200 blocks unless you are querying an archival node)
  13. // will take a loooong time if you check too many blocks :)
  14. const firstBlock = 800000
  15. const lastBlock = 801000
  16. const eventTypes:string[] = []
  17. const extrinsicTypes: string[] = []
  18. for (let blockHeight=firstBlock; blockHeight<lastBlock; blockHeight++) {
  19. const blockHash = await api.rpc.chain.getBlockHash(blockHeight)
  20. const events = await api.query.system.events.at(blockHash) as Vec<EventRecord>;
  21. const getBlock = await api.rpc.chain.getBlock(blockHash) as SignedBlock
  22. const extrinsics = getBlock.block.extrinsics as Vec<Extrinsic>
  23. for (let { event } of events) {
  24. const section = event.section
  25. const method = event.method
  26. const eventType = section+`:`+method
  27. if (!eventTypes.includes(eventType)) {
  28. eventTypes.push(eventType)
  29. }
  30. }
  31. for (let i=0; i<extrinsics.length; i++) {
  32. const section = extrinsics[i].method.section
  33. const method = extrinsics[i].method.method
  34. const extrinsicType = section+`:`+method
  35. if (!extrinsicTypes.includes(extrinsicType)) {
  36. extrinsicTypes.push(extrinsicType)
  37. }
  38. }
  39. }
  40. console.log("number of unique event types in range:",eventTypes.length)
  41. console.log("list of the unique event types in range:")
  42. console.log(JSON.stringify(eventTypes, null, 4))
  43. console.log("")
  44. console.log("number of unique extrinsic types in range",extrinsicTypes.length)
  45. console.log("list of the unique extrinsic types in range:")
  46. console.log(JSON.stringify(extrinsicTypes, null, 4))
  47. api.disconnect()
  48. }
  49. main()