الدرس رقم 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 中編寫一個簡單的智能合約,該合約可以使用預言機來穫取最新的以太坊價格併處理返回的數據。 在下一節課中,我們將學習如何部署該合約,使用預言機的最佳實踐和其中的細微差別。

إخلاء المسؤولية
* ينطوي الاستثمار في العملات الرقمية على مخاطر كبيرة. فيرجى المتابعة بحذر. ولا تهدف الدورة التدريبية إلى تقديم المشورة الاستثمارية.
* تم إنشاء الدورة التدريبية من قبل المؤلف الذي انضم إلى مركز التعلّم في Gate. ويُرجى العلم أنّ أي رأي يشاركه المؤلف لا يمثّل مركز التعلّم في Gate.
الكتالوج
الدرس رقم 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 中編寫一個簡單的智能合約,該合約可以使用預言機來穫取最新的以太坊價格併處理返回的數據。 在下一節課中,我們將學習如何部署該合約,使用預言機的最佳實踐和其中的細微差別。

إخلاء المسؤولية
* ينطوي الاستثمار في العملات الرقمية على مخاطر كبيرة. فيرجى المتابعة بحذر. ولا تهدف الدورة التدريبية إلى تقديم المشورة الاستثمارية.
* تم إنشاء الدورة التدريبية من قبل المؤلف الذي انضم إلى مركز التعلّم في Gate. ويُرجى العلم أنّ أي رأي يشاركه المؤلف لا يمثّل مركز التعلّم في Gate.
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, Thailand, 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.