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,720 points
2,393 views

2 answers to this question.

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 aryya
• 7,460 points
0 votes
Muchas gracias. ?Como puedo iniciar sesion?
answered May 2, 2020 by aqowcmbevs

Related Questions In Blockchain

0 votes
1 answer

How to swap an Ethereum ERC-20 and Neo NEP5 token with a smart contract?

Each blockchain is its own separated administration. ...READ MORE

answered Apr 12, 2022 in Blockchain by Soham
• 9,710 points
781 views
+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 27, 2018 in Blockchain by Johnathon
• 9,090 points
1,960 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 10, 2018 in Blockchain by Shashank
• 10,400 points
15,590 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
• 17,100 points
715 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
1,927 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,455 views
+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
1,417 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 6, 2018 in Blockchain by aryya
• 7,460 points
809 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 aryya
• 7,460 points
547 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP