Cleanup type extraction overrides (#2719)
* Combine contract type get/init * Cleanup type overrides
This commit is contained in:
@@ -45,7 +45,7 @@ export default class ContractRegistry {
|
||||
let typeDef = this.typeDefs[offset];
|
||||
|
||||
if (!typeDef) {
|
||||
typeDef = this.extractType(this.getAbiType(id), id) as TypeDef;
|
||||
typeDef = this.extractType(this.getAbiType(id), id);
|
||||
|
||||
this.typeDefs[offset] = typeDef;
|
||||
|
||||
@@ -61,9 +61,9 @@ export default class ContractRegistry {
|
||||
return typeDef;
|
||||
}
|
||||
|
||||
private extractType (inkType: MtType, id: MtLookupTypeId): Pick<TypeDef, never> {
|
||||
private extractType (inkType: MtType, id: MtLookupTypeId): TypeDef {
|
||||
const path = [...inkType.path];
|
||||
let typeDef;
|
||||
let typeDef: Omit<TypeDef, 'type'>;
|
||||
|
||||
if (inkType.path.join('::').startsWith('ink_env::types::') || inkType.def.isPrimitive) {
|
||||
typeDef = this.extractPrimitive(inkType);
|
||||
@@ -100,7 +100,7 @@ export default class ContractRegistry {
|
||||
});
|
||||
}
|
||||
|
||||
private extractVariant ({ variants }: MtTypeDefVariant, id: MtLookupTypeId): Pick<TypeDef, never> {
|
||||
private extractVariant ({ variants }: MtTypeDefVariant, id: MtLookupTypeId): Omit<TypeDef, 'type'> {
|
||||
const { params, path } = this.getAbiType(id);
|
||||
const specialVariant = path[0].toString();
|
||||
|
||||
@@ -125,7 +125,7 @@ export default class ContractRegistry {
|
||||
};
|
||||
}
|
||||
|
||||
private extractVariantSub (variants: MtVariant[]): Pick<TypeDef, any>[] {
|
||||
private extractVariantSub (variants: MtVariant[]): TypeDef[] {
|
||||
const isAllUnitVariants = variants.every(({ fields }) => fields.length === 0);
|
||||
|
||||
if (isAllUnitVariants) {
|
||||
@@ -141,13 +141,15 @@ export default class ContractRegistry {
|
||||
}));
|
||||
}
|
||||
|
||||
return variants.map(({ fields, name }) => ({
|
||||
...this.extractFields(fields),
|
||||
name: name.toString()
|
||||
}));
|
||||
return variants.map(({ fields, name }) =>
|
||||
withTypeString({
|
||||
...this.extractFields(fields),
|
||||
name: name.toString()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private extractFields (fields: MtField[]): Pick<TypeDef, any> {
|
||||
private extractFields (fields: MtField[]): Omit<TypeDef, 'type'> {
|
||||
const [isStruct, isTuple] = fields.reduce(([isAllNamed, isAllUnnamed], { name }) => ([
|
||||
isAllNamed && name.isSome,
|
||||
isAllUnnamed && name.isNone
|
||||
@@ -177,7 +179,7 @@ export default class ContractRegistry {
|
||||
: { info, sub };
|
||||
}
|
||||
|
||||
private extractArray ({ len: length, type }: MtTypeDefArray): Pick<TypeDef, never> {
|
||||
private extractArray ({ len: length, type }: MtTypeDefArray): Omit<TypeDef, 'type'> {
|
||||
assert(!length || length.toNumber() <= 256, 'ContractRegistry: Only support for [Type; <length>], where length > 256');
|
||||
|
||||
return {
|
||||
@@ -187,7 +189,7 @@ export default class ContractRegistry {
|
||||
};
|
||||
}
|
||||
|
||||
private extractSequence ({ type }: MtTypeDefSequence, id: MtLookupTypeId): Pick<TypeDef, never> {
|
||||
private extractSequence ({ type }: MtTypeDefSequence, id: MtLookupTypeId): Omit<TypeDef, 'type'> {
|
||||
assert(!!type, `ContractRegistry: Invalid sequence type found at id ${id.toString()}`);
|
||||
|
||||
return {
|
||||
@@ -196,7 +198,7 @@ export default class ContractRegistry {
|
||||
};
|
||||
}
|
||||
|
||||
private extractTuple (ids: MtTypeDefTuple): Pick<TypeDef, never> {
|
||||
private extractTuple (ids: MtTypeDefTuple): Omit<TypeDef, 'type'> {
|
||||
return ids.length === 1
|
||||
? this.typeDefAt(ids[0])
|
||||
: {
|
||||
@@ -205,7 +207,7 @@ export default class ContractRegistry {
|
||||
};
|
||||
}
|
||||
|
||||
private extractPrimitive (inkType: MtType): Pick<TypeDef, never> {
|
||||
private extractPrimitive (inkType: MtType): TypeDef {
|
||||
if (inkType.def.isPrimitive) {
|
||||
return {
|
||||
info: TypeDefInfo.Plain,
|
||||
|
||||
@@ -488,7 +488,8 @@
|
||||
{
|
||||
"info": 10,
|
||||
"sub": [],
|
||||
"name": "Confirmed"
|
||||
"name": "Confirmed",
|
||||
"type": "()"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
@@ -509,7 +510,8 @@
|
||||
{
|
||||
"info": 10,
|
||||
"sub": [],
|
||||
"name": "Confirmed"
|
||||
"name": "Confirmed",
|
||||
"type": "()"
|
||||
},
|
||||
{
|
||||
"info": 6,
|
||||
@@ -677,4 +679,4 @@
|
||||
},
|
||||
"type": "Option<Vec<u8>>"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
@@ -5,11 +5,9 @@ import { TypeDef, TypeDefInfo } from '../../create/types';
|
||||
|
||||
import { assert, isNumber, isUndefined } from '@polkadot/util';
|
||||
|
||||
export const SPECIAL_TYPES = ['AccountId', 'AccountIndex', 'Address', 'Balance'];
|
||||
const stringIdentity = <T extends { toString: () => string }> (value: T): string => value.toString();
|
||||
|
||||
const identity = <T> (value: T): T => value;
|
||||
|
||||
export function paramsNotation (outer: string, inner?: string | any[], transform: (_: any) => string = identity): string {
|
||||
export function paramsNotation <T> (outer: string, inner?: T | T[], transform: (_: T) => string = stringIdentity): string {
|
||||
return `${outer}${
|
||||
inner
|
||||
? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>`
|
||||
@@ -18,7 +16,7 @@ export function paramsNotation (outer: string, inner?: string | any[], transform
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
function encodeWithParams (typeDef: Pick<TypeDef, any>, outer = typeDef.displayName || typeDef.type): string {
|
||||
function encodeWithParams (typeDef: TypeDef, outer = typeDef.displayName || typeDef.type): string {
|
||||
const { info, params, sub } = typeDef;
|
||||
|
||||
switch (info) {
|
||||
@@ -34,7 +32,6 @@ function encodeWithParams (typeDef: Pick<TypeDef, any>, outer = typeDef.displayN
|
||||
return paramsNotation(outer, params || sub, (param) => encodeTypeDef(param));
|
||||
|
||||
default:
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return outer;
|
||||
}
|
||||
}
|
||||
@@ -57,10 +54,10 @@ function encodeSubTypes (sub: TypeDef[], asEnum?: boolean): string {
|
||||
);
|
||||
}
|
||||
|
||||
function encodeEnum (typeDef: Pick<TypeDef, any>): string {
|
||||
function encodeEnum (typeDef: TypeDef): string {
|
||||
assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Enum type');
|
||||
|
||||
const sub = typeDef.sub as TypeDef[];
|
||||
const sub = typeDef.sub;
|
||||
|
||||
// c-like enums have all Null entries
|
||||
// TODO We need to take the disciminant into account and auto-add empty entries
|
||||
@@ -69,29 +66,28 @@ function encodeEnum (typeDef: Pick<TypeDef, any>): string {
|
||||
: encodeSubTypes(sub, true);
|
||||
}
|
||||
|
||||
function encodeStruct (typeDef: Pick<TypeDef, any>): string {
|
||||
function encodeStruct (typeDef: TypeDef): string {
|
||||
assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Struct type');
|
||||
|
||||
return encodeSubTypes(typeDef.sub as TypeDef[]);
|
||||
return encodeSubTypes(typeDef.sub);
|
||||
}
|
||||
|
||||
function encodeTuple (typeDef: Pick<TypeDef, any>): string {
|
||||
function encodeTuple (typeDef: TypeDef): string {
|
||||
assert(typeDef.sub && Array.isArray(typeDef.sub), 'Unable to encode Tuple type');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
return `(${(typeDef.sub as TypeDef[]).map((type) => encodeTypeDef(type)).join(', ')})`;
|
||||
return `(${typeDef.sub.map((type) => encodeTypeDef(type)).join(', ')})`;
|
||||
}
|
||||
|
||||
function encodeUInt ({ length }: Pick<TypeDef, any>, type: 'Int' | 'UInt'): string {
|
||||
function encodeUInt ({ length }: TypeDef, type: 'Int' | 'UInt'): string {
|
||||
assert(isNumber(length), 'Unable to encode VecFixed type');
|
||||
|
||||
return `${type}<${length}>`;
|
||||
}
|
||||
|
||||
function encodeVecFixed ({ length, sub }: Pick<TypeDef, any>): string {
|
||||
assert(isNumber(length) && !isUndefined(sub), 'Unable to encode VecFixed type');
|
||||
function encodeVecFixed ({ length, sub }: TypeDef): string {
|
||||
assert(isNumber(length) && !isUndefined(sub) && !Array.isArray(sub), 'Unable to encode VecFixed type');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/restrict-template-expressions
|
||||
return `[${sub.type};${length}]`;
|
||||
}
|
||||
|
||||
@@ -119,15 +115,15 @@ const encoders: Record<TypeDefInfo, (typeDef: TypeDef) => string> = {
|
||||
[TypeDefInfo.VecFixed]: (typeDef: TypeDef): string => encodeVecFixed(typeDef)
|
||||
};
|
||||
|
||||
function encodeType (typeDef: Pick<TypeDef, any>): string {
|
||||
const encoder = encoders[(typeDef as TypeDef).info];
|
||||
function encodeType (typeDef:TypeDef): string {
|
||||
const encoder = encoders[typeDef.info];
|
||||
|
||||
assert(encoder, `Cannot encode type: ${JSON.stringify(typeDef)}`);
|
||||
|
||||
return encoder(typeDef as TypeDef);
|
||||
return encoder(typeDef);
|
||||
}
|
||||
|
||||
export function encodeTypeDef (typeDef: Pick<TypeDef, any>): string {
|
||||
export function encodeTypeDef (typeDef: TypeDef): string {
|
||||
assert(!isUndefined(typeDef.info), `Invalid type definition with no instance info, ${JSON.stringify(typeDef)}`);
|
||||
|
||||
return typeDef.displayName || [TypeDefInfo.Enum, TypeDefInfo.Struct].includes(typeDef.info)
|
||||
@@ -135,11 +131,9 @@ export function encodeTypeDef (typeDef: Pick<TypeDef, any>): string {
|
||||
: encodeType(typeDef);
|
||||
}
|
||||
|
||||
export function withTypeString (typeDef: Pick<TypeDef, any>): Pick<TypeDef, any> {
|
||||
export function withTypeString (typeDef: Omit<TypeDef, 'type'>): TypeDef {
|
||||
return {
|
||||
...typeDef,
|
||||
type: SPECIAL_TYPES.includes(typeDef.displayName)
|
||||
? typeDef.displayName as string
|
||||
: encodeType(typeDef)
|
||||
type: encodeType(typeDef as TypeDef)
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user