Complexity adjustments (#2803)

* Complexity adjustments

* cleanups

* Small cleanups

* Additional

* revert era

* ExtrinsicEra
This commit is contained in:
Jaco Greeff
2020-10-29 14:20:10 +01:00
committed by GitHub
parent 63fd5baa8f
commit 94ebb321f1
15 changed files with 105 additions and 145 deletions
+2 -4
View File
@@ -4,7 +4,7 @@
import { H256 } from '@polkadot/types/interfaces';
import { AnyJson, Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
import { assert, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
import Null from '../primitive/Null';
import { typeToConstructor } from './utils';
@@ -188,9 +188,7 @@ export default class Option<T extends Codec> implements Codec {
* @description Returns the value that the Option represents (if available), throws if null
*/
public unwrap (): T {
if (this.isNone) {
throw new Error('Option: unwrapping a None value');
}
assert(this.isSome, 'Option: unwrapping a None value');
return this.#raw;
}
+1 -1
View File
@@ -3,7 +3,7 @@
import { AnyNumber, AnyU8a, AnyString, Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { isU8a, u8aConcat, isHex, hexToU8a } from '@polkadot/util';
import { isU8a, isHex, hexToU8a, u8aConcat } from '@polkadot/util';
import { decodeU8a, mapToTypeMap, typeToConstructor } from './utils';
import AbstractArray from './AbstractArray';
+1 -3
View File
@@ -187,9 +187,7 @@ export function getTypeClass<T extends Codec = Codec> (registry: Registry, value
const getFn = infoMapping[value.info];
if (!getFn) {
throw new Error(`Unable to construct class from ${JSON.stringify(value)}`);
}
assert(getFn, `Unable to construct class from ${JSON.stringify(value)}`);
return getFn(registry, value) as Constructor<T>;
}
+1 -3
View File
@@ -274,9 +274,7 @@ export class TypeRegistry implements Registry {
public getOrThrow <T extends Codec = Codec> (name: string, msg?: string): Constructor<T> {
const Type = this.get<T>(name);
if (isUndefined(Type)) {
throw new Error(msg || `type ${name} not found`);
}
assert(!isUndefined(Type), msg || `type ${name} not found`);
return Type;
}
+11 -13
View File
@@ -4,7 +4,7 @@
import { Registry } from '../types';
import BN from 'bn.js';
import { isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { isBigInt, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import Base from '../codec/Base';
@@ -12,7 +12,7 @@ import AccountIndex from '../generic/AccountIndex';
import EthereumAccountId from './AccountId';
// eslint-disable-next-line no-use-before-define
type AnyAddress = BN | LookupSource | EthereumAccountId | AccountIndex | number[] | Uint8Array | number | string;
type AnyAddress = BigInt | BN | LookupSource | EthereumAccountId | AccountIndex | number[] | Uint8Array | number | string;
export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
@@ -55,17 +55,15 @@ export default class LookupSource extends Base<EthereumAccountId | AccountIndex>
/** @internal */
private static _decodeAddress (registry: Registry, value: AnyAddress): EthereumAccountId | AccountIndex {
if (value instanceof EthereumAccountId || value instanceof AccountIndex) {
return value;
} else if (value instanceof LookupSource) {
return value._raw;
} else if (isBn(value) || isNumber(value)) {
return registry.createType('AccountIndex', value);
} else if (Array.isArray(value) || isHex(value) || isU8a(value)) {
return decodeU8a(registry, u8aToU8a(value));
}
return decodeString(registry, value);
return value instanceof LookupSource
? value._raw
: value instanceof EthereumAccountId || value instanceof AccountIndex
? value
: isBn(value) || isNumber(value) || isBigInt(value)
? registry.createType('AccountIndex', value)
: Array.isArray(value) || isHex(value) || isU8a(value)
? decodeU8a(registry, u8aToU8a(value))
: decodeString(registry, value);
}
/**
+3 -7
View File
@@ -4,7 +4,7 @@
import { AnyU8a, IExtrinsicEra, Registry } from '../types';
import BN from 'bn.js';
import { assert, bnToBn, formatNumber, hexToU8a, isHex, isU8a, isObject, u8aToBn } from '@polkadot/util';
import { assert, bnToBn, formatNumber, hexToU8a, isHex, isU8a, isObject, u8aToBn, u8aToU8a } from '@polkadot/util';
import Enum from '../codec/Enum';
import Tuple from '../codec/Tuple';
@@ -67,12 +67,8 @@ export class MortalEra extends Tuple {
/** @internal */
private static _decodeMortalEra (registry: Registry, value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue {
if (isHex(value)) {
return MortalEra._decodeMortalU8a(registry, hexToU8a(value));
} else if (Array.isArray(value)) {
return MortalEra._decodeMortalU8a(registry, new Uint8Array(value));
} else if (isU8a(value)) {
return MortalEra._decodeMortalU8a(registry, value);
if (isU8a(value) || isHex(value) || Array.isArray(value)) {
return MortalEra._decodeMortalU8a(registry, u8aToU8a(value));
} else if (isObject(value)) {
return MortalEra._decodeMortalObject(registry, value);
} else if (!value) {
+11 -13
View File
@@ -4,7 +4,7 @@
import { Registry } from '../types';
import BN from 'bn.js';
import { isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { isBigInt, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import Base from '../codec/Base';
@@ -12,7 +12,7 @@ import AccountId from './AccountId';
import AccountIndex from './AccountIndex';
// eslint-disable-next-line no-use-before-define
type AnyAddress = BN | LookupSource | AccountId | AccountIndex | number[] | Uint8Array | number | string;
type AnyAddress = BigInt | BN | LookupSource | AccountId | AccountIndex | number[] | Uint8Array | number | string;
export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
@@ -55,17 +55,15 @@ export default class LookupSource extends Base<AccountId | AccountIndex> {
/** @internal */
private static _decodeAddress (registry: Registry, value: AnyAddress): AccountId | AccountIndex {
if (value instanceof AccountId || value instanceof AccountIndex) {
return value;
} else if (value instanceof LookupSource) {
return value._raw;
} else if (isBn(value) || isNumber(value)) {
return registry.createType('AccountIndex', value);
} else if (Array.isArray(value) || isHex(value) || isU8a(value)) {
return decodeU8a(registry, u8aToU8a(value));
}
return decodeString(registry, value);
return value instanceof LookupSource
? value._raw
: value instanceof AccountId || value instanceof AccountIndex
? value
: isBn(value) || isNumber(value) || isBigInt(value)
? registry.createType('AccountIndex', value)
: Array.isArray(value) || isHex(value) || isU8a(value)
? decodeU8a(registry, u8aToU8a(value))
: decodeString(registry, value);
}
/**
+1 -1
View File
@@ -10,7 +10,7 @@ import Enum from '../codec/Enum';
import Bytes from './Bytes';
/** @internal */
function decodeDataU8a (registry: Registry, value: Uint8Array): [any, number | undefined] {
function decodeDataU8a (registry: Registry, value: Uint8Array): [undefined | null | Uint8Array, number | undefined] {
if (!value.length) {
return [undefined, undefined];
}
@@ -11,13 +11,13 @@ import Null from './Null';
* An unknown type that fails on construction with the type info
*/
export default class DoNotConstruct extends Null {
constructor (registry: Registry, typeName: string) {
constructor (registry: Registry, typeName = 'DoNotConstruct') {
super(registry);
throw new Error(`Cannot construct unknown type ${typeName}`);
}
public static with (typeName = 'DoNotConstruct'): Constructor {
public static with (typeName?: string): Constructor {
return class extends DoNotConstruct {
constructor (registry: Registry) {
super(registry, typeName);