> ## Documentation Index
> Fetch the complete documentation index at: https://seilabs-docs-evm-cookbook.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# RedStone

> Complete guide to integrating RedStone oracles with Sei

RedStone is a modular oracle network. It supplies real-time data feeds to smart contracts through an approach called "data-on-demand". RedStone does not push data to the blockchain constantly. Instead, it delivers signed data packages directly to smart contracts when the transaction executes. This architecture significantly reduces gas costs. Data freshness and reliability stay high.

## What you'll be doing in this guide

This tutorial shows you how to:

1. Integrate RedStone's data feeds into your application on Sei
2. Get real-time price data for the SEI token with RedStone's SDK and data packages
3. Understand RedStone's "data-on-demand" architecture
4. Implement data fetching and verification

By the end of this guide, you will have a working demo. It can fetch SEI price data from RedStone's oracle network with the RedStone data package system.

## Prerequisites

Before you start this tutorial, make sure that you have:

### Technical requirements

* **Solidity knowledge**: A basic understanding of smart contract development in Solidity
* **JavaScript and Node.js**: To fetch data off-chain with the RedStone SDK
* **Development environment**: Remix IDE, Hardhat, or a similar Solidity development setup
* **Sei network access**: An RPC endpoint and access to Sei Mainnet

### Required dependencies

* RedStone SDK (`@redstone-finance/sdk`)
* RedStone EVM Connector (`@redstone-finance/evm-connector`)

#### Install

```bash theme={null}
# npm
npm install @redstone-finance/sdk @redstone-finance/evm-connector

# yarn
yarn add @redstone-finance/sdk @redstone-finance/evm-connector

# pnpm
pnpm add @redstone-finance/sdk @redstone-finance/evm-connector
```

### Sei network configuration

Make sure that your development environment is configured for Sei:

* **Mainnet RPC**: `https://evm-rpc.sei-apis.com`
* **Chain ID**: 1329 (Sei Mainnet)

## RedStone architecture overview

RedStone uses a "data-on-demand" model:

1. **Data providers**: Collect data from multiple sources and sign data packages
2. **Data packages**: Signed data structures that contain price information and metadata
3. **Gateway network**: Distributes data packages to consumers
4. **Smart contracts**: Verify signatures and extract data on-chain

## Steps to fetch SEI price data

This JavaScript code fetches SEI price data with RedStone's SDK:

```javascript theme={null}
import { requestDataPackages, getSignersForDataServiceId } from '@redstone-finance/sdk';

async function fetchSEIPrice() {
  try {
    // Fetch data packages from RedStone DDL based on provided configuration
    const dataPackages = await requestDataPackages({
      // For production environment, "redstone-primary-prod" is the standard service
      dataServiceId: 'redstone-primary-prod',

      // Array of tokens to fetch - we're requesting SEI price data
      dataPackagesIds: ['SEI'],

      // Ensure minimum number of signers for each token
      // 'uniqueSignersCount' packages closest to median are returned
      // Throws error if there are fewer signers available
      uniqueSignersCount: 3,

      // (Optional) Wait time for gateway responses in milliseconds
      // Default: 500ms, here we set it to 1000ms for better reliability
      waitForAllGatewaysTimeMs: 1000,

      // (Optional) Filter out packages older than specified time
      // Here we accept packages up to 60 seconds old
      maxTimestampDeviationMS: 60 * 1000,

      // (Optional) Accept packages only from authorized signers
      // This provides additional security against man-in-the-middle attacks
      authorizedSigners: getSignersForDataServiceId('redstone-primary-prod'),

      // (Optional) Don't throw error for missing feeds
      // Useful when requesting multiple tokens where some might be unavailable
      ignoreMissingFeed: true
    });

    // Extract SEI price from the data package
    const seiPrice = dataPackages['SEI'][0].dataPackage.dataPoints[0]?.numericDataPointArgs.value;

    console.log('SEI price:', seiPrice);
    console.log('Full data package:', dataPackages['SEI'][0]);

    return {
      price: seiPrice,
      timestamp: dataPackages['SEI'][0].dataPackage.timestampMilliseconds,
      dataPackage: dataPackages['SEI'][0]
    };
  } catch (error) {
    console.error('Error fetching SEI price:', error);
    throw error;
  }
}

// Usage example
fetchSEIPrice()
  .then((result) => {
    console.log(`SEI Price: $${result.price}`);
    console.log(`Last Updated: ${new Date(result.timestamp)}`);
  })
  .catch((error) => {
    console.error('Failed to fetch price:', error);
  });
```

### Data package structure

A RedStone data package contains:

* **dataPoints**: An array of price data points
* **timestampMilliseconds**: When the data was signed
* **signature**: The cryptographic signature from the data provider
* **dataPackageId**: The identifier of the specific data feed (for example, "SEI")

## Resources

* [RedStone documentation](https://docs.redstone.finance/)
* [RedStone GitHub repository](https://github.com/redstone-finance)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.