devInitAliceLead.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { types } from '@joystream/types'
  2. import { ApiPromise, Keyring, WsProvider } from '@polkadot/api'
  3. import { SubmittableExtrinsic } from '@polkadot/api/types'
  4. async function main() {
  5. // Init api
  6. const WS_URI = process.env.WS_URI || 'ws://127.0.0.1:9944'
  7. console.log(`Initializing the api (${WS_URI})...`)
  8. const provider = new WsProvider(WS_URI)
  9. const api = await ApiPromise.create({ provider, types })
  10. // Init ALICE keypair
  11. const keyring = new Keyring({ type: 'sr25519' })
  12. keyring.addFromUri('//Alice', { name: 'Alice' })
  13. const ALICE = keyring.getPairs()[0]
  14. let nonce = (await api.query.system.account(ALICE.address)).nonce.toNumber()
  15. const stdCall = (tx: SubmittableExtrinsic<'promise'>) => tx.signAndSend(ALICE, { nonce: nonce++ })
  16. const sudoCall = (tx: SubmittableExtrinsic<'promise'>) => api.tx.sudo.sudo(tx).signAndSend(ALICE, { nonce: nonce++ })
  17. // Create membership if not already created
  18. let aliceMemberId: number | undefined = (await api.query.members.memberIdsByControllerAccountId(ALICE.address))
  19. .toArray()[0]
  20. ?.toNumber()
  21. if (aliceMemberId === undefined) {
  22. console.log('Sending Alice member account creation extrinsic...')
  23. aliceMemberId = (await api.query.members.nextMemberId()).toNumber()
  24. await stdCall(api.tx.members.buyMembership(0, 'alice', null, null))
  25. } else {
  26. console.log(`Alice member id found: ${aliceMemberId}...`)
  27. }
  28. // Set Alice as lead if lead not already set
  29. if ((await api.query.contentDirectoryWorkingGroup.currentLead()).isNone) {
  30. const newOpeningId = (await api.query.contentDirectoryWorkingGroup.nextOpeningId()).toNumber()
  31. const newApplicationId = (await api.query.contentDirectoryWorkingGroup.nextApplicationId()).toNumber()
  32. // Create curator lead opening
  33. console.log('Sending Create Curator Lead Opening extrinsic...')
  34. await sudoCall(
  35. api.tx.contentDirectoryWorkingGroup.addOpening(
  36. { CurrentBlock: null }, // activate_at
  37. { max_review_period_length: 9999 }, // OpeningPolicyCommitment
  38. 'api-examples curator opening', // human_readable_text
  39. 'Leader' // opening_type
  40. )
  41. )
  42. // Apply to lead opening
  43. console.log('Sending Apply to Curator Lead Opening as Alice extrinsic...')
  44. await stdCall(
  45. api.tx.contentDirectoryWorkingGroup.applyOnOpening(
  46. aliceMemberId, // member id
  47. newOpeningId, // opening id
  48. ALICE.address, // address
  49. null, // opt role stake
  50. null, // opt appl. stake
  51. 'api-examples curator opening appl.' // human_readable_text
  52. )
  53. )
  54. // Begin review period
  55. console.log('Sending Begin Applicant Review extrinsic...')
  56. await sudoCall(api.tx.contentDirectoryWorkingGroup.beginApplicantReview(newOpeningId))
  57. // Fill opening
  58. console.log('Sending Fill Opening extrinsic...')
  59. await sudoCall(
  60. api.tx.contentDirectoryWorkingGroup.fillOpening(
  61. newOpeningId, // opening id
  62. api.createType('ApplicationIdSet', [newApplicationId]), // succesful applicants
  63. null // reward policy
  64. )
  65. )
  66. } else {
  67. console.log('Curators lead already exists, skipping...')
  68. }
  69. }
  70. main()
  71. .then(() => process.exit())
  72. .catch((e) => console.error(e))