link-horizontalTutorial: Integrating Chainlink Oracles with Streams

Somnia Data Streams provides a powerful, on-chain, and composable storage layer. Chainlink Oraclesarrow-up-right provide secure, reliable, and decentralized external data feeds.

When you combine them, you unlock a powerful new capability: creating historical, queryable, on-chain data streams from real-world data.

Chainlink Price Feeds are designed to provide the latest price of an asset. They are not designed to provide a queryable history. You cannot easily ask a Price Feed, "What was the price of ETH 48 hours ago?"

By integrating Chainlink with Somnia Streams, you can build a "snapshot bot" that reads from Chainlink at regular intervals and appends the price to a Somnia Data Stream. This creates a permanent, verifiable, and historical on-chain feed that any other DApp or user can read and trust.

Objectives & Deliverable

  • Objective: Fetch off-chain data (a price feed) with Chainlink and store it historically via Somnia Streams.

  • Key Takeaway: Combining external "truth" sources with Somnia's composable storage to create new, valuable on-chain data products.

  • Deliverable: A hybrid "Snapshot Bot" that reads from Chainlink on the Sepolia testnet and publishes to a historical price feed on the Somnia Testnet.

What You'll Build

  1. A New Schema: A priceFeedSchema to store price data.

  2. A Chainlink Reader: A script using viem to read the latestRoundData from Chainlink's ETH/USD feed on the Sepolia testnet.

  3. A Snapshot Bot: A script that reads from Chainlink (Sepolia) and writes to Somnia Data Streams (Somnia Testnet).

  4. A History Reader: A script to read our new historical price feed from Somnia Data Streams.

This tutorial demonstrates a true hybrid-chain application.

Prerequisites

  • Node.js 18+ and npm.

  • @somnia-chain/streams, viem, and dotenv installed.

  • A wallet with Somnia Testnet tokens (for publishing) and Sepolia testnet ETH (for gas, though we are only reading, so a public RPC is fine).

Environment Setup

Create a .env file. You will need RPC URLs for both chains and a private key for the Somnia Testnet (to pay for publishing).

Project Setup

Set up your project with viem and the Streams SDK.

1. Chain Configuration

We need to define both chains we are interacting with.

src/lib/chain.ts

2. Client Configuration

We will create two separate clients:

  • A Somnia SDK client (with a wallet) to write data.

  • A Sepolia Public Client (read-only) to read from Chainlink.

src/lib/clients.ts

Step 1: Define the Price Feed Schema

Our schema will store the core data from Chainlink's feed.

src/lib/schema.ts

  • timestamp: The updatedAt time from Chainlink.

  • price: The answer (e.g., ETH price).

  • roundId: The Chainlink round ID, to prevent duplicates.

  • pair: A string to identify the feed (e.g., "ETH/USD").

Let's create a dedicated file to handle fetching data from Chainlink. We will use the ETH/USD feed on Sepolia.

src/lib/chainlinkReader.ts

Step 3: Build the Snapshot Bot (The Hybrid App)

This is the core of our project. This script will:

  1. Fetch the latest price from Chainlink (using our module).

  2. Encode this data using our priceFeedSchema.

  3. Publish the data to Somnia Data Streams.

src/scripts/snapshotBot.ts

To run your bot:

Add a script to package.json: "snapshot": "ts-node src/scripts/snapshotBot.ts"

Run it: npm run snapshot

You can run this script multiple times. It will only add new data if Chainlink's roundId has changed.

Step 4: Read Your Historical Price Feed

Now for the payoff. Let's create a script that reads our new on-chain history from Somnia Streams.

src/scripts/readHistory.ts

To read the history:

Add to package.json: "history": "ts-node src/scripts/readHistory.ts"

Run it: npm run history

Expected Output:

Conclusion: Key Takeaways

You have successfully built a hybrid, cross-chain application.

  • You combined an external "truth source" (Chainlink) with Somnia's composable storage layer (Somnia Data Streams).

  • You created a new, valuable, on-chain data product: a historical, queryable price feed that any dApp on Somnia can now read from and trust.

  • You demonstrated the power of the publisher address as a verifiable source. Any dApp can now consume your feed, knowing it was published by your trusted bot.

This pattern can be extended to any external data source: weather, sports results, IoT data, and more. You can run the snapshotBot.ts script as a cron job or serverless function to create a truly autonomous, on-chain oracle.

Last updated