Skip to content

Commit

Permalink
Add isEthereum to ChainProperties (#5686)
Browse files Browse the repository at this point in the history
* Add isEthereum to ChainProperties

* Decription

* Set isEthereum in registry

* lint

* Change type to Bool

* Registry getter

* Fix test
  • Loading branch information
fgamundi authored Sep 1, 2023
1 parent 00b26f4 commit 74a969b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
8 changes: 6 additions & 2 deletions packages/types/src/create/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>('ChainProperties', [{ ss58Format, tokenDecimals, tokenSymbol }]);
return registry.createTypeUnsafe<ChainProperties>('ChainProperties', [{ isEthereum, ss58Format, tokenDecimals, tokenSymbol }]);
}

export class TypeRegistry implements Registry {
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 15 additions & 5 deletions packages/types/src/generic/ChainProperties.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,51 @@ 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);
});

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);
Expand All @@ -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']
Expand All @@ -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']
Expand Down
14 changes: 12 additions & 2 deletions packages/types/src/generic/ChainProperties.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -35,7 +35,9 @@ function decodeValue (registry: Registry, key: string, value: unknown): unknown
? createValue(registry, 'Option<Vec<u32>>' as 'Vec<u32>', value)
: key === 'tokenSymbol'
? createValue(registry, 'Option<Vec<Text>>' as 'Vec<Text>', value)
: value;
: key === 'isEthereum'
? createValue(registry, 'Bool', value, false)
: value;
}

function decode (registry: Registry, value?: Map<string, unknown> | Record<string, unknown> | null): Record<string, unknown> {
Expand All @@ -49,6 +51,7 @@ function decode (registry: Registry, value?: Map<string, unknown> | Record<strin

return all;
}, {
isEthereum: registry.createTypeUnsafe('Bool', []),
ss58Format: registry.createTypeUnsafe('Option<u32>', []),
tokenDecimals: registry.createTypeUnsafe('Option<Vec<u32>>', []),
tokenSymbol: registry.createTypeUnsafe('Option<Vec<Text>>', [])
Expand All @@ -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
*/
Expand Down

0 comments on commit 74a969b

Please sign in to comment.