Created
September 15, 2016 22:26
-
-
Save shayanb/2ed8d948a78cdcdf0f33eb02ed7684bc to your computer and use it in GitHub Desktop.
simple NodeJS app to display triggered events on a smart contract
This file contains 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
// This is a simple NodeJS app to display triggered events on a smart contract | |
// you need your contract ABI and deployed address and also a synced geth running | |
// github.com/shayanb | |
var optionsABI = [YOUR_CONTRACT_ABI] | |
var contractAddress = "0xYOUR_CONTRACT_ADDRESS" | |
var Web3 = require('web3'); | |
if (typeof web3 !== 'undefined') { | |
web3 = new Web3(web3.currentProvider); | |
} else { | |
// set the provider you want from Web3.providers | |
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); | |
} | |
console.log("Eth Node Version: ", web3.version.node); | |
//console.log("Network: " ,web3.version.network, web3.version.ethereum); | |
console.log("Connected: ", web3.isConnected(), web3.currentProvider); | |
console.log("syncing: ", web3.eth.syncing, ", Latest Block: ",web3.eth.blockNumber); | |
console.log("Accounts[0]: " , web3.eth.accounts[0], ":",web3.eth.getBalance(web3.eth.accounts[0]).toNumber()) | |
OptionsContract = initContract(optionsABI, contractAddress) | |
function initContract(contractAbi, contractAddress) { | |
var MyContract = web3.eth.contract(contractAbi); | |
var contractInstance = MyContract.at(contractAddress); | |
var event = contractInstance.allEvents() | |
console.log("listening for events on ", contractAddress) | |
// watch for changes | |
event.watch(function(error, result){ //This is where events can trigger changes in UI | |
if (!error) | |
console.log(result); | |
}); | |
return contractInstance | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
event.watch function is not a function. It is changes in the newer versions. Any update here how to solve this?