BLOG — Updates

222 days ago

Unlock Real-Time Event-Driven Web3 Functions Executions

Tl;dr

Never miss an event on the block!

With this NEW Web3 Functions update, developers can easily respond to on-chain events in real time;

  • Gelato's Web3 Functions now enable immediate responses to on-chain events, outperforming the continuous checks required by event polling.
  • Low latency with parallel runs, robust event listeners, quick response times, and efficient resource use by triggering functions upon on-chain event emission.
  • Optimize perpetual trading through real-time alerts, enable off-chain data queries for smart contracts, and provide instant on-chain alerts and notifications.
  • Implementation is streamlined with Web3FunctionEventContext for on-chain event processing, allowing for quick event log parsing and handling.

Introducing Event Triggers

Event-driven Web3 Functions detect occurrences of smart contract events and respond instantly to execute the corresponding actions.

Previously, Web3 Functions using event polling checked at predetermined intervals for on-chain events. While this method had some effectiveness, it was inefficient and often resulted in slow responses. The system kept checking, even if no relevant event was emitted.

With the recent introduction of Event Triggers in Gelato's Web3 Functions, there's been a significant change. Rather than relying on periodic checks, the system is now respond instantly once a specific event is detected on the blockchain.

Let's compare the two approaches:

Key Advantages

High Throughput:

Gelato's Web3 Functions run in parallel mode by default. They can simultaneously listen to multiple events, and each detected event triggers a separate Web3 Function run. Designed for spikes in on-chain activity, they handle up to 15 runs per second.

Robustness:

The event-triggered Web3 Functions come with a robust, built-in event listener. This lets developers concentrate on app development without worrying about missed events, block confirmations, or reorgs.

Low Latency:

With the new approach, Gelato’s Web3 Functions execute immediately upon detecting an event, eliminating the latency previously seen with event polling.

Resource Optimization: Web3 Functions only run when an event occurs, reducing unnecessary runs and optimizing computation costs.

Use Cases

Perpetual Protocol:

Gelato's Web3 Functions with event triggers bring transformative changes to perpetual contract trading. Instant responses to critical events like liquidation alerts and margin maintenance are now possible. Traders enhance their strategies using these triggers for real-time price alerts and capitalizing on arbitrage opportunities.

Dynamic NFTs:

Launch a dynamic NFT with AI-generated art by configuring the Web3 Function to trigger the NFT Mint event. Afterward, call an AI generator and reveal the NFT on-chain.

Request/Response for Off-chain Data:

Query off-chain data for your smart contract by configuring the Web3 Function to trigger the Smart Contract Request event. Then, call an external API and relay the fetched data on-chain.

On-chain Alerts & Notifications:

Monitor specific on-chain actions by setting up your Web3 Functions to trigger desired Smart Contract Events. Send notifications through webhooks to platforms like Slack, Discord, or even email, and integrate with alerting systems like PagerDuty or OpsGenie.

Implementation

Implement Event-driven Web3 Function in 2 easy steps:

1. Changes in Web3 Function Task

To enable event-driven Web3 Functions, replace the traditional Web3FunctionContext with the new Web3FunctionEventContext. This new context includes a log property that holds the full event log, which you can parse and process.

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}`);


  return { canExec: false, message: `Event processed ${log.transactionHash}` };
});

2. Creating the Task

  1. Go to https://beta.app.gelato.network/
  2. Select the "Event Trigger" option.
  3. Input the contract address responsible for emitting the event you want to listen to.
  4. Choose the specific event you wish to monitor from the pre-populated ABI list. You can also use a custom ABI if your contract isn't verified.
  5. Optionally, set your own block confirmations delay or leave it empty for the Web3 Function to trigger as soon as the event occurs.

View Event Logs

Voila! Once you've set up your task, you can monitor its execution by checking the task logs. These logs will display new run records only when your chosen on-chain event is emitted.

About Gelato

Gelato is a Web3 Cloud Platform empowering developers to create automated, gasless, and off-chain-aware Layer 2 chains and smart contracts. Over 400 web3 projects rely on Gelato for years to facilitate millions of transactions in DeFi, NFTs, and gaming.

  • Gelato RaaS: Deploy your own tailor-made ZK or OP L2 chains in a single click with native Account Abstraction and all Gelato middleware baked in.

  • Web3 Functions: Connect your smart contracts to off-chain data & computation by running decentralized cloud functions.

  • Automate: Automate your smart contracts by executing transactions automatically in a reliable, developer-friendly & decentralized manner.

  • Relay: Give your users access to reliable, robust, and scalable gasless transactions via a simple-to-use API.

  • Account Abstraction SDK: Gelato has partnered with Safe, to build a fully-fledged Account Abstraction SDK, combining Gelato's industry's best gasless transaction capabilities, with the industry's most secure smart contract wallet.

Subscribe to our newsletter and turn on your Twitter notifications to get the most recent updates about the Gelato ecosystem! If you are interested in being part of the Gelato team and building the future of the Internet browse the open positions and apply here.