Add isEthereum to ChainProperties (#5686)

* Add isEthereum to ChainProperties

* Decription

* Set isEthereum in registry

* lint

* Change type to Bool

* Registry getter

* Fix test
This commit is contained in:
Francisco Gamundi
2023-09-01 07:41:04 +03:00
committed by GitHub
parent 00b26f48b4
commit 74a969bb54
3 changed files with 33 additions and 9 deletions
+6 -2
View File
@@ -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 {
@@ -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()
@@ -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']
+12 -2
View File
@@ -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<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> {
@@ -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>>', [])
@@ -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
*/