-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest-il-formula.js
More file actions
141 lines (121 loc) · 4.49 KB
/
test-il-formula.js
File metadata and controls
141 lines (121 loc) · 4.49 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
// Test script for impermanent loss formula validation
// This script validates the mathematical implementation without requiring the server
function calculateImpermanentLoss(entryPriceRatio, currentPriceRatio) {
// Validate inputs
if (entryPriceRatio <= 0 || currentPriceRatio <= 0) {
throw new Error('Price ratios must be positive numbers');
}
// Calculate price ratio (current/entry)
const priceRatio = currentPriceRatio / entryPriceRatio;
// Apply the standard IL formula: 2 * sqrt(price_ratio) / (1 + price_ratio) - 1
const impermanentLoss = (2 * Math.sqrt(priceRatio)) / (1 + priceRatio) - 1;
// Convert to percentage
const impermanentLossPercentage = impermanentLoss * 100;
return {
entryPriceRatio,
currentPriceRatio,
priceRatio,
impermanentLossPercentage,
};
}
function runFormulaTests() {
console.log('🧮 Testing Impermanent Loss Formula Implementation');
console.log('==================================================\n');
const testCases = [
{
entry: 1.0,
current: 1.0,
description: 'No price change (should be 0% IL)',
expected: 0
},
{
entry: 1.0,
current: 1.5,
description: '50% price increase (should show negative IL)',
expected: -2.02 // Approximate expected value
},
{
entry: 1.0,
current: 0.5,
description: '50% price decrease (should show negative IL)',
expected: -2.02 // Symmetric case
},
{
entry: 1.0,
current: 2.0,
description: '100% price increase',
expected: -5.72 // Approximate expected value
},
{
entry: 1.0,
current: 0.25,
description: '75% price decrease',
expected: -11.25 // Approximate expected value
},
];
let allTestsPassed = true;
testCases.forEach((testCase, index) => {
try {
const result = calculateImpermanentLoss(testCase.entry, testCase.current);
console.log(`Test ${index + 1}: ${testCase.description}`);
console.log(` Entry: ${testCase.entry}, Current: ${testCase.current}`);
console.log(` Price Ratio: ${result.priceRatio.toFixed(4)}`);
console.log(` IL Percentage: ${result.impermanentLossPercentage.toFixed(4)}%`);
// Check if result is reasonable (should be negative for price changes)
if (testCase.entry === testCase.current) {
if (Math.abs(result.impermanentLossPercentage) < 0.0001) {
console.log(' ✅ PASS: No impermanent loss when prices are equal');
} else {
console.log(' ❌ FAIL: Should be 0% when prices are equal');
allTestsPassed = false;
}
} else {
if (result.impermanentLossPercentage < 0) {
console.log(' ✅ PASS: Negative impermanent loss as expected');
} else {
console.log(' ❌ FAIL: Should be negative for price changes');
allTestsPassed = false;
}
}
// Check if the result is within reasonable range
if (Math.abs(result.impermanentLossPercentage) > 50) {
console.log(' ⚠️ WARNING: Very high impermanent loss percentage');
}
} catch (error) {
console.log(` ❌ ERROR: ${error.message}`);
allTestsPassed = false;
}
console.log('');
});
// Test error cases
console.log('Testing Error Cases:');
console.log('-------------------');
const errorTestCases = [
{ entry: 0, current: 1.0, description: 'Zero entry price' },
{ entry: 1.0, current: 0, description: 'Zero current price' },
{ entry: -1.0, current: 1.0, description: 'Negative entry price' },
{ entry: 1.0, current: -1.0, description: 'Negative current price' },
];
errorTestCases.forEach((testCase, index) => {
try {
calculateImpermanentLoss(testCase.entry, testCase.current);
console.log(`Error Test ${index + 1}: ${testCase.description}`);
console.log(' ❌ FAIL: Should have thrown an error');
allTestsPassed = false;
} catch (error) {
console.log(`Error Test ${index + 1}: ${testCase.description}`);
console.log(' ✅ PASS: Correctly threw error');
}
console.log('');
});
console.log('==================================================');
if (allTestsPassed) {
console.log('🎉 All tests passed! The formula implementation is correct.');
} else {
console.log('❌ Some tests failed. Please review the implementation.');
}
console.log('==================================================');
return allTestsPassed;
}
// Run the tests
runFormulaTests();