From b3d592357fdd586910503a023bee0d76db825ea2 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 2 Feb 2020 19:17:33 +0100 Subject: [PATCH] Override for Vote type (not a struct, not caught) (#1794) * Override for Vote type (not a struct, not caught) * Generate simple Vote object (first, Struct to be done) * Comment for hex type (string) * Expand conviction (like we should on enum) * Cool, use conviction def for input * aye is required * Remove FIXMEs --- CHANGELOG.md | 1 + packages/api/src/types/augment/tx.ts | 4 ++-- packages/types/src/primitive/Generic/Vote.ts | 7 +++++-- packages/types/src/scripts/generate/tx.ts | 4 ++-- packages/types/src/scripts/util/derived.ts | 7 ++++++- packages/types/src/types.ts | 5 +++++ 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 349da4044b..2c198d4ec3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # 1.1.0-beta.x +- Add support for `balances.account` in all applicable derives (incl. `vesting` module) - Inject and use SignedExtensions by name from V11 metadata (V4 extrinsics only) - Add typings for `api.tx.*.*` - the `polkadot-types-from-chain` now also generates these - Remove additional V1-only derives `(staking.recentlyOffline`, `balances.nominatorsFor`) diff --git a/packages/api/src/types/augment/tx.ts b/packages/api/src/types/augment/tx.ts index 6cd7fd60e7..5cdb5030f0 100644 --- a/packages/api/src/types/augment/tx.ts +++ b/packages/api/src/types/augment/tx.ts @@ -456,7 +456,7 @@ declare module '@polkadot/api/types/submittable' { * - One DB change, one DB entry. * # **/ - vote: AugmentedSubmittable<(refIndex: Compact | AnyNumber | Uint8Array, vote: Vote | string | Uint8Array) => SubmittableExtrinsic>; + vote: AugmentedSubmittable<(refIndex: Compact | AnyNumber | Uint8Array, vote: Vote | { aye: boolean; conviction?: number | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') } | boolean | string | Uint8Array) => SubmittableExtrinsic>; /** * 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. @@ -465,7 +465,7 @@ declare module '@polkadot/api/types/submittable' { * - One DB change, one DB entry. * # **/ - proxyVote: AugmentedSubmittable<(refIndex: Compact | AnyNumber | Uint8Array, vote: Vote | string | Uint8Array) => SubmittableExtrinsic>; + proxyVote: AugmentedSubmittable<(refIndex: Compact | AnyNumber | Uint8Array, vote: Vote | { aye: boolean; conviction?: number | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') } | boolean | string | Uint8Array) => SubmittableExtrinsic>; /** * Schedule an emergency cancellation of a referendum. Cannot happen twice to the same * referendum. diff --git a/packages/types/src/primitive/Generic/Vote.ts b/packages/types/src/primitive/Generic/Vote.ts index be7b910f81..0db3aa0643 100644 --- a/packages/types/src/primitive/Generic/Vote.ts +++ b/packages/types/src/primitive/Generic/Vote.ts @@ -2,7 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Registry } from '../../types'; +import { ArrayElementType, Registry } from '../../types'; +import democracyDef from '../../interfaces/democracy/definitions'; import { Conviction } from '../../interfaces/democracy'; import { isBoolean, isNumber, isU8a, isUndefined } from '@polkadot/util'; @@ -11,10 +12,12 @@ import { createType } from '../../codec/create'; import U8aFixed from '../../codec/U8aFixed'; import Bool from '../Bool'; +export const convictionNames = democracyDef.types.Conviction._enum; + // eslint-disable-next-line @typescript-eslint/ban-types type InputTypes = boolean | number | Boolean | Uint8Array | { aye: boolean; - conviction?: number | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x'); + conviction?: number | ArrayElementType; }; // For votes, the topmost bit indicated aye/nay, the lower bits indicate the conviction diff --git a/packages/types/src/scripts/generate/tx.ts b/packages/types/src/scripts/generate/tx.ts index 47fbb73072..7fedc3e59e 100644 --- a/packages/types/src/scripts/generate/tx.ts +++ b/packages/types/src/scripts/generate/tx.ts @@ -43,13 +43,13 @@ function generateModule (registry: Registry, allDefs: object, { calls, name }: M const params = args.map(({ name, type }): [string, string, string] => { const typeStr = type.toString(); const similarTypes = getSimilarTypes(allDefs, registry, typeStr, imports).map((type): string => - type.startsWith('(') + type.startsWith('(') || type.startsWith('{') ? type : formatType(allDefs, type, imports) ); const nameStr = mapName(name); - setImports(allDefs, imports, [...similarTypes.filter((type): boolean => !type.startsWith('(')), typeStr]); + setImports(allDefs, imports, [...similarTypes.filter((type): boolean => !type.startsWith('(') && !type.startsWith('{')), typeStr]); return [`${nameStr}: ${similarTypes.join(' | ')}`, nameStr, typeStr]; }); diff --git a/packages/types/src/scripts/util/derived.ts b/packages/types/src/scripts/util/derived.ts index 267b912d8e..1ed711f7d3 100644 --- a/packages/types/src/scripts/util/derived.ts +++ b/packages/types/src/scripts/util/derived.ts @@ -13,9 +13,12 @@ import Enum from '../../codec/Enum'; import Option from '../../codec/Option'; import Struct from '../../codec/Struct'; import Vec from '../../codec/Vec'; +import Vote, { convictionNames as _voteConvictions } from '../../primitive/Generic/Vote'; +import * as primitiveClasses from '../../primitive'; import { formatType } from './formatting'; import { setImports, TypeImports } from './imports'; -import * as primitiveClasses from '../../primitive'; + +const voteConvictions = _voteConvictions.map((c): string => `'${c}'`).join(' | '); // From `T`, generate `Compact, Option, Vec` /** @internal */ @@ -96,6 +99,8 @@ export function getSimilarTypes (definitions: object, registry: Registry, type: possibleTypes.push('object', 'string', 'Uint8Array'); } else if (isChildClass(Option, clazz)) { 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)) { possibleTypes.push('string', 'Uint8Array'); } else if (isChildClass(String, clazz)) { diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index 4e3046cb47..903dbe867b 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -13,6 +13,11 @@ import { Signer } from '@polkadot/api/types'; export * from './codec/types'; +// helper to xtract keys from an array +export type ArrayElementType> = T extends ReadonlyArray + ? ElementType + : never; + export type BareOpts = boolean | Record; export type InterfaceTypes = keyof InterfaceRegistry;