assert type on all asXxx getters (#879)

* assert type on all asXxx getters

* Convert via built-in asVx

* Implicit index

* Re-add V0 (not dropped here, but dropped somewhere)

* MetadataDeprectated -> MetadataDeprecated
This commit is contained in:
Jaco Greeff
2019-05-07 16:01:35 +02:00
committed by GitHub
parent 07792ff54a
commit 28399544cb
11 changed files with 236 additions and 24 deletions
@@ -20,13 +20,16 @@ import v2ToV1 from './v2/toV1';
import v3ToV2 from './v3/toV2'; import v3ToV2 from './v3/toV2';
import v4ToV3 from './v4/toV3'; import v4ToV3 from './v4/toV3';
class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4> { class MetadataDeprecated extends Null {
}
class MetadataEnum extends EnumType<MetadataDeprecated | MetadataV0 | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4> {
constructor (value?: any) { constructor (value?: any) {
super({ super({
MetadataV0, // once rolled-out, can replace this with Null MetadataV0, // once rolled-out, can replace this with MetadataDeprecated
MetadataV1, // once rolled-out, can replace this with Null MetadataV1, // once rolled-out, can replace this with MetadataDeprecated
MetadataV2, // once rolled-out, can replace this with Null MetadataV2, // once rolled-out, can replace this with MetadataDeprecated
MetadataV3, // once rolled-out, can replace this with Null MetadataV3, // once rolled-out, can replace this with MetadataDeprecated
MetadataV4 MetadataV4
}, value); }, value);
} }
@@ -35,6 +38,8 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V0 object * @description Returns the wrapped values as a V0 object
*/ */
get asV0 (): MetadataV0 { get asV0 (): MetadataV0 {
assert(this.isV0, `Cannot convert '${this.type}' via asV0`);
return this.value as MetadataV0; return this.value as MetadataV0;
} }
@@ -42,6 +47,8 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V1 object * @description Returns the wrapped values as a V1 object
*/ */
get asV1 (): MetadataV1 { get asV1 (): MetadataV1 {
assert(this.isV1, `Cannot convert '${this.type}' via asV1`);
return this.value as MetadataV1; return this.value as MetadataV1;
} }
@@ -49,6 +56,8 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V2 object * @description Returns the wrapped values as a V2 object
*/ */
get asV2 (): MetadataV2 { get asV2 (): MetadataV2 {
assert(this.isV2, `Cannot convert '${this.type}' via asV2`);
return this.value as MetadataV2; return this.value as MetadataV2;
} }
@@ -56,6 +65,8 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V3 object * @description Returns the wrapped values as a V3 object
*/ */
get asV3 (): MetadataV3 { get asV3 (): MetadataV3 {
assert(this.isV3, `Cannot convert '${this.type}' via asV3`);
return this.value as MetadataV3; return this.value as MetadataV3;
} }
@@ -63,9 +74,53 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V4 object * @description Returns the wrapped values as a V4 object
*/ */
get asV4 (): MetadataV4 { get asV4 (): MetadataV4 {
assert(this.isV4, `Cannot convert '${this.type}' via asV4`);
return this.value as MetadataV4; return this.value as MetadataV4;
} }
/**
* @description `true` if Deprecated
*/
get isDeprecated (): boolean {
return this.type === 'MetadataDeprectated';
}
/**
* @description `true` if V0
*/
get isV0 (): boolean {
return this.type === 'MetadataV0';
}
/**
* @description `true` if V1
*/
get isV1 (): boolean {
return this.type === 'MetadataV1';
}
/**
* @description `true` if V2
*/
get isV2 (): boolean {
return this.type === 'MetadataV2';
}
/**
* @description `true` if V3
*/
get isV3 (): boolean {
return this.type === 'MetadataV3';
}
/**
* @description `true` if V4
*/
get isV4 (): boolean {
return this.type === 'MetadataV4';
}
/** /**
* @description The version this metadata represents * @description The version this metadata represents
*/ */
@@ -139,7 +194,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
assert([2, 3, 4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v1`); assert([2, 3, 4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v1`);
if (isUndefined(this._convertedV1)) { if (isUndefined(this._convertedV1)) {
this._convertedV1 = v2ToV1(this.metadata.asV2); this._convertedV1 = v2ToV1(this.asV2);
} }
return this._convertedV1; return this._convertedV1;
@@ -156,7 +211,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
assert([3, 4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v2`); assert([3, 4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v2`);
if (isUndefined(this._convertedV2)) { if (isUndefined(this._convertedV2)) {
this._convertedV2 = v3ToV2(this.metadata.asV3); this._convertedV2 = v3ToV2(this.asV3);
} }
return this._convertedV2; return this._convertedV2;
@@ -173,7 +228,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
assert([4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v2`); assert([4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v2`);
if (isUndefined(this._convertedV3)) { if (isUndefined(this._convertedV3)) {
this._convertedV3 = v4ToV3(this.metadata.asV4); this._convertedV3 = v4ToV3(this.asV4);
} }
return this.metadata.asV3; return this.metadata.asV3;
+20 -7
View File
@@ -4,6 +4,8 @@
import { AnyNumber } from '../../types'; import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import Enum from '../../codec/Enum'; import Enum from '../../codec/Enum';
import EnumType from '../../codec/EnumType'; import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct'; import Struct from '../../codec/Struct';
@@ -77,17 +79,12 @@ export class StorageFunctionType extends EnumType<PlainType | MapType> {
}, value, index); }, value, index);
} }
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/** /**
* @description The value as a mapped value * @description The value as a mapped value
*/ */
get asMap (): MapType { get asMap (): MapType {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapType; return this.value as MapType;
} }
@@ -95,9 +92,25 @@ export class StorageFunctionType extends EnumType<PlainType | MapType> {
* @description The value as a [[Type]] value * @description The value as a [[Type]] value
*/ */
get asType (): PlainType { get asType (): PlainType {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainType; return this.value as PlainType;
} }
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
get isPlainType (): boolean {
return this.toNumber() === 0;
}
/** /**
* @description Returns the string representation of the value * @description Returns the string representation of the value
*/ */
+20 -7
View File
@@ -4,6 +4,8 @@
import { AnyNumber } from '../../types'; import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import EnumType from '../../codec/EnumType'; import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct'; import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector'; import Vector from '../../codec/Vector';
@@ -58,17 +60,12 @@ export class StorageFunctionType extends EnumType<PlainType | MapType> {
}, value, index); }, value, index);
} }
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/** /**
* @description The value as a mapped value * @description The value as a mapped value
*/ */
get asMap (): MapType { get asMap (): MapType {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapType; return this.value as MapType;
} }
@@ -76,9 +73,25 @@ export class StorageFunctionType extends EnumType<PlainType | MapType> {
* @description The value as a [[Type]] value * @description The value as a [[Type]] value
*/ */
get asType (): PlainType { get asType (): PlainType {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainType; return this.value as PlainType;
} }
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
get isPlainType (): boolean {
return this.toNumber() === 0;
}
/** /**
* @description Returns the string representation of the value * @description Returns the string representation of the value
*/ */
+15
View File
@@ -4,6 +4,8 @@
import { AnyNumber } from '../../types'; import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import EnumType from '../../codec/EnumType'; import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct'; import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector'; import Vector from '../../codec/Vector';
@@ -74,6 +76,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value * @description The value as a mapped value
*/ */
get asDoubleMap (): DoubleMapType { get asDoubleMap (): DoubleMapType {
assert(this.isDoubleMap, `Cannot convert '${this.type}' via asDoubleMap`);
return this.value as DoubleMapType; return this.value as DoubleMapType;
} }
@@ -81,6 +85,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value * @description The value as a mapped value
*/ */
get asMap (): MapType { get asMap (): MapType {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapType; return this.value as MapType;
} }
@@ -88,6 +94,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a [[Type]] value * @description The value as a [[Type]] value
*/ */
get asType (): PlainType { get asType (): PlainType {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainType; return this.value as PlainType;
} }
@@ -105,6 +113,13 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
return this.toNumber() === 1; return this.toNumber() === 1;
} }
/**
* @description `true` if the storage entry is a plain type
*/
get isPlainType (): boolean {
return this.toNumber() === 0;
}
/** /**
* @description Returns the string representation of the value * @description Returns the string representation of the value
*/ */
+15
View File
@@ -4,6 +4,8 @@
import { AnyNumber } from '../../types'; import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import Enum from '../../codec/Enum'; import Enum from '../../codec/Enum';
import EnumType from '../../codec/EnumType'; import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct'; import Struct from '../../codec/Struct';
@@ -168,6 +170,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value * @description The value as a mapped value
*/ */
get asDoubleMap (): DoubleMapType { get asDoubleMap (): DoubleMapType {
assert(this.isDoubleMap, `Cannot convert '${this.type}' via asDoubleMap`);
return this.value as DoubleMapType; return this.value as DoubleMapType;
} }
@@ -175,6 +179,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value * @description The value as a mapped value
*/ */
get asMap (): MapType { get asMap (): MapType {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapType; return this.value as MapType;
} }
@@ -182,6 +188,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a [[Type]] value * @description The value as a [[Type]] value
*/ */
get asType (): PlainType { get asType (): PlainType {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainType; return this.value as PlainType;
} }
@@ -199,6 +207,13 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
return this.toNumber() === 1; return this.toNumber() === 1;
} }
/**
* @description `true` if the storage entry is a plain type
*/
get isPlainType (): boolean {
return this.toNumber() === 0;
}
/** /**
* @description Returns the string representation of the value * @description Returns the string representation of the value
*/ */
+16
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType'; import EnumType from '../codec/EnumType';
import Vector from '../codec/Vector'; import Vector from '../codec/Vector';
import Null from '../primitive/Null'; import Null from '../primitive/Null';
@@ -88,6 +90,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Broadcast]] * @description Returns the item as a [[Broadcast]]
*/ */
get asBroadcast (): Broadcast { get asBroadcast (): Broadcast {
assert(this.isBroadcast, `Cannot convert '${this.type}' via asBroadcast`);
return this.value as Broadcast; return this.value as Broadcast;
} }
@@ -95,6 +99,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Dropped]] * @description Returns the item as a [[Dropped]]
*/ */
get asDropped (): Dropped { get asDropped (): Dropped {
assert(this.isDropped, `Cannot convert '${this.type}' via asDropped`);
return this.value as Dropped; return this.value as Dropped;
} }
@@ -102,6 +108,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Finalized]] * @description Returns the item as a [[Finalized]]
*/ */
get asFinalized (): Finalized { get asFinalized (): Finalized {
assert(this.isFinalized, `Cannot convert '${this.type}' via asFinalized`);
return this.value as Finalized; return this.value as Finalized;
} }
@@ -109,6 +117,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Future]] * @description Returns the item as a [[Future]]
*/ */
get asFuture (): Future { get asFuture (): Future {
assert(this.isFuture, `Cannot convert '${this.type}' via asFuture`);
return this.value as Future; return this.value as Future;
} }
@@ -116,6 +126,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Invalid]] * @description Returns the item as a [[Invalid]]
*/ */
get asInvalid (): Invalid { get asInvalid (): Invalid {
assert(this.isInvalid, `Cannot convert '${this.type}' via asInvalid`);
return this.value as Invalid; return this.value as Invalid;
} }
@@ -123,6 +135,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Ready]] * @description Returns the item as a [[Ready]]
*/ */
get asReady (): Ready { get asReady (): Ready {
assert(this.isReady, `Cannot convert '${this.type}' via asReady`);
return this.value as Ready; return this.value as Ready;
} }
@@ -130,6 +144,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Usurped]] * @description Returns the item as a [[Usurped]]
*/ */
get asUsurped (): Usurped { get asUsurped (): Usurped {
assert(this.isUsurped, `Cannot convert '${this.type}' via asUsurped`);
return this.value as Usurped; return this.value as Usurped;
} }
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType'; import EnumType from '../codec/EnumType';
import Struct from '../codec/Struct'; import Struct from '../codec/Struct';
import Tuple from '../codec/Tuple'; import Tuple from '../codec/Tuple';
@@ -66,6 +68,8 @@ export class ValidityAttestation extends EnumType<Null | ImplicitCandidateSignat
* @description Returns the item as a [[ExplicitCandidateSignature]] * @description Returns the item as a [[ExplicitCandidateSignature]]
*/ */
get asExplicitCandidateSignature (): ExplicitCandidateSignature { get asExplicitCandidateSignature (): ExplicitCandidateSignature {
assert(this.toNumber() === 2, `Cannot convert '${this.type}' via asExplicitCandidateSignature`);
return this.value as ExplicitCandidateSignature; return this.value as ExplicitCandidateSignature;
} }
@@ -73,6 +77,8 @@ export class ValidityAttestation extends EnumType<Null | ImplicitCandidateSignat
* @description Returns the item as a [[ImplicitCandidateSignature]] * @description Returns the item as a [[ImplicitCandidateSignature]]
*/ */
get asImplicitCandidateSignature (): ImplicitCandidateSignature { get asImplicitCandidateSignature (): ImplicitCandidateSignature {
assert(this.toNumber() === 1, `Cannot convert '${this.type}' via asImplicitCandidateSignature`);
return this.value as ImplicitCandidateSignature; return this.value as ImplicitCandidateSignature;
} }
} }
+2 -2
View File
@@ -74,7 +74,7 @@ export default class ContractInfo extends EnumType<Alive | Tombstone> {
* @description Returns the item as an [[Alive]] * @description Returns the item as an [[Alive]]
*/ */
get asAlive (): Alive { get asAlive (): Alive {
assert(this.isAlive, `Cannot convert index ${this.toNumber()} to 'Alive'`); assert(this.isAlive, `Cannot convert '${this.type}' via asAlive`);
return this.value as Alive; return this.value as Alive;
} }
@@ -83,7 +83,7 @@ export default class ContractInfo extends EnumType<Alive | Tombstone> {
* @description Returns the item as an [[Tombstone]] * @description Returns the item as an [[Tombstone]]
*/ */
get asTombstone (): Tombstone { get asTombstone (): Tombstone {
assert(this.isTombstone, `Cannot convert index ${this.toNumber()} to 'Tombstone'`); assert(this.isTombstone, `Cannot convert '${this.type}' via asTombstone`);
return this.value as Tombstone; return this.value as Tombstone;
} }
+35
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType'; import EnumType from '../codec/EnumType';
import Struct from '../codec/Struct'; import Struct from '../codec/Struct';
import Tuple from '../codec/Tuple'; import Tuple from '../codec/Tuple';
@@ -103,6 +105,8 @@ export class Consensus extends Tuple {
* @description The slot and signature extracted from the raw data (assuming Aura) * @description The slot and signature extracted from the raw data (assuming Aura)
*/ */
get asAura (): [U64, Signature] { get asAura (): [U64, Signature] {
assert(this.isAura, 'Invalid engine for asAura conversion');
const raw = this.data.toU8a(true); const raw = this.data.toU8a(true);
return [ return [
@@ -133,6 +137,8 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @description Returns the item as a [[AuthoritiesChange]] * @description Returns the item as a [[AuthoritiesChange]]
*/ */
get asAuthoritiesChange (): AuthoritiesChange { get asAuthoritiesChange (): AuthoritiesChange {
assert(this.isAuthoritiesChange, `Cannot convert '${this.type}' via asAuthoritiesChange`);
return this.value as AuthoritiesChange; return this.value as AuthoritiesChange;
} }
@@ -140,6 +146,8 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @description Returns the item as a [[ChangesTrieRoot]] * @description Returns the item as a [[ChangesTrieRoot]]
*/ */
get asChangesTrieRoot (): ChangesTrieRoot { get asChangesTrieRoot (): ChangesTrieRoot {
assert(this.isChangesTrieRoot, `Cannot convert '${this.type}' via asChangesTrieRoot`);
return this.value as ChangesTrieRoot; return this.value as ChangesTrieRoot;
} }
@@ -147,6 +155,8 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @desciption Retuns the item as a [[Consensus]] * @desciption Retuns the item as a [[Consensus]]
*/ */
get asConsensus (): Consensus { get asConsensus (): Consensus {
assert(this.isConsensus, `Cannot convert '${this.type}' via asConsensus`);
return this.value as Consensus; return this.value as Consensus;
} }
@@ -154,6 +164,8 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @description Returns the item as a [[Other]] * @description Returns the item as a [[Other]]
*/ */
get asOther (): Other { get asOther (): Other {
assert(this.isOther, `Cannot convert '${this.type}' via asOther`);
return this.value as Other; return this.value as Other;
} }
@@ -161,9 +173,25 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @description Returns the item as a [[Seal]] * @description Returns the item as a [[Seal]]
*/ */
get asSeal (): Seal { get asSeal (): Seal {
assert(this.isSeal, `Cannot convert '${this.type}' via asSeal`);
return this.value as Seal; return this.value as Seal;
} }
/**
* @description Returns true on [[AuthoritiesChange]]
*/
get isAuthoritiesChange (): boolean {
return this.type === 'AuthoritiesChange';
}
/**
* @description Returns true on [[ChangesTrieRoot]]
*/
get isChangesTrieRoot (): boolean {
return this.type === 'ChangesTrieRoot';
}
/** /**
* @description Returns true on [[Consensus]] * @description Returns true on [[Consensus]]
*/ */
@@ -171,6 +199,13 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
return this.type === 'Consensus'; return this.type === 'Consensus';
} }
/**
* @description Returns true on [[Other]]
*/
get isOther (): boolean {
return this.type === 'Other';
}
/** /**
* @description Returns true on [[Seal]] * @description Returns true on [[Seal]]
*/ */
+6
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType'; import EnumType from '../codec/EnumType';
import Struct from '../codec/Struct'; import Struct from '../codec/Struct';
import Null from '../primitive/Null'; import Null from '../primitive/Null';
@@ -41,6 +43,8 @@ export class Phase extends EnumType<ApplyExtrinsic | Finalization> {
* @description Returns the item as a [[ApplyExtrinsic]] * @description Returns the item as a [[ApplyExtrinsic]]
*/ */
get asApplyExtrinsic (): ApplyExtrinsic { get asApplyExtrinsic (): ApplyExtrinsic {
assert(this.isApplyExtrinsic, `Cannot convert '${this.type}' via asApplyExtrinsic`);
return this.value as ApplyExtrinsic; return this.value as ApplyExtrinsic;
} }
@@ -48,6 +52,8 @@ export class Phase extends EnumType<ApplyExtrinsic | Finalization> {
* @description Returns the item as a [[Finalization]] * @description Returns the item as a [[Finalization]]
*/ */
get asFinalization (): Finalization { get asFinalization (): Finalization {
assert(this.isFinalization, `Cannot convert '${this.type}' via asFinalization`);
return this.value as Finalization; return this.value as Finalization;
} }
@@ -4,6 +4,8 @@
import { AnyNumber } from '../types'; import { AnyNumber } from '../types';
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType'; import EnumType from '../codec/EnumType';
import Struct from '../codec/Struct'; import Struct from '../codec/Struct';
import U32 from '../primitive/U32'; import U32 from '../primitive/U32';
@@ -132,6 +134,8 @@ export class MisbehaviorKind extends EnumType<BftProposeOutOfTurn | BftDoublePro
* @description Returns the item as a [[BftDoubleCommit]] * @description Returns the item as a [[BftDoubleCommit]]
*/ */
get asBftDoubleCommit (): BftDoubleCommit { get asBftDoubleCommit (): BftDoubleCommit {
assert(this.isBftDoubleCommit, `Cannot convert '${this.type}' via asBftDoubleCommit`);
return this.value as BftDoubleCommit; return this.value as BftDoubleCommit;
} }
@@ -139,6 +143,8 @@ export class MisbehaviorKind extends EnumType<BftProposeOutOfTurn | BftDoublePro
* @description Returns the item as a [[BftDoublePrepare]] * @description Returns the item as a [[BftDoublePrepare]]
*/ */
get asBftDoublePrepare (): BftDoublePrepare { get asBftDoublePrepare (): BftDoublePrepare {
assert(this.isBftDoublePrepare, `Cannot convert '${this.type}' via asBftDoublePrepare`);
return this.value as BftDoublePrepare; return this.value as BftDoublePrepare;
} }
@@ -146,6 +152,8 @@ export class MisbehaviorKind extends EnumType<BftProposeOutOfTurn | BftDoublePro
* @description Returns the item as a [[BftDoublePropose]] * @description Returns the item as a [[BftDoublePropose]]
*/ */
get asBftDoublePropose (): BftDoublePropose { get asBftDoublePropose (): BftDoublePropose {
assert(this.isBftDoublePropose, `Cannot convert '${this.type}' via asBftDoublePropose`);
return this.value as BftDoublePropose; return this.value as BftDoublePropose;
} }
@@ -153,8 +161,38 @@ export class MisbehaviorKind extends EnumType<BftProposeOutOfTurn | BftDoublePro
* @description Returns the item as a [[BftProposeOutOfTurn]] * @description Returns the item as a [[BftProposeOutOfTurn]]
*/ */
get asBftProposeOutOfTurn (): BftProposeOutOfTurn { get asBftProposeOutOfTurn (): BftProposeOutOfTurn {
assert(this.isBftProposeOutOfTurn, `Cannot convert '${this.type}' via asBftProposeOutOfTurn`);
return this.value as BftProposeOutOfTurn; return this.value as BftProposeOutOfTurn;
} }
/**
* @description true when this is a BftDoubleCommit
*/
get isBftDoubleCommit (): boolean {
return this.type === 'BftDoubleCommit';
}
/**
* @description true when this is a BftDoublePrepare
*/
get isBftDoublePrepare (): boolean {
return this.type === 'BftDoublePrepare';
}
/**
* @description true when this is a BftDoublePropose
*/
get isBftDoublePropose (): boolean {
return this.type === 'BftDoublePropose';
}
/**
* @description true when this is a BftProposeOutOfTurn
*/
get isBftProposeOutOfTurn (): boolean {
return this.type === 'BftProposeOutOfTurn';
}
} }
type MisbehaviorReportValue = { type MisbehaviorReportValue = {