How to get notified when an event triggers on ethereum smart contract?

0 votes

When someone makes a transfer from my account, an event gets trigger name

event Transfer(address indexed from, address indexed to, uint to)

Now I want to get notified when this event occurs on smart contract. I tried different things like filter, watch, subscription and etc. But nothing works as per need.

I also have another query What does filter, subscribe, and watch exactly do. I am always getting confused between these terms. Can someone give a clear idea?

Jun 8, 2018 in Blockchain by charlie_brown
• 7,710 points
210 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Here's a simple example for web3js 1.0.0.beta*:

function handler (event) {
   console.log(event.returnValues);
}

function errorCallback (err) {
   console.error(err);
}

let subscription = contractObj.events.TestEvent().subscription;
subscription.on('data', handler).on('error', errorCallback);

To unsubscribe:

subscription.unsubscribe(function (result) {
   console.log(result)
});

Example of usage in class:

class Listener {
  constructor(event, handler) {
    this.subscription = event;
    this.subscription.on('data', handler).on('error', this.errorCallback);
  }

  errorCallback(err) {
    console.log(err);
  }
}


class Test {
  constructor(contractObj) {
    this.contractObj = contractObj;
    this.createListener();
  }

  createListener() {
    let self = this;
    this.listener = new Listener(this.contractObj.events.TestEvent(), function (event) {
      self.returnValues = event.returnValues;
    });
  }
}
answered Jun 8, 2018 by ariaholic
• 7,320 points

Related Questions In Blockchain

+1 vote
1 answer

How to deploy ethereum smart contracts on a website?

There are many ways to do this: 1 ...READ MORE

answered Mar 26, 2018 in Blockchain by Johnathon
• 9,050 points
58 views
+1 vote
4 answers

How to estimate the cost for deploying smart contract on mainnet?

Since you have already deployed the contract ...READ MORE

answered Apr 9, 2018 in Blockchain by Shashank
• 10,330 points
961 views
0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 16,450 points
29 views
0 votes
1 answer

Error in sending value to an Ethereum Smart Contract.

It is stated in Solidity 4.0 documentation ...READ MORE

answered Jul 23, 2018 in Blockchain by Perry
• 16,450 points
30 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
64 views
0 votes
1 answer
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
92 views
0 votes
1 answer

How to prevent the smart contract from being modified and deployed in the blockchain network?

To expand on Matthew's answer, each state ...READ MORE

answered Jul 5, 2018 in Blockchain by ariaholic
• 7,320 points
31 views
0 votes
1 answer

how to modify smart contract template for self defined functionality

TL;DR; This code simply defines the start ...READ MORE

answered Aug 1, 2018 in Blockchain by ariaholic
• 7,320 points
25 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.