Build low latency oracles using Web3 Functions

Gelato Team

Mar 28, 2023

Summary

  • Gelato + Redstone enables modular, customizable, low latency oracles that update prices on-chain every minute.

  • This powers new possibilities for leveraged products like Float, as well as derivative trading, options, lending markets, and more.

Gelato x Redstone

Introduction

Combining Redstone’s on-demand oracles with Gelato’s Web3 Functions unlocks faster DeFi applications and smoother user experiences. Redstone queries multiple data sources and signs the data off-chain, only pushing on-chain when needed. This means lower gas costs, higher refresh rates, and more secure data delivery.

Developers can integrate Gelato Web3 Functions to fetch data from Redstone and push it on-chain based on conditions, merging Redstone’s security with Gelato’s automation and computation layer.

Why is data latency important?

Latency is the delay between updates. In oracles, it measures how quickly new data is written on-chain. Low latency ensures fresh prices and reliable decision-making — essential in fast-moving crypto markets where a few seconds of stale data can lead to losses.

Float Use Case

Float leverages Redstone and Gelato to process transactions 20x faster. Float enables peer-to-peer minting of leveraged tokens that track returns of assets like ETH or BTC without collateral management or liquidation risks.

Behind the scenes, Float maintains stablecoin collateral pools backing these tokens. Gelato Web3 Functions pull validated prices from Redstone oracles and update balances on-chain whenever needed, giving Float faster and more efficient updates — and users a seamless experience.

🎧 Learn more in our Twitter Space with Float and Redstone.

Modern Hybrid Oracles vs Chainlink

Chainlink traditionally uses a request-response model requiring multiple on-chain/off-chain transactions, introducing latency and stale prices. Alternatively, pushing all data on-chain wastes gas and doesn’t scale well.

Pull-based oracles like Redstone offer a hybrid: data is signed off-chain, retrieved on-demand, and pushed on-chain only when needed. Gelato Web3 Functions make this customizable — updating prices based on arbitrary on-chain or off-chain conditions.

Example Web3 Function

Web3Function.onRun(async (context: Web3FunctionContext) => {
  const { userArgs, provider } = context;
  const oracleAddress = userArgs.oracleAddress as string;
  const oracle = new Contract(oracleAddress, ORACLE_ABI, provider);

  // Wrap with Redstone
  const wrappedOracle = WrapperBuilder.wrap(oracle).usingDataService(
    {
      dataServiceId: "redstone-rapid-demo",
      uniqueSignersCount: 1,
      dataFeeds: ["ETH"],
      disablePayloadsDryRun: true,
    },
    ["https://d33trozg86ya9x.cloudfront.net"]
  );

  const decimals = await wrappedOracle.decimals();
  const livePrice: BigNumber = await wrappedOracle.getLivePrice();
  const storedPrice: BigNumber = await wrappedOracle.getStoredPrice();
  console.log(`Live: ${livePrice.toString()}, Stored: ${storedPrice.toString()}`);

  const deviation: BigNumber = await wrappedOracle.getPriceDeviation();
  const deviationPrct = (deviation.toNumber() / 10 ** decimals) * 100;
  if (deviationPrct < 0.2) {
    return { canExec: false, message: "No update: deviation too small" };
  }

  const { data } = await wrappedOracle.populateTransaction.updatePrice();
  return { canExec: true, callData: data };
});

Example Redstone Oracle Contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@redstone-finance/evm-connector/contracts/data-services/RapidDemoConsumerBase.sol";

contract RedstoneOracle is RapidDemoConsumerBase {
    uint8 private immutable _decimals = 8;
    uint256 private _price = 0;

    function decimals() external pure returns (uint8) {
        return _decimals;
    }

    function getStoredPrice() external view returns (uint256) {
        return _price;
    }

    function getLivePrice() public view returns (uint256) {
        return getOracleNumericValueFromTxMsg(bytes32("ETH"));
    }

    function _computeDeviation(uint256 newPrice, uint256 oldPrice) 
        internal pure returns (uint) {
        if (oldPrice == 0) return 1 * 10 ** _decimals;
        if (newPrice > oldPrice) 
            return ((newPrice - oldPrice) * 10 ** _decimals) / oldPrice;
        else 
            return ((oldPrice - newPrice) * 10 ** _decimals) / oldPrice;
    }

    function getPriceDeviation() external view returns (uint) {
        return _computeDeviation(getLivePrice(), _price);
    }

    function updatePrice() public {
        _price = getLivePrice();
    }
}

About Gelato

Gelato is the Web3 Cloud Platform powering automated, gasless, and off-chain-aware smart contracts. Trusted by 400+ projects across DeFi, NFTs, and gaming.

  • Gelato RaaS: Deploy ZK or OP chains with Gelato infra built-in.

  • Web3 Functions: Connect smart contracts to off-chain data & computation.

  • Automate: Execute smart contracts automatically with resolvers & triggers.

  • Relay: Enable gasless transactions for users via Gelato’s API.

  • Account Abstraction SDK: Built with Safe, combining Gelato gasless infra with the most secure wallet.

👉 Follow Gelato on Twitter for updates.
👉 Explore careers at Gelato.

Resources

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.