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
This commit is contained in:
Jaco Greeff
2020-02-02 19:17:33 +01:00
committed by GitHub
parent f499946f1f
commit b3d592357f
6 changed files with 21 additions and 7 deletions
+5 -2
View File
@@ -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<typeof convictionNames>;
};
// For votes, the topmost bit indicated aye/nay, the lower bits indicate the conviction
+2 -2
View File
@@ -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];
});
+6 -1
View File
@@ -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<T>, Option<T>, Vec<T>`
/** @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)) {
+5
View File
@@ -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<unknown>> = T extends ReadonlyArray<infer ElementType>
? ElementType
: never;
export type BareOpts = boolean | Record<string, boolean>;
export type InterfaceTypes = keyof InterfaceRegistry;