Skip to content

Instantly share code, notes, and snippets.

@shayanb
Created September 15, 2016 22:26
Show Gist options
  • Save shayanb/2ed8d948a78cdcdf0f33eb02ed7684bc to your computer and use it in GitHub Desktop.
Save shayanb/2ed8d948a78cdcdf0f33eb02ed7684bc to your computer and use it in GitHub Desktop.
simple NodeJS app to display triggered events on a smart contract
// 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
}
@emrem6
Copy link

emrem6 commented Jun 8, 2020

event.watch function is not a function. It is changes in the newer versions. Any update here how to solve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment