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();
Parameter | Description |
---|---|
err | Error |
event | Event Name emitted from the Smart Contract. |