UNPKG

2.38 kBJavaScriptView Raw
1import { Json } from '@polkadot/types-codec';
2import { isFunction, isNull, isUndefined } from '@polkadot/util';
3function createValue(registry, type, value, asArray = true) {
4 // We detect codec here as well - when found, generally this is constructed from itself
5 if (value && isFunction(value.unwrapOrDefault)) {
6 return value;
7 }
8 return registry.createTypeUnsafe(type, [
9 asArray
10 ? isNull(value) || isUndefined(value)
11 ? null
12 : Array.isArray(value)
13 ? value
14 : [value]
15 : value
16 ]);
17}
18function decodeValue(registry, key, value) {
19 return key === 'ss58Format'
20 ? createValue(registry, 'Option<u32>', value, false)
21 : key === 'tokenDecimals'
22 ? createValue(registry, 'Option<Vec<u32>>', value)
23 : key === 'tokenSymbol'
24 ? createValue(registry, 'Option<Vec<Text>>', value)
25 : key === 'isEthereum'
26 ? createValue(registry, 'Bool', value, false)
27 : value;
28}
29function decode(registry, value) {
30 return (
31 // allow decoding from a map as well (ourselves)
32 value && isFunction(value.entries)
33 ? [...value.entries()]
34 : Object.entries(value || {})).reduce((all, [key, value]) => {
35 all[key] = decodeValue(registry, key, value);
36 return all;
37 }, {
38 isEthereum: registry.createTypeUnsafe('Bool', []),
39 ss58Format: registry.createTypeUnsafe('Option<u32>', []),
40 tokenDecimals: registry.createTypeUnsafe('Option<Vec<u32>>', []),
41 tokenSymbol: registry.createTypeUnsafe('Option<Vec<Text>>', [])
42 });
43}
44export class GenericChainProperties extends Json {
45 constructor(registry, value) {
46 super(registry, decode(registry, value));
47 }
48 /**
49 * @description The chain uses Ethereum addresses
50 */
51 get isEthereum() {
52 return this.getT('isEthereum');
53 }
54 /**
55 * @description The chain ss58Format
56 */
57 get ss58Format() {
58 return this.getT('ss58Format');
59 }
60 /**
61 * @description The decimals for each of the tokens
62 */
63 get tokenDecimals() {
64 return this.getT('tokenDecimals');
65 }
66 /**
67 * @description The symbols for the tokens
68 */
69 get tokenSymbol() {
70 return this.getT('tokenSymbol');
71 }
72}