-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
237 lines (190 loc) Β· 7.7 KB
/
main.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
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
let wallet = {
"lt" : "",
"hash" : "",
"api_key" : "b0f5c4749308355b4373e494805e5dfa1b84a71cbec822df8cb6f6886aa080ba"
};
let getTonPrice = coins => coins * 10 ** (-9);
let changeURL = address => history.pushState(null, null, `/ton-searcher/?q=${address}`);
let numberWithSpaces = coins => {
// the function adds spaces to large numbers
if (coins == 0) return;
if (String(coins).length == 1) return coins;
let Spaces = x => x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
let integerSpaces = Spaces(Number(String(coins).split('.')[0]));
let str = String(coins).split('').reverse().join('');
let remainderNumber = x => {
for (i = 0; i < str.length; i++) {
if (str[i] == ".") break;
x.push(str[i])
}
return x;
}
//let remainderNumberReverse = remainderNumber([]).reverse();
//let fractionalPart = '00';
//if (remainderNumberReverse.length >= 2) fractionalPart = `${remainderNumberReverse[0]}${remainderNumberReverse[1]}`;
//else if (remainderNumberReverse.length == 1) fractionalPart = `${remainderNumberReverse[0]}0`;
//return `${integerSpaces},${fractionalPart}`;
return coins = `${integerSpaces},${remainderNumber([]).reverse().join('')}`;
};
let tonPriceUSD = async coins => {
let resp;
try {
let request = await fetch("https://api.coingecko.com/api/v3/simple/price?ids=the-open-network&vs_currencies=USD");
resp = await request.json();
} catch {return tonPriceUSD(coins);}
let money = numberWithSpaces(coins * resp['the-open-network']['usd']);
document.getElementById("wallet-balance-usd").innerHTML = `$${money}`;
};
async function getAddressInfo(address) {
document.getElementById("main-wallet-block").style.display = "none";
let resp;
try {
let request = await fetch(`https://toncenter.com/api/v2/getExtendedAddressInformation?address=${address}`);
if (request.status == 416) {
document.getElementById("error-block").style.display = "block";
document.getElementById("main-wallet-block").style.display = "none";
document.getElementById("main-transactions-block").innerHTML = "";
document.getElementById("main-transactions-block").style.display = "none";
return;
} else {
document.getElementById("error-block").style.display = "none";
document.getElementById("main-wallet-block").style.display = "block";
document.getElementById("main-transactions-block").style.display = "block";
}
resp = await request.json();
} catch {return getAddressInfo(address);}
console.log(resp)
let ton = (resp.result.balance == "-1") ? 0 : getTonPrice(resp.result.balance);
// Checking for a zero balance
if (ton == 0) {
document.getElementById("wallet-balance-usd").innerHTML = 0;
document.getElementById("main-wallet-block").style.display = "none";
document.getElementById("main-transactions-block").style.display = "none";
document.getElementById("no-transactions-block").style.display = "block";
} else {
tonPriceUSD(ton);
let toncoins = numberWithSpaces(ton);
ton = toncoins;
}
wallet['lt'] = resp.result.last_transaction_id['lt'];
wallet['hash'] = resp.result.last_transaction_id['hash'];
let encdURI = encodeURIComponent(wallet['hash']).replace(/['()]/g, escape).replace(/\*/g, '%2A').replace(/%(?:7C|60|5E)/g, unescape);
wallet['hash'] = encdURI;
document.getElementById("wallet-address").innerHTML = resp.result.address.account_address;
document.getElementById("wallet-balance").innerHTML = `${ton} π`;
document.getElementById("main-wallet-block").style.display = "block";
getAddressState(address);
}
async function getAddressState(address) {
let resp;
try {
let request = await fetch(`https://toncenter.com/api/v2/getAddressState?api_key=${wallet['api_key']}&address=${address}`);
resp = await request.json();
} catch {return getAddressState(address);}
document.getElementById("wallet-state").innerHTML = resp.result;
getTransactions(address);
}
async function getTransactions(address) {
let base;
try {
let request = await fetch(`https://toncenter.com/api/v2/getTransactions?api_key=${wallet['api_key']}&address=${address}&limit=100<=${wallet['lt']}&hash=${wallet['hash']}&to_lt=0&archival=true`);
let resp = await request.json();
base = await resp.result;
} catch {return getTransactions(address);}
console.log(address)
console.log(base)
if (base.length == 0) {
document.getElementById("no-transactions-block").style.display = "block";
document.getElementById("main-transactions-block").style.display = "none";
return;
} else document.getElementById("no-transactions-block").style.display = "none";
await transactionBlock(base);
}
function transactionBlock(base) {
let transactionList = document.getElementById("main-transactions-block");
transactionList.innerHTML = "";
transactionList.style.display = "none";
function createTransactionsList(data) {
let mainDiv = document.createElement('div');
data.forEach((item, index) => {
if (index == 0) {
let balanceDiv = document.createElement('div');
if (data[1] == "From") {
balanceDiv.innerHTML = `+${item} π`;
balanceDiv.style.color = "green";
}
else {
balanceDiv.innerHTML = `-${item} π`;
balanceDiv.style.color = "maroon";
}
mainDiv.append(balanceDiv);
} else if (index == 1) {
let fromToDiv = document.createElement('div');
fromToDiv.innerHTML = `<span>${item}</span> - ${data[2]}`;
mainDiv.append(fromToDiv);
} else if (index == 3) {
let fee = document.createElement('div');
fee.innerHTML = `Fee: ${item}`;
mainDiv.append(fee);
} else if (index == 4) {
let time = document.createElement('div');
time.innerHTML = item;
mainDiv.append(time);
}
});
transactionList.append(mainDiv);
}
for (i = 0; i < base.length; i++) {
if (base[i]['out_msgs'].length == 0) {
let coin = getTonPrice(base[i]['in_msg']['value']);
let time = new Date(parseInt(base[i]['utime'] + "000")).toLocaleString();
let adrs = base[i]['in_msg']['source'];
let tfee = `${getTonPrice(base[i]['fee'])} π`;
createTransactionsList([coin, "From", adrs, tfee, time]);
} else {
let coin = getTonPrice(base[i]['out_msgs'][0]['value']);
let time = new Date(parseInt(base[i]['utime'] + "000")).toLocaleString();
let adrs = base[i]['out_msgs'][0]['destination'];
let tfee = `${getTonPrice(base[i]['fee'])} π`;
createTransactionsList([coin, "To", adrs, tfee, time]);
}
}
transactionList.style.display = "block";
}
document.getElementById("main-search-input").addEventListener("focus", (e) => {
document.getElementById("main-search-input").addEventListener("keydown", (e) => start(e));
});
document.getElementById("main-search-input").addEventListener("focus", (e) => {
document.getElementById("main-search-input").removeEventListener("keydown", (e) => start(e));
});
window.onload = () => {
let main = window.location;
if (main.search == "") return;
else {
let url = main.search.split('').splice(3).join('');
if (url.length > 0) {
if (url.length < 48 || url.length > 48) {
document.getElementById("main-wallet-block").style.display = "none";
document.getElementById("main-transactions-block").style.display = "none";
document.getElementById("error-block").style.display = "block";
return;
}
}
document.getElementById("main-search-input").value = url;
changeURL(String(url));
return getAddressInfo(String(url));
}
};
function start(e) {
if (e.keyCode == 13) {
let inpText = document.getElementById("main-search-input").value.trim();
if (inpText == "" || inpText.length < 48 || inpText.length > 48) {
document.getElementById("main-wallet-block").style.display = "none";
document.getElementById("main-transactions-block").style.display = "none";
document.getElementById("error-block").style.display = "block";
return;
}
changeURL(String(inpText));
getAddressInfo(inpText);
}
}