#feed #data #price #response #yggdrasil #contract

bin+lib price-feeds

yggdrasil 数据推送交互包

1 个不稳定版本

0.1.1 2024 年 8 月 3 日
0.1.0 2024 年 8 月 3 日

#9 in #yggdrasil

Download history 210/week @ 2024-08-02 7/week @ 2024-08-09

每月下载 217

MIT 许可证

120KB
351

Cosmwasm 数据推送集成指南

简介

本指南解释了如何从 Cosmos 链上的 Yggdrasil 数据推送中读取数据。它涵盖了获取数据推送地址、请求数据推送以及使用回调消息处理响应。适用于初学者和经验丰富的开发者。

获取数据推送地址

在文档的相关部分找到地址及其相应的合约地址。

在智能合约中使用数据推送包

将以下行代码添加到您的合约的 Cargo.toml 文件中

price-feeds = "0.1.0"

请求数据推送

请求单个数据推送

请求单个价格推送的示例代码

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"))
}

处理价格推送响应

处理单个价格推送响应

处理单个价格推送响应的示例代码

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()))
}

请求数据推送

请求单个数据推送

请求单个价格推送的示例代码

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"))
}

处理价格推送响应

处理单个价格推送响应

处理单个价格推送响应的示例代码

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.

依赖项

~4–7.5MB
~152K SLoC