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";
const NFT_ABI = [
"event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)",
];
Web3Function.onRun(async (context: Web3FunctionEventContext) => {
// Get event log from Web3FunctionEventContext
const { log } = context;
// Parse your event from ABI
const nft = new Interface(NFT_ABI);
const event = nft.parseLog(log);
// Handle event data
const { 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: