> For the complete documentation index, see [llms.txt](https://qi-blockchain.gitbook.io/qie-oracle/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qi-blockchain.gitbook.io/qie-oracle/how-qie-oracle-works/smart-contract-integration.md).

# 📦 Smart Contract Integration

This section explains how developers can integrate the `AggregatorV3` oracle contract into their own decentralized applications (dApps) to access off-chain or externally-fed data within smart contracts.

Your dApp can consume reliable, on-chain data using our `AggregatorV3` oracle, which is compatible with the standard Chainlink AggregatorV3 interface — making integration simple and familiar for many Web3 developers.

***

### 🔗 Integration Overview

To use the oracle in your dApp:

1. **Import the AggregatorV3Interface**
2. **Set the oracle contract address**
3. **Call the oracle’s functions (`latestRoundData`, `getRoundData`, etc.)**
4. **Handle data appropriately in your business logic**

***

### 🛠️ Step-by-Step Guide

#### 1. Import the AggregatorV3Interface

If you're using Solidity:

```
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
```

Note: This works because our oracle implements the same interface as Chainlink, so it's fully compatible.

***

#### 2. Declare the Oracle Interface

Declare a reference to the oracle in your consuming contract:

```
AggregatorV3Interface public priceFeed;
```

You’ll initialize this reference in the constructor or a setup function.

***

#### 3. Initialize with the Oracle Address

```
constructor(address _oracleAddress) {
    priceFeed = AggregatorV3Interface(_oracleAddress);
}
```

> Replace `_oracleAddress` with the actual deployed address of the `AggregatorV3` oracle contract.

***

#### 4. Read the Latest Data

To get the most recent answer:

```
function getLatestAnswer() public view returns (int256) {
    (
        , 
        int256 answer,
        , 
        , 
    ) = priceFeed.latestRoundData();
    return answer;
}

```

You can also access timestamps and round information if needed:

```
function getLatestData() public view returns (
    uint80 roundId,
    int256 answer,
    uint256 startedAt,
    uint256 updatedAt,
    uint80 answeredInRound
) {
    return priceFeed.latestRoundData();
}

```

***

#### 5. Read Specific Round Data (Optional)

If your use case requires historical data, you can retrieve data from a specific round:

```
function getHistoricalAnswer(uint80 roundId) public view returns (int256) {
    (
        , 
        int256 answer,
        , 
        , 
    ) = priceFeed.getRoundData(roundId);
    return answer;
}

```

> ⚠️ Ensure the round exists — otherwise, the call will revert.

***

### 🧠 Use Cases

Here are a few example use cases where your dApp might benefit from this oracle:

* **Price Feeds:** Display the current price of a token or asset.
* **Stablecoin Collateralization:** Check price before minting/burning.
* **Betting & Prediction Markets:** Resolve outcomes based on real-world values.
* **Rebasing Tokens:** Use oracle data for supply adjustments.

***

### ⚠️ Best Practices

* **Validate Oracle Output:** Ensure the returned data is not stale or zero.
* **Handle Errors:** If no data is available, `latestRoundData()` will revert. Use `try/catch` in higher-level frameworks or check availability on-chain.
* **Secure the Oracle Address:** Set the oracle address only once or via a governance mechanism to avoid manipulation.

***

### 🧪 Testing Integration

When testing locally or in staging:

* You can deploy a mock oracle using the same interface (`AggregatorV3Interface`) and manually update values using the `updateAnswer(int256)` function.
* This is useful for simulating real-world scenarios and testing dApp logic against different oracle values.

```
// Call this in your tests from the oracle owner's account
mockOracle.updateAnswer(12345); // sets new answer

```

### ✅ Summary

By following the standard `AggregatorV3Interface`, our oracle contract allows seamless integration into any dApp that supports Chainlink-style data feeds.

Integration steps are straightforward:

1. Import the interface
2. Set the oracle address
3. Call `latestRoundData()` or `getRoundData()`
4. Use the result in your business logic

With this, your smart contracts can interact with externally-fed data reliably and efficiently.
