// Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { Codec, Registry } from '../types'; import { isFunction, isNull, isUndefined } from '@polkadot/util'; import { Json } from '../codec/Json'; import { Option } from '../codec/Option'; import { Vec } from '../codec/Vec'; import { Text } from '../primitive/Text'; import { u32 } from '../primitive/U32'; function createValue (registry: Registry, type: string, value: unknown, asArray = true): Option { // We detect codec here as well - when found, generally this is constructed from itself if (value && isFunction((value as Option).unwrapOrDefault)) { return value as Option; } return registry.createType( type as 'Option', asArray ? isNull(value) || isUndefined(value) ? null : Array.isArray(value) ? value : [value] : value ); } function decodeValue (registry: Registry, key: string, value: unknown): unknown { return key === 'ss58Format' ? createValue(registry, 'Option', value, false) : key === 'tokenDecimals' ? createValue(registry, 'Option>' as 'Vec', value) : key === 'tokenSymbol' ? createValue(registry, 'Option>' as 'Vec', value) : value; } function decode (registry: Registry, value?: Map | Record | null): Record { return ( // allow decoding from a map as well (ourselves) value && isFunction((value as Map).entries) ? [...(value as Map).entries()] : Object.entries(value || {}) ).reduce((all: Record, [key, value]) => { all[key] = decodeValue(registry, key, value); return all; }, { ss58Format: registry.createType('Option'), tokenDecimals: registry.createType('Option>' as 'Vec'), tokenSymbol: registry.createType('Option>' as 'Vec') }); } export class GenericChainProperties extends Json { constructor (registry: Registry, value?: Map | Record | null) { super(registry, decode(registry, value)); } /** * @description The chain ss58Format */ public get ss58Format (): Option { return this.get('ss58Format') as Option; } /** * @description The decimals for each of the tokens */ public get tokenDecimals (): Option> { return this.get('tokenDecimals') as Option>; } /** * @description The symbols for the tokens */ public get tokenSymbol (): Option> { return this.get('tokenSymbol') as Option>; } }