-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathnativeTokenPeriodTransfer.test.ts
More file actions
313 lines (275 loc) · 9.36 KB
/
Copy pathnativeTokenPeriodTransfer.test.ts
File metadata and controls
313 lines (275 loc) · 9.36 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
import { isStrictHexString } from '@metamask/utils';
import { describe, it, expect } from 'vitest';
import { createNativeTokenPeriodTransferTerms } from '../../src/caveats/nativeTokenPeriodTransfer';
describe('createNativeTokenPeriodTransferTerms', () => {
const EXPECTED_BYTE_LENGTH = 96; // 32 bytes for each of the 3 parameters
it('creates valid terms for standard parameters', () => {
const periodAmount = 1000000000000000000n; // 1 ETH in wei
const periodDuration = 3600; // 1 hour in seconds
const startDate = 1640995200; // 2022-01-01 00:00:00 UTC
const result = createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
});
expect(result).toHaveLength(194);
const expectedPeriodAmount =
'0000000000000000000000000000000000000000000000000de0b6b3a7640000';
const expectedPeriodDuration =
'0000000000000000000000000000000000000000000000000000000000000e10';
const expectedStartDate =
'0000000000000000000000000000000000000000000000000000000061cf9980';
expect(result).toStrictEqual(
`0x${expectedPeriodAmount}${expectedPeriodDuration}${expectedStartDate}`,
);
});
it('creates valid terms for small values', () => {
const periodAmount = 1n;
const periodDuration = 1;
const startDate = 1;
const result = createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
});
expect(result).toStrictEqual(
'0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001',
);
});
it('creates valid terms for large values', () => {
const periodAmount = 100000000000000000000n; // 100 ETH in wei
const periodDuration = 86400; // 1 day in seconds
const startDate = 2000000000; // Far future timestamp
const result = createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
});
expect(result).toHaveLength(194);
expect(isStrictHexString(result)).toBe(true);
expect(result.length).toBe(194); // Additional length validation
});
it('creates valid terms for maximum safe values', () => {
const periodAmount =
115792089237316195423570985008687907853269984665640564039457584007913129639935n; // max uint256
const periodDuration = Number.MAX_SAFE_INTEGER;
const startDate = Number.MAX_SAFE_INTEGER;
const result = createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
});
expect(result).toHaveLength(194);
expect(isStrictHexString(result)).toBe(true);
expect(result.length).toBe(194); // Additional length validation
});
it('throws an error for zero period amount', () => {
const periodAmount = 0n;
const periodDuration = 3600;
const startDate = 1640995200;
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
}),
).toThrow('Invalid periodAmount: must be a positive number');
});
it('throws an error for negative period amount', () => {
const periodAmount = -1n;
const periodDuration = 3600;
const startDate = 1640995200;
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
}),
).toThrow('Invalid periodAmount: must be a positive number');
});
it('throws an error for zero period duration', () => {
const periodAmount = 1000000000000000000n;
const periodDuration = 0;
const startDate = 1640995200;
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
}),
).toThrow('Invalid periodDuration: must be a positive number');
});
it('throws an error for negative period duration', () => {
const periodAmount = 1000000000000000000n;
const periodDuration = -1;
const startDate = 1640995200;
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
}),
).toThrow('Invalid periodDuration: must be a positive number');
});
it('throws an error for zero start date', () => {
const periodAmount = 1000000000000000000n;
const periodDuration = 3600;
const startDate = 0;
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
}),
).toThrow('Invalid startDate: must be a positive number');
});
it('throws an error for negative start date', () => {
const periodAmount = 1000000000000000000n;
const periodDuration = 3600;
const startDate = -1;
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
}),
).toThrow('Invalid startDate: must be a positive number');
});
it('throws an error for undefined periodAmount', () => {
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount: undefined as any,
periodDuration: 3600,
startDate: 1640995200,
}),
).toThrow();
});
it('throws an error for null periodAmount', () => {
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount: null as any,
periodDuration: 3600,
startDate: 1640995200,
}),
).toThrow();
});
it('throws an error for undefined periodDuration', () => {
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount: 1000000000000000000n,
periodDuration: undefined as any,
startDate: 1640995200,
}),
).toThrow();
});
it('throws an error for null periodDuration', () => {
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount: 1000000000000000000n,
periodDuration: null as any,
startDate: 1640995200,
}),
).toThrow();
});
it('throws an error for undefined startDate', () => {
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount: 1000000000000000000n,
periodDuration: 3600,
startDate: undefined as any,
}),
).toThrow();
});
it('throws an error for null startDate', () => {
expect(() =>
createNativeTokenPeriodTransferTerms({
periodAmount: 1000000000000000000n,
periodDuration: 3600,
startDate: null as any,
}),
).toThrow();
});
it('handles edge case with very small period amount', () => {
const periodAmount = 1n; // 1 wei
const periodDuration = 1;
const startDate = 1;
const result = createNativeTokenPeriodTransferTerms({
periodAmount,
periodDuration,
startDate,
});
expect(isStrictHexString(result)).toBe(true);
expect(result).toHaveLength(194);
expect(result.length).toBe(194); // Additional length validation
});
// Tests for bytes return type
describe('bytes return type', () => {
it('returns Uint8Array when bytes encoding is specified', () => {
const periodAmount = 1000000000000000000n; // 1 ETH in wei
const periodDuration = 3600; // 1 hour in seconds
const startDate = 1640995200; // 2022-01-01 00:00:00 UTC
const result = createNativeTokenPeriodTransferTerms(
{
periodAmount,
periodDuration,
startDate,
},
{ out: 'bytes' },
);
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(EXPECTED_BYTE_LENGTH);
});
it('returns Uint8Array for small values with bytes encoding', () => {
const periodAmount = 1n;
const periodDuration = 1;
const startDate = 1;
const result = createNativeTokenPeriodTransferTerms(
{
periodAmount,
periodDuration,
startDate,
},
{ out: 'bytes' },
);
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(96);
// Each parameter should be 32 bytes, with the value at the end
const expectedBytes = new Array(96).fill(0);
expectedBytes[31] = 1; // periodAmount = 1
expectedBytes[63] = 1; // periodDuration = 1
expectedBytes[95] = 1; // startDate = 1
expect(Array.from(result)).toEqual(expectedBytes);
});
it('returns Uint8Array for large values with bytes encoding', () => {
const periodAmount = 100000000000000000000n; // 100 ETH in wei
const periodDuration = 86400; // 1 day in seconds
const startDate = 2000000000; // Far future timestamp
const result = createNativeTokenPeriodTransferTerms(
{
periodAmount,
periodDuration,
startDate,
},
{ out: 'bytes' },
);
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(96);
});
it('returns Uint8Array for maximum safe values with bytes encoding', () => {
const periodAmount =
115792089237316195423570985008687907853269984665640564039457584007913129639935n; // max uint256
const periodDuration = Number.MAX_SAFE_INTEGER;
const startDate = Number.MAX_SAFE_INTEGER;
const result = createNativeTokenPeriodTransferTerms(
{
periodAmount,
periodDuration,
startDate,
},
{ out: 'bytes' },
);
expect(result).toBeInstanceOf(Uint8Array);
expect(result).toHaveLength(96);
});
});
});