Skip to content

Commit 32b3f10

Browse files
authored
Merge pull request #1509 from onflow/cf/band-docs
Add Band Oracle docs
2 parents 8d37210 + 0149489 commit 32b3f10

File tree

2 files changed

+313
-27
lines changed

2 files changed

+313
-27
lines changed
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
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.

docs/ecosystem/defi-liquidity/defi-contracts.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,26 @@ Below is a list of commonly used DeFi contracts on Flow:
3838

3939
#### Flow EVM Mainnet
4040

41-
| Contract Name | Flow EVM Mainnet Address |
42-
| -------------------------------------------- | -------------------------------------------- |
43-
| [StableKittyFactoryNG.sol (KittyPunch)][1] | `0x4412140D52C1F5834469a061927811Abb6026dB7` |
44-
| [TwoKittyFactory.sol (KittyPunch)][2] | `0xf0E48dC92f66E246244dd9F33b02f57b0E69fBa9` |
45-
| [TriKittyFactory.sol (KittyPunch)][3] | `0xebd098c60b1089f362AC9cfAd9134CBD29408226` |
46-
| [KittyRouterNgPoolsOnly.sol (KittyPunch)][4] | `0x87048a97526c4B66b71004927D24F61DEFcD6375` |
47-
| [PunchSwapV2Router02.sol (KittyPunch)][5] | `0xf45AFe28fd5519d5f8C1d4787a4D5f724C0eFa4d` |
48-
| [PunchSwapV2Factory.sol (KittyPunch)][6] | `0x29372c22459a4e373851798bFd6808e71EA34A71` |
49-
| [TrenchesTokensBuyer.sol (KittyPunch)][7] | `0x6D0e081Acc28eA9ee6b7fD293eC03F97147b026d` |
41+
| Contract Name | Flow EVM Mainnet Address | Docs |
42+
| -------------------------------------------- | -------------------------------------------- | --------------------- |
43+
| [StableKittyFactoryNG.sol (KittyPunch)][1] | `0x4412140D52C1F5834469a061927811Abb6026dB7` | [Docs][kittypunch-doc] |
44+
| [TwoKittyFactory.sol (KittyPunch)][2] | `0xf0E48dC92f66E246244dd9F33b02f57b0E69fBa9` | [Docs][kittypunch-doc] |
45+
| [TriKittyFactory.sol (KittyPunch)][3] | `0xebd098c60b1089f362AC9cfAd9134CBD29408226` | [Docs][kittypunch-doc] |
46+
| [KittyRouterNgPoolsOnly.sol (KittyPunch)][4] | `0x87048a97526c4B66b71004927D24F61DEFcD6375` | [Docs][kittypunch-doc] |
47+
| [PunchSwapV2Router02.sol (KittyPunch)][5] | `0xf45AFe28fd5519d5f8C1d4787a4D5f724C0eFa4d` | [Docs][kittypunch-doc] |
48+
| [PunchSwapV2Factory.sol (KittyPunch)][6] | `0x29372c22459a4e373851798bFd6808e71EA34A71` | [Docs][kittypunch-doc] |
49+
| [TrenchesTokensBuyer.sol (KittyPunch)][7] | `0x6D0e081Acc28eA9ee6b7fD293eC03F97147b026d` | [Docs][kittypunch-doc] |
5050

5151
#### Flow Cadence Mainnet
5252

53-
| Contract Name | Flow Cadence Mainnet Address | [CLI](https://developers.flow.com/build/tools/flow-cli/dependency-manager) |
54-
| ----------------------------------- | ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
55-
| [SwapFactory.cdc (IncrementFi)][22] | `0xb063c16cac85dbd1` | <CopyButton text="flow dependencies install mainnet://0xb063c16cac85dbd1.SwapFactory" title="Copy install command (mainnet)" /> |
56-
| [SwapPair (IncrementFi)][23] | `0xecbda466e7f191c7` | <CopyButton text="flow dependencies install mainnet://0xecbda466e7f191c7.SwapPair" title="Copy install command (mainnet)" /> |
57-
| [SwapError (IncrementFi)][24] | `0xb78ef7afa52ff906` | <CopyButton text="flow dependencies install mainnet://0xb78ef7afa52ff906.SwapError" title="Copy install command (mainnet)" /> |
58-
| [SwapInterfaces (IncrementFi)][25] | `0xb78ef7afa52ff906` | <CopyButton text="flow dependencies install mainnet://0xb78ef7afa52ff906.SwapInterfaces" title="Copy install command (mainnet)" /> |
59-
| [SwapConfig (IncrementFi)][26] | `0xb78ef7afa52ff906` | <CopyButton text="flow dependencies install mainnet://0xb78ef7afa52ff906.SwapConfig" title="Copy install command (mainnet)" /> |
60-
| [SwapRouter (IncrementFi)][27] | `0xa6850776a94e6551` | <CopyButton text="flow dependencies install mainnet://0xa6850776a94e6551.SwapRouter" title="Copy install command (mainnet)" /> |
53+
| Contract Name | Flow Cadence Mainnet Address | [CLI](https://developers.flow.com/build/tools/flow-cli/dependency-manager) | Docs |
54+
| ----------------------------------- | ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | -------------------------- |
55+
| [SwapFactory.cdc (IncrementFi)][22] | `0xb063c16cac85dbd1` | <CopyButton text="flow dependencies install mainnet://0xb063c16cac85dbd1.SwapFactory" title="Copy install command (mainnet)" /> | [Docs][incrementfi-doc] |
56+
| [SwapPair (IncrementFi)][23] | `0xecbda466e7f191c7` | <CopyButton text="flow dependencies install mainnet://0xecbda466e7f191c7.SwapPair" title="Copy install command (mainnet)" /> | [Docs][incrementfi-doc] |
57+
| [SwapError (IncrementFi)][24] | `0xb78ef7afa52ff906` | <CopyButton text="flow dependencies install mainnet://0xb78ef7afa52ff906.SwapError" title="Copy install command (mainnet)" /> | [Docs][incrementfi-doc] |
58+
| [SwapInterfaces (IncrementFi)][25] | `0xb78ef7afa52ff906` | <CopyButton text="flow dependencies install mainnet://0xb78ef7afa52ff906.SwapInterfaces" title="Copy install command (mainnet)" /> | [Docs][incrementfi-doc] |
59+
| [SwapConfig (IncrementFi)][26] | `0xb78ef7afa52ff906` | <CopyButton text="flow dependencies install mainnet://0xb78ef7afa52ff906.SwapConfig" title="Copy install command (mainnet)" /> | [Docs][incrementfi-doc] |
60+
| [SwapRouter (IncrementFi)][27] | `0xa6850776a94e6551` | <CopyButton text="flow dependencies install mainnet://0xa6850776a94e6551.SwapRouter" title="Copy install command (mainnet)" /> | [Docs][incrementfi-doc] |
6161

6262
## Bridges & Cross-Chain Messaging
6363

@@ -102,30 +102,30 @@ Below is a list of commonly used DeFi contracts on Flow:
102102

103103
#### Flow Cadence Testnet
104104

105-
| Contract Name | Flow Cadence Testnet Address | [CLI](https://developers.flow.com/build/tools/flow-cli/dependency-manager) |
106-
| ----------------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
107-
| [PublicPriceOracle.cdc (IncrementFi)][31] | `0x8232ce4a3aff4e94` | <CopyButton text="flow dependencies install testnet://0x8232ce4a3aff4e94.PublicPriceOracle" title="Copy install command (testnet)" /> |
108-
| [BandOracle.cdc (Band)][32] | `0x9fb6606c300b5051` | <CopyButton text="flow dependencies install testnet://0x9fb6606c300b5051.BandOracle" title="Copy install command (testnet)" /> |
105+
| Contract Name | Flow Cadence Testnet Address | [CLI](https://developers.flow.com/build/tools/flow-cli/dependency-manager) | Docs |
106+
| ----------------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- |
107+
| [PublicPriceOracle.cdc (IncrementFi)][31] | `0x8232ce4a3aff4e94` | <CopyButton text="flow dependencies install testnet://0x8232ce4a3aff4e94.PublicPriceOracle" title="Copy install command (testnet)" /> | [Docs][incrementfi-doc] |
108+
| [BandOracle.cdc (Band)][32] | `0x9fb6606c300b5051` | <CopyButton text="flow dependencies install testnet://0x9fb6606c300b5051.BandOracle" title="Copy install command (testnet)" /> | [Docs][band-oracle-doc] |
109109

110110
#### Flow Cadence Mainnet
111111

112-
| Contract Name | Flow Cadence Mainnet Address | [CLI](https://developers.flow.com/build/tools/flow-cli/dependency-manager) |
113-
| ----------------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
114-
| [PublicPriceOracle.cdc (IncrementFi)][19] | `0xec67451f8a58216a` | <CopyButton text="flow dependencies install mainnet://0xec67451f8a58216a.PublicPriceOracle" title="Copy install command (mainnet)" /> |
115-
| [BandOracle.cdc (Band) Protocol][33] | `0x6801a6222ebf784a` | <CopyButton text="flow dependencies install mainnet://0x6801a6222ebf784a.BandOracle" title="Copy install command (mainnet)" /> |
112+
| Contract Name | Flow Cadence Mainnet Address | [CLI](https://developers.flow.com/build/tools/flow-cli/dependency-manager) | Docs |
113+
| ----------------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- |
114+
| [PublicPriceOracle.cdc (IncrementFi)][19] | `0xec67451f8a58216a` | <CopyButton text="flow dependencies install mainnet://0xec67451f8a58216a.PublicPriceOracle" title="Copy install command (mainnet)" /> | [Docs][incrementfi-doc] |
115+
| [BandOracle.cdc (Band) Protocol][33] | `0x6801a6222ebf784a` | <CopyButton text="flow dependencies install mainnet://0x6801a6222ebf784a.BandOracle" title="Copy install command (mainnet)" /> | [Docs][band-oracle-doc] |
116116

117117
## Ethereum Attestation Service
118118

119119
More information can be found on the Credora docs site for [EAS on Flow](https://credora.gitbook.io/eas-for-flow).
120120

121-
Testnet EAS Explorer: [https://flow-testnet.easscan.credora.io] (https://flow-testnet.easscan.credora.io)
121+
Testnet EAS Explorer: [https://flow-testnet.easscan.credora.io](https://flow-testnet.easscan.credora.io)
122122

123123
| Contract Name | Flow EVM Testnet Address |
124124
| ------------------------------------------------------- | -------------------------------------------- |
125125
| [SchemaRegistry.sol (Ethereum Attestation Service)][29] | `0x97900F59828Da4187607Cb8F84f49e3944199d18` |
126126
| [EAS.sol (Ethereum Attestation Service)][30] | `0xBCF2dA8f82fb032A2474c92Ec5b70C95A83fc0cc` |
127127

128-
Mainnet EAS Explorer: [https://flow.easscan.credora.io] (https://flow.easscan.credora.io)
128+
Mainnet EAS Explorer: [https://flow.easscan.credora.io](https://flow.easscan.credora.io)
129129

130130
| Contract Name | Flow EVM Mainnet Address |
131131
| ------------------------------------------------------- | -------------------------------------------- |
@@ -169,3 +169,6 @@ Mainnet EAS Explorer: [https://flow.easscan.credora.io] (https://flow.easscan.cr
169169
[35]: https://docs.debridge.finance/dln-the-debridge-liquidity-network-protocol/deployed-contracts
170170
[36]: https://relay.link/bridge
171171
[37]: https://docs.relay.link/resources/contract-addresses
172+
[band-oracle-doc]: ./band-oracle
173+
[incrementfi-doc]: https://docs.increment.fi/
174+
[kittypunch-doc]: https://kittypunch.gitbook.io/kittypunch-docs

0 commit comments

Comments
 (0)