Smart Contracts Prices (v3)
To query prices from Band's data feeds through smart contracts, the contract looking to use the price values should reference Band's PacketConsumerProxy contract. This contract exposes getPrice and getPriceBatch functions.
getPrice takes a string as the input, the signal ID. It then queries the PacketConsumerProxy contract for the latest rate for that signal, and returns a Price struct, shown below.
struct Price {
uint64 price; // the price of a signal ID.
int64 timestamp; // UNIX epoch of the last time when price gets updated.
}
getPriceBatch instead takes a list of the signal IDs. It then proceeds to similarly query the price for each signal at each index, and returns an array of Price structs.
For example, if we call getPriceBatch with ['CS:BTC-USD','CS:ETH-USD','CS:BNB-USD'], the returned Price array will contain information regarding the signals:
CS:BTC-USDCS:ETH-USDCS:BNB-USD
Example Usage
The contract code below demonstrates a simple usage of the new PacketConsumerProxy contract and the getPrice function.
pragma solidity ^0.8.23;
interface IPacketConsumerProxy {
/// A structure returned whenever someone requests for standard reference data.
struct Price {
uint64 price; // the price of a signal ID.
int64 timestamp; // UNIX epoch of the last time when price gets updated.
}
/// Returns the price data for the given signal ID. Revert if not available.
function getPrice(string memory _signalId)
external
view
returns (Price memory);
/// Similar to getPrice, but with multiple signal IDs at once.
function getPriceBatch(string[] memory _signalIds)
external
view
returns (Price[] memory);
}
contract DemoOracle {
IPacketConsumerProxy proxy;
uint64 public price;
constructor(IPacketConsumerProxy _proxy) {
proxy = _proxy;
}
function getSinglePrice() external view returns (uint64) {
IPacketConsumerProxy.Price memory data = proxy.getPrice("CS:BTC-USD");
return data.price;
}
function getMultiPrices() external view returns (uint64[] memory) {
string[] memory signalIds = new string[](2);
signalIds[0] = "CS:BTC-USD";
signalIds[1] = "CS:ETH-USD";
IPacketConsumerProxy.Price[] memory data = proxy.getPriceBatch(signalIds);
uint64[] memory prices = new uint64[](2);
prices[0] = data[0].price;
prices[1] = data[1].price;
return prices;
}
function savePrice(string memory _signalId) external {
IPacketConsumerProxy.Price memory data = proxy.getPrice(_signalId);
price = data.price;
}
}