Skip to content

Commit

Permalink
Expand simple enums in type generation (polkadot-js#1795)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr authored Feb 2, 2020
1 parent 6cd0074 commit 6ba28c8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
16 changes: 8 additions & 8 deletions packages/api/src/types/augment/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ declare module '@polkadot/api/types/submittable' {
* the `origin` falls below _existential deposit_ and gets removed as dust.
* # </weight>
**/
bond: AugmentedSubmittable<(controller: Address | string | AccountId | AccountIndex | Uint8Array, value: Compact<BalanceOf> | AnyNumber | Uint8Array, payee: RewardDestination | number | any) => SubmittableExtrinsic<ApiType>>;
bond: AugmentedSubmittable<(controller: Address | string | AccountId | AccountIndex | Uint8Array, value: Compact<BalanceOf> | AnyNumber | Uint8Array, payee: RewardDestination | ('Staked' | 'Stash' | 'Controller') | number | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Add some extra amount that have appeared in the stash `free_balance` into the balance up
* for staking.
Expand Down Expand Up @@ -352,7 +352,7 @@ declare module '@polkadot/api/types/submittable' {
* - Writes are limited to the `origin` account key.
* # </weight>
**/
setPayee: AugmentedSubmittable<(payee: RewardDestination | number | any) => SubmittableExtrinsic<ApiType>>;
setPayee: AugmentedSubmittable<(payee: RewardDestination | ('Staked' | 'Stash' | 'Controller') | number | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* (Re-)set the controller of a stash.
* Effects will be felt at the beginning of the next era.
Expand Down Expand Up @@ -456,7 +456,7 @@ declare module '@polkadot/api/types/submittable' {
* - One DB change, one DB entry.
* # </weight>
**/
vote: AugmentedSubmittable<(refIndex: Compact<ReferendumIndex> | AnyNumber | Uint8Array, vote: Vote | { aye: boolean; conviction?: number | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') } | boolean | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
vote: AugmentedSubmittable<(refIndex: Compact<ReferendumIndex> | AnyNumber | Uint8Array, vote: Vote | { aye: boolean; conviction?: ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') | number } | boolean | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Vote in a referendum on behalf of a stash. If `vote.is_aye()`, the vote is to enact
* the proposal; otherwise it is a vote to keep the status quo.
Expand All @@ -465,7 +465,7 @@ declare module '@polkadot/api/types/submittable' {
* - One DB change, one DB entry.
* # </weight>
**/
proxyVote: AugmentedSubmittable<(refIndex: Compact<ReferendumIndex> | AnyNumber | Uint8Array, vote: Vote | { aye: boolean; conviction?: number | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') } | boolean | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
proxyVote: AugmentedSubmittable<(refIndex: Compact<ReferendumIndex> | AnyNumber | Uint8Array, vote: Vote | { aye: boolean; conviction?: ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') | number } | boolean | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Schedule an emergency cancellation of a referendum. Cannot happen twice to the same
* referendum.
Expand Down Expand Up @@ -540,7 +540,7 @@ declare module '@polkadot/api/types/submittable' {
* - One extra DB entry.
* # </weight>
**/
delegate: AugmentedSubmittable<(to: AccountId | string | Uint8Array, conviction: Conviction | number | any) => SubmittableExtrinsic<ApiType>>;
delegate: AugmentedSubmittable<(to: AccountId | string | Uint8Array, conviction: Conviction | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') | number | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Undelegate vote.
* # <weight>
Expand Down Expand Up @@ -991,7 +991,7 @@ declare module '@polkadot/api/types/submittable' {
* one storage-exists.
* # </weight>
**/
setSubs: AugmentedSubmittable<(subs: Vec<ITuple<[AccountId, Data]>> | ([AccountId | string | Uint8Array, Data | number | any])[]) => SubmittableExtrinsic<ApiType>>;
setSubs: AugmentedSubmittable<(subs: Vec<ITuple<[AccountId, Data]>> | ([AccountId | string | Uint8Array, Data | object | number | Uint8Array | string])[]) => SubmittableExtrinsic<ApiType>>;
/**
* Clear an account's identity info and all sub-account and return all deposits.
* Payment: All reserved balances on the account are returned.
Expand Down Expand Up @@ -1094,7 +1094,7 @@ declare module '@polkadot/api/types/submittable' {
* - One event.
* # </weight>
**/
provideJudgement: AugmentedSubmittable<(regIndex: Compact<RegistrarIndex> | AnyNumber | Uint8Array, target: Address | string | AccountId | AccountIndex | Uint8Array, judgement: IdentityJudgement | number | any) => SubmittableExtrinsic<ApiType>>;
provideJudgement: AugmentedSubmittable<(regIndex: Compact<RegistrarIndex> | AnyNumber | Uint8Array, target: Address | string | AccountId | AccountIndex | Uint8Array, judgement: IdentityJudgement | object | number | Uint8Array | string) => SubmittableExtrinsic<ApiType>>;
/**
* Remove an account's identity and sub-account information and slash the deposits.
* Payment: Reserved balances from `set_subs` and `set_identity` are slashed and handled by
Expand Down Expand Up @@ -1360,7 +1360,7 @@ declare module '@polkadot/api/types/submittable' {
* Total Complexity: O(M + logM + B + X)
* # </weight>
**/
judgeSuspendedCandidate: AugmentedSubmittable<(who: AccountId | string | Uint8Array, judgement: SocietyJudgement | number | any) => SubmittableExtrinsic<ApiType>>;
judgeSuspendedCandidate: AugmentedSubmittable<(who: AccountId | string | Uint8Array, judgement: SocietyJudgement | ('Rebid' | 'Reject' | 'Approve') | number | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Allows root origin to change the maximum number of members in society.
* Max membership count must be greater than 1.
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-core/src/jsonrpc.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface RpcInterface {
author: {
insertKey(keyType: Text | string, suri: Text | string, publicKey: Bytes | string | Uint8Array): Observable<Bytes>;
pendingExtrinsics(): Observable<Vec<Extrinsic>>;
removeExtrinsic(bytesOrHash: Vec<ExtrinsicOrHash> | (ExtrinsicOrHash | number | any)[]): Observable<Vec<Hash>>;
removeExtrinsic(bytesOrHash: Vec<ExtrinsicOrHash> | (ExtrinsicOrHash | object | number | Uint8Array | string)[]): Observable<Vec<Hash>>;
rotateKeys(): Observable<Bytes>;
submitAndWatchExtrinsic(extrinsic: IExtrinsic): Observable<ExtrinsicStatus>;
submitExtrinsic(extrinsic: IExtrinsic): Observable<Hash>;
Expand Down
16 changes: 15 additions & 1 deletion packages/types/src/codec/Enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ export default class Enum extends Base<Codec> {
return this._index;
}

/**
* @description true if this is a basic enum (no values)
*/
public get isBasic (): boolean {
return this._isBasic;
}

/**
* @description Checks if the Enum points to a [[Null]] type
*/
Expand All @@ -196,11 +203,18 @@ export default class Enum extends Base<Codec> {
return this.raw instanceof Null;
}

/**
* @description The available keys for this enum
*/
public get defKeys (): string[] {
return Object.keys(this._def);
}

/**
* @description The name of the type this enum value represents
*/
public get type (): string {
return Object.keys(this._def)[this._index];
return this.defKeys[this._index];
}

/**
Expand Down
41 changes: 26 additions & 15 deletions packages/types/src/scripts/util/derived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import * as primitiveClasses from '../../primitive';
import { formatType } from './formatting';
import { setImports, TypeImports } from './imports';

const voteConvictions = _voteConvictions.map((c): string => `'${c}'`).join(' | ');
function arrayToStrType (arr: string[]): string {
return `(${arr.map((c): string => `'${c}'`).join(' | ')})`;
}

const voteConvictions = arrayToStrType(_voteConvictions);

// From `T`, generate `Compact<T>, Option<T>, Vec<T>`
/** @internal */
Expand Down Expand Up @@ -70,9 +74,9 @@ export function getSimilarTypes (definitions: object, registry: Registry, type:
return ['StorageKey', 'string', 'Uint8Array', 'any'];
}

const clazz = ClassOfUnsafe(registry, type);
const Clazz = ClassOfUnsafe(registry, type);

if (isChildClass(Vec, clazz)) {
if (isChildClass(Vec, Clazz)) {
const subDef = (getTypeDef(type).sub) as TypeDef;

if (subDef.info === TypeDefInfo.Plain) {
Expand All @@ -86,24 +90,31 @@ export function getSimilarTypes (definitions: object, registry: Registry, type:
} else {
throw new Error(`Unhandled subtype in Vec, ${JSON.stringify(subDef)}`);
}
} else if (isChildClass(Enum, clazz)) {
// TODO Handle this more gracefully (expand actual options)
possibleTypes.push('number', 'any');
} else if (isChildClass(AbstractInt as unknown as Constructor<any>, clazz) || isChildClass(Compact, clazz)) {
} else if (isChildClass(Enum, Clazz)) {
const e = new Clazz(registry) as Enum;

if (e.isBasic) {
possibleTypes.push(arrayToStrType(e.defKeys), 'number', 'Uint8Array');
} else {
// TODO Handle this more gracefully (expand actual options)
possibleTypes.push('object', 'number', 'Uint8Array', 'string');
}
} else if (isChildClass(AbstractInt as unknown as Constructor<any>, Clazz) || isChildClass(Compact, Clazz)) {
possibleTypes.push('AnyNumber', 'Uint8Array');
} else if (isChildClass(ClassOf(registry, 'Address'), clazz)) {
} else if (isChildClass(ClassOf(registry, 'Address'), Clazz)) {
possibleTypes.push('string', 'AccountId', 'AccountIndex', 'Uint8Array');
} else if (isChildClass(ClassOf(registry, 'bool'), clazz)) {
} else if (isChildClass(ClassOf(registry, 'bool'), Clazz)) {
possibleTypes.push('boolean', 'Uint8Array');
} else if (isChildClass(Struct, clazz)) {
} else if (isChildClass(Struct, Clazz)) {
possibleTypes.push('object', 'string', 'Uint8Array');
} else if (isChildClass(Option, clazz)) {
} else if (isChildClass(Option, Clazz)) {
// TODO inspect container
possibleTypes.push('null', 'object', 'string', 'Uint8Array');
} else if (isChildClass(Vote, clazz)) {
possibleTypes.push(`{ aye: boolean; conviction?: number | (${voteConvictions}) }`, 'boolean', 'string', 'Uint8Array');
} else if (isChildClass(Uint8Array, clazz)) {
} else if (isChildClass(Vote, Clazz)) {
possibleTypes.push(`{ aye: boolean; conviction?: ${voteConvictions} | number }`, 'boolean', 'string', 'Uint8Array');
} else if (isChildClass(Uint8Array, Clazz)) {
possibleTypes.push('string', 'Uint8Array');
} else if (isChildClass(String, clazz)) {
} else if (isChildClass(String, Clazz)) {
possibleTypes.push('string');
}

Expand Down

0 comments on commit 6ba28c8

Please sign in to comment.