Using Data Feeds Offchain (Stellar)

Chainlink Data Feeds are the quickest way to access market prices for real-world assets. This guide demonstrates how to read Chainlink Data Feeds on the Stellar Testnet offchain using a Soroban RPC simulation with the Stellar CLI and the Stellar JavaScript SDK. To learn how to use Data Feeds in your onchain Soroban contracts, see the Using Data Feeds Onchain guide.

To get the full list of Chainlink Data Feeds on Stellar, see the Price Feed Contract Addresses page with Stellar selected.

Requirements

Make sure you have the Stellar CLI installed. You can run stellar --version in your terminal to verify if the CLI is correctly installed.

You also need Node.js 18 or higher to run the JavaScript SDK example.

Read a feed with the Stellar CLI

You can read a Chainlink Data Feed offchain without deploying a contract by simulating a call to the proxy contract using the stellar contract invoke command with the --simulate flag. This does not submit a transaction and requires no XLM.

  1. Run the following command, replacing <TESTNET_PROXY_ADDRESS> with the Stellar Testnet proxy contract address and <TESTNET_BTC_USD_DATA_ID> with the data_id for the feed you want to read. The latest_round function takes a decimals argument, which you set to 18 to match the DECIMALS constant used by the data feeds contracts:

    stellar contract invoke \
    --id <TESTNET_PROXY_ADDRESS> \
    --network testnet \
    --simulate \
    -- latest_round \
    --data_id <TESTNET_BTC_USD_DATA_ID> \
    --decimals 18
    

    Expect an output similar to the following:

    [<latest_round_data>]
    

    Where <latest_round_data> is the serialized round data for the feed, including the round_id, answer, and timestamp fields.

Read a feed with the Stellar JavaScript SDK

You can also read a Chainlink Data Feed offchain using the Stellar JavaScript SDK with a Soroban RPC simulation.

  1. Create a new directory for your project and initialize it:

    mkdir stellar-offchain && cd stellar-offchain && npm init -y
    
  2. Install the Stellar JavaScript SDK:

    npm install @stellar/stellar-sdk
    
  3. Create a read-feed.js file with the following contents. This script simulates a call to the proxy contract's latest_round function for a given data_id and decimals:

    const { SorobanRpc, Address, xdr } = require("@stellar/stellar-sdk")
    
    const rpc = new SorobanRpc.Server("https://soroban-testnet.stellar.org")
    const proxy = new Address("<TESTNET_PROXY_ADDRESS>")
    const dataId = "<TESTNET_BTC_USD_DATA_ID>"
    const decimals = 18
    
    async function readLatestRound() {
      const dataIdBytes = xdr.ScVal.scvVec(
        [...Buffer.from(dataId.replace("0x", ""), "hex")].map((b) => xdr.ScVal.scvU32(b))
      )
    
      const result = await rpc.simulateContract({
        contractAddress: proxy,
        functionName: "latest_round",
        args: [dataIdBytes, xdr.ScVal.scvU32(decimals)],
      })
    
      console.log("Latest round:", result)
    }
    
    readLatestRound().catch(console.error)
    
  4. Run the script:

    node read-feed.js
    

    Expect an output that includes the latest round data for the feed, including the answer and timestamp.

What's next

Get the latest Chainlink content straight to your inbox.