method.watch()

使用watch监听智能合约方法发出的事件,可以定义捕获某些事件时要执行的方法。

用法

//示例 1
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
  }
});

//示例 2
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
  }
});

参数类型
无需传递参数

返回值类型
Object

示例

//示例 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();

//示例 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();