devInitContentLead.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { types } from '@joystream/types'
  2. import { ApiPromise, WsProvider } from '@polkadot/api'
  3. import { SubmittableExtrinsic } from '@polkadot/api/types'
  4. import { ExtrinsicsHelper, getAlicePair, getKeyFromSuri } from '../src/helpers/extrinsics'
  5. async function main() {
  6. // Init api
  7. const WS_URI = process.env.WS_URI || 'ws://127.0.0.1:9944'
  8. console.log(`Initializing the api (${WS_URI})...`)
  9. const provider = new WsProvider(WS_URI)
  10. const api = await ApiPromise.create({ provider, types })
  11. const LeadKeyPair = process.env.LEAD_URI ? getKeyFromSuri(process.env.LEAD_URI) : getAlicePair()
  12. const SudoKeyPair = process.env.SUDO_URI ? getKeyFromSuri(process.env.SUDO_URI) : getAlicePair()
  13. const txHelper = new ExtrinsicsHelper(api)
  14. const sudo = (tx: SubmittableExtrinsic<'promise'>) => api.tx.sudo.sudo(tx)
  15. // Create membership if not already created
  16. let memberId: number | undefined = (await api.query.members.memberIdsByControllerAccountId(LeadKeyPair.address))
  17. .toArray()[0]
  18. ?.toNumber()
  19. // Only buy membership if LEAD_URI is not provided
  20. if (memberId === undefined && process.env.LEAD_URI) {
  21. throw new Error('Make sure Controller key LEAD_URI is for a member')
  22. }
  23. if (memberId === undefined) {
  24. console.log('Perparing member account creation extrinsic...')
  25. memberId = (await api.query.members.nextMemberId()).toNumber()
  26. await txHelper.sendAndCheck(
  27. LeadKeyPair,
  28. [api.tx.members.buyMembership(0, 'alice', null, null)],
  29. 'Failed to setup member account'
  30. )
  31. }
  32. console.log(`Making member id: ${memberId} the content lead.`)
  33. // Create a new lead opening
  34. if ((await api.query.contentDirectoryWorkingGroup.currentLead()).isNone) {
  35. const newOpeningId = (await api.query.contentDirectoryWorkingGroup.nextOpeningId()).toNumber()
  36. const newApplicationId = (await api.query.contentDirectoryWorkingGroup.nextApplicationId()).toNumber()
  37. // Create curator lead opening
  38. console.log('Perparing Create Curator Lead Opening extrinsic...')
  39. await txHelper.sendAndCheck(
  40. SudoKeyPair,
  41. [
  42. sudo(
  43. api.tx.contentDirectoryWorkingGroup.addOpening(
  44. { CurrentBlock: null }, // activate_at
  45. { max_review_period_length: 9999 }, // OpeningPolicyCommitment
  46. 'bootstrap curator opening', // human_readable_text
  47. 'Leader' // opening_type
  48. )
  49. ),
  50. ],
  51. 'Failed to create Content Curators Lead opening!'
  52. )
  53. // Apply to lead opening
  54. console.log('Perparing Apply to Curator Lead Opening as extrinsic...')
  55. await txHelper.sendAndCheck(
  56. LeadKeyPair,
  57. [
  58. api.tx.contentDirectoryWorkingGroup.applyOnOpening(
  59. memberId, // member id
  60. newOpeningId, // opening id
  61. LeadKeyPair.address, // address
  62. null, // opt role stake
  63. null, // opt appl. stake
  64. 'bootstrap curator opening' // human_readable_text
  65. ),
  66. ],
  67. 'Failed to apply on lead opening!'
  68. )
  69. const extrinsics: SubmittableExtrinsic<'promise'>[] = []
  70. // Begin review period
  71. console.log('Perparing Begin Applicant Review extrinsic...')
  72. extrinsics.push(sudo(api.tx.contentDirectoryWorkingGroup.beginApplicantReview(newOpeningId)))
  73. // Fill opening
  74. console.log('Perparing Fill Opening extrinsic...')
  75. extrinsics.push(
  76. sudo(
  77. api.tx.contentDirectoryWorkingGroup.fillOpening(
  78. newOpeningId, // opening id
  79. api.createType('ApplicationIdSet', [newApplicationId]), // succesful applicants
  80. null // reward policy
  81. )
  82. )
  83. )
  84. await txHelper.sendAndCheck(SudoKeyPair, extrinsics, 'Failed to initialize Content Curators Lead!')
  85. } else {
  86. console.log('Curators lead already exists, skipping...')
  87. }
  88. }
  89. main()
  90. .then(() => process.exit())
  91. .catch((e) => console.error(e))