fetchMembersData.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import createApi from './api'
  2. import { ApiPromise } from '@polkadot/api'
  3. import { MemberId, Membership } from '@joystream/types/augment/all'
  4. import { BlockHash } from '@polkadot/types/interfaces'
  5. import { MemberJson } from '../types'
  6. import fs from 'fs'
  7. import path from 'path'
  8. async function main() {
  9. const api = await createApi()
  10. const blockNumner = parseInt(process.env.AT_BLOCK_NUMBER || '')
  11. const hash = process.env.AT_BLOCK_NUMBER ? await api.rpc.chain.getBlockHash(blockNumner) : undefined
  12. if (!hash) {
  13. console.warn('No AT_BLOCK_NUMBER was specified! Exporting from current block...')
  14. }
  15. const members = await getAllMembers(api, hash)
  16. fs.writeFileSync(path.resolve(__dirname, '../data/members.json'), JSON.stringify(members, undefined, 4))
  17. const lastMemberId = Math.max(...members.map((m) => parseInt(m.memberId)))
  18. console.log(`${members.length} members exported & saved! Last member id: ${lastMemberId}`)
  19. await api.disconnect()
  20. }
  21. async function getAllMembers(api: ApiPromise, hash?: BlockHash): Promise<MemberJson[]> {
  22. const memberStorageEntries = hash
  23. ? await api.query.members.membershipById.entriesAt(hash)
  24. : await api.query.members.membershipById.entries()
  25. const memberEntries: [MemberId, Membership][] = memberStorageEntries.map(([{ args: [memberId] }, member]) => [
  26. memberId,
  27. member,
  28. ])
  29. const members: MemberJson[] = memberEntries.map(([memberId, member]) => ({
  30. memberId: memberId.toString(),
  31. rootAccount: member.root_account.toString(),
  32. controllerAccount: member.controller_account.toString(),
  33. handle: member.handle.toString(),
  34. avatarUri: member.avatar_uri.toString(),
  35. about: member.about.toString(),
  36. registeredAtTime: member.registered_at_time.toNumber(),
  37. registeredAtBlock: member.registered_at_block.toNumber(),
  38. }))
  39. return members
  40. }
  41. main()
  42. .then(() => process.exit())
  43. .catch(console.error)