
ChainlinkDataFeed
本文最后更新于 2024-04-24,本文发布时间距今超过 90 天, 文章内容可能已经过时。最新内容请以官方内容为准
Chainlink Data Feed
The Chainlink Data Feed is a decentralized oracle network that provides real-time data feeds for a variety of assets. It is designed to be used by smart contracts to fetch data from external sources such as weather, stock prices, and other data sources.
1. Chainlink Data Feed(喂价)
Chainlink Data Feeds 又称喂价,这项服务可以让用户的智能合约以最快的方式获得特定资产标的价格,不论你使用的是链上的智能合约和还是链下应用,都可以通过单一请求,从 Chainlink Data Feeds 获得资产的价格数据。
2. Usage
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract DataFeedTask {
AggregatorV3Interface internal linkPriceFeed;
AggregatorV3Interface internal btcPriceFeed;
AggregatorV3Interface internal ethPriceFeed;
address public owner;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* 通过 Remix 部署在非本地环境中时
* 通过 https://docs.chain.link/data-feeds/price-feeds/addresses,获得 Aggregator Sepolia 测试网合约地址
*
*/
constructor(address _linkPriceFeed, address _btcPriceFeed, address _ethPriceFeed) {
owner = msg.sender;
// from web
// LINK / USD: 0xc59E3633BAAC79493d908e63626716e204A45EdF
// BTC / USD 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43
// ETH / USD: 0x694AA1769357215DE4FAC081bf1f309aDC325306
linkPriceFeed = AggregatorV3Interface(address(_linkPriceFeed));
btcPriceFeed = AggregatorV3Interface(address(_btcPriceFeed));
ethPriceFeed = AggregatorV3Interface(address(_ethPriceFeed));
}
/**
* 获得 link/usd 的价格数据
*/
function getLinkLatestPrice() public view returns (int256) {
(,int256 answer,,,) = linkPriceFeed.latestRoundData();
return answer;
}
/**
* 获得 btc/usd 的价格数据
*/
function getBtcLatestPrice() public view returns (int256) {
(,int256 answer,,,) = btcPriceFeed.latestRoundData();
return answer;
}
/**
* 获得 eth/usd 的价格数据
*/
function getEthLatestPrice() public view returns (int256) {
(,int256 answer,,,) = ethPriceFeed.latestRoundData();
return answer;
}
/**
* 获取 link/usd, btc/usd, eth/usd 价格
*/
function getLinkPriceFeed() public view returns (AggregatorV3Interface) {
return linkPriceFeed;
}
function getBtcPriceFeed() public view returns (AggregatorV3Interface) {
return btcPriceFeed;
}
function getEthPriceFeed() public view returns (AggregatorV3Interface) {
return ethPriceFeed;
}
}
3. Chainlink Data Feed 相关链接
- 视频教程(中文):讲解了 Chainlink Data Feed 的原理并且进行代码演示。
- Data Feed 官方技术文档:官方技术文档 Data Feed 部分,包括原理讲解和样例合约。
- 视频教程(英文):讲解了 Chainlink Data Feed 的原理。
- Data Feed 应用页面:你可以在这个页面看到 Chainlink 所提供的交易对的具体信息,比如说资产信息,节点运营商信息,网络信息,数据更新规则等等。
- Data Feed 聚合合约地址列表:你的智能合约中需要使用 VRFCoordinator 来集成 Chainlink VRF 的服务。
4. More about Token conversion
- BTC / USD = 10 ** 8
- ETH / USD = 10 ** 8
- LINK / USD = 10 ** 8
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Unic
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果