get-events-and-extrinsics.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /*
  9. If you want to play around on our staging network, go ahead and connect to this staging network instead.
  10. const provider = new WsProvider('wss://alexandria-testing-1.joystream.app/staging/rpc:9944');
  11. There's a bunch of tokens on the account: 5HdYzMVpJv3c4omqwKKr7SpBgzrdRRYBwoNVhJB2Y8xhUbfK,
  12. with seed: "emotion soul hole loan journey what sport inject dwarf cherry ankle lesson"
  13. please transfer (what you need only) to your own account, and don't test runtime upgrades :D
  14. */
  15. // Create the API and wait until ready
  16. const api = await ApiPromise.create({ provider, types })
  17. // get all extrinsic and event types in a range of blocks (only works for last 200 blocks unless you are querying an archival node)
  18. // will take a loooong time if you check too many blocks :)
  19. const firstBlock = 1
  20. const lastBlock = 10000
  21. const eventTypes:string[] = []
  22. const extrinsicTypes: string[] = []
  23. for (let blockHeight=firstBlock; blockHeight<lastBlock; blockHeight++) {
  24. const blockHash = await api.rpc.chain.getBlockHash(blockHeight)
  25. const events = await api.query.system.events.at(blockHash) as Vec<EventRecord>;
  26. const getBlock = await api.rpc.chain.getBlock(blockHash) as SignedBlock
  27. const extrinsics = getBlock.block.extrinsics as Vec<Extrinsic>
  28. for (let { event } of events) {
  29. const section = event.section
  30. const method = event.method
  31. const eventType = section+`:`+method
  32. if (!eventTypes.includes(eventType)) {
  33. eventTypes.push(eventType)
  34. }
  35. }
  36. for (let i=0; i<extrinsics.length; i++) {
  37. const section = extrinsics[i].method.sectionName
  38. const method = extrinsics[i].method.methodName
  39. const extrinsicType = section+`:`+method
  40. if (!extrinsicTypes.includes(extrinsicType)) {
  41. extrinsicTypes.push(extrinsicType)
  42. }
  43. }
  44. }
  45. console.log("number of unique event types in range:",eventTypes.length)
  46. console.log("list of the unique event types in range:")
  47. console.log(JSON.stringify(eventTypes, null, 4))
  48. console.log("")
  49. console.log("number of unique extrinsic types in range",extrinsicTypes.length)
  50. console.log("list of the unique extrinsic types in range:")
  51. console.log(JSON.stringify(extrinsicTypes, null, 4))
  52. api.disconnect()
  53. }
  54. main()