第3课

Building a Simple Oracle-Integrated Contract

Now that we have our Remix IDE set up and the necessary Chainlink libraries imported, we're going to craft a basic smart contract that integrates with an oracle. This will allow us to fetch and handle external data.

Drafting the Contract: Basics of Oracle Integration

  1. Starting with the Basics:
    Let’s begin by defining our contract, specifying the Solidity version, and importing the Chainlink library we’ll use:
    ```
    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. 
In this segment, we've specified that our contract will use a Chainlink Price Feed. The constructor takes an address for the price feed contract on the Ethereum network.

1. 
Fetching Data from the Oracle

1. 
Let's extend our contract to fetch the latest Ethereum price:

Solidity
function getLatestEthPrice() public view returns (int) {
(,int price,,,) = priceFeed.latestRoundData();
return price;
}


1. 
The `latestRoundData()` function from the Chainlink Aggregator interface gives us various data, including the most recent price.

## Handling Oracle Responses: Managing Data Once It's Received

Data fetched from oracles often come in raw formats that might not be immediately suitable for our needs. It's vital to process this data correctly within our smart contracts:



1. 
Formatting Data

1. 
Let's say the oracle returns Ethereum's price in USD but multiplied by 10^8 to ensure there are no decimals (common in oracle setups). To get the actual price, you'd need to format the data:

Solidity
function getFormattedEthPrice() public view returns (int) {
int rawPrice = getLatestEthPrice();
return rawPrice / 10**8;
}


1. 
This function fetches the raw price, then divides it by 10^8 to get the real-world value.

1. 
Error Handling

1. 
Always account for the possibility of the oracle failing to fetch data:

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. 
Here, the `latestRoundData()` function also provides a timestamp. If the timestamp is 0, it likely means the oracle failed to retrieve data, and we handle that with a `require` statement.
Your full code should look like this:

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;
}

}
```

By the end of this lesson, you should have a basic oracle-integrated smart contract drafted within Remix. This contract fetches the latest Ethereum price and handles the returned data. In our next lessons, we’ll deploy this contract and further delve into best practices and nuances.

免责声明
* 投资有风险,入市须谨慎。本课程不作为投资理财建议。
* 本课程由入驻Gate Learn的作者创作,观点仅代表作者本人,绝不代表Gate Learn赞同其观点或证实其描述。
目录
第3课

Building a Simple Oracle-Integrated Contract

Now that we have our Remix IDE set up and the necessary Chainlink libraries imported, we're going to craft a basic smart contract that integrates with an oracle. This will allow us to fetch and handle external data.

Drafting the Contract: Basics of Oracle Integration

  1. Starting with the Basics:
    Let’s begin by defining our contract, specifying the Solidity version, and importing the Chainlink library we’ll use:
    ```
    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. 
In this segment, we've specified that our contract will use a Chainlink Price Feed. The constructor takes an address for the price feed contract on the Ethereum network.

1. 
Fetching Data from the Oracle

1. 
Let's extend our contract to fetch the latest Ethereum price:

Solidity
function getLatestEthPrice() public view returns (int) {
(,int price,,,) = priceFeed.latestRoundData();
return price;
}


1. 
The `latestRoundData()` function from the Chainlink Aggregator interface gives us various data, including the most recent price.

## Handling Oracle Responses: Managing Data Once It's Received

Data fetched from oracles often come in raw formats that might not be immediately suitable for our needs. It's vital to process this data correctly within our smart contracts:



1. 
Formatting Data

1. 
Let's say the oracle returns Ethereum's price in USD but multiplied by 10^8 to ensure there are no decimals (common in oracle setups). To get the actual price, you'd need to format the data:

Solidity
function getFormattedEthPrice() public view returns (int) {
int rawPrice = getLatestEthPrice();
return rawPrice / 10**8;
}


1. 
This function fetches the raw price, then divides it by 10^8 to get the real-world value.

1. 
Error Handling

1. 
Always account for the possibility of the oracle failing to fetch data:

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. 
Here, the `latestRoundData()` function also provides a timestamp. If the timestamp is 0, it likely means the oracle failed to retrieve data, and we handle that with a `require` statement.
Your full code should look like this:

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;
}

}
```

By the end of this lesson, you should have a basic oracle-integrated smart contract drafted within Remix. This contract fetches the latest Ethereum price and handles the returned data. In our next lessons, we’ll deploy this contract and further delve into best practices and nuances.

免责声明
* 投资有风险,入市须谨慎。本课程不作为投资理财建议。
* 本课程由入驻Gate Learn的作者创作,观点仅代表作者本人,绝不代表Gate Learn赞同其观点或证实其描述。