-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
206 lines (181 loc) · 5.95 KB
/
script.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
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
let coinMb = "ADA";
let coinBinance = "ADABRL";
const methodMb = "ticker";
let urlMb = `https://www.mercadobitcoin.net/api/${coinMb}/${methodMb}/`;
let urlBinance = `https://api.binance.com/api/v3/trades?symbol=${coinBinance}`;
// update binance price
const updateBinance = (response) => {
document.querySelector(".ada-binance").textContent = parseFloat(
response.slice(-1)[0]["price"]
).toFixed(2);
};
// update mb price
const updateMb = (response) => {
document.querySelector(".ada-mb").textContent = parseFloat(
response["ticker"]["last"]
).toFixed(2);
};
const computeArbitrage = (adaPriceBinance, adaPriceMb) => {
let result;
if (adaPriceBinance > adaPriceMb) {
result =
((adaPriceBinance - adaPriceMb) / adaPriceBinance).toFixed(4) + "%";
} else if (adaPriceMb > adaPriceBinance) {
result = ((adaPriceMb - adaPriceBinance) / adaPriceMb).toFixed(4) + "%";
} else if (adaPriceMb === adaPriceBinance) {
result = 0;
}
return result;
};
fetch(urlBinance)
.then((res) => res.json())
.then((data) => updateBinance(data))
.catch((error) => console.log(error));
fetch(urlMb)
.then((res) => res.json())
.then((data) => updateMb(data))
.catch((error) => console.log(error));
document
.querySelector(".ada-binance")
.addEventListener("DOMSubtreeModified", () => {
document.querySelector(".arb-gain").textContent = computeArbitrage(
parseFloat(document.querySelector(".ada-binance").textContent),
parseFloat(document.querySelector(".ada-mb").textContent)
);
document.querySelector(".withdrawl-binance-cost").textContent = (
1 * parseFloat(document.querySelector(".ada-binance").textContent)
).toFixed(2);
document.querySelector(".withdrawl-mb-cost").textContent = (
1 * parseFloat(document.querySelector(".ada-mb").textContent)
).toFixed(2);
document.querySelector(".sell-mb-cost").textContent = (
0.347545 * parseFloat(document.querySelector(".ada-mb").textContent)
).toFixed(2);
// document.querySelector('.sell-binance-cost').textContent = (0.1*parseFloat(document.querySelector('.ada-binance').textContent)).toFixed(2);
});
const computeGain = () => {
let adaPriceBinance = parseFloat(
document.querySelector(".ada-binance").textContent
);
let adaPriceMb = parseFloat(document.querySelector(".ada-mb").textContent);
let investimentoRaw = document.querySelector("#currency-field").value;
investimento = parseInt(
investimentoRaw.split(" ")[1].split(".")[0].replaceAll(",", "")
);
console.log(adaPriceBinance);
console.log(adaPriceMb);
console.log(investimento);
let net;
if (adaPriceBinance > adaPriceMb) {
// buy/sell cost (0.347545 ADA)
let adaQtdMb = (investimento - 0.347545 * adaPriceBinance) / adaPriceMb;
console.log(adaQtdMb);
// adaQtdMb = adaQtdMb-1.3;
// transfer cost
let adaQtdBinance = adaQtdMb - adaPriceMb;
let brlBinance = adaQtdBinance * adaPriceBinance * (1 - 0.001);
net = brlBinance - investimento;
} else if (adaPriceBinance < adaPriceMb) {
let adaQtdBinance = investimento / adaPriceBinance;
// transfer cost
let adaQtdMb = adaQtdBinance - adaPriceBinance;
// buy/sell cost
brlMb = adaQtdMb * adaPriceMb * (1 - 0.001);
net = brlMb - investimento;
} else {
let net = 0;
}
document.querySelector(".result").textContent = net.toFixed(2);
};
// currency format
$("input[data-type='currency']").on({
keyup: function () {
formatCurrency($(this));
},
blur: function () {
formatCurrency($(this), "blur");
},
});
function formatNumber(n) {
// format number 1000000 to 1,234,567
return n.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function formatCurrency(input, blur) {
// appends $ to value, validates decimal side
// and puts cursor back in right position.
// get input value
var input_val = input.val();
// don't validate empty input
if (input_val === "") {
return;
}
// original length
var original_len = input_val.length;
// initial caret position
var caret_pos = input.prop("selectionStart");
// check for decimal
if (input_val.indexOf(".") >= 0) {
// get position of first decimal
// this prevents multiple decimals from
// being entered
var decimal_pos = input_val.indexOf(".");
// split number by decimal point
var left_side = input_val.substring(0, decimal_pos);
var right_side = input_val.substring(decimal_pos);
// add commas to left side of number
left_side = formatNumber(left_side);
// validate right side
right_side = formatNumber(right_side);
// On blur make sure 2 numbers after decimal
if (blur === "blur") {
right_side += "00";
}
// Limit decimal to only 2 digits
right_side = right_side.substring(0, 2);
// join number by .
input_val = "BRL " + left_side + "." + right_side;
} else {
// no decimal entered
// add commas to number
// remove all non-digits
input_val = formatNumber(input_val);
input_val = "BRL " + input_val;
// final formatting
if (blur === "blur") {
input_val += ".00";
}
}
// send updated string to input
input.val(input_val);
// put caret back in the right position
var updated_len = input_val.length;
caret_pos = updated_len - original_len + caret_pos;
input[0].setSelectionRange(caret_pos, caret_pos);
}
// https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript
// function timeConverter(UNIX_timestamp) {
// var a = new Date(UNIX_timestamp * 1000);
// var months = [
// "Jan",
// "Feb",
// "Mar",
// "Apr",
// "May",
// "Jun",
// "Jul",
// "Aug",
// "Sep",
// "Oct",
// "Nov",
// "Dec",
// ];
// var year = a.getFullYear();
// var month = months[a.getMonth()];
// var date = a.getDate();
// var hour = a.getHours();
// var min = a.getMinutes();
// var sec = a.getSeconds();
// var time =
// date + " " + month + " " + year + " " + hour + ":" + min + ":" + sec;
// return time;
// }