|
@@ -1,6 +1,8 @@
|
|
|
import { BagId, DynamicBagType, DynamicBagTypeKey, Static, Dynamic } from '@joystream/types/storage'
|
|
|
import { WorkingGroup } from '@joystream/types/common'
|
|
|
-import { ApiPromise } from '@polkadot/api'
|
|
|
+import { registry } from '@joystream/types'
|
|
|
+import { createType } from '@polkadot/types'
|
|
|
+import { InterfaceTypes } from '@polkadot/types/types'
|
|
|
import ExitCodes from '../../command-base/ExitCodes'
|
|
|
import { CLIError } from '@oclif/errors'
|
|
|
|
|
@@ -26,8 +28,10 @@ export class BagIdValidationError extends CLIError {
|
|
|
* @param bagType - dynamic bag type string
|
|
|
* @returns The DynamicBagType instance.
|
|
|
*/
|
|
|
-export function parseDynamicBagType(api: ApiPromise, bagType: DynamicBagTypeKey): DynamicBagType {
|
|
|
- return api.createType('DynamicBagType', bagType)
|
|
|
+export function parseDynamicBagType(
|
|
|
+ bagType: DynamicBagTypeKey
|
|
|
+): DynamicBagType {
|
|
|
+ return createJoystreamType('DynamicBagType', bagType)
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -41,8 +45,8 @@ export function parseDynamicBagType(api: ApiPromise, bagType: DynamicBagTypeKey)
|
|
|
* @param bagId - bag ID in string format
|
|
|
* @returns The BagId instance.
|
|
|
*/
|
|
|
-export function parseBagId(api: ApiPromise, bagId: string): BagId {
|
|
|
- const parser = new BagIdParser(api, bagId)
|
|
|
+export function parseBagId(bagId: string): BagId {
|
|
|
+ const parser = new BagIdParser(bagId)
|
|
|
|
|
|
return parser.parse()
|
|
|
}
|
|
@@ -52,12 +56,10 @@ export function parseBagId(api: ApiPromise, bagId: string): BagId {
|
|
|
*/
|
|
|
class BagIdParser {
|
|
|
bagId: string
|
|
|
- api: ApiPromise
|
|
|
bagIdParts: string[]
|
|
|
|
|
|
- constructor(api: ApiPromise, bagId: string) {
|
|
|
+ constructor(bagId: string) {
|
|
|
this.bagId = bagId
|
|
|
- this.api = api
|
|
|
|
|
|
this.bagIdParts = bagId.trim().toLowerCase().split(':')
|
|
|
|
|
@@ -89,8 +91,8 @@ class BagIdParser {
|
|
|
// Try to construct static council bag ID.
|
|
|
if (this.bagIdParts[1] === 'council') {
|
|
|
if (this.bagIdParts.length === 2) {
|
|
|
- const staticBagId: Static = this.api.createType('Static', 'Council')
|
|
|
- const constructedBagId: BagId = this.api.createType('BagId', {
|
|
|
+ const staticBagId: Static = createJoystreamType('Static', 'Council')
|
|
|
+ const constructedBagId: BagId = createJoystreamType('BagId', {
|
|
|
'Static': staticBagId,
|
|
|
})
|
|
|
|
|
@@ -106,11 +108,14 @@ class BagIdParser {
|
|
|
|
|
|
for (const group of groups) {
|
|
|
if (group.toLowerCase() === actualGroup) {
|
|
|
- const workingGroup: WorkingGroup = this.api.createType('WorkingGroup', group)
|
|
|
- const staticBagId: Static = this.api.createType('Static', {
|
|
|
+ const workingGroup: WorkingGroup = createJoystreamType(
|
|
|
+ 'WorkingGroup',
|
|
|
+ group
|
|
|
+ )
|
|
|
+ const staticBagId: Static = createJoystreamType('Static', {
|
|
|
'WorkingGroup': workingGroup,
|
|
|
})
|
|
|
- const constructedBagId: BagId = this.api.createType('BagId', {
|
|
|
+ const constructedBagId: BagId = createJoystreamType('BagId', {
|
|
|
'Static': staticBagId,
|
|
|
})
|
|
|
|
|
@@ -142,8 +147,11 @@ class BagIdParser {
|
|
|
const dynamic = {} as Record<DynamicBagTypeKey, number>
|
|
|
dynamic[dynamicBagType as DynamicBagTypeKey] = parsedId
|
|
|
|
|
|
- const dynamicBagId: Dynamic = this.api.createType('Dynamic', dynamic)
|
|
|
- const constructedBagId: BagId = this.api.createType('BagId', {
|
|
|
+ const dynamicBagId: Dynamic = createJoystreamType(
|
|
|
+ 'Dynamic',
|
|
|
+ dynamic
|
|
|
+ )
|
|
|
+ const constructedBagId: BagId = createJoystreamType('BagId', {
|
|
|
'Dynamic': dynamicBagId,
|
|
|
})
|
|
|
|
|
@@ -156,3 +164,13 @@ class BagIdParser {
|
|
|
throw new BagIdValidationError(`Invalid dynamic bagId: ${this.bagId}`)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Creates Joystream type using type registry.
|
|
|
+ */
|
|
|
+function createJoystreamType<T extends keyof InterfaceTypes>(
|
|
|
+ type: T,
|
|
|
+ value: unknown
|
|
|
+): InterfaceTypes[T] {
|
|
|
+ return createType(registry, type, value)
|
|
|
+}
|