diff --git a/packages/types/src/create/registry.ts b/packages/types/src/create/registry.ts index e49fdffa513e..ebbb915df3cd 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -161,9 +161,9 @@ function extractProperties (registry: TypeRegistry, metadata: Metadata): ChainPr return original; } - const { tokenDecimals, tokenSymbol } = original || {}; + const { isEthereum, tokenDecimals, tokenSymbol } = original || {}; - return registry.createTypeUnsafe('ChainProperties', [{ ss58Format, tokenDecimals, tokenSymbol }]); + return registry.createTypeUnsafe('ChainProperties', [{ isEthereum, ss58Format, tokenDecimals, tokenSymbol }]); } export class TypeRegistry implements Registry { @@ -218,6 +218,10 @@ export class TypeRegistry implements Registry { return [12]; } + public get chainIsEthereum (): boolean { + return this.#chainProperties?.isEthereum.isTrue || false; + } + public get chainSS58 (): number | undefined { return this.#chainProperties?.ss58Format.isSome ? this.#chainProperties.ss58Format.unwrap().toNumber() diff --git a/packages/types/src/generic/ChainProperties.spec.ts b/packages/types/src/generic/ChainProperties.spec.ts index ec8739ee05e2..8b0dbf7e7858 100644 --- a/packages/types/src/generic/ChainProperties.spec.ts +++ b/packages/types/src/generic/ChainProperties.spec.ts @@ -11,32 +11,36 @@ describe('ChainProperties', (): void => { it('decodes from a null value (setting defaults)', (): void => { expect( [...registry.createType('ChainProperties', null).keys()] - ).toEqual(['ss58Format', 'tokenDecimals', 'tokenSymbol']); + ).toEqual(['isEthereum', 'ss58Format', 'tokenDecimals', 'tokenSymbol']); }); it('decodes from an actual JSON', (): void => { - const { ss58Format, tokenDecimals, tokenSymbol } = registry.createType('ChainProperties', JSON.parse('{"ss58Format":2,"tokenDecimals":12,"tokenSymbol":"KSM"}')); + const { isEthereum, ss58Format, tokenDecimals, tokenSymbol } = registry.createType('ChainProperties', JSON.parse('{"isEthereum":false,"ss58Format":2,"tokenDecimals":12,"tokenSymbol":"KSM"}')); expect(ss58Format.unwrap().eq(2)).toBe(true); expect(tokenDecimals.unwrap().eq([12])).toBe(true); expect(tokenSymbol.unwrap().eq(['KSM'])).toBe(true); + expect(isEthereum.isFalse).toBe(true); }); it('decodes from an actual object (multiple tokens)', (): void => { - const { ss58Format, tokenDecimals, tokenSymbol } = registry.createType('ChainProperties', { + const { isEthereum, ss58Format, tokenDecimals, tokenSymbol } = registry.createType('ChainProperties', { + isEthereum: undefined, ss58Format: undefined, tokenDecimals: [10, 12], tokenSymbol: ['pDOT', 'pKSM'] }); + expect(isEthereum.isFalse).toBe(true); expect(ss58Format.isNone).toBe(true); expect(tokenDecimals.unwrap().eq([10, 12])).toBe(true); expect(tokenSymbol.unwrap().eq(['pDOT', 'pKSM'])).toBe(true); }); it('decodes from an object, flagged for non-existent ss58Format', (): void => { - const { ss58Format, tokenDecimals, tokenSymbol } = registry.createType('ChainProperties', { tokenSymbol: 'DEV' }); + const { isEthereum, ss58Format, tokenDecimals, tokenSymbol } = registry.createType('ChainProperties', { tokenSymbol: 'DEV' }); + expect(isEthereum.isFalse).toBe(true); expect(ss58Format.isNone).toBe(true); expect(tokenDecimals.isNone).toBe(true); expect(tokenSymbol.isSome).toBe(true); @@ -44,12 +48,14 @@ describe('ChainProperties', (): void => { it('decodes from a ChainProperties object', (): void => { const original = registry.createType('ChainProperties', { + isEthereum: true, ss58Format: 2, tokenDecimals: 15, tokenSymbol: 'KSM' }); - const { ss58Format, tokenDecimals, tokenSymbol } = registry.createType('ChainProperties', original); + const { isEthereum, ss58Format, tokenDecimals, tokenSymbol } = registry.createType('ChainProperties', original); + expect(isEthereum.isTrue).toBe(true); expect(ss58Format.unwrap().eq(2)).toBe(true); expect(tokenDecimals.unwrap().eq([15])).toBe(true); expect(tokenSymbol.unwrap().eq(['KSM'])).toBe(true); @@ -58,11 +64,13 @@ describe('ChainProperties', (): void => { it('has a sane toHuman (single tokenDecimals)', (): void => { expect( registry.createType('ChainProperties', { + isEthereum: false, ss58Format: 42, tokenDecimals: registry.createType('u32', 9), tokenSymbol: ['Unit', 'Aux1'] }).toHuman() ).toEqual({ + isEthereum: false, ss58Format: '42', tokenDecimals: ['9'], tokenSymbol: ['Unit', 'Aux1'] @@ -72,11 +80,13 @@ describe('ChainProperties', (): void => { it('has a sane toHuman (multiple tokenDecimals)', (): void => { expect( registry.createType('ChainProperties', { + isEthereum: false, ss58Format: 2, tokenDecimals: [registry.createType('u32', 12), 8], tokenSymbol: ['KSM', 'BTC'] }).toHuman() ).toEqual({ + isEthereum: false, ss58Format: '2', tokenDecimals: ['12', '8'], tokenSymbol: ['KSM', 'BTC'] diff --git a/packages/types/src/generic/ChainProperties.ts b/packages/types/src/generic/ChainProperties.ts index b45e565086a0..f9bcad8e143c 100644 --- a/packages/types/src/generic/ChainProperties.ts +++ b/packages/types/src/generic/ChainProperties.ts @@ -1,7 +1,7 @@ // Copyright 2017-2023 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Option, Text, u32, Vec } from '@polkadot/types-codec'; +import type { bool as Bool, Option, Text, u32, Vec } from '@polkadot/types-codec'; import type { Registry } from '@polkadot/types-codec/types'; import type { Codec } from '../types/index.js'; @@ -35,7 +35,9 @@ function decodeValue (registry: Registry, key: string, value: unknown): unknown ? createValue(registry, 'Option>' as 'Vec', value) : key === 'tokenSymbol' ? createValue(registry, 'Option>' as 'Vec', value) - : value; + : key === 'isEthereum' + ? createValue(registry, 'Bool', value, false) + : value; } function decode (registry: Registry, value?: Map | Record | null): Record { @@ -49,6 +51,7 @@ function decode (registry: Registry, value?: Map | Record', []), tokenDecimals: registry.createTypeUnsafe('Option>', []), tokenSymbol: registry.createTypeUnsafe('Option>', []) @@ -60,6 +63,13 @@ export class GenericChainProperties extends Json { super(registry, decode(registry, value)); } + /** + * @description The chain uses Ethereum addresses + */ + public get isEthereum (): Bool { + return this.getT('isEthereum'); + } + /** * @description The chain ss58Format */