-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
184 lines (162 loc) · 6.74 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title> Curve IL Calculator </title>
<link rel="stylesheet" href="styles.css">
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
type="application/javascript"></script>
<script src="./config.js"> </script>
<script src="./util.js"> </script>
<script src="./test.js"> </script>
</head>
<body>
<div style="position: relative; height: 50px;">
<div class="icon-bar">
<a href="https://www.dives.fyi"><img src="images/dives.png"
class="icon"></a>
<a href="https://github.com/orgs/divesresearch/repositories"><img
src="images/github.png" class="icon"></a>
<a href="https://twitter.com/DivesResearch"><img
src="images/twitter.png" class="icon"/></a>
<a href="https://t.me/divesresearch"><img src="images/telegram.png"
class="icon"/></a>
</div>
</div>
<div class="box" style="background: #3465a4;">
<h1> Curve <abbr title="Impermanent Loss">IL</abbr> Calculator </h1>
</div>
<div class="box">
<fieldset>
<legend> <b> Calculator </b> </legend>
<br>
<center>
<div class="container">
<div class="custom-select">
<select id="blockchains">
<option value="" disabled selected>
Select a Blockchain
</option>
</select>
</div>
<div class="custom-select">
<select name="pools" id="pools">
<option value="" disabled selected>
Select a Pool
</option>
</select>
</div>
</div>
<br>
<div id="prices"> </div>
<button id="calculate"> Calculate </button>
<div id="result" style="margin-top: 15x;">
</div>
</center>
</fieldset>
</div>
<div class="box">
<fieldset>
<legend> <b> Info/Disclaimer </b> </legend>
<p> This app is not an official app from Curve.fi
and is developed by Dives Research voluntarily.</p>
<p>To see the calculations behind the app, read our <a
href="https://dives.fyi/blog/curve-il-calculator/">article</a>.</p>
</fieldset>
</div>
<br>
<script>
const
[blockchains, pools, calculate, prices, result] =
["blockchains", "pools", "calculate", "prices", "result"]
.map( id => document.getElementById(id) )
chainDatas
.map( ({chain_name : name}) => {
const option = document.createElement("option")
option.text = name
return option
})
.forEach( (option) =>
blockchains.add( option )
)
blockchains.addEventListener('change', () => {
const
chainName =
blockchains.options[blockchains.selectedIndex].value,
poolNames =
poolDatas
.filter(poolData => poolData.chain == chainName)
.map(({name}) => name )
for(let i = pools.options.length - 1; i > 0; i--) {
pools.remove(i);
}
for(const pName of poolNames){
const option = document.createElement("option")
option.text = pName
pools.add(option)
}
})
pools.addEventListener('change', () => {
const tokens = pools.options[pools.selectedIndex].value.split('-')
prices.innerHTML = ``
for(let i = 0; i < tokens.length; i++){
prices.innerHTML += `
<input type="number" class="text" id="initial_price_${i}" name="fname"
value="" placeholder="Initial ${tokens[i]} Price" >
<input type="number" class="text" id="final_price_${i}" name="fname"
value="" placeholder="Final ${tokens[i]} Price" >
`
}
})
calculate.addEventListener('click', async () => {
result.innerHTML = '<img src="images/clock.gif" width="30px" \
height="30px"/>'
const
tokens = pools.options[pools.selectedIndex].value.split('-'),
[chainName, poolName] =
[blockchains, pools]
.map( selector =>
selector.options[selector.selectedIndex].value ),
[initialPricesArgs, finalPricesArgs] =
['initial_price', 'final_price']
.map( idPrefix => {
let res = []
for(let i = 0; i < tokens.length; i++){
res.push(
document.getElementById(idPrefix + '_' +
i).value
)
}
return res
})
if(
[initialPricesArgs, finalPricesArgs]
.find(prices =>
prices.find( (price) => price <= 0 ) != undefined)
){
result.innerHTML = 'Enter a positive number'
return
}
const
[initialPrices, finalPrices] =
[initialPricesArgs, finalPricesArgs]
.map( prices => prices.map(parseFloat) ),
{A, n} = await fetchPoolDatas(chainName, poolName),
{ initialAmounts, finalAmounts, impermanentLoss } =
calculateIL(A, initialPrices, finalPrices, n),
[initialAmountsStr, finalAmountsStr] =
[initialAmounts, finalAmounts]
.map( amounts =>
amounts
.map(parseFloat)
.map( amount => amount.toFixed(4) )
.join(' - ')
)
result.innerText = `
Initial distribution: ${ initialAmountsStr }
Final distribution: ${ finalAmountsStr }
Impermanent Loss: ${parseFloat(impermanentLoss).toFixed(2)}%
`
})
</script>
</body>
</html>