-
Notifications
You must be signed in to change notification settings - Fork 30
/
withdraw.js
110 lines (98 loc) · 3.64 KB
/
withdraw.js
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
var token_balance;
var token_supply;
async function update_balances() {
var default_account = (await web3.eth.getAccounts())[0];
if (default_account) {
for (let i = 0; i < N_COINS; i++)
wallet_balances[i] = parseInt(await coins[i].methods.balanceOf(default_account).call());
token_balance = parseInt(await swap_token.methods.balanceOf(default_account).call());
}
for (let i = 0; i < N_COINS; i++)
balances[i] = parseInt(await swap.methods.balances(i).call());
token_supply = parseInt(await swap_token.methods.totalSupply().call());
}
function handle_change_amounts(i) {
return function() {
for (let j = 0; j < N_COINS; j++) {
var cur = $('#currency_' + j);
if ((this.value > (balances[i] * c_rates[i] * token_balance / token_supply)) & (j == i))
cur.css('background-color', 'red')
else
cur.css('background-color', 'blue');
cur.css('color', 'aqua');
}
var share = $('#liquidity-share');
share.val('---');
share.css('background-color', '#707070');
share.css('color', '#d0d0d0');
}
}
function handle_change_share() {
var share = $('#liquidity-share');
var val = share.val();
share.css('background-color', 'blue');
share.css('color', 'aqua');
if (val == '---') {
share.val('0.0');
val = 0;
}
else if ((val > 100) | (val < 0))
share.css('background-color', 'red');
for (let i = 0; i < N_COINS; i++) {
var cur = $('#currency_' + i);
if ((val >=0) & (val <= 100))
cur.val((val / 100 * balances[i] * c_rates[i] * token_balance / token_supply).toFixed(2))
else
cur.val('0.00');
cur.css('background-color', '#707070');
cur.css('color', '#d0d0d0');
}
}
async function handle_remove_liquidity() {
var share = $('#liquidity-share');
var share_val = share.val();
var deadline = Math.floor((new Date()).getTime() / 1000) + trade_timeout;
var amounts = $("[id^=currency_]").toArray().map(x => $(x).val());
for (let i = 0; i < N_COINS; i++)
amounts[i] = Math.floor(amounts[i] / c_rates[i]); // -> c-tokens
var min_amounts = amounts.map(x => BigInt(Math.floor(0.97 * x)).toString());
amount = amounts.map(x => BigInt(x).toString());
var txhash;
var default_account = (await web3.eth.getAccounts())[0];
if (share_val == '---') {
await swap.methods.remove_liquidity_imbalance(amounts, deadline).send({'from': default_account});
}
else {
var amount = BigInt(Math.floor(share_val / 100 * token_balance)).toString();
if (share_val == 100)
amount = await swap_token.methods.balanceOf(default_account).call();
await swap.methods.remove_liquidity(amount, deadline, min_amounts).send({'from': default_account});
}
await update_balances();
update_fee_info();
}
function init_ui() {
for (let i = 0; i < N_COINS; i++) {
$('#currency_' + i).focus(handle_change_amounts(i));
$('#currency_' + i).on('input', handle_change_amounts(i));
}
$('#liquidity-share').focus(handle_change_share);
$('#liquidity-share').on('input', handle_change_share);
handle_change_share();
update_fee_info();
$("#remove-liquidity").click(handle_remove_liquidity);
}
window.addEventListener('load', async () => {
init_menu();
if (window.ethereum)
{
window.web3 = new Web3(ethereum);
await ethereum.enable();
}
else
window.web3 = new Web3(infura_url);
await init_contracts();
await update_rates();
await update_balances();
init_ui();
});