> 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/developer-guide/handling-edge-cases-and-best-practices.md).

# ⚠️ Handling Edge Cases & Best Practices

While the **QIE Oracle** is designed for reliability and consistency, developers should account for potential edge cases and follow recommended best practices to ensure safe integration into smart contracts and dApps.

***

#### 🚫 Handling Common Edge Cases

**🔤 Invalid Asset Symbol**

If your oracle implementation supports asset symbols (e.g., `getPrice("BTC")`), calling the function with an **unsupported or misspelled symbol** will **revert** the transaction.

> ✅ **Tip:** Always validate or whitelist symbols before calling the oracle.

***

**📉 Unexpected Price Values**

Due to market volatility or delayed updates, prices may temporarily **spike** or **crash**. Implement **on-chain sanity checks** to prevent incorrect logic execution.

```
function safeFetchBTCPrice() public view returns (uint256) {
    uint256 price = btcOracle.getPrice("BTC");
    require(price > 0, "Invalid BTC price");
    require(price < 1_000_000_000 ether, "BTC price too high");
    return price;
}

```

> ✅ Set upper and lower bounds for acceptable price ranges.

***

**🕒 Oracle Update Delays**

QIE Oracle updates prices at **regular intervals**, but **not every block**.

> ⚠️ Do not assume price data is real-time within the same block. For sensitive use cases (like flash loans or arbitrage), verify `updatedAt` timestamp from `latestRoundData()`.

***

### ✅ Best Practices

#### 🪙 Use Multiple Assets

If your dApp supports multiple tokens (e.g., DeFi lending or trading), consider fetching **multiple asset prices** to reduce reliance on a single price feed.

***

#### 🧠 Implement Rate Limiting / Caching

Avoid repeatedly calling the oracle in the same transaction or within tight loops.

* **Cache prices on-chain** for a few blocks
* **Store recent price in state**, and refresh only when needed

> This helps **reduce gas costs** and improves efficiency.

***

#### 🧾 Validate External Data

Always include **sanity checks** in your contract logic for:

* Price magnitude
* Max allowed percentage change
* Timestamp freshness

Example:

```
require(block.timestamp - lastUpdated < 300, "Price is outdated");
```

***

#### 🛡️ Do Not Overwrite Oracle Data

Oracle contracts are maintained by the QIE system. Never attempt to manually modify oracle state or push updates.

> 🛑 Direct writes to oracle contracts are restricted to authorized updaters only.

***

#### 🧪 Test on Testnet First

Before deploying to QIE Mainnet:

* Use **QIE Testnet RPC**
* Deploy your contracts
* Connect to deployed **testnet oracle contracts**
* Simulate edge cases and validate behavior

***

### 🔚 Conclusion

By following these practices, developers can:

* Safely consume oracle data
* Protect their dApps from abnormal price behavior
* Ensure secure and efficient usage of QIE Oracle in DeFi and other use cases

> ✅ Good oracle hygiene is critical for building trustworthy, secure Web3 applications.
