UNPKG

1.89 kBJavaScriptView Raw
1// Copyright 2017-2023 @polkadot/types authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3
4import { Json } from '@polkadot/types-codec';
5import { isFunction, isNull, isUndefined } from '@polkadot/util';
6function createValue(registry, type, value, asArray = true) {
7 // We detect codec here as well - when found, generally this is constructed from itself
8 if (value && isFunction(value.unwrapOrDefault)) {
9 return value;
10 }
11 return registry.createTypeUnsafe(type, [asArray ? isNull(value) || isUndefined(value) ? null : Array.isArray(value) ? value : [value] : value]);
12}
13function decodeValue(registry, key, value) {
14 return key === 'ss58Format' ? createValue(registry, 'Option<u32>', value, false) : key === 'tokenDecimals' ? createValue(registry, 'Option<Vec<u32>>', value) : key === 'tokenSymbol' ? createValue(registry, 'Option<Vec<Text>>', value) : value;
15}
16function decode(registry, value) {
17 return (
18 // allow decoding from a map as well (ourselves)
19 value && isFunction(value.entries) ? [...value.entries()] : Object.entries(value || {})).reduce((all, [key, value]) => {
20 all[key] = decodeValue(registry, key, value);
21 return all;
22 }, {
23 ss58Format: registry.createTypeUnsafe('Option<u32>', []),
24 tokenDecimals: registry.createTypeUnsafe('Option<Vec<u32>>', []),
25 tokenSymbol: registry.createTypeUnsafe('Option<Vec<Text>>', [])
26 });
27}
28export class GenericChainProperties extends Json {
29 constructor(registry, value) {
30 super(registry, decode(registry, value));
31 }
32
33 /**
34 * @description The chain ss58Format
35 */
36 get ss58Format() {
37 return this.getT('ss58Format');
38 }
39
40 /**
41 * @description The decimals for each of the tokens
42 */
43 get tokenDecimals() {
44 return this.getT('tokenDecimals');
45 }
46
47 /**
48 * @description The symbols for the tokens
49 */
50 get tokenSymbol() {
51 return this.getT('tokenSymbol');
52 }
53}
\No newline at end of file