|
@@ -5,63 +5,74 @@ import { Constructor, Codec, RegistryTypes, Registry } from '@polkadot/types/typ
|
|
|
import { TypeRegistry } from '@polkadot/types'
|
|
|
import fs from 'fs'
|
|
|
import path from 'path'
|
|
|
+import _ from 'lodash'
|
|
|
|
|
|
const OUTPUT_PATH = path.join(__dirname, '../../augment/all/defs.json')
|
|
|
|
|
|
-function normalizeDef(registry: Registry, defOrConstructor: any, typeName: string): RegistryTypes[string] {
|
|
|
+function normalizeDef(registry: Registry, defOrConstructor: unknown, typeName: string): RegistryTypes[string] {
|
|
|
if (typeof defOrConstructor === 'string') {
|
|
|
+ let typeName: string = defOrConstructor
|
|
|
// Replace unhandled BTreeSet with Vec
|
|
|
// FIXME: Remove after updating @polkadot/api!
|
|
|
- defOrConstructor = defOrConstructor.replace('BTreeSet<', 'Vec<')
|
|
|
+ typeName = typeName.replace('BTreeSet<', 'Vec<')
|
|
|
// Workaround for "Unhandled type VecFixed"
|
|
|
- defOrConstructor = defOrConstructor.replace('[u8;32]', 'Hash')
|
|
|
- return defOrConstructor
|
|
|
- } else if (typeof defOrConstructor === 'function') {
|
|
|
- const defString = new (defOrConstructor as Constructor<Codec>)(registry).toRawType().toString()
|
|
|
- let obj: any
|
|
|
+ typeName = typeName.replace('[u8;32]', 'Hash')
|
|
|
+ return typeName
|
|
|
+ }
|
|
|
+
|
|
|
+ if (typeof defOrConstructor === 'function') {
|
|
|
+ const TypeConstructor = defOrConstructor as Constructor<Codec>
|
|
|
+ const defString = new TypeConstructor(registry).toRawType().toString()
|
|
|
+ let obj: RegistryTypes[string]
|
|
|
|
|
|
try {
|
|
|
obj = JSON.parse(defString)
|
|
|
} catch (e) {
|
|
|
- // def if just a type name:
|
|
|
+ // def is a string (type name)
|
|
|
return defString
|
|
|
}
|
|
|
|
|
|
- // def is an object:
|
|
|
- const normalizedObj: any = {}
|
|
|
- if (obj._enum && Array.isArray(obj._enum)) {
|
|
|
- // Enum as array - No need to normalize
|
|
|
+ // String (type name) - no need to normalize
|
|
|
+ if (typeof obj === 'string') {
|
|
|
return obj
|
|
|
- } else if (obj._enum && !Array.isArray(obj._enum)) {
|
|
|
- // Enum as object - normalize properties
|
|
|
- normalizedObj._enum = {}
|
|
|
- Object.entries(obj._enum).forEach(([key, value]) => {
|
|
|
+ }
|
|
|
+
|
|
|
+ // Enum as array - no need to normalize
|
|
|
+ if (typeof obj === 'object' && '_enum' in obj && Array.isArray(obj._enum)) {
|
|
|
+ return obj
|
|
|
+ }
|
|
|
+
|
|
|
+ // Enum as object - normalize properties
|
|
|
+ if (typeof obj === 'object' && '_enum' in obj && typeof obj._enum === 'object' && !Array.isArray(obj._enum)) {
|
|
|
+ const normalizedEnumDef = _.mapValues(obj._enum, (value, key) => {
|
|
|
const normalizedValue = normalizeDef(registry, value, `${typeName}[${key}]`)
|
|
|
if (typeof normalizedValue !== 'string') {
|
|
|
throw new Error(
|
|
|
`Too many nested definitions in ${typeName} enum. Did you forget to expose some types in the registry?`
|
|
|
)
|
|
|
}
|
|
|
- normalizedObj._enum[key] = normalizedValue
|
|
|
- })
|
|
|
- } else if (obj._set) {
|
|
|
- // Set - we don't need those now, but perhaps worth looking into at some point
|
|
|
+ return normalizedValue
|
|
|
+ }) as Record<string, string>
|
|
|
+ return { _enum: normalizedEnumDef }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set - not supported now
|
|
|
+ if ('_set' in obj) {
|
|
|
throw new Error('_set definitions are not supported yet!')
|
|
|
- } else {
|
|
|
- // Struct - normalize properties
|
|
|
- for (const [key, value] of Object.entries(obj)) {
|
|
|
- // Prevent interface clashes
|
|
|
+ }
|
|
|
+
|
|
|
+ // Struct - normalize properties
|
|
|
+ if (typeof obj === 'object' && !('_enum' in obj) && !('_set' in obj)) {
|
|
|
+ return _.mapValues(obj, (value, key) => {
|
|
|
const normalizedValue = normalizeDef(registry, value, `${typeName}[${key}]`)
|
|
|
if (typeof normalizedValue !== 'string') {
|
|
|
throw new Error(
|
|
|
`Too many nested definitions in ${typeName} struct. Did you forget to expose some types in the registry?`
|
|
|
)
|
|
|
}
|
|
|
- normalizedObj[key] = normalizedValue
|
|
|
- }
|
|
|
+ return normalizedValue
|
|
|
+ })
|
|
|
}
|
|
|
-
|
|
|
- return normalizedObj
|
|
|
}
|
|
|
|
|
|
throw new Error(`Unkown type entry for ${typeName} found in registry!`)
|
|
@@ -72,7 +83,7 @@ function defsFromTypes(types: RegistryTypes) {
|
|
|
registry.setKnownTypes({ types })
|
|
|
registry.register(types)
|
|
|
const defs: RegistryTypes = {}
|
|
|
- Object.entries(registry.knownTypes.types as any).forEach(([typeName, defOrConstructor]) => {
|
|
|
+ Object.entries(registry.knownTypes.types as Omit<RegistryTypes, string>).forEach(([typeName, defOrConstructor]) => {
|
|
|
const def = normalizeDef(registry, defOrConstructor, typeName)
|
|
|
defs[typeName] = def
|
|
|
})
|