Ethereum smart contract event trigger notification

0 votes

I am using a Ethereum smart contract which contains a trigger mechanism that is triggered every time a transaction is made. What I want is to get notified whenever the event is triggered. How can I implement a notification mechanism?

The event code is as follows:

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

Aug 1, 2018 in Blockchain by digger
• 26,740 points
1,910 views

1 answer to this question.

0 votes

I have an example here 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);

/////// code to unsubscribe

subscription.unsubscribe(function (result) {

   console.log(result)

});

///// code for usage 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 Aug 1, 2018 by slayer
• 29,350 points

Related Questions In Blockchain

0 votes
2 answers

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

Muchas gracias. ?Como puedo iniciar sesion? READ MORE

answered May 2, 2020 in Blockchain by aqowcmbevs
2,169 views
0 votes
1 answer

Where are the transaction details of a smart contract is written in Ethereum?

First ques:where in the blockchain is a ...READ MORE

answered Jul 11, 2018 in Blockchain by Shashank
• 10,400 points
623 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
• 17,100 points
1,153 views
+1 vote
1 answer
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,692 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,233 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,210 points
1,248 views
0 votes
1 answer

Error when trying to deploy Ethereum smart contract using web3j

Create a new credential based on the Credentials.create ...READ MORE

answered Oct 3, 2018 in Blockchain by slayer
• 29,350 points
1,407 views
0 votes
1 answer

How to store photos in Hyperledger smart contract?

To do this, you need to represent ...READ MORE

answered Jul 9, 2018 in Blockchain by slayer
• 29,350 points
687 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