Last active
July 31, 2023 06:53
-
-
Save sogoiii/61e1e7e60a3c162f956610c6f325d534 to your computer and use it in GitHub Desktop.
Listening to web3 events in version 0.20.4 and below
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
const Web3 = require('web3') // Works with web3 0.20.4 | |
const contractArtifact = require('./build/contracts/TutorialToken.json') | |
const web3 = new Web3() | |
const providerUrl = 'http://localhost:8545' | |
const provider = new Web3.providers.HttpProvider(providerUrl) | |
web3.setProvider(provider) | |
const networkId = web3.version.network | |
const contractAddr = contractArtifact.networks[networkId].address | |
const TutorialToken = web3.eth.contract(contractArtifact.abi, contractAddr) | |
const contract = TutorialToken.at(contractAddr) | |
const event = contract.Transfer() | |
event.watch(function(error, result){ | |
if (error) { return console.error(error) } | |
let { args: { from, to, value }, blockNumber } = result | |
console.log(`----BlockNumber (${blockNumber})----`) | |
console.log(`from = ${from}`) | |
console.log(`to = ${to}`) | |
console.log(`value = ${value}`) | |
console.log(`----BlockNumber (${blockNumber})----`) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment