-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathproposalActionsDecoderUtils.test.ts
More file actions
470 lines (409 loc) · 23.5 KB
/
Copy pathproposalActionsDecoderUtils.test.ts
File metadata and controls
470 lines (409 loc) · 23.5 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import { addressUtils } from '../../../../utils';
import { type IGetValidationRulesParams, proposalActionsDecoderUtils } from './proposalActionsDecoderUtils';
describe('ProposalActionsDecoder utils', () => {
describe('getFieldName', () => {
it('prepends the formPrefix to the field name when defined', () => {
const formPrefix = 'actions.0';
const fieldName = 'data';
expect(proposalActionsDecoderUtils.getFieldName(fieldName, formPrefix)).toEqual(
`${formPrefix}.${fieldName}`,
);
});
it('returns the field name when form prefix is not defined', () => {
const fieldName = 'inputData.parameters.0.value';
expect(proposalActionsDecoderUtils.getFieldName(fieldName, undefined)).toEqual(fieldName);
});
});
describe('validateValue', () => {
const validateRequiredSpy = jest.spyOn(proposalActionsDecoderUtils, 'validateRequired');
const validateBooleanSpy = jest.spyOn(proposalActionsDecoderUtils, 'validateBoolean');
const validateAddressSpy = jest.spyOn(addressUtils, 'isAddress');
const validateBytesSpy = jest.spyOn(proposalActionsDecoderUtils, 'validateBytes');
const validateUnsignedNumberSpy = jest.spyOn(proposalActionsDecoderUtils, 'validateUnsignedNumber');
const validateSignedNumberSpy = jest.spyOn(proposalActionsDecoderUtils, 'validateSignedNumber');
const validateNumberRangeSpy = jest.spyOn(proposalActionsDecoderUtils, 'validateNumberRange');
afterEach(() => {
validateRequiredSpy.mockReset();
validateBooleanSpy.mockReset();
validateAddressSpy.mockReset();
validateBytesSpy.mockReset();
validateUnsignedNumberSpy.mockReset();
validateSignedNumberSpy.mockReset();
validateNumberRangeSpy.mockReset();
});
afterAll(() => {
validateRequiredSpy.mockRestore();
validateBooleanSpy.mockRestore();
validateAddressSpy.mockRestore();
validateBytesSpy.mockRestore();
validateUnsignedNumberSpy.mockRestore();
validateSignedNumberSpy.mockRestore();
validateNumberRangeSpy.mockRestore();
});
const buildValidateValueParams = (params?: Partial<IGetValidationRulesParams>): IGetValidationRulesParams => ({
label: '',
type: '',
errorMessages: {
required: () => 'required-error',
boolean: () => 'bool-error',
address: () => 'address-error',
bytes: () => 'bytes-error',
unsignedNumber: () => 'uint-error',
signedNumber: () => 'int-error',
numberRange: () => 'range-error',
},
...params,
});
it('returns required error message when value is required but not set', () => {
validateRequiredSpy.mockReturnValue(false);
const params = buildValidateValueParams({ required: true });
expect(proposalActionsDecoderUtils.validateValue(undefined, params)).toEqual('required-error');
});
it('returns undefined when value is required and valid', () => {
validateRequiredSpy.mockReturnValue(true);
const params = buildValidateValueParams({ required: true });
expect(proposalActionsDecoderUtils.validateValue('value', params)).toBeUndefined();
});
it('returns undefined when value is an empty string and not required', () => {
const params = buildValidateValueParams({ type: 'string', required: false });
expect(proposalActionsDecoderUtils.validateValue('', params)).toBeUndefined();
});
it('returns bool error message when value has bool type and is not valid', () => {
validateBooleanSpy.mockReturnValue(false);
const params = buildValidateValueParams({ type: 'bool' });
expect(proposalActionsDecoderUtils.validateValue('tru', params)).toEqual('bool-error');
});
it('returns true when value has bool type and is valid', () => {
validateBooleanSpy.mockReturnValue(true);
const params = buildValidateValueParams({ type: 'bool' });
expect(proposalActionsDecoderUtils.validateValue('false', params)).toBeTruthy();
});
it('returns address error message when value has address type and is not valid', () => {
validateAddressSpy.mockReturnValue(false);
const params = buildValidateValueParams({ type: 'address' });
expect(proposalActionsDecoderUtils.validateValue('0x123', params)).toEqual('address-error');
});
it('returns true when value has address type and is valid', () => {
validateBooleanSpy.mockReturnValue(true);
const params = buildValidateValueParams({ type: 'address' });
expect(
proposalActionsDecoderUtils.validateValue('0x0B2a45c2bCb56dA84920585f985087973c715364', params),
).toBeTruthy();
});
it('returns bytes error message when value has bytes type and is not valid', () => {
validateBytesSpy.mockReturnValue(false);
const params = buildValidateValueParams({ type: 'bytes' });
expect(proposalActionsDecoderUtils.validateValue('0x1', params)).toEqual('bytes-error');
});
it('returns true when value has bytes type and is valid', () => {
validateBytesSpy.mockReturnValue(true);
const params = buildValidateValueParams({ type: 'bytes32' });
expect(proposalActionsDecoderUtils.validateValue('0x0001', params)).toBeTruthy();
});
it('returns uint error message when value has uint type and is not valid', () => {
validateUnsignedNumberSpy.mockReturnValue(false);
const params = buildValidateValueParams({ type: 'uint' });
expect(proposalActionsDecoderUtils.validateValue('-1', params)).toEqual('uint-error');
});
it('returns true when value has uint type and is valid', () => {
validateUnsignedNumberSpy.mockReturnValue(true);
const params = buildValidateValueParams({ type: 'uint16' });
expect(proposalActionsDecoderUtils.validateValue('10', params)).toBeTruthy();
});
it('returns range error message when value has uint type and does not fit its bit-width', () => {
validateUnsignedNumberSpy.mockReturnValue(true);
validateNumberRangeSpy.mockReturnValue(false);
const params = buildValidateValueParams({ type: 'uint8' });
expect(proposalActionsDecoderUtils.validateValue('300', params)).toEqual('range-error');
});
it('returns int error message when value has int type and is not valid', () => {
validateSignedNumberSpy.mockReturnValue(false);
const params = buildValidateValueParams({ type: 'int256' });
expect(proposalActionsDecoderUtils.validateValue('-', params)).toEqual('int-error');
});
it('returns range error message when value has int type and does not fit its bit-width', () => {
validateSignedNumberSpy.mockReturnValue(true);
validateNumberRangeSpy.mockReturnValue(false);
const params = buildValidateValueParams({ type: 'int8' });
expect(proposalActionsDecoderUtils.validateValue('-129', params)).toEqual('range-error');
});
it('returns true when value has int type and is valid', () => {
const params = buildValidateValueParams({ type: 'int32' });
expect(proposalActionsDecoderUtils.validateValue('-5', params)).toBeTruthy();
});
it('validates addresses with strict checksum validation', () => {
const value = '0x0B2a45c2bCb56dA84920585f985087973c715364';
const params = buildValidateValueParams({ type: 'address' });
proposalActionsDecoderUtils.validateValue(value, params);
expect(validateAddressSpy).toHaveBeenCalledWith(value, { strict: true });
});
});
describe('validateRequired', () => {
it('returns false when value is not defined or empty string', () => {
expect(proposalActionsDecoderUtils.validateRequired(null)).toBeFalsy();
expect(proposalActionsDecoderUtils.validateRequired(undefined)).toBeFalsy();
expect(proposalActionsDecoderUtils.validateRequired('')).toBeFalsy();
});
it('returns true when value is defined', () => {
expect(proposalActionsDecoderUtils.validateRequired('test')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateRequired(true)).toBeTruthy();
expect(proposalActionsDecoderUtils.validateRequired(false)).toBeTruthy();
});
});
describe('validateBoolean', () => {
it('returns false when value is not a valid boolean', () => {
expect(proposalActionsDecoderUtils.validateBoolean(undefined)).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBoolean(null)).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBoolean('')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBoolean('fals')).toBeFalsy();
});
it('returns true when value is a valid boolean', () => {
expect(proposalActionsDecoderUtils.validateBoolean('true')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateBoolean('false')).toBeTruthy();
});
});
describe('validateBytes', () => {
it('returns false when value is not a valid dynamic bytes value', () => {
expect(proposalActionsDecoderUtils.validateBytes('bytes', undefined)).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBytes('bytes', '0x1')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBytes('bytes', '0x123')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBytes('bytes', '0xG1')).toBeFalsy();
});
it('returns false when value is not a valid static bytes value', () => {
expect(proposalActionsDecoderUtils.validateBytes('bytes16', undefined)).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBytes('bytes16', '0xab-00')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBytes('bytes8', '0x0123abcd')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateBytes('bytes4', '0x00112233aaAA0000')).toBeFalsy();
});
it('returns true when value is a valid dynamic bytes value', () => {
expect(proposalActionsDecoderUtils.validateBytes('bytes', '0x')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateBytes('bytes', '0x00')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateBytes('bytes', '0xab99')).toBeTruthy();
});
it('returns true when value is a valid static bytes value', () => {
expect(proposalActionsDecoderUtils.validateBytes('bytes4', '0x0123abcd')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateBytes('bytes8', '0x00112233aaAA0000')).toBeTruthy();
});
});
describe('validateUnsignedNumber', () => {
it('returns false when value is not a valid uint value', () => {
expect(proposalActionsDecoderUtils.validateUnsignedNumber(undefined)).toBeFalsy();
expect(proposalActionsDecoderUtils.validateUnsignedNumber('')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateUnsignedNumber('-1')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateUnsignedNumber('79.11')).toBeFalsy();
});
it('returns true when value is a valid uint value', () => {
expect(proposalActionsDecoderUtils.validateUnsignedNumber('0')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateUnsignedNumber('8645312')).toBeTruthy();
});
});
describe('validateSignedNumber', () => {
it('returns false when value is not a valid int value', () => {
expect(proposalActionsDecoderUtils.validateSignedNumber(undefined)).toBeFalsy();
expect(proposalActionsDecoderUtils.validateSignedNumber('')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateSignedNumber('-')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateSignedNumber('79.11')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateSignedNumber('1-2')).toBeFalsy();
});
it('returns true when value is a valid int value', () => {
expect(proposalActionsDecoderUtils.validateSignedNumber('-1')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateSignedNumber('0')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateSignedNumber('8645312')).toBeTruthy();
});
});
describe('validateNumberRange', () => {
it('returns false when value does not fit the bit-width of the type', () => {
expect(proposalActionsDecoderUtils.validateNumberRange('uint8', '256')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateNumberRange('uint16', '65536')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateNumberRange('int8', '128')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateNumberRange('int8', '-129')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateNumberRange('uint', '-1')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateNumberRange('uint256', (2n ** 256n).toString())).toBeFalsy();
});
it('returns true when value fits the bit-width of the type', () => {
expect(proposalActionsDecoderUtils.validateNumberRange('uint8', '255')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateNumberRange('int8', '-128')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateNumberRange('int8', '127')).toBeTruthy();
expect(proposalActionsDecoderUtils.validateNumberRange('uint', (2n ** 256n - 1n).toString())).toBeTruthy();
expect(proposalActionsDecoderUtils.validateNumberRange('int', (-(2n ** 255n)).toString())).toBeTruthy();
});
it('returns false when value cannot be parsed as an integer', () => {
expect(proposalActionsDecoderUtils.validateNumberRange('uint8', 'abc')).toBeFalsy();
});
it('returns false when the type has an invalid bit-width', () => {
expect(proposalActionsDecoderUtils.validateNumberRange('uint0', '0')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateNumberRange('uint12', '0')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateNumberRange('uint264', '0')).toBeFalsy();
expect(proposalActionsDecoderUtils.validateNumberRange('int9999', '0')).toBeFalsy();
});
});
describe('isArrayType', () => {
it('returns false when type is not an array type', () => {
expect(proposalActionsDecoderUtils.isArrayType('uint')).toBeFalsy();
expect(proposalActionsDecoderUtils.isArrayType('tuple')).toBeFalsy();
});
it('returns true when type is an array type', () => {
expect(proposalActionsDecoderUtils.isArrayType('uint[]')).toBeTruthy();
expect(proposalActionsDecoderUtils.isArrayType('address[][]')).toBeTruthy();
});
});
describe('isTupleType', () => {
it('returns false when type is not a tuple type', () => {
expect(proposalActionsDecoderUtils.isTupleType('uint')).toBeFalsy();
expect(proposalActionsDecoderUtils.isTupleType('address[]')).toBeFalsy();
});
it('returns true when type is a tuple type', () => {
expect(proposalActionsDecoderUtils.isTupleType('tuple')).toBeTruthy();
});
});
describe('isNumberType', () => {
it('returns false when type is not a number type', () => {
expect(proposalActionsDecoderUtils.isNumberType('address')).toBeFalsy();
expect(proposalActionsDecoderUtils.isNumberType('tuple')).toBeFalsy();
});
it('returns true when type is a number type', () => {
expect(proposalActionsDecoderUtils.isNumberType('uint256')).toBeTruthy();
expect(proposalActionsDecoderUtils.isNumberType('int')).toBeTruthy();
expect(proposalActionsDecoderUtils.isNumberType('int[]')).toBeTruthy();
});
});
describe('isUnsignedNumberType', () => {
it('returns false when type is not an unsigned number type', () => {
expect(proposalActionsDecoderUtils.isUnsignedNumberType('int')).toBeFalsy();
expect(proposalActionsDecoderUtils.isUnsignedNumberType('address')).toBeFalsy();
});
it('returns true when type is an unsigned number type', () => {
expect(proposalActionsDecoderUtils.isUnsignedNumberType('uint')).toBeTruthy();
expect(proposalActionsDecoderUtils.isUnsignedNumberType('uint[]')).toBeTruthy();
});
});
describe('isSignedNumberType', () => {
it('returns false when type is not a signed number type', () => {
expect(proposalActionsDecoderUtils.isSignedNumberType('uint')).toBeFalsy();
expect(proposalActionsDecoderUtils.isSignedNumberType('tuple')).toBeFalsy();
});
it('returns true when type is a signed number type', () => {
expect(proposalActionsDecoderUtils.isSignedNumberType('int')).toBeTruthy();
expect(proposalActionsDecoderUtils.isSignedNumberType('int[]')).toBeTruthy();
});
});
describe('getArrayItemType', () => {
it('returns the item type of the array', () => {
expect(proposalActionsDecoderUtils.getArrayItemType('uint[]')).toEqual('uint');
expect(proposalActionsDecoderUtils.getArrayItemType('tuple[]')).toEqual('tuple');
expect(proposalActionsDecoderUtils.getArrayItemType('int[][]')).toEqual('int[]');
});
});
describe('formValuesToFunctionParameters', () => {
it('returns the values of all action parameters', () => {
const params = ['0x00', ['0', true, 'test']];
const actionParameters = [
{ name: '_data', value: params[0] },
{ name: '_struct', value: params[1] },
];
const formValues = { inputData: { parameters: actionParameters } };
expect(proposalActionsDecoderUtils.formValuesToFunctionParameters(formValues)).toEqual(params);
});
it('correctly extracts form values when using a form prefix', () => {
const params = ['0', [['0x123', true], '0']];
const actionParameters = [
{ name: '_data', value: params[0] },
{ name: '_struct', value: params[1] },
];
const formPrefix = 'actions.0';
const formValues = { actions: [{ inputData: { parameters: actionParameters } }] };
expect(proposalActionsDecoderUtils.formValuesToFunctionParameters(formValues, formPrefix)).toEqual(params);
});
});
describe('getNestedParameters', () => {
it('correctly maps the values for array type', () => {
const parameter = {
name: 'addresses',
type: 'address[]',
value: ['0x8Da8bfAc659D7608323652fa2013E43F589b62Cc', '0x0aAb5717E90043d54e363aAfd32Cd449A358aF62'],
};
expect(proposalActionsDecoderUtils.getNestedParameters(parameter)).toEqual([
{ name: parameter.name, type: 'address', value: parameter.value[0] },
{ name: parameter.name, type: 'address', value: parameter.value[1] },
]);
});
it('returns empty array for array type with unsupported values', () => {
const parameter = { name: 'addresses', type: 'uint[]', value: [123] };
expect(proposalActionsDecoderUtils.getNestedParameters(parameter)).toEqual([]);
});
it('correctly maps the values for tuple type', () => {
const parameter = {
name: 'voteSettings',
type: 'tuple',
value: ['0', '1000000', true],
components: [
{ name: 'votingMode', type: 'uint8' },
{ name: 'supportThreshold', type: 'uint32' },
{ name: 'boolTest', type: 'uint32' },
],
};
expect(proposalActionsDecoderUtils.getNestedParameters(parameter)).toEqual([
{ ...parameter.components[0], value: parameter.value[0] },
{ ...parameter.components[1], value: parameter.value[1] },
{ ...parameter.components[2], value: parameter.value[2] },
]);
});
it('ignores value when it contains unsupported values for tuple type', () => {
const parameter = {
name: 'plugin',
type: 'tuple',
value: ['0x8Da8bfAc659D7608323652fa2013E43F589b62Cc', 111],
components: [
{ name: 'address', type: 'address' },
{ name: 'isManual', type: 'bool' },
],
};
expect(proposalActionsDecoderUtils.getNestedParameters(parameter)).toEqual([
{ ...parameter.components[0], value: undefined },
{ ...parameter.components[1], value: undefined },
]);
});
it('returns the correct parameters for tuple type with no value', () => {
const parameter = {
name: 'action',
type: 'tuple',
value: undefined,
components: [
{ name: 'to', type: 'address' },
{ name: 'data', type: 'bytes' },
],
};
expect(proposalActionsDecoderUtils.getNestedParameters(parameter)).toEqual([
{ ...parameter.components[0], value: undefined },
{ ...parameter.components[1], value: undefined },
]);
});
});
describe('guardArrayType', () => {
it('returns false when value is not an array', () => {
expect(proposalActionsDecoderUtils['guardArrayType'](null)).toBeFalsy();
expect(proposalActionsDecoderUtils['guardArrayType']('test')).toBeFalsy();
});
it('returns false when value contains unsupported types', () => {
expect(proposalActionsDecoderUtils['guardArrayType']([12])).toBeFalsy();
expect(proposalActionsDecoderUtils['guardArrayType']([[{}]])).toBeFalsy();
});
it('returns true when value contains supported types', () => {
expect(proposalActionsDecoderUtils['guardArrayType']([null])).toBeTruthy();
expect(proposalActionsDecoderUtils['guardArrayType']([undefined])).toBeTruthy();
expect(proposalActionsDecoderUtils['guardArrayType']([['false', '0x123']])).toBeTruthy();
});
});
describe('guardValueType', () => {
it('returns false when value type is not supported', () => {
expect(proposalActionsDecoderUtils['guardValueType']({})).toBeFalsy();
expect(proposalActionsDecoderUtils['guardValueType'](12)).toBeFalsy();
});
it('returns true when value type is supported', () => {
expect(proposalActionsDecoderUtils['guardValueType'](null)).toBeTruthy();
expect(proposalActionsDecoderUtils['guardValueType'](undefined)).toBeTruthy();
expect(proposalActionsDecoderUtils['guardValueType']('0x')).toBeTruthy();
expect(proposalActionsDecoderUtils['guardValueType'](false)).toBeTruthy();
});
});
});