update-dynamic-bag-policy.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { flags } from '@oclif/command'
  2. import { DynamicBagTypeKey } from '@joystream/types/storage'
  3. import AccountsCommandBase from '../../command-base/accounts'
  4. import DefaultCommandBase from '../../command-base/default'
  5. import { createType } from '@joystream/types'
  6. export default class LeaderUpdateDynamicBagPolicy extends AccountsCommandBase {
  7. static description = `Update dynamic bag creation policy (number of buckets by family that should store given dynamic bag type).
  8. Requires distribution working group leader permissions.`
  9. static flags = {
  10. type: flags.enum<DynamicBagTypeKey>({
  11. char: 't',
  12. description: 'Dynamic bag type',
  13. options: ['Member', 'Channel'],
  14. required: true,
  15. }),
  16. policy: flags.build({
  17. parse: (v) => {
  18. const pair = v.split(':')
  19. if (pair.length !== 2 || !/^\d+$/.test(pair[0]) || !/^\d+$/.test(pair[1])) {
  20. throw new Error(`Expected {familyId}:{numberOfBuckets} pair, got: ${v}`)
  21. }
  22. return [parseInt(pair[0]), parseInt(pair[1])] as [number, number]
  23. },
  24. })({
  25. char: 'p',
  26. description: 'Key-value pair of {familyId}:{numberOfBuckets}',
  27. multiple: true,
  28. default: [],
  29. }),
  30. ...DefaultCommandBase.flags,
  31. }
  32. static examples = [`$ joystream-distributor leader:update-dynamic-bag-policy -t Member -p 1:5 2:10 3:5`]
  33. async run(): Promise<void> {
  34. const { type, policy } = this.parse(LeaderUpdateDynamicBagPolicy).flags
  35. const leadKey = await this.getDistributorLeadKey()
  36. this.log(`Updating dynamic bag policy...`, {
  37. type,
  38. policy: policy.map(([familyId, numberOfBuckets]) => ({ familyId, numberOfBuckets })),
  39. })
  40. await this.sendAndFollowTx(
  41. await this.getDecodedPair(leadKey),
  42. this.api.tx.storage.updateFamiliesInDynamicBagCreationPolicy(
  43. type,
  44. createType('DynamicBagCreationPolicyDistributorFamiliesMap', new Map(policy))
  45. )
  46. )
  47. this.log('Dynamic bag creation policy succesfully updated!')
  48. }
  49. }