Last active
August 21, 2018 19:21
-
-
Save gakonst/e351f8b8aad2c8a13b96d43a98e4de9a to your computer and use it in GitHub Desktop.
Examples of event logging in web3.js. Take note that when used with testrpc/ganache, there are various bugs where events don't get fired or get fired twice. More here: https://github.com/trufflesuite/ganache-cli/issues/338. I also prefer using async/await instead of callbacks.
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
pragma solidity ^0.4.18; | |
contract EventTest { | |
event LogEventTest(uint _x); | |
event LogOtherEventTest(address indexed _sender); | |
function emitEvent(uint x) public { | |
LogEventTest(x); | |
} | |
function emitOtherEvent() public { | |
LogOtherEventTest(msg.sender); | |
} | |
} |
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
var EventTest = artifacts.require("./EventTest.sol"); | |
contract('EventTest', function(accounts) { | |
let eventTest; | |
let eventHandler; | |
let otherEventHandler; | |
// This gets run once in the beginning of the test. | |
// Typical practice in ICO tests is to have `beforeEach` instead of `before` | |
// so that the contract is reinstantiated at the beginning of every test. | |
before('Deploy contract', async function() { | |
console.log('deploying...') | |
eventTest = await EventTest.new() | |
}); | |
// Calls the `emitEvent` function 3 times. We expect to see the events logged one by one in our terminal. | |
it("will watch for events", async function() { | |
// Initialize event that will monitor `LogEventTest` events from block 0 to the latest block | |
eventHandler = eventTest.LogEventTest({fromBlock: 0, toBlock: 'latest'}); | |
// Starts watching asynchronously for `LogEventTest` events | |
eventHandler.watch((error, log) => { | |
// Do whatever you want | |
if (!error) { | |
console.log('Watched Log:', log.args); | |
} | |
}); | |
await eventTest.emitEvent(42); | |
await eventTest.emitEvent(31337); | |
await eventTest.emitEvent(1337); | |
// Should stop watching (listening) for more events when you are done. | |
// eventHandler.stopWatching(); | |
}); | |
// Get all past events from a specific event | |
it('emits 2 other events and fetch them from blockchain history', async function() { | |
await eventTest.emitOtherEvent(); | |
await eventTest.emitOtherEvent({from: accounts[1]}); | |
let otherEventHandler = eventTest.LogOtherEventTest({fromBlock: 0, toBlock: 'latest'}); | |
// Instead of watching, eventHandler.get will fetch all `LogOtherEventTest` logs | |
// from the smart contract and return them as a list. | |
otherEventHandler.get((error, logs) => { | |
console.log('Getting all `LogOtherEventTest` type events') | |
if (!error) { | |
logs.forEach(log => console.log(log.args)) | |
} | |
}); | |
}); | |
it('gets all of the contract\'s events', async function() { | |
let contractEvents = eventTest.allEvents({fromBlock: 0, toBlock: 'latest'}); | |
contractEvents.get((error, logs) => { | |
console.log('Getting all contract events') | |
if (!error) { | |
logs.forEach(log => console.log(log.args)) | |
} | |
}); | |
}); | |
}); |
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
[~/eth_dev/EventHandling, 1, master+5, 35s]: truffle test | |
Using network 'development'. | |
Compiling ./contracts/EventTest.sol... | |
Contract: EventTest | |
deploying... | |
Watched Log: { _x: BigNumber { s: 1, e: 1, c: [ 42 ] } } | |
Watched Log: { _x: BigNumber { s: 1, e: 1, c: [ 42 ] } } | |
✓ will watch for events (389ms) | |
Watched Log: { _x: BigNumber { s: 1, e: 4, c: [ 31337 ] } } | |
Watched Log: { _x: BigNumber { s: 1, e: 3, c: [ 1337 ] } } | |
✓ emits 2 other events and fetch them from blockchain history (259ms) | |
✓ gets all of the contract's events | |
3 passing (904ms) | |
Getting all `LogOtherEventTest` type events | |
{ _sender: '0x2b716301dc5db5ad70e4ce673501b53812d16af3' } | |
Getting all contract events | |
{ _x: BigNumber { s: 1, e: 1, c: [ 42 ] } } | |
{ _x: BigNumber { s: 1, e: 4, c: [ 31337 ] } } | |
{ _x: BigNumber { s: 1, e: 3, c: [ 1337 ] } } | |
{ _sender: '0x6a341d7835e4163deb192df32e249ad17743025d' } | |
{ _sender: '0x2b716301dc5db5ad70e4ce673501b53812d16af3' } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx!