-
Notifications
You must be signed in to change notification settings - Fork 3
/
ListenToEventsTruffeAnyWeb3.js
44 lines (40 loc) · 1.46 KB
/
ListenToEventsTruffeAnyWeb3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const Web3 = require('web3') // Web3 0.20.4 or web3 1 beta
const truffleContract = require("truffle-contract")
const contractArtifact = require('./build/contracts/TutorialToken.json')
const providerUrl = 'http://localhost:8545'
const provider = new Web3.providers.HttpProvider(providerUrl)
const contract = truffleContract(contractArtifact)
contract.setProvider(provider)
// dirty hack for [email protected] support for localhost testrpc, see https://github.com/trufflesuite/truffle-contract/issues/56#issuecomment-331084530
if (typeof contract.currentProvider.sendAsync !== "function") {
contract.currentProvider.sendAsync = function() {
return contract.currentProvider.send.apply(
contract.currentProvider,
arguments
);
};
}
contract.deployed()
.then(contractInstance => {
const event = contractInstance.Transfer(null, {fromBlock: 0}, (err, res) => {
if(err) {
throw Error(err)
}
})
event.watch(function(error, result){
if (error) { return console.log(error) }
if (!error) {
// DO ALL YOUR WORK HERE!
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})----`)
}
})
})
.catch(e => {
console.error('Catastrophic Error!')
console.error(e)
})