definitions.ts 1013 B

123456789101112131415161718192021222324252627282930
  1. import { Struct, TypeRegistry } from '@polkadot/types'
  2. import { RegistryTypes } from '@polkadot/types/types'
  3. import defs from'./defs.json'
  4. const emptyStruct = new Struct(new TypeRegistry(), {})
  5. // Prevents errors in interfaces that will be generated based on those types
  6. // ie. an interface cannot have fields like "values" and extend Struct (which also has "values") at the same time
  7. const normalizedDefs = {} as RegistryTypes
  8. Object.entries(defs).forEach(([typeName, typeDef]) => {
  9. if (typeof typeDef !== 'string' && !typeDef.hasOwnProperty('_enum') && !typeDef.hasOwnProperty('_set')) {
  10. // definition is a struct:
  11. const normalizedDef = {} as Record<string, string>
  12. Object.entries(typeDef).forEach(([key, value]) => {
  13. if ((emptyStruct as any)[key] !== undefined) {
  14. return
  15. }
  16. normalizedDef[key] = value as string
  17. })
  18. normalizedDefs[typeName] = normalizedDef;
  19. }
  20. else {
  21. normalizedDefs[typeName] = typeDef
  22. }
  23. })
  24. export default {
  25. types: normalizedDefs
  26. }