|
| 1 | +--- |
| 2 | +id: band-oracle |
| 3 | +title: Band Oracle on Flow |
| 4 | +description: Band Protocol provides decentralized oracle services on Flow, delivering real-time price feeds and data for DeFi applications. |
| 5 | +keywords: |
| 6 | + - Band Oracle |
| 7 | + - Band Protocol |
| 8 | + - oracle |
| 9 | + - price feeds |
| 10 | + - data feeds |
| 11 | + - DeFi |
| 12 | + - Flow blockchain |
| 13 | + - Flow Cadence |
| 14 | + - smart contracts |
| 15 | +sidebar_position: 6 |
| 16 | +sidebar_label: Band Oracle |
| 17 | +--- |
| 18 | + |
| 19 | +import CopyButton from '@site/src/components/CopyButton'; |
| 20 | + |
| 21 | +# Band Oracle with Cadence |
| 22 | + |
| 23 | +The Band Protocol Oracle contract enables Flow blockchain applications to access real-time price data from the [Band Protocol Oracle network](https://faq.bandprotocol.com/). The oracle provides a comprehensive set of cryptocurrency and fiat currency price quotes from the Band Standard Dataset, making them available to any Cadence application, contract, or transaction. |
| 24 | + |
| 25 | +## Contract Addresses |
| 26 | + |
| 27 | +| Network | Address | [CLI](https://developers.flow.com/build/tools/flow-cli/dependency-manager) | Explorer | |
| 28 | +|---------|---------|----------|----------| |
| 29 | +| Testnet | `0x9fb6606c300b5051` | <CopyButton text="flow dependencies install testnet://0x9fb6606c300b5051.BandOracle" title="Copy install command (testnet)" /> | [View Contract](https://testnet.flowscan.io/contract/A.9fb6606c300b5051.BandOracle) | |
| 30 | +| Mainnet | `0x6801a6222ebf784a` | <CopyButton text="flow dependencies install mainnet://0x6801a6222ebf784a.BandOracle" title="Copy install command (mainnet)" /> | [View Contract](https://flowscan.io/contract/A.6801a6222ebf784a.BandOracle) | |
| 31 | + |
| 32 | +## Supported Symbols |
| 33 | + |
| 34 | +### Cryptocurrency Pairs (against USD) |
| 35 | +- **Major**: ETH, FLOW, USDC, USDT, WBTC, BNB, XRP, ADA, DOGE, POL (MATIC) |
| 36 | +- **Layer 1**: SOL, DOT, AVAX, ATOM, XLM, TRX, SUI |
| 37 | +- **DeFi**: AAVE, LINK, CRV, OP, UNI, SUSHI, CAKE, DYDX, 1INCH, BAT |
| 38 | +- **Others**: LTC, SHIB, DAI, FTM |
| 39 | + |
| 40 | +### Fiat Currency Pairs (against USD) |
| 41 | +- **Asian**: KRW, INR, HKD, TWD, THB, JPY, MYR, PHP, CNY, SGD |
| 42 | +- **European**: PLN, CZK, EUR, GBP, CHF, RUB, SEK, TRY |
| 43 | +- **Americas**: BRL, CAD |
| 44 | +- **Oceanic**: AUD, NZD |
| 45 | + |
| 46 | +## How It Works |
| 47 | + |
| 48 | +### Architecture |
| 49 | + |
| 50 | +The Band Oracle contract maintains a decentralized price feed system with three key components: |
| 51 | + |
| 52 | +1. **Data Storage**: Price data is stored in a contract-level dictionary `symbolsRefData: {String: RefData}` where each symbol maps to its latest price information. |
| 53 | + |
| 54 | +2. **Data Updates**: Authorized BandChain relayers continuously update price data from the Band Protocol network to keep prices current. |
| 55 | + |
| 56 | +3. **Data Access**: Any user or contract can query the latest price data through public functions, enabling real-time price integrations. |
| 57 | + |
| 58 | +### Data Structure |
| 59 | + |
| 60 | +Price data is stored using the `RefData` struct: |
| 61 | + |
| 62 | +```cadence |
| 63 | +access(all) struct RefData { |
| 64 | + // USD-rate, multiplied by 1e9 |
| 65 | + access(all) var rate: UInt64 |
| 66 | + // UNIX epoch when data was last resolved |
| 67 | + access(all) var timestamp: UInt64 |
| 68 | + // BandChain request identifier for this data |
| 69 | + access(all) var requestID: UInt64 |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +When querying prices, you receive a `ReferenceData` struct: |
| 74 | + |
| 75 | +```cadence |
| 76 | +access(all) struct ReferenceData { |
| 77 | + // Rate as integer multiplied by 1e18 |
| 78 | + access(all) var integerE18Rate: UInt256 |
| 79 | + // Rate as a fixed-point decimal |
| 80 | + access(all) var fixedPointRate: UFix64 |
| 81 | + // Timestamp of base symbol data |
| 82 | + access(all) var baseTimestamp: UInt64 |
| 83 | + // Timestamp of quote symbol data |
| 84 | + access(all) var quoteTimestamp: UInt64 |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### Data Normalization |
| 89 | + |
| 90 | +All price data is stored with a USD conversion rate. When you query for price conversions between two non-USD symbols, the contract derives the rate from their respective USD rates. For example, to get ETH/EUR, the contract calculates: `(ETH/USD) / (EUR/USD)`. |
| 91 | + |
| 92 | +## Features |
| 93 | + |
| 94 | +### Price Queries |
| 95 | +- Query any supported symbol pair in real-time |
| 96 | +- Get both integer (e18 precision) and fixed-point decimal rates |
| 97 | +- Access timestamp information to verify data freshness |
| 98 | +- Track BandChain request IDs for transparency |
| 99 | + |
| 100 | +### Fee Structure |
| 101 | +- Configurable fee system for oracle usage (currently set to zero) |
| 102 | +- Fee collected in FLOW tokens |
| 103 | +- Query current fee using `BandOracle.getFee()` |
| 104 | + |
| 105 | +### Event Monitoring |
| 106 | +The contract emits events to notify applications of updates: |
| 107 | + |
| 108 | +```cadence |
| 109 | +// Emitted when symbol prices are updated |
| 110 | +access(all) event BandOracleSymbolsUpdated( |
| 111 | + symbols: [String], |
| 112 | + relayerID: UInt64, |
| 113 | + requestID: UInt64 |
| 114 | +) |
| 115 | +
|
| 116 | +// Emitted when a symbol is removed |
| 117 | +access(all) event BandOracleSymbolRemoved(symbol: String) |
| 118 | +``` |
| 119 | + |
| 120 | +## Usage Guide |
| 121 | + |
| 122 | +### Basic Price Query (Transaction) |
| 123 | + |
| 124 | +To query price data from a transaction: |
| 125 | + |
| 126 | +```cadence |
| 127 | +import "BandOracle" |
| 128 | +import "FlowToken" |
| 129 | +import "FungibleToken" |
| 130 | +
|
| 131 | +transaction(baseSymbol: String, quoteSymbol: String) { |
| 132 | +
|
| 133 | + let payment: @{FungibleToken.Vault} |
| 134 | +
|
| 135 | + prepare(acct: auth(BorrowValue) &Account) { |
| 136 | + // Borrow reference to user's FLOW vault |
| 137 | + let vaultRef = acct.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>( |
| 138 | + from: /storage/flowTokenVault |
| 139 | + ) ?? panic("Cannot borrow reference to signer's FLOW vault") |
| 140 | +
|
| 141 | + // Withdraw payment for oracle fee |
| 142 | + self.payment <- vaultRef.withdraw(amount: BandOracle.getFee()) |
| 143 | + } |
| 144 | +
|
| 145 | + execute { |
| 146 | + // Get reference data |
| 147 | + let priceData = BandOracle.getReferenceData( |
| 148 | + baseSymbol: baseSymbol, |
| 149 | + quoteSymbol: quoteSymbol, |
| 150 | + payment: <- self.payment |
| 151 | + ) |
| 152 | +
|
| 153 | + log("Rate (fixed-point): ".concat(priceData.fixedPointRate.toString())) |
| 154 | + log("Rate (integer e18): ".concat(priceData.integerE18Rate.toString())) |
| 155 | + log("Base timestamp: ".concat(priceData.baseTimestamp.toString())) |
| 156 | + log("Quote timestamp: ".concat(priceData.quoteTimestamp.toString())) |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +### Example: ETH/USD Price |
| 162 | +```cadence |
| 163 | +// Get ETH price in USD |
| 164 | +let priceData = BandOracle.getReferenceData( |
| 165 | + baseSymbol: "ETH", |
| 166 | + quoteSymbol: "USD", |
| 167 | + payment: <- flowPayment |
| 168 | +) |
| 169 | +// priceData.fixedPointRate contains ETH price in USD |
| 170 | +``` |
| 171 | + |
| 172 | +### Example: Cross-Currency Conversion |
| 173 | +```cadence |
| 174 | +// Get EUR price in JPY |
| 175 | +let priceData = BandOracle.getReferenceData( |
| 176 | + baseSymbol: "EUR", |
| 177 | + quoteSymbol: "JPY", |
| 178 | + payment: <- flowPayment |
| 179 | +) |
| 180 | +// priceData.fixedPointRate contains EUR/JPY exchange rate |
| 181 | +``` |
| 182 | + |
| 183 | +### Contract Integration |
| 184 | + |
| 185 | +Here's how to integrate the oracle into your smart contract: |
| 186 | + |
| 187 | +```cadence |
| 188 | +import "BandOracle" |
| 189 | +import "FlowToken" |
| 190 | +import "FungibleToken" |
| 191 | +
|
| 192 | +access(all) contract MyDeFiContract { |
| 193 | +
|
| 194 | + // Store a vault to pay for oracle fees |
| 195 | + access(self) let oracleFeeVault: @{FungibleToken.Vault} |
| 196 | +
|
| 197 | + access(all) fun getTokenPriceInUSD(tokenSymbol: String): UFix64 { |
| 198 | + // Withdraw payment for oracle |
| 199 | + let payment <- self.oracleFeeVault.withdraw( |
| 200 | + amount: BandOracle.getFee() |
| 201 | + ) |
| 202 | +
|
| 203 | + // Query the oracle |
| 204 | + let priceData = BandOracle.getReferenceData( |
| 205 | + baseSymbol: tokenSymbol, |
| 206 | + quoteSymbol: "USD", |
| 207 | + payment: <- payment |
| 208 | + ) |
| 209 | +
|
| 210 | + return priceData.fixedPointRate |
| 211 | + } |
| 212 | +
|
| 213 | + access(all) fun swapTokens(amount: UFix64, maxPrice: UFix64) { |
| 214 | + // Get current price |
| 215 | + let currentPrice = self.getTokenPriceInUSD(tokenSymbol: "ETH") |
| 216 | +
|
| 217 | + // Verify price is acceptable |
| 218 | + if currentPrice > maxPrice { |
| 219 | + panic("Price too high") |
| 220 | + } |
| 221 | +
|
| 222 | + // Proceed with swap logic... |
| 223 | + } |
| 224 | +
|
| 225 | + init() { |
| 226 | + // Initialize vault for oracle fees |
| 227 | + self.oracleFeeVault <- FlowToken.createEmptyVault( |
| 228 | + vaultType: Type<@FlowToken.Vault>() |
| 229 | + ) |
| 230 | + } |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +## Best Practices |
| 235 | + |
| 236 | +### 1. Listen for Price Updates |
| 237 | + |
| 238 | +Monitor the `BandOracleSymbolsUpdated` event to keep your contract's stored prices up-to-date: |
| 239 | + |
| 240 | +```cadence |
| 241 | +// Listen for this event in your application |
| 242 | +access(all) event BandOracleSymbolsUpdated( |
| 243 | + symbols: [String], |
| 244 | + relayerID: UInt64, |
| 245 | + requestID: UInt64 |
| 246 | +) |
| 247 | +``` |
| 248 | + |
| 249 | +When you detect an update for symbols your app uses, trigger a transaction to refresh your stored prices. |
| 250 | + |
| 251 | + |
| 252 | +## Advanced Features |
| 253 | + |
| 254 | +### Converting Between Number Formats |
| 255 | + |
| 256 | +The contract provides a utility function to convert between integer and fixed-point representations: |
| 257 | + |
| 258 | +```cadence |
| 259 | +// Convert e18 integer to fixed-point decimal |
| 260 | +let fixedPoint = BandOracle.e18ToFixedPoint(rate: integerE18Rate) |
| 261 | +``` |
| 262 | + |
| 263 | +### Fee Management |
| 264 | + |
| 265 | +For contract administrators, the oracle supports dynamic fee configuration: |
| 266 | + |
| 267 | +```cadence |
| 268 | +// Query current fee |
| 269 | +let currentFee = BandOracle.getFee() |
| 270 | +
|
| 271 | +// Fee can be updated by the fee collector (admin only) |
| 272 | +// feeCollector.setFee(fee: 0.001) // 0.001 FLOW per query |
| 273 | +``` |
| 274 | + |
| 275 | +## Resources |
| 276 | + |
| 277 | +- [Band Protocol FAQ](https://faq.bandprotocol.com/) |
| 278 | +- [Band Standard Dataset](https://data.bandprotocol.com/) |
| 279 | +- [Cadence Language Reference](https://cadence-lang.org/) |
| 280 | + |
| 281 | +--- |
| 282 | + |
| 283 | +**Note**: The oracle currently charges no fees for usage, but this may change in the future. Always check `BandOracle.getFee()` before querying to ensure your contract has sufficient FLOW tokens allocated. |
0 commit comments