Handle scale-info Range (#3899)
* Handle scale-info Range * Handle Range in PortableRegistry * Cleanups * Rebuild, fix tests (Range to be moved)
This commit is contained in:
@@ -76,7 +76,7 @@ function tsEnum (registry: Registry, definitions: Record<string, ModuleTypes>, {
|
||||
return '';
|
||||
|
||||
default:
|
||||
throw new Error(`Enum: ${enumName || 'undefined'}: Unhandled type ${TypeDefInfo[info]}`);
|
||||
throw new Error(`Enum: ${enumName || 'undefined'}: Unhandled nested "${TypeDefInfo[info]}" type`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -44,7 +44,7 @@ import type { AccountStatus, AccountValidity } from '@polkadot/types/interfaces/
|
||||
import type { ActiveRecovery, RecoveryConfig } from '@polkadot/types/interfaces/recovery';
|
||||
import type { RpcMethods } from '@polkadot/types/interfaces/rpc';
|
||||
import type { AccountId, AccountId20, AccountId32, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, BlockNumberFor, BlockNumberOf, Call, CallHash, CallHashOf, ChangesTrieConfiguration, ChangesTrieSignal, CodecHash, Consensus, ConsensusEngineId, Digest, DigestItem, EncodedJustification, ExtrinsicsWeight, Fixed128, Fixed64, FixedI128, FixedI64, FixedU128, FixedU64, H1024, H128, H160, H2048, H256, H32, H512, H64, Hash, Header, HeaderPartial, I32F32, Index, IndicesLookupSource, Justification, Justifications, KeyTypeId, KeyValue, LockIdentifier, LookupSource, LookupTarget, ModuleId, Moment, MultiAddress, MultiSigner, OpaqueCall, Origin, OriginCaller, PalletId, PalletVersion, PalletsOrigin, Pays, PerU16, Perbill, Percent, Permill, Perquintill, Phantom, PhantomData, PreRuntime, Releases, RuntimeDbWeight, Seal, SealV0, SignedBlock, SignedBlockWithJustification, SignedBlockWithJustifications, Slot, StorageData, StorageProof, TransactionInfo, TransactionPriority, TransactionStorageProof, U32F32, ValidatorId, ValidatorIdOf, Weight, WeightMultiplier } from '@polkadot/types/interfaces/runtime';
|
||||
import type { Si0Field, Si0LookupTypeId, Si0Path, Si0Type, Si0TypeDef, Si0TypeDefArray, Si0TypeDefBitSequence, Si0TypeDefCompact, Si0TypeDefComposite, Si0TypeDefPhantom, Si0TypeDefPrimitive, Si0TypeDefSequence, Si0TypeDefTuple, Si0TypeDefVariant, Si0TypeParameter, Si0Variant, SiField, SiLookupTypeId, SiPath, SiType, SiTypeDef, SiTypeDefArray, SiTypeDefBitSequence, SiTypeDefCompact, SiTypeDefComposite, SiTypeDefPrimitive, SiTypeDefSequence, SiTypeDefTuple, SiTypeDefVariant, SiTypeParameter, SiVariant } from '@polkadot/types/interfaces/scaleInfo';
|
||||
import type { Si0Field, Si0LookupTypeId, Si0Path, Si0Type, Si0TypeDef, Si0TypeDefArray, Si0TypeDefBitSequence, Si0TypeDefCompact, Si0TypeDefComposite, Si0TypeDefPhantom, Si0TypeDefPrimitive, Si0TypeDefSequence, Si0TypeDefTuple, Si0TypeDefVariant, Si0TypeParameter, Si0Variant, SiField, SiLookupTypeId, SiPath, SiType, SiTypeDef, SiTypeDefArray, SiTypeDefBitSequence, SiTypeDefCompact, SiTypeDefComposite, SiTypeDefPrimitive, SiTypeDefRange, SiTypeDefSequence, SiTypeDefTuple, SiTypeDefVariant, SiTypeParameter, SiVariant } from '@polkadot/types/interfaces/scaleInfo';
|
||||
import type { Period, Priority, SchedulePeriod, SchedulePriority, Scheduled, ScheduledTo254, TaskAddress } from '@polkadot/types/interfaces/scheduler';
|
||||
import type { BeefyKey, FullIdentification, IdentificationTuple, Keys, MembershipProof, SessionIndex, SessionKeys1, SessionKeys10, SessionKeys10B, SessionKeys2, SessionKeys3, SessionKeys4, SessionKeys5, SessionKeys6, SessionKeys6B, SessionKeys7, SessionKeys7B, SessionKeys8, SessionKeys8B, SessionKeys9, SessionKeys9B, ValidatorCount } from '@polkadot/types/interfaces/session';
|
||||
import type { Bid, BidKind, SocietyJudgement, SocietyVote, StrikeCount, VouchingStatus } from '@polkadot/types/interfaces/society';
|
||||
@@ -810,6 +810,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
SiTypeDefCompact: SiTypeDefCompact;
|
||||
SiTypeDefComposite: SiTypeDefComposite;
|
||||
SiTypeDefPrimitive: SiTypeDefPrimitive;
|
||||
SiTypeDefRange: SiTypeDefRange;
|
||||
SiTypeDefSequence: SiTypeDefSequence;
|
||||
SiTypeDefTuple: SiTypeDefTuple;
|
||||
SiTypeDefVariant: SiTypeDefVariant;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { Vec } from '../codec/Vec';
|
||||
import type { PortableType } from '../interfaces/metadata';
|
||||
import type { SiField, SiLookupTypeId, SiPath, SiType, SiTypeDefArray, SiTypeDefBitSequence, SiTypeDefCompact, SiTypeDefComposite, SiTypeDefSequence, SiTypeDefTuple, SiTypeDefVariant, SiTypeParameter, SiVariant } from '../interfaces/scaleInfo';
|
||||
import type { SiField, SiLookupTypeId, SiPath, SiType, SiTypeDefArray, SiTypeDefBitSequence, SiTypeDefCompact, SiTypeDefComposite, SiTypeDefRange, SiTypeDefSequence, SiTypeDefTuple, SiTypeDefVariant, SiTypeParameter, SiVariant } from '../interfaces/scaleInfo';
|
||||
import type { Text } from '../primitive/Text';
|
||||
import type { Type } from '../primitive/Type';
|
||||
import type { Registry, TypeDef } from '../types';
|
||||
@@ -239,7 +239,7 @@ export class GenericPortableRegistry extends Struct {
|
||||
}
|
||||
|
||||
#extract (type: SiType, lookupIndex: number): TypeDef {
|
||||
const path = [...type.path];
|
||||
const namespace = [...type.path].join('::');
|
||||
let typeDef: TypeDef;
|
||||
const primType = getPrimitivePath(type.path);
|
||||
|
||||
@@ -258,6 +258,8 @@ export class GenericPortableRegistry extends Struct {
|
||||
typeDef = this.#extractHistoric(lookupIndex, type.def.asHistoricMetaCompat);
|
||||
} else if (type.def.isPrimitive) {
|
||||
typeDef = this.#extractPrimitive(lookupIndex, type);
|
||||
} else if (type.def.isRange) {
|
||||
typeDef = this.#extractRange(lookupIndex, type.def.asRange);
|
||||
} else if (type.def.isSequence) {
|
||||
typeDef = this.#extractSequence(lookupIndex, type.def.asSequence);
|
||||
} else if (type.def.isTuple) {
|
||||
@@ -265,21 +267,21 @@ export class GenericPortableRegistry extends Struct {
|
||||
} else if (type.def.isVariant) {
|
||||
typeDef = this.#extractVariant(lookupIndex, type, type.def.asVariant);
|
||||
} else {
|
||||
throw new Error(`Invalid type at index ${lookupIndex}: No handler for ${type.def.toString()}`);
|
||||
throw new Error(`No SiTypeDef handler for ${type.def.toString()}`);
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`PortableRegistry: ${lookupIndex}: Error extracting ${stringify(type)}: ${(error as Error).message}`);
|
||||
throw new Error(`PortableRegistry: ${lookupIndex}${namespace ? ` (${namespace})` : ''}: Error extracting ${stringify(type)}: ${(error as Error).message}`);
|
||||
}
|
||||
|
||||
return {
|
||||
docs: type.docs.map((d) => d.toString()),
|
||||
namespace: path.join('::'),
|
||||
namespace,
|
||||
...typeDef
|
||||
};
|
||||
}
|
||||
|
||||
#extractArray (lookupIndex: number, { len: length, type }: SiTypeDefArray): TypeDef {
|
||||
assert(!length || length.toNumber() <= 256, () => `PortableRegistry: ${lookupIndex}: Only support for [Type; <length>], where length <= 256`);
|
||||
#extractArray (_: number, { len: length, type }: SiTypeDefArray): TypeDef {
|
||||
assert(!length || length.toNumber() <= 256, 'Only support for [Type; <length>], where length <= 256');
|
||||
|
||||
return withTypeString(this.registry, {
|
||||
info: TypeDefInfo.VecFixed,
|
||||
@@ -324,8 +326,8 @@ export class GenericPortableRegistry extends Struct {
|
||||
: this.#extractFields(lookupIndex, fields);
|
||||
}
|
||||
|
||||
#extractCompositeSet (lookupIndex: number, params: SiTypeParameter[], fields: SiField[]): TypeDef {
|
||||
assert(params.length === 1 && fields.length === 1, () => `PortableRegistry: ${lookupIndex}: Set handling expects since param and single field`);
|
||||
#extractCompositeSet (_: number, params: SiTypeParameter[], fields: SiField[]): TypeDef {
|
||||
assert(params.length === 1 && fields.length === 1, 'Set handling expects param/field as single entries');
|
||||
|
||||
return withTypeString(this.registry, {
|
||||
info: TypeDefInfo.Set,
|
||||
@@ -347,7 +349,7 @@ export class GenericPortableRegistry extends Struct {
|
||||
]),
|
||||
[true, true]);
|
||||
|
||||
assert(isTuple || isStruct, () => `PortableRegistry: ${lookupIndex}: Invalid fields type detected, expected either Tuple (all unnamed) or Struct (all named)`);
|
||||
assert(isTuple || isStruct, 'Invalid fields type detected, expected either Tuple (all unnamed) or Struct (all named)');
|
||||
|
||||
if (fields.length === 0) {
|
||||
return {
|
||||
@@ -452,6 +454,19 @@ export class GenericPortableRegistry extends Struct {
|
||||
};
|
||||
}
|
||||
|
||||
#extractRange (lookupIndex: number, { end, start }: SiTypeDefRange): TypeDef {
|
||||
// NOTE Currently we don't handle SiTypeDefRange.inclusive
|
||||
return withTypeString(this.registry, {
|
||||
info: TypeDefInfo.Range,
|
||||
lookupIndex,
|
||||
lookupName: this.#names[lookupIndex],
|
||||
sub: [
|
||||
this.#createSiDef(start),
|
||||
this.#createSiDef(end)
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
#extractSequence (lookupIndex: number, { type }: SiTypeDefSequence): TypeDef {
|
||||
const sub = this.#createSiDef(type);
|
||||
|
||||
@@ -501,9 +516,9 @@ export class GenericPortableRegistry extends Struct {
|
||||
} else if (specialVariant === 'Result') {
|
||||
return withTypeString(this.registry, {
|
||||
info: TypeDefInfo.Result,
|
||||
sub: params.map(({ type }) => this.#createSiDef(type.unwrap())).map((def, index) => ({
|
||||
sub: params.map(({ type }, index) => ({
|
||||
name: ['Ok', 'Error'][index],
|
||||
...def
|
||||
...this.#createSiDef(type.unwrap())
|
||||
}))
|
||||
});
|
||||
} else if (variants.length === 0) {
|
||||
|
||||
@@ -40,9 +40,12 @@ export default {
|
||||
Sequence: 'SiTypeDefSequence',
|
||||
Array: 'SiTypeDefArray',
|
||||
Tuple: 'SiTypeDefTuple',
|
||||
// Range: 'SiTypeDefRange',
|
||||
Primitive: 'SiTypeDefPrimitive',
|
||||
Compact: 'SiTypeDefCompact',
|
||||
BitSequence: 'SiTypeDefBitSequence',
|
||||
// FIXME Move to commented-out area as soon as available on Substrate master
|
||||
Range: 'SiTypeDefRange',
|
||||
// NOTE: This is specific to the implementation for pre-v14 metadata
|
||||
// compatibility (always keep this as the last entry in the enum)
|
||||
HistoricMetaCompat: 'Type'
|
||||
@@ -62,12 +65,14 @@ export default {
|
||||
SiTypeDefComposite: {
|
||||
fields: 'Vec<SiField>'
|
||||
},
|
||||
SiTypeDefVariant: {
|
||||
variants: 'Vec<SiVariant>'
|
||||
},
|
||||
SiTypeDefPrimitive: {
|
||||
_enum: ['Bool', 'Char', 'Str', 'U8', 'U16', 'U32', 'U64', 'U128', 'U256', 'I8', 'I16', 'I32', 'I64', 'I128', 'I256']
|
||||
},
|
||||
SiTypeDefRange: {
|
||||
start: 'SiLookupTypeId',
|
||||
end: 'SiLookupTypeId',
|
||||
inclusive: 'bool'
|
||||
},
|
||||
SiTypeDefSequence: {
|
||||
type: 'SiLookupTypeId'
|
||||
},
|
||||
@@ -76,6 +81,9 @@ export default {
|
||||
name: 'Text',
|
||||
type: 'Option<SiLookupTypeId>'
|
||||
},
|
||||
SiTypeDefVariant: {
|
||||
variants: 'Vec<SiVariant>'
|
||||
},
|
||||
SiVariant
|
||||
}
|
||||
} as Definitions;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
|
||||
/* eslint-disable */
|
||||
|
||||
import type { Compact, Enum, Null, Option, Struct, Text, Type, Vec, u32, u64, u8 } from '@polkadot/types';
|
||||
import type { Compact, Enum, Null, Option, Struct, Text, Type, Vec, bool, u32, u64, u8 } from '@polkadot/types';
|
||||
|
||||
/** @name Si0Field */
|
||||
export interface Si0Field extends Struct {
|
||||
@@ -158,6 +158,8 @@ export interface SiTypeDef extends Enum {
|
||||
readonly asCompact: SiTypeDefCompact;
|
||||
readonly isBitSequence: boolean;
|
||||
readonly asBitSequence: SiTypeDefBitSequence;
|
||||
readonly isRange: boolean;
|
||||
readonly asRange: SiTypeDefRange;
|
||||
readonly isHistoricMetaCompat: boolean;
|
||||
readonly asHistoricMetaCompat: Type;
|
||||
}
|
||||
@@ -203,6 +205,13 @@ export interface SiTypeDefPrimitive extends Enum {
|
||||
readonly isI256: boolean;
|
||||
}
|
||||
|
||||
/** @name SiTypeDefRange */
|
||||
export interface SiTypeDefRange extends Struct {
|
||||
readonly start: SiLookupTypeId;
|
||||
readonly end: SiLookupTypeId;
|
||||
readonly inclusive: bool;
|
||||
}
|
||||
|
||||
/** @name SiTypeDefSequence */
|
||||
export interface SiTypeDefSequence extends Struct {
|
||||
readonly type: SiLookupTypeId;
|
||||
|
||||
Reference in New Issue
Block a user