From 5c8bad5bb901998332e27ffa83d508a65239880a Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 16 Dec 2021 06:51:52 +0100 Subject: [PATCH] getTyped to cleanup this.get('...') (#4334) * getTyped to cleanup this.get('...') * shorten * Adjust --- packages/types-codec/src/native/Json.ts | 4 +++ packages/types-codec/src/native/Struct.ts | 4 +++ packages/types/src/ethereum/AccountId.ts | 6 +--- packages/types/src/ethereum/LookupSource.ts | 27 +++++++++--------- packages/types/src/extrinsic/SignerPayload.ts | 22 +++++++-------- packages/types/src/extrinsic/v4/Extrinsic.ts | 4 +-- .../src/extrinsic/v4/ExtrinsicPayload.ts | 18 ++++++------ .../src/extrinsic/v4/ExtrinsicSignature.ts | 10 +++---- packages/types/src/generic/Block.ts | 4 +-- packages/types/src/generic/Call.ts | 8 +++--- packages/types/src/generic/ChainProperties.ts | 6 ++-- packages/types/src/generic/Event.ts | 4 +-- packages/types/src/generic/LookupSource.ts | 28 +++++++++---------- .../types/src/metadata/MetadataVersioned.ts | 4 +-- .../PortableRegistry/PortableRegistry.ts | 2 +- 15 files changed, 77 insertions(+), 74 deletions(-) diff --git a/packages/types-codec/src/native/Json.ts b/packages/types-codec/src/native/Json.ts index dfeba8f43e..f9d25bf9c5 100644 --- a/packages/types-codec/src/native/Json.ts +++ b/packages/types-codec/src/native/Json.ts @@ -64,6 +64,10 @@ export class Json extends Map implements Codec { return compareMap(this, other); } + public getT (key: string): T { + return this.get(key) as unknown as T; + } + /** * @description Unimplemented, will throw */ diff --git a/packages/types-codec/src/native/Struct.ts b/packages/types-codec/src/native/Struct.ts index 5bcab6c6dd..5dae933937 100644 --- a/packages/types-codec/src/native/Struct.ts +++ b/packages/types-codec/src/native/Struct.ts @@ -137,6 +137,10 @@ export class Struct< return Object.keys(this.#Types); } + public getT (key: string): T { + return this.get(key) as unknown as T; + } + /** * @description Checks if the value is an empty value */ diff --git a/packages/types/src/ethereum/AccountId.ts b/packages/types/src/ethereum/AccountId.ts index a18059224a..467d824f69 100644 --- a/packages/types/src/ethereum/AccountId.ts +++ b/packages/types/src/ethereum/AccountId.ts @@ -32,10 +32,6 @@ export class GenericEthereumAccountId extends U8aFixed { super(registry, decodeAccountId(value), 160); } - public static encode (value: Uint8Array): string { - return ethereumEncode(value); - } - /** * @description Compares the value of the input to see if there is a match */ @@ -61,7 +57,7 @@ export class GenericEthereumAccountId extends U8aFixed { * @description Returns the string representation of the value */ public override toString (): string { - return GenericEthereumAccountId.encode(this); + return ethereumEncode(this); } /** diff --git a/packages/types/src/ethereum/LookupSource.ts b/packages/types/src/ethereum/LookupSource.ts index 41e4457652..ce10fa8a66 100644 --- a/packages/types/src/ethereum/LookupSource.ts +++ b/packages/types/src/ethereum/LookupSource.ts @@ -41,6 +41,18 @@ function decodeU8a (registry: CodecRegistry, value: Uint8Array): GenericEthereum return registry.createTypeUnsafe('AccountIndex', [u8aToBn(value.subarray(offset, offset + length), true)]); } +function decodeAddressOrIndex (registry: CodecRegistry, value: AnyAddress): GenericEthereumAccountId | GenericAccountIndex { + return value instanceof GenericEthereumLookupSource + ? value.inner + : value instanceof GenericEthereumAccountId || value instanceof GenericAccountIndex + ? value + : isU8a(value) || Array.isArray(value) || isHex(value) + ? decodeU8a(registry, u8aToU8a(value)) + : isBn(value) || isNumber(value) || isBigInt(value) + ? registry.createTypeUnsafe('AccountIndex', [value]) + : decodeString(registry, value); +} + /** * @name GenericEthereumLookupSource * @description @@ -51,20 +63,7 @@ function decodeU8a (registry: CodecRegistry, value: Uint8Array): GenericEthereum */ export class GenericEthereumLookupSource extends Base { constructor (registry: CodecRegistry, value: AnyAddress = new Uint8Array()) { - super(registry, GenericEthereumLookupSource._decodeAddress(registry, value)); - } - - /** @internal */ - private static _decodeAddress (registry: CodecRegistry, value: AnyAddress): GenericEthereumAccountId | GenericAccountIndex { - return value instanceof GenericEthereumLookupSource - ? value.inner - : value instanceof GenericEthereumAccountId || value instanceof GenericAccountIndex - ? value - : isU8a(value) || Array.isArray(value) || isHex(value) - ? decodeU8a(registry, u8aToU8a(value)) - : isBn(value) || isNumber(value) || isBigInt(value) - ? registry.createTypeUnsafe('AccountIndex', [value]) - : decodeString(registry, value); + super(registry, decodeAddressOrIndex(registry, value)); } /** diff --git a/packages/types/src/extrinsic/SignerPayload.ts b/packages/types/src/extrinsic/SignerPayload.ts index cff3549a89..fbe83aabff 100644 --- a/packages/types/src/extrinsic/SignerPayload.ts +++ b/packages/types/src/extrinsic/SignerPayload.ts @@ -64,47 +64,47 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign } get address (): Address { - return this.get('address') as Address; + return this.getT('address'); } get blockHash (): Hash { - return this.get('blockHash') as Hash; + return this.getT('blockHash'); } get blockNumber (): BlockNumber { - return this.get('blockNumber') as BlockNumber; + return this.getT('blockNumber'); } get era (): ExtrinsicEra { - return this.get('era') as ExtrinsicEra; + return this.getT('era'); } get genesisHash (): Hash { - return this.get('genesisHash') as Hash; + return this.getT('genesisHash'); } get method (): Call { - return this.get('method') as Call; + return this.getT('method'); } get nonce (): Compact { - return this.get('nonce') as Compact; + return this.getT('nonce'); } get runtimeVersion (): RuntimeVersion { - return this.get('runtimeVersion') as RuntimeVersion; + return this.getT('runtimeVersion'); } get signedExtensions (): Vec { - return this.get('signedExtensions') as Vec; + return this.getT('signedExtensions'); } get tip (): Compact { - return this.get('tip') as Compact; + return this.getT('tip'); } get version (): u8 { - return this.get('version') as u8; + return this.getT('version'); } /** diff --git a/packages/types/src/extrinsic/v4/Extrinsic.ts b/packages/types/src/extrinsic/v4/Extrinsic.ts index 1ce28170e0..d52f7ef6e7 100644 --- a/packages/types/src/extrinsic/v4/Extrinsic.ts +++ b/packages/types/src/extrinsic/v4/Extrinsic.ts @@ -63,14 +63,14 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl { * @description The [[Call]] this extrinsic wraps */ public get method (): Call { - return this.get('method') as Call; + return this.getT('method'); } /** * @description The [[ExtrinsicSignatureV4]] */ public get signature (): ExtrinsicSignatureV4 { - return this.get('signature') as ExtrinsicSignatureV4; + return this.getT('signature'); } /** diff --git a/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts b/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts index 82b4940ce0..6c7f88da63 100644 --- a/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts @@ -41,56 +41,56 @@ export class GenericExtrinsicPayloadV4 extends Struct { * @description The block [[Hash]] the signature applies to (mortal/immortal) */ public get blockHash (): Hash { - return this.get('blockHash') as Hash; + return this.getT('blockHash'); } /** * @description The [[ExtrinsicEra]] */ public get era (): ExtrinsicEra { - return this.get('era') as ExtrinsicEra; + return this.getT('era'); } /** * @description The genesis [[Hash]] the signature applies to (mortal/immortal) */ public get genesisHash (): Hash { - return this.get('genesisHash') as Hash; + return this.getT('genesisHash'); } /** * @description The [[Bytes]] contained in the payload */ public get method (): Bytes { - return this.get('method') as Bytes; + return this.getT('method'); } /** * @description The [[Index]] */ public get nonce (): Compact { - return this.get('nonce') as Compact; + return this.getT('nonce'); } /** * @description The specVersion for this signature */ public get specVersion (): u32 { - return this.get('specVersion') as u32; + return this.getT('specVersion'); } /** * @description The tip [[Balance]] */ public get tip (): Compact { - return this.get('tip') as Compact; + return this.getT('tip'); } /** * @description The transactionVersion for this signature */ public get transactionVersion (): u32 { - return this.get('transactionVersion') as u32; + return this.getT('transactionVersion'); } /** @@ -98,7 +98,7 @@ export class GenericExtrinsicPayloadV4 extends Struct { * The (optional) asset id for this signature for chains that support transaction fees in assets */ public get assetId (): Option { - return this.get('assetId') as Option; + return this.getT('assetId'); } /** diff --git a/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts b/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts index e942b42ef2..f3ce0f2133 100644 --- a/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts +++ b/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts @@ -80,14 +80,14 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig * @description The [[ExtrinsicEra]] (mortal or immortal) this signature applies to */ public get era (): ExtrinsicEra { - return this.get('era') as ExtrinsicEra; + return this.getT('era'); } /** * @description The [[Index]] for the signature */ public get nonce (): Compact { - return this.get('nonce') as Compact; + return this.getT('nonce'); } /** @@ -102,21 +102,21 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig * @description The raw [[ExtrinsicSignature]] */ public get multiSignature (): ExtrinsicSignature { - return this.get('signature') as ExtrinsicSignature; + return this.getT('signature'); } /** * @description The [[Address]] that signed */ public get signer (): Address { - return this.get('signer') as Address; + return this.getT('signer'); } /** * @description The [[Balance]] tip */ public get tip (): Compact { - return this.get('tip') as Compact; + return this.getT('tip'); } protected _injectSignature (signer: Address, signature: ExtrinsicSignature, payload: GenericExtrinsicPayloadV4): IExtrinsicSignature { diff --git a/packages/types/src/generic/Block.ts b/packages/types/src/generic/Block.ts index fd64d635b6..7b4cf9a62b 100644 --- a/packages/types/src/generic/Block.ts +++ b/packages/types/src/generic/Block.ts @@ -47,7 +47,7 @@ export class GenericBlock extends Struct { * @description The [[Extrinsic]] contained in the block */ public get extrinsics (): Vec { - return this.get('extrinsics') as Vec; + return this.getT('extrinsics'); } /** @@ -61,6 +61,6 @@ export class GenericBlock extends Struct { * @description The [[Header]] of the block */ public get header (): Header { - return this.get('header') as Header; + return this.getT('header'); } } diff --git a/packages/types/src/generic/Call.ts b/packages/types/src/generic/Call.ts index 4d2f750df0..71782b316c 100644 --- a/packages/types/src/generic/Call.ts +++ b/packages/types/src/generic/Call.ts @@ -144,7 +144,7 @@ export class GenericCall extends Struct implement * @description The arguments for the function call */ public get args (): A { - return [...(this.get('args') as Struct).values()] as A; + return [...this.getT('args').values()] as A; } /** @@ -158,21 +158,21 @@ export class GenericCall extends Struct implement * @description The argument entries */ public get argsEntries (): [string, Codec][] { - return [...(this.get('args') as Struct).entries()]; + return [...this.getT('args').entries()]; } /** * @description The encoded `[sectionIndex, methodIndex]` identifier */ public get callIndex (): Uint8Array { - return (this.get('callIndex') as GenericCallIndex).toU8a(); + return this.getT('callIndex').toU8a(); } /** * @description The encoded data */ public get data (): Uint8Array { - return (this.get('args') as Struct).toU8a(); + return this.getT('args').toU8a(); } /** diff --git a/packages/types/src/generic/ChainProperties.ts b/packages/types/src/generic/ChainProperties.ts index 20ef8d78e2..4bcfc9193a 100644 --- a/packages/types/src/generic/ChainProperties.ts +++ b/packages/types/src/generic/ChainProperties.ts @@ -63,20 +63,20 @@ export class GenericChainProperties extends Json { * @description The chain ss58Format */ public get ss58Format (): Option { - return this.get('ss58Format') as Option; + return this.getT('ss58Format'); } /** * @description The decimals for each of the tokens */ public get tokenDecimals (): Option> { - return this.get('tokenDecimals') as Option>; + return this.getT('tokenDecimals'); } /** * @description The symbols for the tokens */ public get tokenSymbol (): Option> { - return this.get('tokenSymbol') as Option>; + return this.getT('tokenSymbol'); } } diff --git a/packages/types/src/generic/Event.ts b/packages/types/src/generic/Event.ts index cbc2809200..c986e2aee1 100644 --- a/packages/types/src/generic/Event.ts +++ b/packages/types/src/generic/Event.ts @@ -112,14 +112,14 @@ export class GenericEvent extends Struct implements IEvent { * @description The wrapped [[EventData]] */ public get data (): GenericEventData { - return this.get('data') as GenericEventData; + return this.getT('data'); } /** * @description The [[EventId]], identifying the raw event */ public get index (): EventId { - return this.get('index') as EventId; + return this.getT('index'); } /** diff --git a/packages/types/src/generic/LookupSource.ts b/packages/types/src/generic/LookupSource.ts index 699ceaec82..ea2e8cd70e 100644 --- a/packages/types/src/generic/LookupSource.ts +++ b/packages/types/src/generic/LookupSource.ts @@ -41,6 +41,19 @@ function decodeU8a (registry: CodecRegistry, value: Uint8Array): GenericAccountI return registry.createTypeUnsafe('AccountIndex', [u8aToBn(value.subarray(offset, offset + length), true)]); } +/** @internal */ +function decodeAddressOrIndex (registry: CodecRegistry, value: AnyAddress): GenericAccountId | GenericAccountIndex { + return value instanceof GenericLookupSource + ? value.inner + : value instanceof GenericAccountId || value instanceof GenericAccountIndex + ? value + : isBn(value) || isNumber(value) || isBigInt(value) + ? registry.createTypeUnsafe('AccountIndex', [value]) + : Array.isArray(value) || isHex(value) || isU8a(value) + ? decodeU8a(registry, u8aToU8a(value)) + : decodeString(registry, value); +} + /** * @name LookupSource * @description @@ -51,20 +64,7 @@ function decodeU8a (registry: CodecRegistry, value: Uint8Array): GenericAccountI */ export class GenericLookupSource extends Base { constructor (registry: CodecRegistry, value: AnyAddress = new Uint8Array()) { - super(registry, GenericLookupSource._decodeAddress(registry, value)); - } - - /** @internal */ - private static _decodeAddress (registry: CodecRegistry, value: AnyAddress): GenericAccountId | GenericAccountIndex { - return value instanceof GenericLookupSource - ? value.inner - : value instanceof GenericAccountId || value instanceof GenericAccountIndex - ? value - : isBn(value) || isNumber(value) || isBigInt(value) - ? registry.createTypeUnsafe('AccountIndex', [value]) - : Array.isArray(value) || isHex(value) || isU8a(value) - ? decodeU8a(registry, u8aToU8a(value)) - : decodeString(registry, value); + super(registry, decodeAddressOrIndex(registry, value)); } /** diff --git a/packages/types/src/metadata/MetadataVersioned.ts b/packages/types/src/metadata/MetadataVersioned.ts index 614217b24d..d682c0cd0b 100644 --- a/packages/types/src/metadata/MetadataVersioned.ts +++ b/packages/types/src/metadata/MetadataVersioned.ts @@ -69,7 +69,7 @@ export class MetadataVersioned extends Struct { * @description the metadata wrapped */ #metadata = (): MetadataAll => { - return this.get('metadata') as MetadataAll; + return this.getT('metadata'); }; /** @@ -137,7 +137,7 @@ export class MetadataVersioned extends Struct { * @description The magicNumber for the Metadata (known constant) */ public get magicNumber (): MagicNumber { - return this.get('magicNumber') as MagicNumber; + return this.getT('magicNumber'); } /** diff --git a/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts b/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts index 8855c321bf..a60b8a0352 100644 --- a/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts +++ b/packages/types/src/metadata/PortableRegistry/PortableRegistry.ts @@ -352,7 +352,7 @@ export class PortableRegistry extends Struct implements ILookup { * @description The types of the registry */ public get types (): Vec { - return this.get('types') as Vec; + return this.getT('types'); } /**