CosmWasm
This section explains how to read data from Yggdrasil data feeds on Cosmos chains. We’ll cover how to get the data feed address, request data feeds, and handle the responses using callback messages. T
Getting the Data Feed Address
First, you'll need the Data Feed Address for the Cosmos chains you’re working with. You can find the addresses and their respective contract addresses in the relevant section of this documentation.
Using Data Feed Crate in your smart contracts
For sending requests and handling them users need to import Data Feed crate in their contracts , this enables them to use all the type definitions and messages of the data feed contract in their contracts . To plug in the crate simply add the following line of code in the Cargo.toml
file of your contract .
Requesting the Data Feed or Data Feeds
You have two options for requesting data feeds:
Request a Single Data Feed
Request Multiple Data Feeds
Request a Single Data Feed
To request a single data feed, you can send a request directly to the data feed contract. Each price feed requires a fee in the native token of the blockchain. Below is the example to Request a single price feed
Request Multiple Data Feeds
To request multiple data feeds, use the PriceFeedReq
type, which allows you to request an array of data feeds in one go. Here’s an example of how to request multiple price feeds in a smart contract:
🌳 Note: The request will fail if you define your own PriceFeedReq struct in the contract. It needs to be imported from the data feed crate.
Handling the Price Feed Response
After sending the request to the data feed contract, you need to specify callback functions to handle the responses. There are two types of callback messages:
ReceivePrice: For handling a single price feed response.
ReceivePrices: For handling multiple price feed responses.
Handling a Single Price Feed Response
To handle a single price feed response, use the ReceivePrice
message. Here’s an example:
Handling Multiple Price Feed Responses
To handle multiple price feed responses, use the ReceivePrices
message. Here’s an example:
Conclusion
By following the steps outlined above, you can efficiently request and handle data feeds in your smart contracts across all Cosmos chains. Make sure to use the provided data feed crate to avoid request failures and customize the callback logic to fit your specific needs. This process will enable seamless integration of accurate and reliable price feeds into your decentralized applications, ensuring you have up-to-date data for your operations.
Last updated