-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.js
169 lines (133 loc) · 5.32 KB
/
token.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
$(document).ready(function () {
var addr = getParam('a');
var contractAddr = getParam('c');
checkNetwork();
if (!$.isEmptyObject(addr) && !$.isEmptyObject(contractAddr)) {
$('#searchAddr').val(addr);
$('#searchContractAddr').val(contractAddr);
getToken();
}
});
$(document).on('click','.btn-go',function(){
getToken();
});
function getToken() {
$('.data-info').empty();
positionFooter();
var addr = $('#searchAddr').val();
var contractAddr = $('#searchContractAddr').val();
var selectedNetwork = getSelectedMainnet();
var selectedTestNetwork = getSelectedTestnet();
if (selectedNetwork.length === 0 && selectedTestNetwork === 0){
alert('Please select network');
return;
} else if ((addr.length === 0) || (contractAddr.length === 0)){
return;
} else if (validateHash(40, addr) === false){
if (addr.length === 40 ){
addr = '0x' + addr;
if (validateHash(40, '0x' + addr) == false) {
alert('Invalid Address');
return;
}
} else {
alert('Invalid Address');
return;
}
} else if (validateHash(40, contractAddr) === false){
if (contractAddr.length === 40 ){
contractAddr = '0x' + contractAddr;
if (validateHash(40, '0x' + contractAddr) == false) {
alert('Invalid Contract Address');
return;
}
} else {
alert('Invalid Contract Address');
return;
}
}
$('.loader').show();
var count = 0;
var totalSelectedNetwork = selectedNetwork.length + selectedTestNetwork.length;
$.each(selectedNetwork, function (key, value) {
callMainnetNetwork('',addr, contractAddr, value, 3)
.then(function (data) {
if (data.error) {
generateTxErr(data.error, value);
} else {
getEtherPrice(data.symbol, 'ETH')
.then(function (ethPrice) {
generateTokenInfo(data, value, ethPrice, contractAddr);
});
}
count++;
if (count == totalSelectedNetwork) {
$('.loader').hide();
$('.datasource').show();
positionFooter();
}
}, function(err){
generateTxErr(err.statusText, value);
count++;
if (count == totalSelectedNetwork) {
$('.loader').hide();
$('.datasource').show();
positionFooter();
}
});
});
$('.loader').show();
$.each(selectedTestNetwork, function (key, value) {
callTestnetNetwork('',addr, contractAddr, value,3)
.then(function (data) {
if (data.error) {
generateTxErr(data.error, value);
} else {
getEtherPrice(data.symbol, 'ETH')
.then(function (ethPrice) {
generateTokenInfo(data, value, ethPrice, contractAddr);
});
}
count++;
if (count == totalSelectedNetwork) {
$('.loader').hide();
$('.datasource').show();
positionFooter();
}
}, function(err){
generateTxErr(err.statusText, value);
count++;
if (count == totalSelectedNetwork) {
$('.loader').hide();
$('.datasource').show();
positionFooter();
}
});
});
}
function generateTokenInfo(data, network, ethPrice, contractAddr) {
try {
var header = '<div class="card mt-3"> <div class="card-body"> <h5 class="card-title">{{network}}</h5>';
var output = '';
if (data.result == null) {
eader += '<div class="row"><div class="col-sm-12">Token Balance not found</div></div>';
output = header.replace('{{network}}', network);
} else {
var lbl = '<div class="row mb-1"><div class="col-sm-2">{{label}}:</div><div class="col-sm-9">{{value}}</div></div>';
output = header.replace('{{network}}', network);
var url = 'etherscan.io';
if (network.indexOf('sepolia') > -1)
url = 'sepolia.' + url;
else if (network.indexOf('goerli') > -1)
url = 'goerli.' + url;
var balance = convertDecimals(parseInt(data.decimal), new BigNumber(data.result));
var value = ethPrice.ETH ? new BigNumber(balance * ethPrice.ETH) + ' ETH <font size="1">(@' + ethPrice.ETH + '/Eth)</font>' : '0.00000000';
output += lbl.replace('{{label}}', 'Balance').replace('{{value}}', balance + ' <a href="https://' + url + '/token/' + contractAddr + '" rel="nofollow">' + data.symbol + '</a>');
output += lbl.replace("{{label}}", 'Value').replace('{{value}}', value);
output += lbl.replace("{{label}}", 'Decimal').replace('{{value}}', data.decimal);
}
$('.data-info').append(output);
} catch (err) {
generateTxErr(err, network);
}
}