๐งพ Example: Fetching BTC & ETH Prices (Solidity)
Hereโs a sample Solidity snippet showing how to fetch real-time prices for BTC and ETH using QIE Oracle contracts. These oracles are based on your custom implementation of AggregatorV3
.
๐ Assumptions
You have deployed two oracle contracts using your custom
AggregatorV3
contract:One for BTC (e.g., BTC/USD)
One for ETH (e.g., ETH/USD)
Both contracts implement
AggregatorV3Interface
The contract addresses are known and passed during deployment
๐งฑ Sample Solidity Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract AssetPriceConsumer {
AggregatorV3Interface public btcOracle;
AggregatorV3Interface public ethOracle;
constructor(address _btcOracle, address _ethOracle) {
btcOracle = AggregatorV3Interface(_btcOracle);
ethOracle = AggregatorV3Interface(_ethOracle);
}
function getLatestBTCPrice() public view returns (int256) {
(
,
int256 price,
,
,
) = btcOracle.latestRoundData();
return price;
}
function getLatestETHPrice() public view returns (int256) {
(
,
int256 price,
,
,
) = ethOracle.latestRoundData();
return price;
}
}
๐งช Usage
This contract allows you to:
Fetch the latest BTC/USD and ETH/USD prices
Use the prices in further contract logic (e.g., swaps, comparisons, liquidations)
You can call:
getLatestBTCPrice(); // Returns the latest BTC price (int256)
getLatestETHPrice(); // Returns the latest ETH price (int256)
โ ๏ธ Notes
Ensure that the oracle contracts are properly updated by the owner before use.
The returned price is an
int256
and may include decimals (checkdecimals()
from the oracle).If the oracle has not been updated yet,
latestRoundData()
will revert.
๐ Bonus: Get Oracle Metadata
You can also fetch metadata like description and decimals:
function getBTCMetadata() public view returns (string memory, uint8) {
return (btcOracle.description(), btcOracle.decimals());
}
Last updated