-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJpycExchange.html
458 lines (458 loc) · 16 KB
/
JpycExchange.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>JPYC Exchange</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.boxinline{
display: inline-block;
padding: 0.5em;
margin: 0.25em;
border-radius: 0.5em;
}
.boxoneline{
display: block;
min-height: 1em;
padding: 0.5em;
margin: 0.25em;
border-radius: 0.5em;
}
.textmain{
color: #000000;
background-color: #ffffff;
text-align: left;
font-size: 1em;
}
.texttitle1{
color: #000000;
background-color: #40a0ff;
text-align: center;
font-size: 1.5em;
font-weight: bold;
}
.texttitle2{
color: #000000;
background-color: #c0e0ff;
text-align: center;
font-size: 1.5em;
}
.textouter{
color: #000000;
background-color: #ffffff;
font-size: 1em;
}
.textfloat{
color: #000000;
background-color: #e0ffff;
text-align: left;
font-size: 1em;
}
.textpopup{
color: #000000;
background-color: #ffffe0;
text-align: left;
font-size: 1rem;
}
.textinput{
color: #000000;
background-color: #ffffff;
font-size: 1em;
}
.textbutton1{
color: #000000;
background-color: #80ff80;
font-size: 1em;
}
.textbutton2{
color: #000000;
background-color: #ff8080;
font-size: 1em;
}
.textoutput{
color: #000000;
background-color: #e0e0e0;
font-size: 1em;
}
.imageinline{
height: 1em;
margin: 0em;
border: 0em;
vertical-align: middle;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script type="text/javascript">
function JpycExchange(){
let t = this;
t.web3 = new Web3("https://polygon-rpc.com/");
t.ContractAddress = "0x44bf48EEe929ec77AD67d0B8868e5744bD120DA0";
t.GetGasPrice = function(){
return new Promise(function(a, b){
let x = new XMLHttpRequest();
x.onload = function(){
if(x.status == 200){
a(parseInt(JSON.parse(x.response).standard) + 1 + "000000000");
}else{
b(x.status);
}
};
x.onerror = function(){
b(null);
}
x.open("GET", "https://gasstation-mainnet.matic.network/");
x.send();
});
};
t.ConnectWallet = function(){
let t = this;
return new Promise(function(a, b){
window.ethereum.request({method : "eth_requestAccounts"}).then(function(x){
window.ethereum.request({method : "wallet_switchEthereumChain", params : [{chainId : "0x89"}]}).then(function(x){
t.web3 = new Web3(window.ethereum);
a();
});
});
});
};
t.BalanceOf = function(Token){
let t = this;
return new Promise(function(a, b){
t.web3.eth.getAccounts().then(function(x){
let abi = [
{
inputs : [
{
name : "",
type : "address"
}
],
name : "balanceOf",
outputs : [
{
name : "",
type : "uint256"
}
],
type : "function"
}
];
let c = new t.web3.eth.Contract(abi, Token);
c.methods.balanceOf(x[0]).call().then(function(x){
a(x);
});
});
});
};
t.Allowance = function(Token){
let t = this;
return new Promise(function(a, b){
t.web3.eth.getAccounts().then(function(x){
let abi = [
{
inputs : [
{
name : "",
type : "address"
},
{
name : "",
type : "address"
}
],
name : "allowance",
outputs : [
{
name : "",
type : "uint256"
}
],
type : "function"
}
];
let c = new t.web3.eth.Contract(abi, Token);
c.methods.allowance(x[0], t.ContractAddress).call().then(function(x){
a(x);
});
});
});
};
t.Approve = function(Token){
let t = this;
return new Promise(function(a, b){
t.web3.eth.getAccounts().then(function(x){
let abi = [
{
inputs : [
{
name : "",
type : "address"
},
{
name : "",
type : "uint256"
}
],
name : "approve",
outputs : [
{
name : "",
type : "bool"
}
],
type : "function"
}
];
let c = new t.web3.eth.Contract(abi, Token);
t.GetGasPrice().then(function(g){
c.methods.approve(t.ContractAddress, t.web3.utils.toBN("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")).send({from : x[0], gas : 100000, gasPrice : g}).then(function(x){
a(x);
});
});
});
});
};
t.Swap = function(TokenIn, TokenOut, Amount, Minimum){
let t = this;
return new Promise(function(a, b){
t.web3.eth.getAccounts().then(function(x){
let abi = [
{
inputs : [
{
name : "tokenIn",
type : "address"
},
{
name : "tokenOut",
type : "address"
},
{
name : "amount",
type : "uint256[]"
},
{
name : "minimum",
type : "uint256"
}
],
name : "swap",
outputs : [],
type : "function"
}
];
let c = new t.web3.eth.Contract(abi, t.ContractAddress);
t.GetGasPrice().then(function(g){
c.methods.swap(TokenIn, TokenOut, Amount, Minimum).send({from : x[0], gas : 1000000, gasPrice : g}).then(function(x){
a(x);
});
});
});
});
};
t.Quote = function(TokenIn, TokenOut, Amount, Precision){
let t = this;
return new Promise(function(a, b){
t.web3.eth.getAccounts().then(function(x){
let abi = [
{
inputs : [
{
name : "tokenIn",
type : "address"
},
{
name : "tokenOut",
type : "address"
},
{
name : "amount",
type : "uint256"
},
{
name : "precision",
type : "uint256"
}
],
name : "quote",
outputs : [
{
name : "",
type : "uint256[]"
},
{
name : "",
type : "uint256"
}
],
type : "function"
}
];
let c = new t.web3.eth.Contract(abi, t.ContractAddress);
c.methods.quote(TokenIn, TokenOut, Amount, Precision).call().then(function(x){
a(x);
});
});
});
};
}
function InitializePopup(Name){
Array.prototype.forEach.call(document.getElementsByName(Name), function(e){
new (function(x){
let t = this;
t.Popup = x;
t.Popup.style.position = "fixed";
t.Popup.style.zIndex = "1";
t.Popup.style.display = "none";
t.Popup.parentNode.addEventListener("mousemove", function(e){
t.Popup.style.left = (e.clientX + 1) + "px";
t.Popup.style.top = (e.clientY + 1) + "px";
});
t.Popup.parentNode.addEventListener("mouseenter", function(e){
t.Popup.style.display = "block";
});
t.Popup.parentNode.addEventListener("mouseleave", function(e){
t.Popup.style.display = "none";
});
})(e);
});
}
function InitializeTokenImage(Name){
let ImageUrl = {
"0x6ae7dfc73e0dde2aa99ac063dcf7e8a63265108c" : "https://assets.coingecko.com/coins/images/17277/small/WoZ8rruL_400x400.png",
"0x2791bca1f2de4661ed88a30c99a7a9449aa84174" : "https://assets.coingecko.com/coins/images/6319/small/USD_Coin_icon.png",
"0x8343091f2499fd4b6174a46d067a920a3b851ff9" : "https://assets.coingecko.com/coins/images/24327/small/jJPY.png"
};
Array.prototype.forEach.call(document.getElementsByName(Name), function(e){
e.src = ImageUrl[e.id.toLowerCase()];
});
}
var JX = new JpycExchange();
function Log(x){
document.getElementById("log").innerText += new Date().toISOString() + " " + x + "\n";
};
function StringToBN(x, Base, Decimals){
let a = x.toString();
if(a.indexOf(".") < 0){
return JX.web3.utils.toBN(a, Base).mul(JX.web3.utils.toBN(Base).pow(JX.web3.utils.toBN(Decimals)));
}
let b = JX.web3.utils.toBN(a.slice(0, a.indexOf(".")), Base).mul(JX.web3.utils.toBN(Base).pow(JX.web3.utils.toBN(Decimals)));
let c = a.slice(a.indexOf(".") + 1, a.indexOf(".") + 1 + Decimals);
let d = JX.web3.utils.toBN(c, Base).mul(JX.web3.utils.toBN(Base).pow(JX.web3.utils.toBN(Decimals - c.length)));
return a.charAt(0) == "-" ? b.sub(d) : b.add(d);
}
function BNToString(x, Base, Decimals){
let a = JX.web3.utils.toBN(x);
let b = a.divmod(JX.web3.utils.toBN(Base).pow(JX.web3.utils.toBN(Decimals)));
return (a.isNeg() ? "-" : "") + b.div.abs().toString(Base) + (Decimals != 0 ? "." + b.mod.abs().toString(Base, Decimals) : "");
}
async function Connect(){
await JX.ConnectWallet();
Log("Connected");
}
async function QuoteJpycForUsdc(){
Amount = StringToBN(document.getElementById("amountjpyc").value, 10, 18);
let Result = await JX.Quote("0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c", "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", Amount, 100);
document.getElementById("amountusdc").value = BNToString(Result[1], 10, 6);
document.getElementById("ratejpyc").innerText = parseFloat(document.getElementById("amountusdc").value) / parseFloat(document.getElementById("amountjpyc").value);
document.getElementById("rateusdc").innerText = parseFloat(document.getElementById("amountjpyc").value) / parseFloat(document.getElementById("amountusdc").value);
document.getElementById("route0").innerText = "QuickSwap : " + (100 * parseFloat(BNToString(Result[0][0], 10, 18)) / parseFloat(document.getElementById("amountjpyc").value)).toFixed() + "%";
document.getElementById("route1").innerText = "Uniswap V3 : " + (100 * parseFloat(BNToString(Result[0][1], 10, 18)) / parseFloat(document.getElementById("amountjpyc").value)).toFixed() + "%";
document.getElementById("route2").innerText = "Curve + Jarvis : " + (100 * parseFloat(BNToString(Result[0][2], 10, 18)) / parseFloat(document.getElementById("amountjpyc").value)).toFixed() + "%";
return Result[0];
}
async function QuoteUsdcForJpyc(){
Amount = StringToBN(document.getElementById("amountusdc").value, 10, 6);
let Result = await JX.Quote("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", "0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c", Amount, 100);
document.getElementById("amountjpyc").value = BNToString(Result[1], 10, 18);
document.getElementById("ratejpyc").innerText = parseFloat(document.getElementById("amountusdc").value) / parseFloat(document.getElementById("amountjpyc").value);
document.getElementById("rateusdc").innerText = parseFloat(document.getElementById("amountjpyc").value) / parseFloat(document.getElementById("amountusdc").value);
document.getElementById("route0").innerText = "QuickSwap : " + (100 * parseFloat(BNToString(Result[0][0], 10, 6)) / parseFloat(document.getElementById("amountusdc").value)).toFixed() + "%";
document.getElementById("route1").innerText = "Uniswap V3 : " + (100 * parseFloat(BNToString(Result[0][1], 10, 6)) / parseFloat(document.getElementById("amountusdc").value)).toFixed() + "%";
document.getElementById("route2").innerText = "Curve + Jarvis : " + (100 * parseFloat(BNToString(Result[0][2], 10, 6)) / parseFloat(document.getElementById("amountusdc").value)).toFixed() + "%";
return Result[0];
}
async function UseMaxJpyc(){
document.getElementById("amountjpyc").value = BNToString(await JX.BalanceOf("0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c"), 10, 18);
await QuoteJpycForUsdc();
}
async function UseMaxUsdc(){
document.getElementById("amountusdc").value = BNToString(await JX.BalanceOf("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"), 10, 6);
await QuoteUsdcForJpyc();
}
async function SwapJpycForUsdc(){
let x = await JX.Allowance("0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c");
if(JX.web3.utils.toBN(x).lt(JX.web3.utils.toBN("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))){
Log("Approve JPYC");
let Tx = await JX.Approve("0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c");
Log("Transaction hash : " + Tx.transactionHash);
}
let Amount = await QuoteJpycForUsdc();
let Minimum = StringToBN((parseFloat(document.getElementById("amountusdc").value) * 0.9999).toString(), 10, 6);
Log("Swap " + document.getElementById("amountjpyc").value + " JPYC for " + (parseFloat(document.getElementById("amountusdc").value) * 0.9999) + " USDC at the minimum");
let Tx = await JX.Swap("0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c", "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", Amount, Minimum);
Log("Transaction hash : " + Tx.transactionHash);
}
async function SwapUsdcForJpyc(){
let x = await JX.Allowance("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174");
if(JX.web3.utils.toBN(x).lt(JX.web3.utils.toBN("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))){
Log("Approve USDC");
let Tx = await JX.Approve("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174");
Log("Transaction hash : " + Tx.transactionHash);
}
let Amount = await QuoteUsdcForJpyc();
let Minimum = StringToBN((parseFloat(document.getElementById("amountjpyc").value) * 0.9999).toString(), 10, 18);
Log("Swap " + document.getElementById("amountusdc").value + " USDC for " + (parseFloat(document.getElementById("amountjpyc").value) * 0.9999) + " JPYC at the minimum");
let Tx = await JX.Swap("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", "0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c", Amount, Minimum);
Log("Transaction hash : " + Tx.transactionHash);
}
</script>
</head>
<body onload="InitializePopup('popup');InitializeTokenImage('token');Connect();">
<div class="boxoneline textouter" style="margin: auto; width: 45em; text-align: center;">
<div class="boxoneline texttitle1"><img name="token" id="0x6ae7dfc73e0dde2aa99ac063dcf7e8a63265108c" class="imageinline">➡<img name="token" id="0x2791bca1f2de4661ed88a30c99a7a9449aa84174" class="imageinline"> JPYC Exchange <img name="token" id="0x2791bca1f2de4661ed88a30c99a7a9449aa84174" class="imageinline">➡<img name="token" id="0x6ae7dfc73e0dde2aa99ac063dcf7e8a63265108c" class="imageinline"></div>
<div class="boxoneline textfloat">
This is an aggregator to choose and combine the routes of QuickSwap USDC/JPYC pool, Uniswap V3 USDC/JPYC pool, Curve 2jpy pool and Jarvis Exchange.<br>
The best rate is automatically shown and used on exchanges.<br>
<form onsubmit="Connect();return false;">
<input type="submit" value="Connect to MetaMask" class="boxinline textbutton1">
</form>
</div>
<div class="boxinline textfloat" style="min-width: 20em; vertical-align: top;">
<div class="boxoneline texttitle2">JPYC <img name="token" id="0x6ae7dfc73e0dde2aa99ac063dcf7e8a63265108c" class="imageinline"></div>
<div class="boxoneline">
<span>Amount<div name="popup" class="boxinline textpopup">The quotation will be shown on the other side when you input here.</div></span><br>
<input type="text" id="amountjpyc" class="boxinline textinput" oninput="QuoteJpycForUsdc();"><br>
Rate<br>
<div id="ratejpyc" class="boxoneline textoutput"></div>
<form onsubmit="SwapJpycForUsdc();return false;">
<input type="button" value="Use max" class="boxinline textbutton1" onclick="UseMaxJpyc();">
<input type="submit" value="Swap JPYC for USDC" class="boxinline textbutton1">
</form>
</div>
</div>
<div class="boxinline textfloat" style="min-width: 20em; vertical-align: top;">
<div class="boxoneline texttitle2">USDC <img name="token" id="0x2791bca1f2de4661ed88a30c99a7a9449aa84174" class="imageinline"></div>
<div class="boxoneline">
<span>Amount<div name="popup" class="boxinline textpopup">The quotation will be shown on the other side when you input here.</div></span><br>
<input type="text" id="amountusdc" class="boxinline textinput" oninput="QuoteUsdcForJpyc();"><br>
Rate<br>
<div id="rateusdc" class="boxoneline textoutput"></div>
<form onsubmit="SwapUsdcForJpyc();return false;">
<input type="button" value="Use max" class="boxinline textbutton1" onclick="UseMaxUsdc();">
<input type="submit" value="Swap USDC for JPYC" class="boxinline textbutton1">
</form>
</div>
</div>
<div class="boxoneline textfloat">
<div class="boxoneline texttitle2">Details</div>
<div id="route0" class="boxoneline textoutput"></div>
<div id="route1" class="boxoneline textoutput"></div>
<div id="route2" class="boxoneline textoutput"></div>
</div>
<div class="boxoneline textfloat">
<div class="boxoneline texttitle2">Log</div>
<div id="log" class="boxoneline textoutput"></div>
</div>
</div>
</body>
</html>