Skip to main content

Accessing real-time asset data using Pyth Price Feeds

This tutorial will guide you through the process of creating a smart contract on Base that utilizes Pyth Network oracles to consume a price feed.


Objectives

By the end of this tutorial you should be able to do the following:

  • Set up a smart contract project for Base using Foundry
  • Install the Pyth smart contracts
  • Consume a Pyth Network price feed within your smart contract
  • Deploy and test your smart contracts on Base

Prerequisites

Foundry

This tutorial requires you to have Foundry installed.

  • From the command-line (terminal), run: curl -L https://foundry.paradigm.xyz | bash
  • Then run foundryup, to install the latest (nightly) build of Foundry

For more information, see the Foundry Book installation guide.

Coinbase Wallet

In order to deploy a smart contract, you will first need a wallet. You can create a wallet by downloading the Coinbase Wallet browser extension.

Wallet funds

Deploying contracts to the blockchain requires a gas fee. Therefore, you will need to fund your wallet with ETH to cover those gas fees.

For this tutorial, you will be deploying a contract to the Base Sepolia test network. You can fund your wallet with Base Sepolia ETH using one of the faucets listed on the Base Network Faucets page.


What is Pyth Network?

Pyth Network focuses on ultra-low latency and real-time data, making it suitable for financial applications that require sub-second updates. Pyth's design emphasizes performance, and it is designed to provide data for a range of traditional and DeFi assets.


Creating a project

Before you can begin writing smart contracts for Base and consuming Pyth price feeds, you need to set up your development environment by creating a Foundry project.

To create a new Foundry project, first create a new directory:

mkdir myproject

Then run:

cd myproject
forge init

This will create a Foundry project, which has the following basic layout:

.
├── foundry.toml
├── script
│ └── Counter.s.sol
├── src
│ └── Counter.sol
└── test
└── Counter.t.sol

Installing Pyth smart contracts

To use Pyth price feeds within your project, you need to install Pyth oracle contracts as a project dependency using forge install.

To install Pyth oracle contracts, run:

forge install pyth-network/[email protected] --no-git --no-commit

Once installed, update your foundry.toml file by appending the following line:

remappings = ['@pythnetwork/pyth-sdk-solidity/=lib/pyth-sdk-solidity']

Writing and compiling the Smart Contract

Once your project has been created and dependencies have been installed, you can now start writing a smart contract.

The Solidity code below defines a smart contract named ExampleContract. The code uses the IPyth interface from the Pyth Solidity SDK.

An instance ofIPyth is defined within the contract that provides functions for consuming Pyth price feeds. The constructor for the IPyth interface expects a contract address to be provided. This address provided in the code example below (0xA2aa501b19aff244D90cc15a4Cf739D2725B5729) corresponds to the Pyth contract address for the Base Sepolia testnet.

info

Pyth also supports other EVM networks, such as Base Mainnet. For a list of all network contract addresses, visit the Pyth documentation.

The contract also contains a function named getLatestPrice. This function takes a provided priceUpdateData that is used to get updated price data, and returns the price given a priceId of a price feed. The smart contract provided below uses a priceId of 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace, which corresponds to the price feed for ETH / USD.

info

Pyth provides a number of price feeds. For a list of available price feeds, visit the Pyth documentation.

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

import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";

contract ExampleContract {
IPyth pyth;

/**
* Network: Base Sepolia (testnet)
* Address: 0xA2aa501b19aff244D90cc15a4Cf739D2725B5729
*/
constructor() {
pyth = IPyth(0xA2aa501b19aff244D90cc15a4Cf739D2725B5729);
}

function getLatestPrice(
bytes[] calldata priceUpdateData
) public payable returns (PythStructs.Price memory) {
// Update the prices to the latest available values and pay the required fee for it. The `priceUpdateData` data
// should be retrieved from our off-chain Price Service API using the `pyth-evm-js` package.
// See section "How Pyth Works on EVM Chains" below for more information.
uint fee = pyth.getUpdateFee(priceUpdateData);
pyth.updatePriceFeeds{ value: fee }(priceUpdateData);

bytes32 priceID = 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace;
// Read the current value of priceID, aborting the transaction if the price has not been updated recently.
// Every chain has a default recency threshold which can be retrieved by calling the getValidTimePeriod() function on the contract.
// Please see IPyth.sol for variants of this function that support configurable recency thresholds and other useful features.
return pyth.getPrice(priceID);
}
}

In your project, add the code provided above to a new file named src/ExampleContract.sol and delete the src/Counter.sol contract that was generated with the project (You can also delete the test/Counter.t.sol and script/Counter.s.sol files).

To compile the new smart contract, run:

forge build

Deploying the smart contract

Setting up your wallet as the deployer

Before you can deploy your smart contract to the Base network, you will need to set up a wallet to be used as the deployer.

To do so, you can use the cast wallet import command to import the private key of the wallet into Foundry's securely encrypted keystore:

cast wallet import deployer --interactive

After running the command above, you will be prompted to enter your private key, as well as a password for signing transactions.

caution

For instructions on how to get your private key from Coinbase Wallet, visit the Coinbase Wallet documentation.

It is critical that you do NOT commit this to a public repo.

To confirm that the wallet was imported as the deployer account in your Foundry project, run:

cast wallet list

Setting up environment variables for Base Sepolia

To setup your environment for deploying to the Base network, create an .env file in the home directory of your project, and add the RPC URL for the Base Sepolia testnet:

BASE_SEPOLIA_RPC="https://sepolia.base.org"

Once the .env file has been created, run the following command to load the environment variables in the current command line session:

source .env

Deploying the smart contract to Base Sepolia

With your contract compiled and environment setup, you are ready to deploy the smart contract to the Base Sepolia Testnet!

For deploying a single smart contract using Foundry, you can use the forge create command. The command requires you to specify the smart contract you want to deploy, an RPC URL of the network you want to deploy to, and the account you want to deploy with.

To deploy the ExampleContract smart contract to the Base Sepolia test network, run the following command:

forge create ./src/ExampleContract.sol:ExampleContract --rpc-url $BASE_SEPOLIA_RPC --account deployer

When prompted, enter the password that you set earlier, when you imported your wallet's private key.

info

Your wallet must be funded with ETH on the Base Sepolia Testnet to cover the gas fees associated with the smart contract deployment. Otherwise, the deployment will fail.

To get testnet ETH for Base Sepolia, see the prerequisites.

After running the command above, the contract will be deployed on the Base Sepolia test network. You can view the deployment status and contract by using a block explorer.


Interacting with the Smart Contract

The getLatestPrice(bytes[]) function of the deployed contract takes a priceUpdateData argument that is used to get the latest price. This data can be fetched using the Hermes web service. Hermes allows users to easily query for recent price updates via a REST API. Make a curl request to fetch the priceUpdateData the priceId, 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace:

curl https://hermes.pyth.network/api/latest_vaas?ids[]=0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace

Once you have the priceUpdateData, you can use Foundry’s cast command-line tool to interact with the smart contract and call the getLatestPrice(bytes[]) function to fetch the latest price of ETH.

To call the getLatestPrice(bytes[]) function of the smart contract, run the following command, replacing <DEPLOYED_ADDRESS> with the address of your deployed contract, and <PRICE_UPDATE_DATA> with the priceUpdateData returned by the Hermes endpoint:

cast call <DEPLOYED_ADDRESS> --rpc-url $BASE_SEPOLIA_RPC "getLatestPrice(bytes[])" <PRICE_UPDATE_DATA>

You should receive the latest ETH / USD price in hexadecimal form.


Conclusion

Congratulations! You have successfully deployed and interacted with a smart contract that consumes a Pyth Network oracle to access a real-time price feed on Base.

To learn more about Oracles and using Pyth Network price feeds within your smart contracts on Base, check out the following resources:

We use cookies and similar technologies on our websites to enhance and tailor your experience, analyze our traffic, and for security and marketing. You can choose not to allow some type of cookies by clicking . For more information see our Cookie Policy.