11// Copyright 2017-2020 @polkadot/api-contract authors & contributors
22// SPDX-License-Identifier: Apache-2.0
33
4- import { ChainProperties , MtField , MtLookupTypeId , MtType , MtTypeDefArray , MtTypeDefVariant , MtTypeDefSequence , MtTypeDefTuple , MtVariant } from '@polkadot/types/interfaces' ;
4+ import { ChainProperties , SiField , SiLookupTypeId , SiType , SiTypeDefArray , SiTypeDefVariant , SiTypeDefSequence , SiTypeDefTuple , SiVariant } from '@polkadot/types/interfaces' ;
55import { InterfaceTypes , TypeDef , TypeDefInfo } from '@polkadot/types/types' ;
66
77import { assert , isUndefined } from '@polkadot/util' ;
88import { TypeRegistry , withTypeString } from '@polkadot/types' ;
99
1010// convert the offset into project-specific, index-1
11- export function getRegistryOffset ( id : MtLookupTypeId ) : number {
11+ export function getRegistryOffset ( id : SiLookupTypeId ) : number {
1212 return id . toNumber ( ) - 1 ;
1313}
1414
1515const PRIMITIVE_ALIAS : Record < string , keyof InterfaceTypes > = {
16+ Char : 'u32' , // Rust char is 4-bytes
1617 Str : 'Text'
1718} ;
1819
1920export default class MetaRegistry extends TypeRegistry {
2021 public readonly metaTypeDefs : TypeDef [ ] = [ ] ;
2122
22- #metaTypes: MtType [ ] = [ ] ;
23+ #siTypes: SiType [ ] = [ ] ;
2324
2425 constructor ( chainProperties ?: ChainProperties ) {
2526 super ( ) ;
@@ -29,11 +30,11 @@ export default class MetaRegistry extends TypeRegistry {
2930 }
3031 }
3132
32- public setMetaTypes ( metaTypes : MtType [ ] ) : void {
33- this . #metaTypes = metaTypes ;
33+ public setMetaTypes ( metaTypes : SiType [ ] ) : void {
34+ this . #siTypes = metaTypes ;
3435 }
3536
36- public getMetaTypeDef ( id : MtLookupTypeId ) : TypeDef {
37+ public getMetaTypeDef ( id : SiLookupTypeId ) : TypeDef {
3738 const offset = getRegistryOffset ( id ) ;
3839 let typeDef = this . metaTypeDefs [ offset ] ;
3940
@@ -54,19 +55,22 @@ export default class MetaRegistry extends TypeRegistry {
5455 return typeDef ;
5556 }
5657
57- #getMetaType = ( id : MtLookupTypeId ) : MtType => {
58- const type = this . #metaTypes [ getRegistryOffset ( id ) ] ;
58+ #getMetaType = ( id : SiLookupTypeId ) : SiType => {
59+ const type = this . #siTypes [ getRegistryOffset ( id ) ] ;
5960
6061 assert ( ! isUndefined ( type ) , `getMetaType:: Unable to find ${ id . toNumber ( ) } in type values` ) ;
6162
62- return this . createType ( 'MtType ' , type ) ;
63+ return this . createType ( 'SiType ' , type ) ;
6364 }
6465
65- #extract = ( type : MtType , id : MtLookupTypeId ) : TypeDef => {
66+ #extract = ( type : SiType , id : SiLookupTypeId ) : TypeDef => {
6667 const path = [ ...type . path ] ;
6768 let typeDef : Omit < TypeDef , 'type' > ;
6869
69- if ( type . path . join ( '::' ) . startsWith ( 'ink_env::types::' ) || type . def . isPrimitive ) {
70+ // TODO solang?
71+ if ( type . path . join ( '::' ) . startsWith ( 'ink_env::types::' ) ) {
72+ typeDef = this . #extractPrimitivePath( type ) ;
73+ } else if ( type . def . isPrimitive ) {
7074 typeDef = this . #extractPrimitive( type ) ;
7175 } else if ( type . def . isComposite ) {
7276 typeDef = this . #extractFields( type . def . asComposite . fields ) ;
@@ -101,8 +105,8 @@ export default class MetaRegistry extends TypeRegistry {
101105 } ) ;
102106 }
103107
104- #extractArray = ( { len : length , type } : MtTypeDefArray ) : Omit < TypeDef , 'type' > => {
105- assert ( ! length || length . toNumber ( ) <= 256 , 'ContractRegistry : Only support for [Type; <length>], where length > 256' ) ;
108+ #extractArray = ( { len : length , type } : SiTypeDefArray ) : Omit < TypeDef , 'type' > => {
109+ assert ( ! length || length . toNumber ( ) <= 256 , 'MetaRegistry : Only support for [Type; <length>], where length > 256' ) ;
106110
107111 return {
108112 info : TypeDefInfo . VecFixed ,
@@ -111,7 +115,7 @@ export default class MetaRegistry extends TypeRegistry {
111115 } ;
112116 }
113117
114- #extractFields = ( fields : MtField [ ] ) : Omit < TypeDef , 'type' > => {
118+ #extractFields = ( fields : SiField [ ] ) : Omit < TypeDef , 'type' > => {
115119 const [ isStruct , isTuple ] = fields . reduce ( ( [ isAllNamed , isAllUnnamed ] , { name } ) => ( [
116120 isAllNamed && name . isSome ,
117121 isAllUnnamed && name . isNone
@@ -141,26 +145,23 @@ export default class MetaRegistry extends TypeRegistry {
141145 : { info, sub } ;
142146 }
143147
144- #extractPrimitive = ( type : MtType ) : TypeDef => {
145- if ( type . def . isPrimitive ) {
146- const typeStr = type . def . asPrimitive . type . toString ( ) ;
148+ #extractPrimitive = ( type : SiType ) : TypeDef => {
149+ const typeStr = type . def . asPrimitive . type . toString ( ) ;
147150
148- return {
149- info : TypeDefInfo . Plain ,
150- // FIXME This should not be as a blanket toLowerCase
151- type : PRIMITIVE_ALIAS [ typeStr ] || typeStr . toLowerCase ( )
152- } ;
153- } else if ( type . path . length > 1 ) {
154- return {
155- info : TypeDefInfo . Plain ,
156- type : type . path [ type . path . length - 1 ] . toString ( )
157- } ;
158- }
151+ return {
152+ info : TypeDefInfo . Plain ,
153+ type : PRIMITIVE_ALIAS [ typeStr ] || typeStr . toLowerCase ( )
154+ } ;
155+ }
159156
160- throw new Error ( 'Invalid primitive type' ) ;
157+ #extractPrimitivePath = ( type : SiType ) : TypeDef => {
158+ return {
159+ info : TypeDefInfo . Plain ,
160+ type : type . path [ type . path . length - 1 ] . toString ( )
161+ } ;
161162 }
162163
163- #extractSequence = ( { type } : MtTypeDefSequence , id : MtLookupTypeId ) : Omit < TypeDef , 'type' > => {
164+ #extractSequence = ( { type } : SiTypeDefSequence , id : SiLookupTypeId ) : Omit < TypeDef , 'type' > => {
164165 assert ( ! ! type , `ContractRegistry: Invalid sequence type found at id ${ id . toString ( ) } ` ) ;
165166
166167 return {
@@ -169,7 +170,7 @@ export default class MetaRegistry extends TypeRegistry {
169170 } ;
170171 }
171172
172- #extractTuple = ( ids : MtTypeDefTuple ) : Omit < TypeDef , 'type' > => {
173+ #extractTuple = ( ids : SiTypeDefTuple ) : Omit < TypeDef , 'type' > => {
173174 return ids . length === 1
174175 ? this . getMetaTypeDef ( ids [ 0 ] )
175176 : {
@@ -178,7 +179,7 @@ export default class MetaRegistry extends TypeRegistry {
178179 } ;
179180 }
180181
181- #extractVariant = ( { variants } : MtTypeDefVariant , id : MtLookupTypeId ) : Omit < TypeDef , 'type' > => {
182+ #extractVariant = ( { variants } : SiTypeDefVariant , id : SiLookupTypeId ) : Omit < TypeDef , 'type' > => {
182183 const { params, path } = this . #getMetaType( id ) ;
183184 const specialVariant = path [ 0 ] . toString ( ) ;
184185
@@ -203,7 +204,7 @@ export default class MetaRegistry extends TypeRegistry {
203204 } ;
204205 }
205206
206- #extractVariantSub = ( variants : MtVariant [ ] ) : TypeDef [ ] => {
207+ #extractVariantSub = ( variants : SiVariant [ ] ) : TypeDef [ ] => {
207208 const isAllUnitVariants = variants . every ( ( { fields } ) => fields . length === 0 ) ;
208209
209210 if ( isAllUnitVariants ) {
0 commit comments