|
@@ -8,13 +8,7 @@ import {
|
|
|
IDistributionBucketOperatorMetadata,
|
|
|
} from '@joystream/metadata-protobuf'
|
|
|
import { CreateInterface, createType } from '@joystream/types'
|
|
|
-import {
|
|
|
- BagId,
|
|
|
- DistributionBucketFamilyId,
|
|
|
- DistributionBucketId,
|
|
|
- DynamicBagId,
|
|
|
- StaticBagId,
|
|
|
-} from '@joystream/types/storage'
|
|
|
+import { BagId, DistributionBucketFamilyId, DynamicBagId, StaticBagId } from '@joystream/types/storage'
|
|
|
import { SubmittableExtrinsic } from '@polkadot/api/types'
|
|
|
import _ from 'lodash'
|
|
|
import { Utils } from '../../utils'
|
|
@@ -169,54 +163,57 @@ export default function createFlow({ families }: InitDistributionConfig) {
|
|
|
api.signAndSendMany(updateFamilyMetadataTxs, distributionLeaderKey),
|
|
|
api.signAndSendMany(updateDynamicBagPolicyTxs, distributionLeaderKey),
|
|
|
])
|
|
|
- const bucketEntries = createBucketResults
|
|
|
+ const bucketIds = createBucketResults
|
|
|
.map((r) => {
|
|
|
- const [familyId, , bucketId] = api.getEvent(r, 'storage', 'DistributionBucketCreated').data
|
|
|
- return [familyId, bucketId] as [DistributionBucketFamilyId, DistributionBucketId]
|
|
|
- })
|
|
|
- .sort((a, b) => a[0].cmp(b[0]) || a[1].cmp(b[1]))
|
|
|
- const bucketById = new Map<number, DistributionBucketConfig>()
|
|
|
- const bucketEntriesByFamilyId = _.groupBy(bucketEntries, ([familyId]) => familyId.toString())
|
|
|
- _.mapValues(bucketEntriesByFamilyId, (entries) => {
|
|
|
- entries.forEach(([familyId, bucketId], bucketInFamilyIndex) => {
|
|
|
- const family = familyById.get(familyId.toNumber())
|
|
|
- if (!family) {
|
|
|
- throw new Error('Family config not found')
|
|
|
- }
|
|
|
- bucketById.set(bucketId.toNumber(), family.buckets[bucketInFamilyIndex])
|
|
|
+ const [, , bucketId] = api.getEvent(r, 'storage', 'DistributionBucketCreated').data
|
|
|
+ return bucketId
|
|
|
})
|
|
|
+ .sort(
|
|
|
+ (a, b) =>
|
|
|
+ a.distribution_bucket_family_id.cmp(b.distribution_bucket_family_id) ||
|
|
|
+ a.distribution_bucket_index.cmp(b.distribution_bucket_index)
|
|
|
+ )
|
|
|
+ const bucketById = new Map<string, DistributionBucketConfig>()
|
|
|
+ bucketIds.forEach((bucketId) => {
|
|
|
+ const familyId = bucketId.distribution_bucket_family_id.toNumber()
|
|
|
+ const bucketIndex = bucketId.distribution_bucket_index.toNumber()
|
|
|
+ const family = familyById.get(familyId)
|
|
|
+ if (!family) {
|
|
|
+ throw new Error(`familyById not found: ${familyId}`)
|
|
|
+ }
|
|
|
+ bucketById.set(bucketId.toString(), family.buckets[bucketIndex])
|
|
|
})
|
|
|
|
|
|
// Invite bucket operators
|
|
|
- const bucketInviteTxs = bucketEntries.map(([familyId, bucketId], i) =>
|
|
|
- api.tx.storage.inviteDistributionBucketOperator(familyId, bucketId, operatorIds[i])
|
|
|
+ const bucketInviteTxs = bucketIds.map((bucketId, i) =>
|
|
|
+ api.tx.storage.inviteDistributionBucketOperator(bucketId, operatorIds[i])
|
|
|
)
|
|
|
await api.signAndSendMany(bucketInviteTxs, distributionLeaderKey)
|
|
|
|
|
|
// Accept invitations
|
|
|
- const acceptInvitationTxs = bucketEntries.map(([familyId, bucketId], i) =>
|
|
|
- api.tx.storage.acceptDistributionBucketInvitation(operatorIds[i], familyId, bucketId)
|
|
|
+ const acceptInvitationTxs = bucketIds.map((bucketId, i) =>
|
|
|
+ api.tx.storage.acceptDistributionBucketInvitation(operatorIds[i], bucketId)
|
|
|
)
|
|
|
await api.signAndSendManyByMany(acceptInvitationTxs, operatorKeys)
|
|
|
|
|
|
// Bucket metadata and static bags
|
|
|
const bucketSetupPromises = _.flatten(
|
|
|
- bucketEntries.map(([familyId, bucketId], i) => {
|
|
|
+ bucketIds.map((bucketId, i) => {
|
|
|
const operatorId = operatorIds[i]
|
|
|
const operatorKey = operatorKeys[i]
|
|
|
- const bucketConfig = bucketById.get(bucketId.toNumber())
|
|
|
+ const bucketConfig = bucketById.get(bucketId.toString())
|
|
|
if (!bucketConfig) {
|
|
|
throw new Error('Bucket config not found')
|
|
|
}
|
|
|
const metadataBytes = Utils.metadataToBytes(DistributionBucketOperatorMetadata, bucketConfig.metadata)
|
|
|
- const setMetaTx = api.tx.storage.setDistributionOperatorMetadata(operatorId, familyId, bucketId, metadataBytes)
|
|
|
+ const setMetaTx = api.tx.storage.setDistributionOperatorMetadata(operatorId, bucketId, metadataBytes)
|
|
|
const setMetaPromise = api.signAndSendMany([setMetaTx], operatorKey)
|
|
|
const updateBagTxs = (bucketConfig.staticBags || []).map((sBagId) => {
|
|
|
return api.tx.storage.updateDistributionBucketsForBag(
|
|
|
createType<BagId, 'BagId'>('BagId', { Static: sBagId }),
|
|
|
- familyId,
|
|
|
- createType('DistributionBucketIdSet', [bucketId.toNumber()]),
|
|
|
- createType('DistributionBucketIdSet', [])
|
|
|
+ bucketId.distribution_bucket_family_id,
|
|
|
+ createType('BTreeSet<DistributionBucketIndex>', [bucketId.distribution_bucket_index]),
|
|
|
+ createType('BTreeSet<DistributionBucketIndex>', [])
|
|
|
)
|
|
|
})
|
|
|
const updateBagsPromise = api.signAndSendMany(updateBagTxs, distributionLeaderKey)
|