Skip to content

Instantly share code, notes, and snippets.

@MrJohnsson77
Forked from shayanb/eventListener.js
Created November 10, 2018 23:39
Show Gist options
  • Save MrJohnsson77/c27d881ab57b4a5a84b89eb76a04fc02 to your computer and use it in GitHub Desktop.
Save MrJohnsson77/c27d881ab57b4a5a84b89eb76a04fc02 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment