forked from AronVanAmmers/curve-ui
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeposit.js
135 lines (114 loc) · 4.73 KB
/
deposit.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
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
var sync_balances;
async function handle_sync_balances() {
sync_balances = $('#sync-balances').prop('checked');
var max_balances = $('#max-balances').prop('checked');
var default_account = (await web3provider.eth.getAccounts())[0];
await update_rates();
for (let i = 0; i < N_COINS; i++) {
wallet_balances[i] = parseInt(await coins[i].methods.balanceOf(default_account).call());
if(!default_account) wallet_balances[i] = 0
}
if (max_balances) {
$(".currencies input").prop('disabled', true);
for (let i = 0; i < N_COINS; i++) {
var val = Math.floor(wallet_balances[i] * c_rates[i] * 100) / 100;
$('#currency_' + i).val(val.toFixed(2));
}
} else
$(".currencies input").prop('disabled', false);
for (let i = 0; i < N_COINS; i++)
balances[i] = parseInt(await swap.methods.balances(i).call());
}
async function handle_add_liquidity() {
var default_account = (await web3provider.eth.getAccounts())[0];
var max_balances = $("#max-balances").is(':checked')
var amounts = $("[id^=currency_]").toArray().map(x => $(x).val());
for (let i = 0; i < N_COINS; i++) {
let amount = cBN(Math.floor(amounts[i] / c_rates[i]).toString()).toFixed(0,1);
let balance = await coins[i].methods.balanceOf(default_account).call();
if(Math.abs(balance/amount-1) < 0.005) {
amounts[i] = cBN(balance).toFixed(0,1);
}
else {
amounts[i] = cBN(Math.floor(amounts[i] / c_rates[i]).toString()).toFixed(0,1); // -> c-tokens
}
}
await ensure_allowance();
var token_amount = 0;
if(parseInt(await swap_token.methods.totalSupply().call()) > 0) {
token_amount = await swap.methods.calc_token_amount(amounts, true).call();
token_amount = cBN(Math.floor(token_amount * 0.99).toString()).toFixed(0,1);
}
await swap.methods.add_liquidity(amounts, token_amount).send({
from: default_account,
gas: 1300000});
await handle_sync_balances();
update_fee_info();
}
async function init_ui() {
let infapproval = true;
for (let i = 0; i < N_COINS; i++) {
var default_account = (await web3provider.eth.getAccounts())[0];
if (cBN(await coins[i].methods.allowance(default_account, swap_address).call()).lte(max_allowance.div(cBN(2)))) {
infapproval = false;
}
$('#currency_' + i).on('input', debounced(100, async function() {
await calc_slippage(true)
var el = $('#currency_' + i);
if (el.val() > wallet_balances[i] * c_rates[i])
el.css('background-color', 'gray')
else
el.css('background-color', 'gray');
if (sync_balances) {
for (let j = 0; j < N_COINS; j++)
if (j != i) {
var el_j = $('#currency_' + j);
if (balances[i] * c_rates[i] > 1) {
// proportional
var newval = $('#currency_'+i).val() / c_rates[i] * balances[j] / balances[i];
newval = Math.floor(newval * c_rates[j] * 100) / 100;
el_j.val(newval);
} else {
// same value as we type
var newval = $('#currency_'+i).val();
el_j.val(newval);
}
// Balance not enough highlight
if (newval > wallet_balances[j] * c_rates[j])
el_j.css('background-color', 'gray')
else
el_j.css('background-color', 'blue');
}
}
}));
}
$('#sync-balances').change(handle_sync_balances);
$('#max-balances').change(handle_sync_balances);
$("#add-liquidity").click(handle_add_liquidity);
}
window.addEventListener('DOMContentLoaded', async () => {
try {
$("#max-balances").prop('disabled', true);
$("#add-liquidity").prop('disabled', true);
await init();
update_fee_info();
await handle_sync_balances();
await calc_slippage(true);
await init_ui();
$("#max-balances").prop('disabled', false);
$("#add-liquidity").prop('disabled', false);
}
catch(err) {
console.error(err)
if(err.reason == 'cancelDialog') {
const web3 = new newWeb3(infura_url);
window.web3provider = web3;
window.web3 = web3
await init_contracts();
update_fee_info();
await handle_sync_balances();
await calc_slippage(true);
await init_ui();
}
}
});