forked from dapphub/dapptools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
101 lines (91 loc) · 2.65 KB
/
index.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
var app = document.querySelector("#app")
var params = {}
var token
onhashchange = function() {
location.reload()
}
location.hash.replace(/^\#\?/, "").split("&").forEach(function(part) {
var [name, value] = part.split("=")
params[name] = value
})
if (!params.token || !params.account) {
var token = prompt("What is the address of the ERC20 token?")
var decimals = prompt("How many decimal places does the token have?")
var account = prompt("Which account are you interested in?")
location.hash = `?token=${token}&decimals=${decimals}&account=${account}`
}
if (!params.decimals) {
location.hash = `?token=${params.token}&decimals=18&account=${params.account}`
} else if (params.decimals > 20) {
alert("Sorry, but the maximum number of decimal places is 20.")
location.hash = `?token=${params.token}&decimals=20&account=${params.account}`
}
var ERC20 = abi([["balanceOf", ["address"], ["uint"]]])
var loading = setTimeout(function() {
app.innerHTML = `
<div class=note>
<h2>Loading token balance...</h2>
This should not take very long. If this message does not
disappear after a few seconds, something might be wrong.
</div>
`
}, 500)
onload = function() {
if (this.web3) {
load()
} else {
clearTimeout(loading)
app.innerHTML = `
<div class=note>
<h2>No web3 provider found</h2>
Consider installing <a href="https://metamask.io">MetaMask</a>, or
cloning this repository and running an Ethereum client locally.
</div>
`
}
}
function load() {
token = web3.eth.contract(ERC20).at(params.token)
token.balanceOf.call(params.account, hopefully(balance => {
clearTimeout(loading)
document.querySelector("#app").innerHTML = `
<table>
<tr>
<th>Token</th>
<td><code>${params.token}</code></td>
</tr>
<tr>
<th>Decimals</th>
<td><code>${params.decimals}</code></td>
</tr>
<tr>
<th>Account</th>
<td><code>${params.account}</code></td>
</tr>
<tr>
<th>Balance</th>
<td><code>${balance.dividedBy(
web3.toBigNumber(10).toPower(params.decimals)
).toFixed(params.decimals)}</code></td>
</tr>
</table>
`
}))
}
function abi(functions) {
var params = types => types.map((type, i) => ({ name: "x" + i, type }))
return functions.map(([name, inputs=[], outputs=[]]) => ({
name, type: "function",
inputs: params(inputs),
outputs: params(outputs),
}))
}
function hopefully(callback) {
return function(error, result) {
if (error) {
alert(error.message)
} else {
callback(result)
}
}
}