-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathfees.test.ts
More file actions
471 lines (398 loc) · 13.9 KB
/
fees.test.ts
File metadata and controls
471 lines (398 loc) · 13.9 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
471
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { amount as sdkAmount } from '@wormhole-foundation/sdk';
import {
calculateFeeOffset,
roundDownToDecimals,
applyOffsetFormula,
} from './fees';
import config from 'config';
// Mock the config module
vi.mock('config', () => ({
default: {
routes: {
get: vi.fn(),
},
},
}));
describe('calculateFeeOffset', () => {
const mockAmount = sdkAmount.fromBaseUnits(10000n, 6); // 0.01 with 6 decimals
const mockToken = {
key: 'Ethereum:0xa0b86a33e6776a1e5e0b3a6f6a1b6d6f7e7c7e8e',
chain: 'Ethereum',
address: '0xa0b86a33e6776a1e5e0b3a6f6a1b6d6f7e7c7e8e',
addressString: '0xa0b86a33e6776a1e5e0b3a6f6a1b6d6f7e7c7e8e',
symbol: 'USDC',
name: 'USD Coin',
decimals: 6,
tokenId: {
chain: 'Ethereum',
address: '0xa0b86a33e6776a1e5e0b3a6f6a1b6d6f7e7c7e8e',
},
display: 'USD Coin',
shortAddress: '0xa0b8...7e8e',
tuple: ['Ethereum', '0xa0b86a33e6776a1e5e0b3a6f6a1b6d6f7e7c7e8e'],
isNativeGasToken: false,
isTokenBridgeWrappedToken: false,
nativeChain: 'Ethereum',
icon: 'usdc.svg',
coinGeckoId: 'usd-coin',
equals: vi.fn(),
toJson: vi.fn(),
} as any;
beforeEach(() => {
vi.clearAllMocks();
});
it('should return undefined for invalid inputs', () => {
// Mock a valid route for testing
const mockRoute = {
rc: {
meta: { name: 'TestRoute' },
config: {
referrerFeeDbps: 10n,
},
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
// Test with undefined route
expect(
calculateFeeOffset(undefined as any, mockAmount, mockToken),
).toBeUndefined();
// Test with undefined amount
expect(
calculateFeeOffset('TestRoute', undefined, mockToken),
).toBeUndefined();
// Test with zero amount
expect(
calculateFeeOffset(
'TestRoute',
sdkAmount.fromBaseUnits(0n, 6),
mockToken,
),
).toBeUndefined();
});
it('should return undefined when route is not found', () => {
vi.mocked(config.routes.get).mockReturnValue(undefined as any);
const result = calculateFeeOffset(
'NonExistentRoute',
mockAmount,
mockToken,
);
expect(result).toBeUndefined();
});
describe('Mayan routes', () => {
it('should calculate fee using getReferrerBps for Mayan routes', () => {
const mockRoute = {
rc: {
meta: { name: 'MayanSwapRoute' },
getReferrerBps: vi.fn().mockReturnValue(100), // 100 bps = 1%
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'MayanSwapRoute',
mockAmount,
mockToken,
mockToken,
);
expect(mockRoute.rc.getReferrerBps).toHaveBeenCalledWith({
source: {
id: mockToken.tokenId,
symbol: mockToken.symbol,
decimals: mockToken.decimals,
},
destination: {
id: mockToken.tokenId,
symbol: mockToken.symbol,
decimals: mockToken.decimals,
},
});
// offset = 10000 * 100 / (10000 - 100) = 101.01... ≈ 101
expect(sdkAmount.units(result!)).toBe(101n);
});
it('should return undefined for Mayan routes with zero bps', () => {
const mockRoute = {
rc: {
meta: { name: 'MayanSwapRoute' },
getReferrerBps: vi.fn().mockReturnValue(0),
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'MayanSwapRoute',
mockAmount,
mockToken,
mockToken,
);
expect(result).toBeUndefined();
});
it('should return undefined for Mayan routes without getReferrerBps', () => {
const mockRoute = {
rc: {
meta: { name: 'MayanSwapRoute' },
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'MayanSwapRoute',
mockAmount,
mockToken,
mockToken,
);
expect(result).toBeUndefined();
});
it('should return undefined for Mayan routes without destination token', () => {
const mockRoute = {
rc: {
meta: { name: 'MayanSwapRoute' },
getReferrerBps: vi.fn().mockReturnValue(100),
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'MayanSwapRoute',
mockAmount,
mockToken,
);
expect(result).toBeUndefined();
expect(mockRoute.rc.getReferrerBps).not.toHaveBeenCalled();
});
});
describe('CCTP Executor routes', () => {
it('should calculate fee using referrerFeeDbps', () => {
const mockRoute = {
rc: {
meta: { name: 'CCTPExecutorRoute' },
config: {
referrerFeeDbps: 10n, // 10 dbps = 0.01%
},
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'CCTPExecutorRoute',
mockAmount,
mockToken,
);
// offset = 10000 * 10 / (100000 - 10) = 1.001... ≈ 1
expect(sdkAmount.units(result!)).toBe(1n);
});
});
describe('NTT Executor routes', () => {
it('should calculate fee using referrerFee.feeDbps', () => {
const mockRoute = {
rc: {
meta: { name: 'NTTExecutorRoute' },
config: {
referrerFee: {
feeDbps: 50n, // 50 dbps = 0.05%
},
},
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'NTTExecutorRoute',
mockAmount,
mockToken,
);
// offset = 10000 * 50 / (100000 - 50) = 5.002... ≈ 5
expect(sdkAmount.units(result!)).toBe(5n);
});
it('should use token-specific override for NTT routes', () => {
const mockRoute = {
rc: {
meta: { name: 'NTTExecutorRoute' },
config: {
referrerFee: {
feeDbps: 50n,
perTokenOverrides: {
[mockToken.addressString]: {
referrerFeeDbps: 20n, // Token-specific override
},
},
},
},
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'NTTExecutorRoute',
mockAmount,
mockToken,
);
// offset = 10000 * 20 / (100000 - 20) = 2.0004... ≈ 2
expect(sdkAmount.units(result!)).toBe(2n);
});
});
describe('Token Bridge Executor routes', () => {
it('should calculate fee using referrerFee.referrerFeeDbps', () => {
const mockRoute = {
rc: {
meta: { name: 'TokenBridgeExecutorRoute' },
config: {
referrerFee: {
referrerFeeDbps: 100n, // 100 dbps = 0.1%
},
},
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'TokenBridgeExecutorRoute',
mockAmount,
mockToken,
);
// offset = 10000 * 100 / (100000 - 100) = 10.001... ≈ 10
expect(sdkAmount.units(result!)).toBe(10n);
});
it('should use token-specific override for Token Bridge Executor routes', () => {
const mockRoute = {
rc: {
meta: { name: 'TokenBridgeExecutorRoute' },
config: {
referrerFee: {
referrerFeeDbps: 100n,
tokenFeeOverrides: {
[mockToken.addressString]: {
referrerFeeDbps: 30n, // Token-specific override
},
},
},
},
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset(
'TokenBridgeExecutorRoute',
mockAmount,
mockToken,
);
// offset = 10000 * 30 / (100000 - 30) = 3.0009... ≈ 3
expect(sdkAmount.units(result!)).toBe(3n);
});
});
it('should return undefined for routes without config', () => {
const mockRoute = {
rc: {
meta: { name: 'SomeRoute' },
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset('SomeRoute', mockAmount, mockToken);
expect(result).toBeUndefined();
});
it('should return undefined for routes with zero fee', () => {
const mockRoute = {
rc: {
meta: { name: 'ZeroFeeRoute' },
config: {
referrerFeeDbps: 0n,
},
},
};
vi.mocked(config.routes.get).mockReturnValue(mockRoute as any);
const result = calculateFeeOffset('ZeroFeeRoute', mockAmount, mockToken);
expect(result).toBeUndefined();
});
});
describe('applyOffsetFormula', () => {
const mockAmount = sdkAmount.fromBaseUnits(10000n, 6); // 0.01 with 6 decimals
it('should calculate correct offset for dbps', () => {
// 10 dbps = 0.01% fee on 0.01 tokens (10000 base units with 6 decimals)
// offset = 10000 * 10 / (100000 - 10) = 10000 * 10 / 99990 = 1.001... ≈ 1
const result = applyOffsetFormula(mockAmount, 10n, 100000n);
expect(sdkAmount.units(result)).toBe(1n);
expect(result.decimals).toBe(6);
});
it('should work with different fee rates', () => {
// Test various fee rates to ensure formula works correctly
// 1 bps = 0.01% fee
const result1bps = applyOffsetFormula(mockAmount, 1n, 10000n);
expect(sdkAmount.units(result1bps)).toBe(1n); // Very small offset
// 500 bps = 5% fee
const result500bps = applyOffsetFormula(mockAmount, 500n, 10000n);
// offset = 10000 * 500 / (10000 - 500) = 10000 * 500 / 9500 = 526.31... ≈ 526
expect(sdkAmount.units(result500bps)).toBe(526n);
// 1000 dbps = 1% fee (same as 100 bps but using dbps denominator)
const result1000dbps = applyOffsetFormula(mockAmount, 1000n, 100000n);
// offset = 10000 * 1000 / (100000 - 1000) = 10000 * 1000 / 99000 = 101.01... ≈ 101
expect(sdkAmount.units(result1000dbps)).toBe(101n);
});
it('should handle edge case with very small fee rates', () => {
// 1 dbps = 0.001% fee - very small
const result = applyOffsetFormula(mockAmount, 1n, 100000n);
// offset = 10000 * 1 / (100000 - 1) = 10000 / 99999 = 0.10000... which gets truncated to 0
expect(sdkAmount.units(result)).toBe(0n);
});
it('should handle edge case with larger amounts', () => {
// Test with a larger amount: 1000 tokens (1000000000 base units with 6 decimals)
const largeAmount = sdkAmount.fromBaseUnits(1000000000n, 6);
// 100 bps = 1% fee
const result = applyOffsetFormula(largeAmount, 100n, 10000n);
// offset = 1000000000 * 100 / (10000 - 100) = 1000000000 * 100 / 9900 = 10101010.1... ≈ 10101010
expect(sdkAmount.units(result)).toBe(10101010n);
expect(result.decimals).toBe(6);
});
it('should work with zero fee rate', () => {
// Edge case: 0 fee rate should return 0 offset
const result = applyOffsetFormula(mockAmount, 0n, 10000n);
expect(sdkAmount.units(result)).toBe(0n);
expect(result.decimals).toBe(6);
});
it('should demonstrate mathematical correctness', () => {
// Verify the mathematical formula works as intended
const amount = sdkAmount.fromBaseUnits(100000n, 6); // 0.1 tokens
const feeRate = 250n; // 250 bps = 2.5%
const denominator = 10000n;
const offset = applyOffsetFormula(amount, feeRate, denominator);
const totalSent = sdkAmount.units(amount) + sdkAmount.units(offset);
// Calculate what the fee would be on the total sent amount
const calculatedFee = (totalSent * feeRate) / denominator;
const userReceives = totalSent - calculatedFee;
// User should receive very close to the original amount (within rounding precision)
expect(userReceives).toBeGreaterThanOrEqual(sdkAmount.units(amount));
expect(userReceives - sdkAmount.units(amount)).toBeLessThanOrEqual(10n); // Allow small rounding difference
});
});
describe('roundDownToDecimals', () => {
it('should not round when token has maxDecimals or fewer', () => {
// 6 decimals - no rounding
expect(roundDownToDecimals(123456n, 6)).toBe(123456n);
// 4 decimals - no rounding
expect(roundDownToDecimals(1234n, 4)).toBe(1234n);
// 0 decimals - no rounding
expect(roundDownToDecimals(100n, 0)).toBe(100n);
});
it('should round down when token has more than maxDecimals', () => {
// 18 decimals, needs rounding down
// 1.234567890123456789 -> 1.234567
const value18 = 1234567890123456789n;
const expected18 = 1234567000000000000n;
expect(roundDownToDecimals(value18, 18)).toBe(expected18);
// 8 decimals, needs rounding down
// 1.23456789 -> 1.234567
const value8 = 123456789n;
const expected8 = 123456700n;
expect(roundDownToDecimals(value8, 8)).toBe(expected8);
});
it('should not round when value is already at maxDecimals precision', () => {
// 18 decimals but value only uses 6 decimal places
const value = 1234560000000000000n;
expect(roundDownToDecimals(value, 18)).toBe(value);
// 10 decimals but value only uses 6 decimal places
const value10 = 12345600000n;
expect(roundDownToDecimals(value10, 10)).toBe(value10);
});
it('should handle edge cases correctly', () => {
// Zero value
expect(roundDownToDecimals(0n, 18)).toBe(0n);
// Very small value that rounds down to zero
const tiny = 1n; // 0.000000000000000001 in 18 decimals
const expectedTiny = 0n; // Rounds down to 0
expect(roundDownToDecimals(tiny, 18)).toBe(expectedTiny);
// Value just below 6 decimal precision
const almostRounded = 1234567999999999999n; // Just below 1.234568
const almostExpected = 1234567000000000000n; // Rounds down to 1.234567
expect(roundDownToDecimals(almostRounded, 18)).toBe(almostExpected);
});
});