Last active
January 29, 2017 01:48
-
-
Save danfinlay/25c6d16d8c941e7dacbfade4c24d190c to your computer and use it in GitHub Desktop.
Simple example of sending a token with ethjs and polling for receipt.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Eth = require('ethjs') | |
var tokenAbi = require('human-standard-token-abi') | |
var tokenAddress = '0x48ff0cbac0acefedf152281ee80e9a0a01d5da63' | |
var secondAddress = '0xC5b8dBAc4c1d3F152cDeb400E2313F309c410aCb' | |
var eth, token | |
window.addEventListener('load', function() { | |
if (typeof window.web3 !== undefined) { | |
eth = new Eth(web3.currentProvider) | |
// Now enjoy! Use it! | |
window.eth = eth | |
startApp() | |
} else { | |
console.log('Better install MetaMask!') | |
} | |
}) | |
function startApp() { | |
const token = eth.contract(tokenAbi).at(tokenAddress) | |
window.token = token | |
console.log('fetching name') | |
token.name().then(function(results) { | |
var name = results[0] | |
console.log('Coin is named ' + name) | |
}) | |
token.totalSupply() | |
.then((results) => { | |
var supply = results[0] | |
console.log(`There are ${supply.toString()} tokens.`) | |
}) | |
.catch(console.error) | |
token.balanceOf(web3.eth.accounts[0]) | |
.then((result) => { | |
var balance = result[0] | |
console.log(`The balance of account ${web3.eth.accounts[0]} is ${balance.toString()}`) | |
}) | |
.catch((error) => { | |
}) | |
sendToken(); | |
} | |
function sendToken() { | |
token.transfer(secondAddress, '10', { from: web3.eth.accounts[0] }) | |
.then((hash) => { | |
console.log('returned!') | |
console.dir(hash) | |
pollForReceipt(hash) | |
}) | |
} | |
function pollForReceipt(hash, freq) { | |
var interval = setInterval(() => { | |
eth.getTransactionReceipt(hash) | |
.then((receipt) => { | |
if (!receipt) return | |
clearInterval(interval) | |
res(receipt) | |
console.log('success!') | |
}) | |
.catch((reason) => { | |
clearInterval(interval) | |
rej(reason) | |
}) | |
}, freq || 1000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment