-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
328 lines (293 loc) · 13.1 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
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
<html>
<head>
<title>AMMO Simulator</title>
<style>
body {
font-family: Arial, sans-serif;
background: #141519;
}
h1 {
text-align: center;
}
canvas {
margin: 0 auto;
display: block;
/* border: 1px solid black; */
}
</style>
</head>
<body>
<h1>AMMO Simulator</h1>
<canvas id="ammCanvas" width="800" height="600"></canvas>
<p id="debug" style="color:white"></p>
<div>
<button onclick="trade(amm, -100);drawVisual()">100 Sell</button>
<button onclick="trade(amm, -10);drawVisual()">10 Sell</button>
<button onclick="trade(amm, -1);drawVisual()">1 Sell</button>
<button onclick="ammo_turn(ammo, amm, 1);drawVisual()">Recalc position</button>
<button onclick="trade(amm, 1);drawVisual()">1 Buy</button>
<button onclick="trade(amm, 10);drawVisual()">10 Buy</button>
<button onclick="trade(amm, 100);drawVisual()">100 Buy</button>
<button onclick="ammo.eth += 10;drawVisual()">[10 Yield]</button>
</div>
<!-- This is a visualization and simulation of a uniswap v3 AMM pool and an assocaited trading strategy.-->
<!--
The first step is build a visualization of liquidity on the AMM pool. We will show one vertical bar for
each price tick. The barchart will be a stacked bar chart showing the amount of each token in that tick.
The bar chart will be built from a data structure that looks like this:
[
{
priceLow: 100,
priceHigh: 101,
ammo: [50, 100],
lps: [34, 60]
},
{
priceLow: 101,
priceHigh: 102,
ammo: [50, 100],
lps: [34, 60]
},
...
]
-->
<script>
let amm = [];
document.amm = amm
// Build sample data
const PRICE_INCREMENT = 0.01;
for(let price_low = 0.15; price_low < 1.5; price_low += PRICE_INCREMENT) {
price_low = Math.round(price_low*1000)/1000;
amm.push({
priceLow: price_low,
priceHigh: price_low + PRICE_INCREMENT,
price: price_low + PRICE_INCREMENT / 2,
ammo: [0, 0],
lps: [0, 0]
});
}
// Draw the AMM
let canvas = document.getElementById('ammCanvas');
let ctx = canvas.getContext('2d');
function drawVisual() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawAmm(amm,ctx);
}
function drawAmm(amm,ctx) {
let barWidth = 5;
let barSpacing = 3;
let barHeight = 500;
let amountToBarHeight = 0.003;
let barX = 0;
let barY = barHeight;
for(let i = 0; i < amm.length; i++) {
let bar = amm[i];
let ammo = bar.ammo;
let lps = bar.lps;
let ammo0Height = barHeight * ammo[0] * amountToBarHeight;
let ammo1Height = barHeight * ammo[1] * amountToBarHeight * bar.price;
let lps0Height = barHeight * lps[0] * amountToBarHeight;
let lps1Height = barHeight * lps[1] * amountToBarHeight * bar.price;
// Draw the bar
ctx.fillStyle = '#636cef';
ctx.fillRect(barX, barY, barWidth, -ammo0Height);
ctx.fillStyle = '#8F9bF1';
ctx.fillRect(barX, barY - ammo0Height, barWidth, -lps0Height);
ctx.fillStyle = 'red';
ctx.fillRect(barX, barY - ammo0Height -lps0Height, barWidth, -ammo1Height);
ctx.fillStyle = '#F38c87';
ctx.fillRect(barX, barY - ammo0Height - ammo1Height -lps0Height, barWidth, -lps1Height);
barX += barWidth + barSpacing;
if(i% 10 == 0){
ctx.fillStyle = '#666';
ctx.fillRect(barX, barY + 3, 2, 10);
}
}
}
// People can trade in the pool. When this happens, we move through the ticks in the
// direction of their trade, and swap for the liquidity availiable in that tick, before
// moving on to the next tick if needed.
// Uniswap v3 uses a constant product AMM, so the prices inside each tick
// vary from the priceLow to the priceHigh.
function trade(amm, ethAmountIn){
let ammoTokenAmountOut = 0
if(ethAmountIn == 0){
return 0;
} else if (ethAmountIn > 0){
// Trading ETH for AMMO
// Start at the lowest price tick
let i = 0;
while(ethAmountIn > 0 && i < amm.length){
let tick = amm[i];
let priceLow = tick.priceLow;
let priceHigh = tick.priceHigh;
// find our current liquidity
let ethInTick = tick.ammo[0] + tick.lps[0];
let ammoTokenInTick = tick.ammo[1] + tick.lps[1];
if(ammoTokenInTick <= 0.0000000001){
i++;
continue;
}
// Determine the max ETH we can trade in this tick
// We need to use the constant product formula, adjusted for
// the tick priceLow and priceHigh.
let maxAmmoTokenPurchaseable = ammoTokenInTick;
let ammoToPurchase = Math.min(maxAmmoTokenPurchaseable, ethAmountIn / tick.price);
// Update the trade
ethSpent = ammoToPurchase * tick.price;
ethAmountIn -= ethSpent;
ammoTokenAmountOut += ammoToPurchase;
// Update the ammo and LPs in the tick, with each getting their share of the ammoToken, and losing their share of ETH
let ammoLiq = tick.ammo[0] + tick.ammo[1]
let lpsLiq = tick.lps[0] + tick.lps[1]
let ammoLiqShare = ammoLiq / (ammoLiq + lpsLiq)
let lpsLiqShare = lpsLiq / (ammoLiq + lpsLiq)
tick.ammo[0] += ethSpent * ammoLiqShare;
tick.ammo[1] -= ammoToPurchase * ammoLiqShare;
tick.lps[0] += ethSpent * lpsLiqShare;
tick.lps[1] -= ammoToPurchase * lpsLiqShare;
i++;
}
} else if (ethAmountIn < 0){
// Trading AMMO for ETH
// Start at the highest price tick
let i = amm.length - 1;
while(ethAmountIn < 0 && i >= 0){
let tick = amm[i];
let priceLow = tick.priceLow;
let priceHigh = tick.priceHigh;
// find our current liquidity
let ethInTick = tick.ammo[0] + tick.lps[0];
let ammoTokenInTick = tick.ammo[1] + tick.lps[1];
// console.log(i, ethInTick, ammoTokenInTick, ethAmountIn)
if(ethInTick <= 0.0000000001){
i--;
continue;
}
let ammoToPurchase = Math.max(-ethInTick / tick.price, ethAmountIn / tick.price);
// Update the trade
ethSpent = ammoToPurchase * tick.price;
ethAmountIn -= ethSpent;
ammoTokenAmountOut += ammoToPurchase;
// Update the ammo and LPs in the tick, with each getting their share of the ammoToken, and losing their share of ETH
// console.log(ethAmountIn)
let ammoLiq = tick.ammo[0] + tick.ammo[1]
let lpsLiq = tick.lps[0] + tick.lps[1]
let ammoLiqShare = ammoLiq / (ammoLiq + lpsLiq)
let lpsLiqShare = lpsLiq / (ammoLiq + lpsLiq)
tick.ammo[0] += ethSpent * ammoLiqShare;
tick.ammo[1] -= ammoToPurchase * ammoLiqShare;
tick.lps[0] += ethSpent * lpsLiqShare;
tick.lps[1] -= ammoToPurchase * lpsLiqShare;
i--;
}
} else {
console.log("We have no idea how this was reached")
}
return ammoTokenAmountOut
}
function lp_turn(amm, t){
}
ammo = {
floorTick: 0,
max: 0.35,
projectTokens: 0,
defaultTokensPerTickInEth: 34.123,
outstandingTokens: 0,
eth: 0
}
function ammo_initialzie(ammo, amm){
const startingEth = 100;
ammo.projectTokens = startingEth / 0.2 * 0.90;
ammo.outstandingTokens = ammo.projectTokens
console.log(ammo)
// Put all the ETH into the lowest price tick
let i=0;
for(i=0; i<amm.length; i++) {
let tick = amm[i];
let ethToMakeThisFloor = ammo.outstandingTokens * tick.price;
if( ethToMakeThisFloor < ammo.eth) {
ammo.floorTick = i;
} else {
break;
}
}
amm[ammo.floorTick].ammo[0] = startingEth;
i++
for(; i<amm.length; i++){
let tick = amm[i];
tick.ammo[0] = 0
tick.ammo[1] = ammo.defaultTokensPerTickInEth / tick.price;
ammo.outstandingTokens += tick.ammo[1]
}
}
function ammo_turn(ammo, amm, t){
// Withdraw all liquidity
let activeTick = 0;
for(let i=0; i<amm.length; i++) {
let tick = amm[i];
if( tick.ammo[1] > 0) {
// Active tick, stop withdrawing
activeTick = i;
break;
}
ammo.eth += tick.ammo[0];
tick.ammo[0] = 0;
}
let myAmmo = 0;
for(let i=0; i<amm.length; i++) {
let tick = amm[i];
if(tick.ammo[1] > 0) {
myAmmo += tick.ammo[1];
}
}
let userAmmo = ammo.outstandingTokens - myAmmo
// Find floor tick.
let floorTick = 0;
let ethToMakeThisFloor;
for(let i=ammo.floorTick; i<amm.length; i++) {
tick = amm[i];
floorTick = i;
ethToMakeThisFloor = userAmmo * tick.price;
if( ethToMakeThisFloor < ammo.eth * 0.8) {
ammo.floorTick = i;
floorTick = i;
} else {
break;
}
}
tick.ammo[0] = Math.min(ethToMakeThisFloor, ammo.eth);
ammo.eth -= tick.ammo[0];
// Add remaining eth between floor and active
if(ammo.eth > 0){
let ethPerTick = ammo.eth / (activeTick - floorTick);
for(let i = floorTick + 1; i < activeTick; i++) {
let tick = amm[i];
tick.ammo[0] = ethPerTick;
ammo.eth -= ethPerTick;
}
}
}
ammo_initialzie(ammo, amm)
trade(amm, 47.2)
// ammo_turn(ammo, amm, 1)
// trade(amm, 80)
// trade(amm, 185)
// ammo_turn(ammo, amm, 1)
drawVisual()
// setInterval(() => {
// if(Math.random()>0.5){
// trade(amm, Math.random() * -25)
// } else {
// trade(amm, Math.random() * 100)
// }
// ammo_turn(ammo, amm, 1)
// drawVisual()
// }, 100)
// debug("AHEHEHE")
function debug(text){
document.getElementById('debug').innerText = text
}
</script>
</body>
</html>