Pelajaran 3

构建一个简单的集成了预言机的合约

现在我们已经设置好了 Remix IDE,并导入了必要的 Chainlink 库,我们将编写一个基础的智能合约,该合约将与预言机集成。这将使我们能够获取和处理外部数据。

草拟智能合约:预言机集成的基础

  1. 从基础开始:
    让我们首先定义我们的合约,指定 Solidity 版本,并导入我们将使用的 Chainlink 代码库:
    ```
    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);
}

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 中编写一个简单的智能合约,该合约可以使用预言机来获取最新的以太坊价格并处理返回的数据。 在下一节课中,我们将学习如何部署该合约,使用预言机的最佳实践和其中的细微差别。

Pernyataan Formal
* Investasi Kripto melibatkan risiko besar. Lanjutkan dengan hati-hati. Kursus ini tidak dimaksudkan sebagai nasihat investasi.
* Kursus ini dibuat oleh penulis yang telah bergabung dengan Gate Learn. Setiap opini yang dibagikan oleh penulis tidak mewakili Gate Learn.
Katalog
Pelajaran 3

构建一个简单的集成了预言机的合约

现在我们已经设置好了 Remix IDE,并导入了必要的 Chainlink 库,我们将编写一个基础的智能合约,该合约将与预言机集成。这将使我们能够获取和处理外部数据。

草拟智能合约:预言机集成的基础

  1. 从基础开始:
    让我们首先定义我们的合约,指定 Solidity 版本,并导入我们将使用的 Chainlink 代码库:
    ```
    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);
}

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 中编写一个简单的智能合约,该合约可以使用预言机来获取最新的以太坊价格并处理返回的数据。 在下一节课中,我们将学习如何部署该合约,使用预言机的最佳实践和其中的细微差别。

Pernyataan Formal
* Investasi Kripto melibatkan risiko besar. Lanjutkan dengan hati-hati. Kursus ini tidak dimaksudkan sebagai nasihat investasi.
* Kursus ini dibuat oleh penulis yang telah bergabung dengan Gate Learn. Setiap opini yang dibagikan oleh penulis tidak mewakili Gate Learn.
It seems that you are attempting to access our services from a Restricted Location where Gate.io is unable to provide services. We apologize for any inconvenience this may cause. Currently, the Restricted Locations include but not limited to: the United States of America, Canada, Cambodia, Cuba, Iran, North Korea and so on. For more information regarding the Restricted Locations, please refer to the User Agreement. Should you have any other questions, please contact our Customer Support Team.