HomeGuidesAPI ReferenceChangelog
GuidesAPI ReferenceCommunityDiscordBlogFAQBug BountyAnnouncementsChange Log
API Reference

method.watch()

Use watch to listen for events emitted by a smart contract method. You can define functions to be executed when certain events are caught.

Usage

let contract = await tronWeb.contract.at('contractAddress'); 
contract.eventMethod().watch((err, event) => {
	if (err){
    return console.error('Error with "method" event:', err);
  }
  if (event) { 
      // some function
  }
});

Parameters
No need to pass parameters

Returns
Object

Example

//Example 1
async function triggercontract(){
    try {
        let instance = await tronWeb.contract().at('TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK');
      
        instance.Transfer().watch((err, eventResult) => {
            if (err) {
                return console.error('Error with "method" event:', err);
            }
            if (eventResult) { 
                console.log('eventResult:',eventResult);
            }
          });

        let res = await instance.transfer('TWbcHNCYzqAGbrQteKnseKJdxfzBHyTfuh',500).send({
            feeLimit:100_000_000,
            callValue:0,
            shouldPollResponse:true
        });
        console.log(res);

    } catch (error) {
        console.log(error);
    }
}
triggercontract();

//Example 2
async function triggercontract(){
    try {
        let instance = await tronWeb.contract().at('TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK');
      
        instance["Transfer"]().watch((err, eventResult) => {
            if (err) {
                return console.error('Error with "method" event:', err);
            }
            if (eventResult) { 
                console.log('eventResult:',eventResult);
            }
          });

        let res = await instance.transfer('TWbcHNCYzqAGbrQteKnseKJdxfzBHyTfuh',500).send({
            feeLimit:100_000_000,
            callValue:0,
            shouldPollResponse:true
        });
        console.log(res);

    } catch (error) {
        console.log(error);
    }
}
triggercontract();
ParameterDescription
errError
eventEvent Name emitted from the Smart Contract.

Filters

You can set filters in the contract.method().watch() function to only listen for events that produce specific, desired output values. Set them like:

contract.method().watch({filters: {"_filter1": "value1", "_filter2": "value2"}}, 
(err, res) => {})

Below is an example of how to implement filters:

//Filter_address is an event with 4112e621d5577311998708f4d7b9f71f86dae138b55
let watchTest = contract.method().watch({filters: {"_address": "0x12e621d5577311998708f4d7b9f71f86dae138b55"}}, (err, res) => {
  if(res) { 
    assert.equal(res.result._sender, accounts.hex[3])
    assert.equal(res.result._receiver, accounts.hex[4])
    assert.equal(res.result._amount, 4000)
    
    watchTest.stop()
  }
})

In the above example, the developer is only listening for events that produce an "_address" value of "0x12e621d5577311998708f4d7b9f71f86dae138b5", If it is an address type, need to replace 41 with 0x and convert hex format address to lower case .