/
index.js
62 lines (57 loc) · 2.45 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
window.addEventListener('load', function(){
if(typeof web3 === 'undefined'){
web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io'));
// alert('Please Download Metamask')
//disable everything
}else{
contractAddress = '0x22e29edf0979b25d3ec3baaf8b16dd92ea44b004';
abi = [{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]
schruteBucks = web3.eth.contract(abi).at(contractAddress);
// setTimeout(getAddresses, 500); //why do I need this?
// getAddresses() //instead of this? Which returns the array with only first element! WTF
// getAccount()
getBalance()
// web3.version.getNetwork(function(e,r){
// if(r == 1){
// console.log("MAINNET");
// }else{
// alert("Please Connect Metamask to Ethereum Main Net");
// }
// })
}
})
getBalance = function(){
web3.eth.getAccounts(function(e, accounts){
if(accounts && accounts.length > 0){
console.log('ACCOUNT: ', accounts[0])
document.getElementById('coinbase').innerHTML = accounts[0];
// var userAddress = accounts[0];
schruteBucks.balanceOf.call(accounts[0],function(e,balance){
if(balance){
console.log('BALANCE: ', balance)
document.getElementById('balance').innerHTML = balance.toNumber();
}else{
console.log(e)
}
})
}else{
console.log(e)
alert("Please open metamask")
}
})
}
transfer = function(){
var transferAddress = document.getElementById('transfer-address').value;
var transferAmount = document.getElementById('transfer-amount').value;
var fromAddress = document.getElementById('coinbase').innerHTML;
var transferButton = document.getElementById('transfer-button');
console.log(transferAddress, transferAmount, fromAddress);
schruteBucks.transfer(transferAddress, transferAmount, {from: fromAddress}, function(e, response){
if (response) {
console.log(response);
document.getElementById('success-field').innerHTML = "Success"
}else{
console.log(e);
}
})
}