Watch Now: Learn more by watching our video Web3 Function Triggers, available on YouTube.
Event Context
For event triggered typescript functions, use the Web3FunctionEventContext instead of the regular Web3FunctionContext on your onRun handler.
The context will then include a log property containing your full event log that you can parse and process.
Event Triggered Typescript Function example
event/index.ts
import { Interface } from"@ethersproject/abi";import { Web3Function, Web3FunctionEventContext } from"@gelatonetwork/web3-functions-sdk";constNFT_ABI= ["event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)",];Web3Function.onRun(async (context:Web3FunctionEventContext) => {// Get event log from Web3FunctionEventContextconst { log } = context;// Parse your event from ABIconstnft=newInterface(NFT_ABI);constevent=nft.parseLog(log);// Handle event dataconst { from,to,tokenId } =event.args;console.log(`Transfer of NFT #${tokenId} from ${from} to ${to} detected`);return { canExec:false, message:`Event processed ${log.transactionHash}` };});
Testing locally
To test your event triggered typescript function, you can add a log.json file in your web3 function directory:
Copy in the log.json file the raw data of the event you want to test: