-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.html
279 lines (264 loc) · 10.8 KB
/
index.html
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<html>
<head>
<title>Kadena Balance Checker</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css">
<link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/pact-lang-api-global.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
function attachCell(tag, value, parentId, cellId){
var node = document.createElement(tag);
var textnode = document.createTextNode(value);
if (!document.getElementById(cellId)){
node.setAttribute("id", cellId);
node.appendChild(textnode);
document.getElementById(parentId).appendChild(node);
} else{
document.getElementById(cellId).textContent = value;
}
}
var chainIds = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
var chainBal = {}
const creationTime = () => Math.round((new Date).getTime()/1000)-15;
//mkMeta takes in account name, chain id, gas price, gas limit, creation time, time-to-live
const dumMeta = (chainId) => Pact.lang.mkMeta("not-real", chainId, 0.00000001, 6000, creationTime(), 600)
async function getVersion(server){
try {
const res = await fetch(`https://${server}/info`);
const resJSON = await res.json();
const av = resJSON.nodeApiVersion;
const nv = resJSON.nodeVersion;
if (resJSON.nodeChains.length !== 10) {
const bh = resJSON.nodeGraphHistory[0][0]
const len = resJSON.nodeGraphHistory[0][1].length
const cut = await fetch(`https://${server}/chainweb/${av}/${nv}/cut`)
const cutJSON = await cut.json();
const h = cutJSON.height
if (h > bh) {
let cids = Array.from(Array(len).keys());
cids = cids.map(x => x.toString())
chainIds = cids;
} else {
chainIds = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
}
} else {
chainIds = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
}
return {
nv: nv
}
}
catch(e){
console.log(e)
attachCell("h4", "Unable to fetch from " + JSON.stringify(server), "total", `total-value`)
}
}
async function getBalance (host, token, acctName, chainId) {
try {
const response = await Pact.fetch.local({
pactCode: `(${token}.details ${acctName})`,
meta: dumMeta(chainId)
}, host(chainId))
const result = response.result;
let bal = result.data
? (typeof result.data.balance ==="number")
? result.data.balance
: (result.data.balance.decimal ? result.data.balance.decimal : 0)
: 0
chainBal[chainId] = Number(bal);
let totalBal = Object.values(chainBal).reduce((accum, cum) => accum+cum, 0);
const balanceElem = document.getElementById(`chain-${chainId}-balance-data`);
const guardElem = document.getElementById(`chain-${chainId}-guard-data`);
attachCell("h4", `Total Balance of ${acctName}: ${totalBal} KDA`, "total", `total-value`)
if (result.status==="success"){
balanceElem.textContent = bal;
balanceElem.className = "";
try {
const {pred, keys} = result.data.guard
guardElem.textContent = pred + "\r\n" + keys.reduce((accum, key) => JSON.stringify(key)+"\r\n" + accum, "")
guardElem.className = "";
} catch {
//Fetch keyset-ref-guard
if (result.data.guard.keysetref){
try {
const response = await Pact.fetch.local({
pactCode: `(describe-keyset "${result.data.guard.keysetref}")`,
meta: dumMeta(chainId)
}, host(chainId))
const {pred, keys} = response.result.data
guardElem.textContent = pred + "\r\n" + keys.reduce((accum, key) => JSON.stringify(key)+"\r\n" + accum, "")
guardElem.className = "";
} catch {
guardElem.textContent = JSON.stringify(result.data.guard)
guardElem.className = "";
}
} else {
guardElem.textContent = JSON.stringify(result.data.guard)
guardElem.className = "";
}
}
} else if (result.status==="failure" && result.error.message.slice(0,24)==="with-read: row not found"){
balanceElem.textContent = "N/A"
balanceElem.className = "warning";
guardElem.textContent = "Doesn't Exist"
guardElem.className = "warning";
} else {
balanceElem.textContent = "N/A"
balanceElem.className = "error";
guardElem.textContent = "Request Failed"
guardElem.className = "error";
}
} catch(e){
console.log(e)
document.getElementById(`chain-${chainId}-balance-data`).textContent = "N/A"
document.getElementById(`chain-${chainId}-balance-data`).className = "error";
document.getElementById(`chain-${chainId}-guard-data`).textContent = "Request Failed"
document.getElementById(`chain-${chainId}-guard-data`).className = "error";
}
}
window.addEventListener('load', function (event) {
if (localStorage.getItem("version")==="1" && localStorage.getItem("kadena-server")){
document.getElementById("server").value = localStorage.getItem("kadena-server");
} else {
document.getElementById("server").value = "api.chainweb.com"
localStorage.setItem("version","1")
}
if (localStorage.getItem("kadena-token")){
document.getElementById("token").value = localStorage.getItem("kadena-token");
}
if (localStorage.getItem("kadena-account")){
document.getElementById("account").value = localStorage.getItem("kadena-account");
}
let search = window.location.href;
let params = new URLSearchParams(search);
if (params.get("account")) document.getElementById("account").value = params.get("account");
if (params.get("token")) document.getElementById("token").value = params.get("token");
if (params.get("version")) {
if (params.get("version") === "mainnet01") document.getElementById("server").value = "api.chainweb.com";
else if (params.get("version") === "testnet04") document.getElementById("server").value = "api.testnet.chainweb.com";
}
if (params.get("account") && params.get("version")) onClick();
}, false);
document.addEventListener('click', async function (event) {
chainBal = {}
if (!event.target.matches('#balance')) return;
event.preventDefault();
onClick();
}, false);
async function onClick() {
const server = document.getElementById('server').value;
const token = document.getElementById('token').value;
const info = await getVersion(server);
const host = (chainId) => `https://${server}/chainweb/0.0/${info.nv}/chain/${chainId}/pact`;
if (document.getElementById('account').value.length<3 || document.getElementById('account').value.length>256){
document.getElementById("account-field").className = "field error"
document.getElementById("kadena-form").className = "ui form error"
} else{
document.getElementById("account-field").className = "field"
document.getElementById("kadena-form").className = "ui form"
localStorage.setItem("kadena-server", document.getElementById('server').value);
localStorage.setItem("kadena-account", document.getElementById('account').value);
attachCell("tr", "", "chain-table", "chain-header");
attachCell("th", "Chain ID", "chain-header", "chainId-header");
attachCell("th", "Guard", "chain-header", "guard-header");
attachCell("th", "Balance", "chain-header", "balance-header");
const acctName = JSON.stringify(document.getElementById('account').value);
chainIds.forEach(id => {
attachCell("tr", "", "balance-data", `chain-${id}`)
attachCell("td", id, `chain-${id}`, `chain-${id}-data`)
attachCell("td", "fetching...", `chain-${id}`, `chain-${id}-guard-data`)
attachCell("td", "fetching...", `chain-${id}`, `chain-${id}-balance-data`)
getBalance(host,token,acctName,id)
});
}
}
document.addEventListener('click', async function (event) {
chainBal = {}
if (!event.target.matches('#balance')) return;
event.preventDefault();
onClick();
}, false);
</script>
</head>
<body>
<div id="main">
<div class="container">
<h1>Kadena Balance Checker</h1>
<div id="subhead">
<span class="headlink">Balance Checker</span>
<a class="headlink" href="modules.html">Code Viewer</a>
</div>
<form id ="kadena-form" class="ui form">
<div id="node-field" class="field">
<label>Target Chainweb Server</label>
<input type="text" id="server" placeholder="Enter Your Node Server">
</div>
<div id="token-field" class="field">
<label>Token Name</label>
<input type="text" id="token" placeholder="Enter the name of the token contract" value="coin" >
</div>
<div id="account-field" class="field">
<label>Your Account Name</label>
<input type="text" id="account" placeholder="Enter Your Account Name">
<div id="acct-err" class="ui center error message">
<div class="header">Account name is at least 3 characters</div>
</div>
</div>
<button id="balance" class="ui primary button">Check Balance</button>
</form>
<div class="result">
<table class = "ui very basic center collapsing table" id="data-table">
<thead id="chain-table"/>
<tbody id="balance-data"/>
</table>
<div id="total"/>
</div>
</div>
</body>
<style>
.container {
text-align: center;
margin-top: 20px;
}
.result {
margin-top: 20px;
}
.check-button {
margin-top: 15px;
}
.field input {
margin-top: 5px;
width: 300px!important;
}
.ui #acct-err {
margin-top: 5px;
width: 300px;
margin: auto;
font-size: 13px;
}
table#data-table {
margin-left:auto;
margin-right:auto;
}
table#data-table th{
text-align: center;
width: 100px;
}
table#data-table td{
text-align: center;
width: 130px;
}
#subhead {
margin-top: -10px;
margin-bottom: 10px;
}
.headlink {
padding: 1em;
}
span.headlink {
color: lightgrey;
}
</style>
</html>