Extends createType decoration with missing (#3872)

* Extends createType decoration with missing

* Simplify

* Correct __IntType

* Uint mismatch
This commit is contained in:
Jaco
2021-08-13 12:58:17 +02:00
committed by GitHub
parent 48620ea7a3
commit 07594a33b5
21 changed files with 171 additions and 101 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Option, u32 } from '@polkadot/types';
import type { Option, u32, u64 } from '@polkadot/types';
import type { BlockNumber, EraIndex, Moment, SessionIndex } from '@polkadot/types/interfaces';
export interface DeriveSessionIndexes {
@@ -15,7 +15,7 @@ export interface DeriveSessionIndexes {
export interface DeriveSessionInfo extends DeriveSessionIndexes {
eraLength: BlockNumber;
isEpoch: boolean;
sessionLength: BlockNumber;
sessionLength: u64;
sessionsPerEra: SessionIndex;
}
+7 -3
View File
@@ -48,11 +48,13 @@ const vv = registry.createType('Vec< Vec< Vec< Vec<u8>> > >');
// vec with tuple
const vt = registry.createType('Vec<(u8, u16)>');
// nested stuff from all-over
const vn = registry.createType('Vec<(u32, (u32, u64), Vec<u8>, Vec<(u32, u64)>, [u8;32], [u128;32])>');
const vn = registry.createType('Vec<(u32, (u32, u64), Vec<u8>, Vec<u32>, Vec<(u32, u64)>, [u8;32], [u128;32])>');
// nested fixed
const nf = registry.createType('[[[u8;32];5];3]');
// with linkage
const tl = registry.createType('(ValidatorPrefsWithCommission, Linkage<AccountId>)');
assert(ee[0].unwrap().unwrap().divn(123) && vb.unwrap().bitLength() && vv.toHuman() && vn.toHuman() && vt.toHuman() && nf.toHuman(), 'All ok');
assert(ee[0].unwrap().unwrap().divn(123) && vb.unwrap().bitLength() && vv.toHuman() && vn[0][3][0].bitLength && vt.toHuman() && nf.toHuman() && tl[1].next, 'All ok');
// tuple & struct
const vs = registry.createType('(u8, {"a":"u32","b":"(u32,u64)"},(u8,u16),{"foo":"Bar"},u16)');
@@ -70,8 +72,10 @@ assert(gg.subarray(1), 'All ok');
// Should end up as VecFixed<u128>
const hh = registry.createType('[u128; 32]');
// maps and sets
const ms = registry.createType('(BTreeSet<u8>, BTreeMap<u16, u32>, HashMap<u64, u128>)');
assert(hh[0].bitLength(), 'All ok');
assert(hh[0].bitLength() && ms[0].strings && ms[1].values && ms[2].keys, 'All ok');
// tuple! ITuple<[u32, Compact<u64>, u128, Codec]>
const tt1 = registry.createType('(u32, Compact<u64>, u128 , Something)');
+9 -2
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, Codec, Constructor, Registry } from '../types';
import type { AnyJson, Codec, Constructor, ISet, Registry } from '../types';
import { compactFromU8a, compactToU8a, isHex, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
@@ -76,7 +76,7 @@ function decodeSet<V extends Codec> (registry: Registry, valType: Constructor<V>
throw new Error('BTreeSet: cannot decode type');
}
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements Codec {
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
public readonly registry: Registry;
public createdAtHash?: Hash;
@@ -125,6 +125,13 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements Codec {
return this.size === 0;
}
/**
* @description The actual set values as a string[]
*/
public get strings (): string[] {
return [...super.values()].map((v) => v.toString());
}
/**
* @description Compares the value of the input to see if there is a match
*/
+1 -3
View File
@@ -1,8 +1,6 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, ITuple } from '../types';
import { TypeRegistry } from '../create/registry';
import { Linkage } from '.';
@@ -34,7 +32,7 @@ describe('Linkage', (): void => {
// actual check
expect(
registry.createType<ITuple<[Codec, Linkage<Codec>]>>(
registry.createType(
'(ValidatorPrefsWithCommission, Linkage<AccountId>)',
'0x0284d7170001da30b68f54f686f586ddb29de12b682dd8bd1404566fb8a8db5dec20aa5b6b36'
).toHuman()
+1 -1
View File
@@ -74,7 +74,7 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
* An Set is an array of string values, represented an an encoded type by
* a bitwise representation of the values.
*/
export class CodecSet extends Set<string> implements ISet {
export class CodecSet extends Set<string> implements ISet<string> {
public readonly registry: Registry;
public createdAtHash?: Hash;
+2 -2
View File
@@ -21,6 +21,8 @@ export { CodecMap, CodecMap as Map } from './Map';
export { Option } from './Option';
export { Raw } from './Raw';
export { Result } from './Result';
export { Range } from './Range';
export { RangeInclusive } from './RangeInclusive';
export { CodecSet, CodecSet as Set } from './Set';
export { Struct } from './Struct';
export { Tuple } from './Tuple';
@@ -29,5 +31,3 @@ export { U8aFixed } from './U8aFixed';
export { Vec } from './Vec';
// export { VecAny } from './VecAny';
export { VecFixed } from './VecFixed';
export { Range } from './Range';
export { RangeInclusive } from './RangeInclusive';
+4 -1
View File
@@ -8,4 +8,7 @@ import { Int } from '../codec/Int';
* @description
* A 128-bit signed integer
*/
export class i128 extends Int.with(128) {}
export class i128 extends Int.with(128) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i128';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { Int } from '../codec/Int';
* @description
* A 16-bit signed integer
*/
export class i16 extends Int.with(16) {}
export class i16 extends Int.with(16) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i16';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { Int } from '../codec/Int';
* @description
* A 256-bit signed integer
*/
export class i256 extends Int.with(256) {}
export class i256 extends Int.with(256) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i256';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { Int } from '../codec/Int';
* @description
* A 32-bit signed integer
*/
export class i32 extends Int.with(32) {}
export class i32 extends Int.with(32) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i32';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { Int } from '../codec/Int';
* @description
* A 64-bit signed integer
*/
export class i64 extends Int.with(64) {}
export class i64 extends Int.with(64) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i64';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { Int } from '../codec/Int';
* @description
* An 8-bit signed integer
*/
export class i8 extends Int.with(8) {}
export class i8 extends Int.with(8) {
// NOTE without this, we cannot properly determine extensions
public readonly __IntType = 'i8';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { UInt } from '../codec/UInt';
* @description
* A 128-bit unsigned integer
*/
export class u128 extends UInt.with(128) {}
export class u128 extends UInt.with(128) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u128';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { UInt } from '../codec/UInt';
* @description
* A 16-bit unsigned integer
*/
export class u16 extends UInt.with(16) {}
export class u16 extends UInt.with(16) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u16';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { UInt } from '../codec/UInt';
* @description
* A 256-bit unsigned integer
*/
export class u256 extends UInt.with(256) {}
export class u256 extends UInt.with(256) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u256';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { UInt } from '../codec/UInt';
* @description
* A 32-bit unsigned integer
*/
export class u32 extends UInt.with(32) {}
export class u32 extends UInt.with(32) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u32';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { UInt } from '../codec/UInt';
* @description
* A 64-bit unsigned integer
*/
export class u64 extends UInt.with(64) {}
export class u64 extends UInt.with(64) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u64';
}
+4 -1
View File
@@ -8,4 +8,7 @@ import { UInt } from '../codec/UInt';
* @description
* An 8-bit unsigned integer
*/
export class u8 extends UInt.with(8) {}
export class u8 extends UInt.with(8) {
// NOTE without this, we cannot properly determine extensions
public readonly __UIntType = 'u8';
}
+89 -75
View File
@@ -1,8 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Compact, Option, Raw, Vec, VecFixed } from '../codec';
import type { Bytes, Null } from '../primitive';
import type { BTreeMap, BTreeSet, CodecSet, Compact, Enum, HashMap, Linkage, Option, Raw, Struct, Vec, VecFixed } from '../codec';
import type { Bytes, Null, u8 } from '../primitive';
import type { Codec, Constructor } from './codec';
import type { ICompact, IEnum, IMap, IMethod, INumber, IOption, IResult, ISet, IStruct, ITuple, IU8a, IVec } from './interfaces';
import type { InterfaceTypes } from './registry';
@@ -14,11 +14,11 @@ export type DetectCodec<T extends Codec, K extends string> =
? InterfaceTypes[K]
: T extends ICompact | IEnum | IMap | IMethod | INumber | IOption | IResult | ISet | IStruct | ITuple | IU8a | IVec
? T
: __ExtractCodec<__ToCodecs<__Tokenize<__Sanitize<K>>[0]>>;
: __Codec<__Codecs<__Tokenize<__Sanitize<K>>[0]>>;
export type DetectConstructor<T extends Codec, K extends string> = Constructor<DetectCodec<T, K>>;
export type __ExtractCodec<V extends Codec[]> =
export type __Codec<V extends Codec[]> =
V[0] extends Codec
? V[0]
: Codec;
@@ -37,34 +37,34 @@ export type __Sanitize<K extends string> =
export type __Value = string | Record<string, unknown> | __Value[];
export type __ToCodec<K extends unknown, C extends Codec[], N extends unknown[]> =
K extends keyof InterfaceTypes
? InterfaceTypes[K]
: K extends unknown[]
? __ToTuple<__ToCodecs<K>>
: K extends Record<string, unknown>
? K['_enum'] extends Record<string, unknown>
? IEnum
: K['_set'] extends Record<string, unknown>
? ISet
: IStruct
: K extends Set<unknown>
? ISet
: K extends '['
? N[0] extends 'u8'
? Raw
: VecFixed<C[0]>
: K extends 'Vec<'
? N[0] extends 'u8'
? Bytes
: Vec<C[0]>
: K extends 'Option<'
? Option<C[0]>
: K extends 'Compact<'
? C[0] extends INumber
? Compact<C[0]>
: Codec
: Codec;
export type __MapWrapOne<C extends Codec> = {
'BTreeSet<': BTreeSet<C>;
'Compact<': C extends INumber ? Compact<C> : Codec;
'Linkage<': Linkage<C>;
'Option<': Option<C>;
'Vec<': C extends u8 ? Bytes : Vec<C>;
'[': C extends u8 ? Raw : VecFixed<C>;
};
// FIXME We don't cater for Int< & UInt< here. These could be problematic, since it has
// a variable number of inner arguments, better would be to just strip them inside the sanitize
export type __MapWrapTwo<K extends Codec, V extends Codec> = {
'BTreeMap<': BTreeMap<K, V>;
'HashMap<': HashMap<K, V>;
};
export type __WrapOne = keyof __MapWrapOne<Codec>;
export type __WrapTwo = keyof __MapWrapTwo<Codec, Codec>;
export type __Wrap = __WrapOne | __WrapTwo;
export type __ToStruct<K extends Record<string, unknown>> =
K['_enum'] extends true
? Enum
: K['_set'] extends true
? CodecSet
: Struct;
export type __ToTuple<O extends Codec[]> =
O[0] extends Codec
@@ -73,48 +73,64 @@ export type __ToTuple<O extends Codec[]> =
: O[0]
: Null;
export type __ToCodecsInner<K extends unknown, N extends unknown[], C extends Codec[]> =
K extends '[' | 'Vec<' | 'Option<' | 'Compact<'
? C extends [Codec, ...infer X]
? [__ToCodec<K, C, N>, ...X]
: never
: [__ToCodec<K, C, N>, ...C];
export type __CodecFirst<K extends unknown, C extends Codec[], X extends unknown[]> = [
K extends keyof InterfaceTypes
? InterfaceTypes[K]
: K extends unknown[]
? __ToTuple<__Codecs<K>>
: K extends Record<string, unknown>
? __ToStruct<K>
: K extends __WrapOne
? __MapWrapOne<C[0]>[K]
: K extends __WrapTwo
? __MapWrapTwo<C[0], C[1]>[K]
: Codec,
...X
];
export type __ToCodecs<T extends unknown[]> =
T extends [infer K, ...infer O]
? __ToCodecsInner<K, O, __ToCodecs<O>>
export type __CodecsNext<K extends unknown, C extends Codec[]> =
K extends __WrapOne
? C extends [Codec, ...infer X]
? __CodecFirst<K, C, X>
: never
: K extends __WrapTwo
? C extends [Codec, Codec, ...infer X]
? __CodecFirst<K, C, X>
: never
: __CodecFirst<K, C, C>;
export type __Codecs<T extends unknown[]> =
T extends [infer K, ...infer N]
? __CodecsNext<K, __Codecs<N>>
: [];
export type __RemoveEmpty<T extends string> =
T extends ''
? []
: [T];
export type __Combine<V extends __Value[], I extends string = '', T extends string = ''> =
[...V, ...__RemoveEmpty<I>, ...__RemoveEmpty<T>];
export type __Combine<V extends __Value[], I extends string> =
I extends ''
? V
: [...V, I];
export type __CombineInner<V extends __Value[], I extends string, X extends __Value> =
[...__RemoveEmpty<I>, ...V, X];
I extends ''
? [...V, X]
: [I, ...V, X];
// TODO At this point structs are fully empty, no field indicators
// FIXME At this point enum/set/struct are empty, no field indicators, just type indicators
export type __TokenizeStruct<T extends [__Value[], string], V extends __Value[], I extends string, R extends string> =
__Tokenize<T[1], __CombineInner<V, I,
R extends `"_set"${string}`
// eslint-disable-next-line @typescript-eslint/ban-types
? { _set: {} }
__Tokenize<T[1], __CombineInner<V, I, R extends `"_set"${string}`
? { _set: true }
: R extends `"_enum"${string}`
// eslint-disable-next-line @typescript-eslint/ban-types
? { _enum: {} }
// eslint-disable-next-line @typescript-eslint/ban-types
: { data: R }
? { _enum: true }
: { _fields: true }
>>;
export type __TokenizeTuple<T extends [__Value[], string], V extends __Value[], I extends string> =
__Tokenize<T[1], __CombineInner<V, I, T[0]>>;
export type __TokenizeWrapper<K extends string, V extends __Value[], I extends string, R extends string> =
export type __TokenizeWrapped<K extends string, V extends __Value[], I extends string, R extends string> =
K extends `${infer X}${R}`
? __Tokenize<R, __Combine<V, `${I}${X}`>>
? X extends '['
? __Tokenize<R, [...__Combine<V, I>, '[']>
: __Tokenize<R, __Combine<V, `${I}${X}`>>
: never;
export type __TokenizeKnown<K extends string, V extends __Value[], I extends string, R extends string> =
@@ -127,20 +143,18 @@ export type __TokenizeKnown<K extends string, V extends __Value[], I extends str
export type __Tokenize<K extends string, V extends __Value[] = [], I extends string = ''> =
K extends '' | ')' | '>' | '}'
? [__Combine<V, I>, '']
: K extends `${'Vec<' | 'Option<' | 'Compact<'}${infer R}`
? __TokenizeWrapper<K, V, I, R>
: K extends `${__Wrap}${infer R}`
? __TokenizeWrapped<K, V, I, R>
: K extends `${',' | '>'}${infer R}`
? __Tokenize<R, __Combine<V, I>>
: K extends `[${infer R}`
? __Tokenize<R, __Combine<V, I, '['>>
: K extends `)${infer R}` | `}${infer R}`
? [__Combine<V, I>, R]
: K extends `(${infer R}`
? __TokenizeTuple<__Tokenize<R>, V, I>
: K extends `{${infer R}`
? __TokenizeStruct<__Tokenize<R>, V, I, R>
: K extends `${keyof InterfaceTypes}${',' | '>'}${infer R}`
? __TokenizeKnown<K, V, I, R>
: K extends `${infer C}${infer R}`
? __Tokenize<R, V, `${I}${C}`>
: [__Combine<V, I>, ''];
: K extends `)${infer R}` | `}${infer R}`
? [__Combine<V, I>, R]
: K extends `(${infer R}`
? __TokenizeTuple<__Tokenize<R>, V, I>
: K extends `{${infer R}`
? __TokenizeStruct<__Tokenize<R>, V, I, R>
: K extends `${keyof InterfaceTypes}${',' | '>'}${infer R}`
? __TokenizeKnown<K, V, I, R>
: K extends `${infer C}${infer R}`
? __Tokenize<R, V, `${I}${C}`>
: [__Combine<V, I>, ''];
-1
View File
@@ -53,7 +53,6 @@ export interface IResult<O extends Codec = Codec, E extends Codec = Codec> exten
export interface ISet<K = string> extends Set<K>, Codec {
readonly strings: string[];
readonly valueEncoded: BN;
}
export interface IStruct<K = string, V extends Codec = Codec> extends Map<K, V>, Codec {