pragma solidity ^0.8.21;
import “@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol”;
contract OracleIntegratedContract {
AggregatorV3Interface internal priceFeed;
// Visibility for constructor removed
constructor(address _priceFeed) {
priceFeed = AggregatorV3Interface(_priceFeed);
}
1.
在這個部分,我們已經指定我們的合約將使用 Chainlink 價格預言機。構造函數接受以太坊網絡上價格預言機合約的地址。
1.
從預言機穫取數據
1.
讓我們擴展我們的合約以穫取最新的以太坊價格:
Solidity
function getLatestEthPrice() public view returns (int) {
(,int price,,,) = priceFeed.latestRoundData();
return price;
}
1.
Chainlink聚合接口的 `latestRoundData()` 函數爲我們提供了各種數據,包括最新的價格。
## 處理預言機響應:收到數據後管理數據
從預言機穫取的數據通常以原始格式提供,可能不立即適用於我們的需求。在我們的智能合約中正確處理這些數據至關重要:
1.
格式化數據
1.
假設預言機以美元爲單位返回以太坊的價格,但乘以 10^8 以確保沒有小數位(這在預言機設置中很常見)。要穫得實際價格,您需要格式化數據:
Solidity
function getFormattedEthPrice() public view returns (int) {
int rawPrice = getLatestEthPrice();
return rawPrice / 10**8;
}
1.
這個函數穫取原始價格數據,然後除以 10^8 以穫得現實世界的價值。
1.
錯誤處理
1.
總是要考慮預言機穫取數據失敗的可能性:
Solidity
function safeGetLatestEthPrice() public view returns (int) {
(,int price,,uint256 timestamp,) = priceFeed.latestRoundData();
require(timestamp > 0, “Failed to fetch data from the oracle”);
return price;
}
1.
在這裡, `latestRoundData()` 函數還提供了一個時間戳。如果時間戳是 0,那很可能意味著預言機未能穫取數據,我們通過一個`require` 語句來處理這種情況。
你的完整代碼應顯示如下:
Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import “@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol”;
contract OracleIntegratedContract {
AggregatorV3Interface internal priceFeed;
// Visibility for constructor removed
constructor(address _priceFeed) {
priceFeed = AggregatorV3Interface(_priceFeed);
}
function getLatestEthPrice() public view returns (int) {
(,int price,,,) = priceFeed.latestRoundData();
return price;
}
function getFormattedEthPrice() public view returns (int) {
int rawPrice = getLatestEthPrice();
return rawPrice / 10**8;
}
function safeGetLatestEthPrice() public view returns (int) {
(,int price,,uint256 timestamp,) = priceFeed.latestRoundData();
require(timestamp > 0, "Failed to fetch data from the oracle");
return price;
}
}
```
在完成本課程後,您應該能夠在 Remix 中編寫一個簡單的智能合約,該合約可以使用預言機來穫取最新的以太坊價格併處理返回的數據。 在下一節課中,我們將學習如何部署該合約,使用預言機的最佳實踐和其中的細微差別。
pragma solidity ^0.8.21;
import “@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol”;
contract OracleIntegratedContract {
AggregatorV3Interface internal priceFeed;
// Visibility for constructor removed
constructor(address _priceFeed) {
priceFeed = AggregatorV3Interface(_priceFeed);
}
1.
在這個部分,我們已經指定我們的合約將使用 Chainlink 價格預言機。構造函數接受以太坊網絡上價格預言機合約的地址。
1.
從預言機穫取數據
1.
讓我們擴展我們的合約以穫取最新的以太坊價格:
Solidity
function getLatestEthPrice() public view returns (int) {
(,int price,,,) = priceFeed.latestRoundData();
return price;
}
1.
Chainlink聚合接口的 `latestRoundData()` 函數爲我們提供了各種數據,包括最新的價格。
## 處理預言機響應:收到數據後管理數據
從預言機穫取的數據通常以原始格式提供,可能不立即適用於我們的需求。在我們的智能合約中正確處理這些數據至關重要:
1.
格式化數據
1.
假設預言機以美元爲單位返回以太坊的價格,但乘以 10^8 以確保沒有小數位(這在預言機設置中很常見)。要穫得實際價格,您需要格式化數據:
Solidity
function getFormattedEthPrice() public view returns (int) {
int rawPrice = getLatestEthPrice();
return rawPrice / 10**8;
}
1.
這個函數穫取原始價格數據,然後除以 10^8 以穫得現實世界的價值。
1.
錯誤處理
1.
總是要考慮預言機穫取數據失敗的可能性:
Solidity
function safeGetLatestEthPrice() public view returns (int) {
(,int price,,uint256 timestamp,) = priceFeed.latestRoundData();
require(timestamp > 0, “Failed to fetch data from the oracle”);
return price;
}
1.
在這裡, `latestRoundData()` 函數還提供了一個時間戳。如果時間戳是 0,那很可能意味著預言機未能穫取數據,我們通過一個`require` 語句來處理這種情況。
你的完整代碼應顯示如下:
Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import “@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol”;
contract OracleIntegratedContract {
AggregatorV3Interface internal priceFeed;
// Visibility for constructor removed
constructor(address _priceFeed) {
priceFeed = AggregatorV3Interface(_priceFeed);
}
function getLatestEthPrice() public view returns (int) {
(,int price,,,) = priceFeed.latestRoundData();
return price;
}
function getFormattedEthPrice() public view returns (int) {
int rawPrice = getLatestEthPrice();
return rawPrice / 10**8;
}
function safeGetLatestEthPrice() public view returns (int) {
(,int price,,uint256 timestamp,) = priceFeed.latestRoundData();
require(timestamp > 0, "Failed to fetch data from the oracle");
return price;
}
}
```
在完成本課程後,您應該能夠在 Remix 中編寫一個簡單的智能合約,該合約可以使用預言機來穫取最新的以太坊價格併處理返回的數據。 在下一節課中,我們將學習如何部署該合約,使用預言機的最佳實踐和其中的細微差別。