Crates.io | price-feeds |
lib.rs | price-feeds |
version | 0.1.1 |
source | src |
created_at | 2024-08-03 19:22:47.730196 |
updated_at | 2024-08-03 19:41:25.787224 |
description | Package for interacting with yggdrasil data feeds |
homepage | |
repository | |
max_upload_size | |
id | 1324506 |
size | 341,312 |
This guide explains how to read data from Yggdrasil data feeds on Cosmos chains. It covers obtaining the data feed address, requesting data feeds, and handling responses using callback messages. Suitable for both beginners and experienced developers.
Find the addresses and their respective contract addresses in the relevant section of the documentation.
Add the following line of code to the Cargo.toml file of your contract:
price-feeds = "0.1.0"
Example code to request a single price feed:
pub fn execute_request_single_price(
deps: DepsMut,
_info: MessageInfo,
pair: String,
) -> Result<Response, ContractError> {
let price_feed_contract = PRICE_FEED_CONTRACT.load(deps.storage)?;
let request_msg = WasmMsg::Execute {
contract_addr: deps.api.addr_validate(&price_feed_contract)?.to_string(),
msg: to_json_binary(&RequestPriceFeed { symbol: pair })?,
funds: vec![Coin {
denom: "uom".to_string(),
amount: Uint128::new(1000),
}],
};
Ok(Response::new()
.add_message(request_msg)
.add_attribute("action", "request_single_price"))
}
Example code to handle a single price feed response:
ExecuteMsg::ReceivePrice { price_response } => execute_receive_price(deps, env, info, price_response),
pub fn execute_receive_price(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
price_response: PriceFeedResponse,
) -> Result<Response, ContractError> {
let price_feed = PriceFeed {
price: price_response.price,
};
PRICE_FEEDS.save(deps.storage, price_response.symbol, &price_feed)?;
Ok(Response::new()
.add_attribute("action", "receive_price")
.add_attribute("price", price_response.price.to_string()))
}
Example code to request a single price feed:
pub fn execute_request_single_price(
deps: DepsMut,
_info: MessageInfo,
pair: String,
) -> Result<Response, ContractError> {
let price_feed_contract = PRICE_FEED_CONTRACT.load(deps.storage)?;
let request_msg = WasmMsg::Execute {
contract_addr: deps.api.addr_validate(&price_feed_contract)?.to_string(),
msg: to_json_binary(&RequestPriceFeed { symbol: pair })?,
funds: vec![Coin {
denom: "uom".to_string(),
amount: Uint128::new(1000),
}],
};
Ok(Response::new()
.add_message(request_msg)
.add_attribute("action", "request_single_price"))
}
Example code to handle a single price feed response:
ExecuteMsg::ReceivePrice { price_response } => execute_receive_price(deps, env, info, price_response),
pub fn execute_receive_price(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
price_response: PriceFeedResponse,
) -> Result<Response, ContractError> {
let price_feed = PriceFeed {
price: price_response.price,
};
PRICE_FEEDS.save(deps.storage, price_response.symbol, &price_feed)?;
Ok(Response::new()
.add_attribute("action", "receive_price")
.add_attribute("price", price_response.price.to_string()))
}
By following the steps outlined above, you can efficiently request and handle data feeds in your smart contracts across all Cosmos chains. Use the provided data feed crate to avoid request failures and customize the callback logic to fit your specific needs. This will enable seamless integration of accurate and reliable price feeds into your decentralized applications, ensuring you have up-to-date data for your operations.