Using Solidity SDK

Easily fetch oracle prices from a Solidity smart contract using the SDK.

The Solidity SDK handles binding the oracle interface to the predeploy address under the hood. With this abstracted away, you can simply inherit from the L2PriceOracle smart contract exported by the SDK and call any of the methods from the API Reference.

An example integration of the Solidity SDK can be found in Templates & Examples.

Installation

To add the Solidity SDK to an existing package run the following:

yarn add @gelatonetwork/gelato-native-oracle-sdk
yarn add @pythnetwork/pyth-sdk-solidity

Installing pyth-sdk-solidity is required since the L2PriceOracle smart contract follows the Pyth interface and returns PythStructs.Price objects.

Integration

Reading price feeds from the oracle via the methods documented in the API Reference is as simple as importing and inheriting from the L2PriceOracle smart contract exported by SDK.

Methods exposed by the inherited L2PriceOracle smart contract are prefixed with an underscore for clarity since they are internal. When calling methods from the API Reference, be sure to prefix the method name with an underscore.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import {
    L2PriceOracle
} from "@gelatonetwork/gelato-native-oracle-sdk/contracts/L2PriceOracle.sol";
import {PythStructs} from "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";

contract Example is L2PriceOracle {
    function foo() external {
        PythStructs.Price memory price = _getPriceUnsafe(
            // ETH/USD Price Feed Identifier (see "Supported Price Feeds")
            0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace
        );
    }
    
    function bar() external {
        PythStructs.Price memory price = _getPriceNoOlderThan(
            // ETH/USD Price Feed Identifier (see "Supported Price Feeds")
            0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace,
            // How far the latest price feed update may be in the past
            5 minutes
        );
    }
}

Last updated