dev.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function aliceKeyPair (runtime_api) {
  2. return runtime_api.identities.keyring.addFromUri('//Alice', null, 'sr25519')
  3. }
  4. // Setup Alice account on a developement chain that was
  5. // just launched as the storage lead, and a storage provider using the same
  6. // key as the role key
  7. const init = async (runtime_api) => {
  8. const alice = aliceKeyPair(runtime_api).address
  9. const providerId = 0 // first assignable id
  10. // Check if setup already completed
  11. if (await runtime_api.workers.isRoleAccountOfStorageProvider(providerId, alice)) {
  12. console.log('Alice already setup as a storage provider')
  13. return
  14. }
  15. // make sure alice is sudo - indirectly checking this is a dev chain
  16. const sudo = await runtime_api.api.query.sudo.key()
  17. if (!sudo.eq(alice)) {
  18. throw new Error('Setup requires Alice to be sudo. Are you sure you are running a devchain?')
  19. }
  20. // register alice as a member if not already registered
  21. console.log(`Registering Alice as a member`)
  22. const aliceMemberId = 0 // first assignable id
  23. runtime_api.identities.registerMember(alice, {
  24. handle: 'alice'
  25. })
  26. // Make alice the storage lead
  27. // prepare set storage lead tx
  28. const setLeadTx = runtime_api.api.tx.storageWorkingGroup.setLead(aliceMemberId, alice)
  29. // make sudo call
  30. console.log('Setting Alice as Lead')
  31. runtime_api.signAndSend(
  32. alice,
  33. runtime_api.api.tx.sudo.sudo(setLeadTx)
  34. )
  35. // create an openinging, apply, start review, fill opening
  36. // Assumption opening id and applicant id, provider id will all be the first ids == 0
  37. // so we don't await each tx to finalize to get the ids. this allows us to
  38. // batch all the transactions into a single block.
  39. console.log('Making Alice a storage provider')
  40. const openTx = runtime_api.api.tx.storageWorkingGroup.addWorkerOpening('CurrentBlock', {
  41. application_rationing_policy: {
  42. 'max_active_applicants': 1
  43. },
  44. max_review_period_length: 1000
  45. // default values for everything else..
  46. }, 'opening0')
  47. runtime_api.signAndSend(alice, openTx)
  48. const openingId = 0 // first id
  49. const applyTx = runtime_api.api.tx.storageWorkingGroup.applyOnWorkerOpening(
  50. aliceMemberId, openingId, alice, null, null, 'alice'
  51. )
  52. runtime_api.signAndSend(alice, applyTx)
  53. const applicantId = 0 // first applicant id
  54. const reviewTx = runtime_api.api.tx.storageWorkingGroup.beginWorkerApplicantReview(openingId)
  55. runtime_api.signAndSend(alice, reviewTx)
  56. const fillTx = runtime_api.api.tx.storageWorkingGroup.fillWorkerOpening(openingId, [applicantId], null)
  57. await runtime_api.signAndSend(alice, fillTx)
  58. // const worker = await runtime_api.workers.storageWorkerByProviderId(providerId)
  59. if (await runtime_api.workers.isRoleAccountOfStorageProvider(providerId, alice)) {
  60. console.log('Setup Successful')
  61. } else { throw new Error('Setup Failed') }
  62. }
  63. const check = async (runtime_api) => {
  64. const providerId = 0
  65. const roleAccountId = aliceKeyPair(runtime_api).address
  66. if (await runtime_api.workers.isRoleAccountOfStorageProvider(providerId, roleAccountId)) {
  67. console.log('Alice is correctly setup as a storage provider')
  68. } else { throw new Error('Alice is not setup as a storage provider') }
  69. const currentLead = await runtime_api.api.query.storageWorkingGroup.currentLead()
  70. if (currentLead.isSome && currentLead.unwrap().role_account_id.eq(roleAccountId)) {
  71. console.log('Alice is correctly setup as the storage lead')
  72. } else { throw new Error('Alice is not the storage lead') }
  73. }
  74. module.exports = {
  75. init,
  76. check,
  77. aliceKeyPair
  78. }