I'm trying to understand how events work. My contract emits an event which is captured by the node (I'm using web3js). I need to extract the submitted parameters (msg.sender, id_, location_). I need these variables to perform other steps.For example I have to make a request to an external data source to know the temperature in that location_. How can I extract them?


emit RequestWeather(msg.sender, id_, location_) ;


myContract.events.RequestWeather({}, function(error, event){ console.log(event); })
.on('data', function(event){
console.log(event); // same results as the optional callback above})
.on('changed', function(event){
// remove event from local database})
.on('error', console.error);
  • do you want to extract values from older events or only from new ones?
    – Peri Kost
    Jan 3 at 19:04
  • @PeriKost new events. but can you tell me how to do it also for old events?
    – Mario Roma
    Jan 3 at 19:11
  • In the options u can give startblock number
    – Majd TL
    Jan 3 at 20:00

Here is an example of both getting new events and old events using web3 contract event subscriptions.

web3.js code

var Web3 = require('web3');
var json = require('./truffle/build/contracts/Contract1.json');
var web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8545'));

const testContract = new web3.eth.Contract(json.abi, "0xEcFcaB0A285d3380E488A39B4BB21e777f8A4EaC");

testContract.getPastEvents('Test', {
    fromBlock: 0,
    toBlock: 'latest'
    events.forEach(event => {
      console.log(`past event - transaction hash: ${event.transactionHash}`)
      console.log(`past event - sender: - ${event.returnValues.sender}`);
      console.log(`past event - id: - ${event.returnValues.id}`);
      console.log(`past event - location: - ${web3.utils.hexToUtf8(event.returnValues.location)}`)

.on('data', function(event){
    console.log(`new event - transaction hash: ${event.transactionHash}`)
    console.log(`new event - sender: ${event.returnValues.sender}`);
    console.log(`new event - id: ${event.returnValues.id}`);
    console.log(`new event - location: ${web3.utils.hexToUtf8(event.returnValues.location)}`);
}).on('error', function(error, receipt) { // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.

Assuming two events have occurred before you start running the program, and one occurs after, the output will look something like this:


past event - transaction hash: 0xa4f29de6d92c80e110705fe7de3563c3dbf8f05f8b8248596eb6d519468838bf
past event - sender: - 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
past event - id: - 1
past event - location: - home
past event - transaction hash: 0xdbd4aad53513b4e822453ad28109effb7f9eec6e01f2413e625827aca583737e
past event - sender: - 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
past event - id: - 2
past event - location: - work
new event - transaction hash: 0xd9e6a2a8d26c80ffc833e2b20b09736a4c123bccff8af46eb792679526b5b557
new event - sender: 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
new event - id: 3
new event - location: school

solidity contract

pragma solidity 0.5.1;

contract Contract1 
  uint256 private id = 0;
  event Test(address sender, uint256 id, bytes32 location);
  function myTestFunction(bytes32 _location) external 
     id = id + 1;
     emit Test(msg.sender, id, _location);
  • so i can do var location = web3.utils.hexToUtf8 (event.returnValues.location) and then use location to make the API request?
    – Mario Roma
    Jan 3 at 20:31
  • Yup, that's correct!
    – Steven V
    Jan 3 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.