Fix c-like enums type encoding (#2703)
* Fix c-like enums type encoding * Update packages/api-contract/src/ContractRegistry.ts * Update packages/types/src/codec/utils/encodeTypes.ts
This commit is contained in:
@@ -53,7 +53,14 @@ export default class ContractRegistry {
|
||||
}
|
||||
|
||||
public setTypeDef (id: MtLookupTypeId): void {
|
||||
this.typeDefs[getRegistryOffset(id)] = this.extractType(this.getAbiType(id), id) as TypeDef;
|
||||
const typeDef = this.extractType(this.getAbiType(id), id) as TypeDef;
|
||||
|
||||
this.typeDefs[getRegistryOffset(id)] = typeDef;
|
||||
|
||||
// we have a displayName for non-primitives and non-results
|
||||
if (typeDef.displayName) {
|
||||
this.registry.register({ [typeDef.displayName]: typeDef.type });
|
||||
}
|
||||
}
|
||||
|
||||
private extractType (inkType: MtType, id: MtLookupTypeId): Pick<TypeDef, never> {
|
||||
|
||||
@@ -5,7 +5,9 @@ import { ApiTypes, DecorateMethod } from '@polkadot/api/types';
|
||||
import { AnyJson, Registry } from '@polkadot/types/types';
|
||||
import { ApiObject } from '../types';
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import Abi from '../Abi';
|
||||
|
||||
export default abstract class Base<ApiType extends ApiTypes> {
|
||||
@@ -18,11 +20,11 @@ export default abstract class Base<ApiType extends ApiTypes> {
|
||||
protected readonly _decorateMethod: DecorateMethod<ApiType>;
|
||||
|
||||
constructor (api: ApiObject<ApiType>, abi: AnyJson | Abi, decorateMethod: DecorateMethod<ApiType>) {
|
||||
this.registry = new TypeRegistry();
|
||||
this.abi = abi instanceof Abi
|
||||
? abi
|
||||
: new Abi(api.registry, abi);
|
||||
: new Abi(this.registry, abi);
|
||||
this.api = api;
|
||||
this.registry = api.registry;
|
||||
this._decorateMethod = decorateMethod;
|
||||
|
||||
assert(this.api.rx.tx.contracts && this.api.rx.tx.contracts.putCode, 'You need to connect to a node with the contracts module, the metadata does not enable api.tx.contracts on this instance');
|
||||
|
||||
@@ -169,7 +169,7 @@
|
||||
"type": "Null"
|
||||
}
|
||||
],
|
||||
"type": "[\"NameAlreadyExists\", \"CallerIsNotOwner\"]"
|
||||
"type": "{ _enum: [\"NameAlreadyExists\", \"CallerIsNotOwner\"] }"
|
||||
}
|
||||
],
|
||||
"info": 7,
|
||||
@@ -203,7 +203,7 @@
|
||||
"type": "Null"
|
||||
}
|
||||
],
|
||||
"type": "[\"NameAlreadyExists\", \"CallerIsNotOwner\"]"
|
||||
"type": "{ _enum: [\"NameAlreadyExists\", \"CallerIsNotOwner\"] }"
|
||||
}
|
||||
],
|
||||
"type": "Result<(), Error>"
|
||||
@@ -235,7 +235,7 @@
|
||||
"type": "Null"
|
||||
}
|
||||
],
|
||||
"type": "[\"NameAlreadyExists\", \"CallerIsNotOwner\"]"
|
||||
"type": "{ _enum: [\"NameAlreadyExists\", \"CallerIsNotOwner\"] }"
|
||||
},
|
||||
{
|
||||
"displayName": "Option",
|
||||
|
||||
@@ -404,7 +404,7 @@
|
||||
"type": "Null"
|
||||
}
|
||||
],
|
||||
"type": "[\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"]"
|
||||
"type": "{ _enum: [\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"] }"
|
||||
}
|
||||
],
|
||||
"info": 7,
|
||||
@@ -486,7 +486,7 @@
|
||||
"type": "Null"
|
||||
}
|
||||
],
|
||||
"type": "[\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"]"
|
||||
"type": "{ _enum: [\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"] }"
|
||||
}
|
||||
],
|
||||
"type": "Result<(), Error>"
|
||||
@@ -566,6 +566,6 @@
|
||||
"type": "Null"
|
||||
}
|
||||
],
|
||||
"type": "[\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"]"
|
||||
"type": "{ _enum: [\"NotOwner\", \"NotApproved\", \"TokenExists\", \"TokenNotFound\", \"CannotInsert\", \"CannotRemove\", \"CannotFetchValue\", \"NotAllowed\"] }"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -73,21 +73,11 @@ function encodeEnum (typeDef: Pick<TypeDef, any>): string {
|
||||
|
||||
const sub = typeDef.sub as TypeDef[];
|
||||
|
||||
const isClikeEnum = sub.reduce(
|
||||
(bool: boolean, { type }: TypeDef): boolean => bool && type === 'Null',
|
||||
true
|
||||
);
|
||||
|
||||
if (isClikeEnum) {
|
||||
return `[${
|
||||
sub
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
.map(({ name }: TypeDef): string => `"${name}"`)
|
||||
.join(', ')
|
||||
}]`;
|
||||
}
|
||||
|
||||
return encodeSubTypes(sub, true);
|
||||
// c-like enums have all Null entries
|
||||
// TODO We need to take the disciminant into account and auto-add empty entries
|
||||
return sub.every(({ type }) => type === 'Null')
|
||||
? `{ _enum: [${sub.map(({ name }, index) => `"${name || `Empty${index}`}"`).join(', ')}] }`
|
||||
: encodeSubTypes(sub, true);
|
||||
}
|
||||
|
||||
function encodeStruct (typeDef: Pick<TypeDef, any>): string {
|
||||
@@ -103,12 +93,8 @@ function encodeTuple (typeDef: Pick<TypeDef, any>): string {
|
||||
|
||||
const sub = typeDef.sub as TypeDef[];
|
||||
|
||||
return `(${
|
||||
sub
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
.map((type: TypeDef): string => encodeTypeDef(type))
|
||||
.join(', ')
|
||||
})`;
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
return `(${sub.map((type: TypeDef) => encodeTypeDef(type)).join(', ')})`;
|
||||
}
|
||||
|
||||
function encodeUInt ({ length }: Pick<TypeDef, any>, type: 'Int' | 'UInt'): string {
|
||||
|
||||
Reference in New Issue
Block a user