1 | import { Json } from '@polkadot/types-codec';
|
2 | import { isFunction, isNull, isUndefined } from '@polkadot/util';
|
3 | function createValue(registry, type, value, asArray = true) {
|
4 |
|
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 | }
|
18 | function 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 | }
|
29 | function decode(registry, value) {
|
30 | return (
|
31 |
|
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 | }
|
44 | export class GenericChainProperties extends Json {
|
45 | constructor(registry, value) {
|
46 | super(registry, decode(registry, value));
|
47 | }
|
48 | |
49 |
|
50 |
|
51 | get isEthereum() {
|
52 | return this.getT('isEthereum');
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | get ss58Format() {
|
58 | return this.getT('ss58Format');
|
59 | }
|
60 | |
61 |
|
62 |
|
63 | get tokenDecimals() {
|
64 | return this.getT('tokenDecimals');
|
65 | }
|
66 | |
67 |
|
68 |
|
69 | get tokenSymbol() {
|
70 | return this.getT('tokenSymbol');
|
71 | }
|
72 | }
|