-
Notifications
You must be signed in to change notification settings - Fork 57
/
erc20balance.js
53 lines (48 loc) · 1.96 KB
/
erc20balance.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
window.addEventListener('load', function () {
if (typeof web3 !== 'undefined') {
console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
window.web3 = new Web3(web3.currentProvider);
} else {
console.log('No Web3 Detected... using HTTP Provider')
window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/<APIKEY>"));
}
})
const promisify = (inner) =>
new Promise((resolve, reject) =>
inner((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
})
);
async function getBalance() {
var address, wei, balance
address = document.getElementById("address").value;
wei = promisify(cb => web3.eth.getBalance(address, cb))
try {
balance = web3.fromWei(await wei, 'ether')
document.getElementById("output").innerHTML = balance + " ETH";
} catch (error) {
document.getElementById("output").innerHTML = error;
}
}
async function getERC20Balance() {
var address, contractAddress, contractABI, tokenContract, decimals, balance, name, symbol, adjustedBalance
address = document.getElementById("address").value
contractAddress = document.getElementById("contractAddress").value
contractABI = human_standard_token_abi
tokenContract = web3.eth.contract(contractABI).at(contractAddress)
decimals = promisify(cb => tokenContract.decimals(cb))
balance = promisify(cb => tokenContract.balanceOf(address, cb))
name = promisify(cb => tokenContract.name(cb))
symbol = promisify(cb => tokenContract.symbol(cb))
try {
adjustedBalance = await balance / Math.pow(10, await decimals)
document.getElementById("output2").innerHTML = adjustedBalance;
document.getElementById("output2").innerHTML += " " + await symbol + " (" + await name + ")";
} catch (error) {
document.getElementById("output2").innerHTML = error;
}
}