generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.ts
172 lines (153 loc) · 4.22 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { getTypeDef } from '@polkadot/types';
import { SpRuntimeDispatchError } from '@polkadot/types/lookup';
import { RegistryError, TypeDef, TypeDefInfo } from '@polkadot/types/types';
import { polymesh } from 'polymesh-types/definitions';
import { PolymeshError } from '~/internal';
import {
ArrayTransactionArgument,
ComplexTransactionArgument,
ErrorCode,
PlainTransactionArgument,
SimpleEnumTransactionArgument,
TransactionArgument,
TransactionArgumentType,
} from '~/types';
import { ROOT_TYPES } from '~/utils/constants';
const { types } = polymesh;
const getRootType = (
type: string
):
| PlainTransactionArgument
| ArrayTransactionArgument
| SimpleEnumTransactionArgument
| ComplexTransactionArgument => {
const rootType = ROOT_TYPES[type];
if (rootType) {
return {
type: rootType,
};
}
if (type === 'Null') {
return {
type: TransactionArgumentType.Null,
};
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const definition = (types as any)[type];
if (!definition) {
return {
type: TransactionArgumentType.Unknown,
};
}
const typeDef = getTypeDef(JSON.stringify(definition));
if (typeDef.info === TypeDefInfo.Plain) {
return getRootType(definition);
}
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return processType(typeDef, '');
};
export const processType = (rawType: TypeDef, name: string): TransactionArgument => {
const { type, info, sub } = rawType;
const arg = {
name,
optional: false,
_rawType: rawType,
};
switch (info) {
case TypeDefInfo.Plain: {
return {
...getRootType(type),
...arg,
};
}
case TypeDefInfo.Compact: {
return {
...processType(sub as TypeDef, name),
...arg,
};
}
case TypeDefInfo.Option: {
return {
...processType(sub as TypeDef, name),
...arg,
optional: true,
};
}
case TypeDefInfo.Tuple: {
return {
type: TransactionArgumentType.Tuple,
...arg,
internal: (sub as TypeDef[]).map((def, index) => processType(def, `${index}`)),
};
}
case TypeDefInfo.Vec: {
return {
type: TransactionArgumentType.Array,
...arg,
internal: processType(sub as TypeDef, ''),
};
}
case TypeDefInfo.VecFixed: {
return {
type: TransactionArgumentType.Text,
...arg,
};
}
case TypeDefInfo.Enum: {
const subTypes = sub as TypeDef[];
const isSimple = subTypes.every(({ type: subType }) => subType === 'Null');
if (isSimple) {
return {
type: TransactionArgumentType.SimpleEnum,
...arg,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
internal: subTypes.map(({ name: subName }) => subName!),
};
}
return {
type: TransactionArgumentType.RichEnum,
...arg,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
internal: subTypes.map(def => processType(def, def.name!)),
};
}
case TypeDefInfo.Struct: {
return {
type: TransactionArgumentType.Object,
...arg,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
internal: (sub as TypeDef[]).map(def => processType(def, def.name!)),
};
}
default: {
return {
type: TransactionArgumentType.Unknown,
...arg,
};
}
}
};
/**
* @hidden
*/
export const handleExtrinsicFailure = (
reject: (reason?: unknown) => void,
error: SpRuntimeDispatchError,
data?: Record<string, unknown>
): void => {
// get revert message from event
let message: string;
if (error.isModule) {
// known error
const mod = error.asModule;
const { section, name, docs }: RegistryError = mod.registry.findMetaError(mod);
message = `${section}.${name}: ${docs.join(' ')}`;
} else if (error.isBadOrigin) {
message = 'Bad origin';
} else if (error.isCannotLookup) {
message = 'Could not lookup information required to validate the transaction';
} else {
message = 'Unknown error';
}
reject(new PolymeshError({ code: ErrorCode.TransactionReverted, message, data }));
};