Accessing real-world data using the Redstone Oracle
This page will explain how you can access real world / off-chain data using Oracles such as Redstone.
RedStone is a data ecosystem that delivers frequently updated, reliable, and diverse data for your dApp and smart contracts deployed on Lisk.
How to pull oracle data from Redstone
To create a smart contract that directly fetches the latest data from the Redstone oracle, follow this guide.
This guide uses the Redstone Pull model to fetch the data.
For an overview of the different modules that Redstone offers to receive oracle data, go to Oracles > Redstone.
Hardhat is used in this guide to create the smart contract. In case you want to use Foundry, check out the Redstone docs for instructions.
Dependencies
- ethers ^5.7.2
- hardhat ^2.14.0
Install the evm connector
Install the @redstone-finance/evm-connector package.
npm install @redstone-finance/evm-connector
Import the evm connector
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
/**
* Imports the EVM connector
*/
import "@redstone-finance/evm-connector/contracts/data-services/RapidDemoConsumerBase.sol";
contract YourContract is RapidDemoConsumerBase {
// ...
}
Get oracle data
Get the oracle data using the functions provided by the EVM connector.
Get a single value
To get a single price feed, use the function getOracleNumericValueFromTxMsg()
and provide the data feed ID as a parameter.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
/**
* Imports the EVM connector
*/
import "@redstone-finance/evm-connector/contracts/data-services/RapidDemoConsumerBase.sol";
contract YourContract is RapidDemoConsumerBase {
// ...
/**
* Returns the latest price of ETH
*/
function getLatestEthPrice() public view returns (uint256) {
bytes32 dataFeedId = bytes32("ETH");
return getOracleNumericValueFromTxMsg(dataFeedId);
}
}
Get multiple values
To get data from multiple price feeds, use the function getOracleNumericValuesFromTxMsg()
and provide the data feed ID array as a parameter.
/**
* Returns the latest prices of ETH and BTC
*/
function getLatestEthBtcPrices() public view returns (uint256[] memory) {
bytes32[] memory dataFeedIds = new bytes32[](2);
dataFeedIds[0] = bytes32("ETH");
dataFeedIds[1] = bytes32("BTC");
uint256[] memory values = getOracleNumericValuesFromTxMsg(dataFeedIds);
return values;
}
Testing
In order to test the EVM connector related functions in your contract, it is necessary to wrap the contract using the WrapperBuilder
provided by the @redstone-finance/evm-connector
package.
import { expect } from "chai";
import { ethers } from "hardhat";
import { WrapperBuilder } from "@redstone-finance/evm-connector";
describe("YourContract", function () {
describe("Redstone", function () {
it("Get ETH price securely", async function () {
const YourContract = await ethers.getContractFactory("YourContract");
const contract = await YourContract.deploy();
const wrappedContract = WrapperBuilder.wrap(contract).usingDataService({
dataFeeds: ["ETH"],
});
// Interact with the contract (getting oracle value securely)
const ethPriceFromContract = await wrappedContract.getLatestEthPrice();
console.log("Latest ETH price:");
console.log({ ethPriceFromContract });
});
});
});
Now run the test:
npx hardhat test
This should output the latest ETH price in the console:
Latest ETH price:
{ ethPriceFromContract: BigNumber { value: "250255087192" } }
Deploying on Lisk
To deploy the smart contract on Lisk Sepolia or Lisk Mainnet, follow the guides