Tutorial about RedStone

Thanhnhimmo
2 min readNov 23, 2023

--

Using RedStone Oracles for Your Blockchain Applications

RedStone is a decentralized oracle network that provides external data and computations to blockchains and dApps. As a developer, you can leverage RedStone oracles to build secure and reliable applications. This tutorial will guide you through integrating with RedStone oracles.

Prerequisites

  • Understanding of smart contract development (Solidity, Rust, etc)
  • Basic knowledge of JSON RPC APIs
  • Node.js installed to run example code
  1. Set up a RedStone API Client

First install the RedStone JS client via npm:

npm install @redstone-finance/api-client

Then import and initialize the client:

const { ApiClient } = require('@redstone-finance/api-client');
const client = new ApiClient({
// mainnet or testnet url
});
  1. Look Up an Existing Feed

RedStone has existing price feeds for assets like ETH/USD. Look this up:

const feed = await client.getFeed({
network: 'ethereum',
pair: 'ETH/USD'
});
console.log(feed.latestValue) // Displays latest ETH price
  1. Create a Custom Feed

Define a custom feed to get any data you need

const feed = await client.createFeed({
network: 'avalanche',
key: 'custom-data',
source: {
// custom source
},

})
  1. Subscribe to Feed Updates

Subscribe to feed data in your app:

const sub = feed.subscribe(data => {
// New data available
})
// Unsubscribe later
sub.unsubscribe()
  1. Integrate Feeds into Smart Contracts

Call the getFeedData method to use feeds in smart contracts:

function myFunc() external {
uint256 data = RedStoneConsumer.getFeedData(
0x123..., // feed address
"key"
);

}

And that’s it! RedStone makes it simple to start leveraging decentralized data in your dApps. Check out the documentation for more details. Let me know if you have any other questions!

--

--