Band Protocol
In addition to data native to the ICON blockchain, ICON developers also have access to various cryptocurrency price data provided by Band Protocol’s oracle.

Standard Reference Dataset Contract Info

Data Available

The price data originates from data requests made on BandChain. Band’s std_reference SCORE on ICON then retrieves and stores the results of those requests. Specifically, the following price pairs are available to be read from the std_reference_proxy contract:
  • BTC/USD
  • ETH/USD
  • ICX/USD
These prices are automatically updated every 5 minutes. The std_reference_proxy itself is currently deployed on ICON Yeouido testnet at cx61a36e5d10412e03c907a507d1e8c6c3856d9964.
The prices themselves are the median of the values retrieved by BandChain’s validators from many sources including CoinGecko, CryptoCompare, Binance, CoinMarketcap, HuobiPro, CoinBasePro, Kraken, Bitfinex, Bittrex, BITSTAMP, OKEX, FTX, HitBTC, ItBit, Bithumb, CoinOne. The data request is then made by executing Band’s aggregator oracle script, the code of which you can view on Band’s proof-of-authority mainnet. Along with the price data, developers will also have access to the latest timestamp the price was updated.
These parameters are intended to act as security parameters to help anyone using the data to verify that the data they are using is what they expect and, perhaps more importantly, actually valid.

Standard Reference Dataset Contract Price Update Process

For the ease of development, the Band Foundation will be maintaining and updating the std_reference contract with the latest price data. In the near future, we will be releasing guides on how developers can create similar contracts themselves to retrieve data from Band’s oracle.

Retrieving the Price Data

The code below shows an example of a relatively simple price database SCORE on ICON which retrieve price data from Band’s std_reference_proxy contract and store it in the contract’s state.
1
===================
2
| |
3
| simple price db |
4
| |
5
===================
6
| ^
7
|(1) |(4)
8
|Asking for |Return
9
|price data |result
10
| |
11
v |
12
=================== (2) Ask ===================
13
| |-------------->| |
14
| std ref proxy | | std ref |
15
| |<--------------| |
16
=================== (3) Result ===================
The contract is able to store exchange rate of any price pair that available on the std_reference contract. For more information on what oracle scripts are and how data requests work on BandChain in general, please see their wiki and developer documentation​
1
from iconservice import *
2
​
3
TAG = "SimplePriceDB"
4
​
5
# Interface of StdReferenceProxy
6
class IStdReferenceProxy(InterfaceScore):
7
@interface
8
def get_reference_data(self, _base: str, _quote: str) -> dict:
9
pass
10
​
11
@interface
12
def get_reference_data_bulk(self, _bases: str, _quotes: str) -> list:
13
pass
14
​
15
​
16
# SimplePriceDB contract
17
class SimplePriceDB(IconScoreBase):
18
def __init__(self, db: IconScoreDatabase) -> None:
19
super().__init__(db)
20
self.std_reference_proxy_address = VarDB(
21
"std_reference_proxy_address", db, value_type=Address
22
)
23
self.prices = DictDB("prices", db, value_type=int)
24
​
25
def on_install(self, _proxy: Address) -> None:
26
super().on_install()
27
self.set_proxy(_proxy)
28
​
29
def on_update(self) -> None:
30
super().on_update()
31
​
32
# Get price of the given pair multiply by 1e18.
33
# For example "BTC/ETH" -> 30.09 * 1e18.
34
@external(readonly=True)
35
def get_price(self, _pair: str) -> int:
36
return self.prices[_pair]
37
​
38
# Sets the proxy contract address, which can only be set by the owner.
39
@external
40
def set_proxy(self, _proxy: Address) -> None:
41
if self.msg.sender != self.owner:
42
self.revert("NOT_AUTHORIZED")
43
​
44
self.std_reference_proxy_address.set(_proxy)
45
​
46
# This function accepts the string coin pairs such as "BTC/ETH".
47
# And then pass it to the std_reference_proxy. After receiving the output
48
# from the std_reference_proxy, the exchange rate of the input pair
49
# is recorded into the state.
50
@external
51
def set_single(self, _pair: str) -> None:
52
proxy = self.create_interface_score(
53
self.std_reference_proxy_address.get(), IStdReferenceProxy
54
)
55
base, quote = _pair.split("/")
56
result = proxy.get_reference_data(base, quote)
57
​
58
self.prices[_pair] = result["rate"]
59
​
60
# This function accepts the string of the encoding of an array of coin pairs
61
# such as '["BTC/ETH", "ETH/USD", "USDT/USD"]'. And then transform the format
62
# of the input to pass it to the std_reference_proxy. After receiving the output
63
# from the std_reference_proxy, each pair's rate is recorded into the state.
64
@external
65
def set_multiple(self, _json_pairs: str) -> None:
66
proxy = self.create_interface_score(
67
self.std_reference_proxy_address.get(), IStdReferenceProxy
68
)
69
​
70
pairs = json_loads(_json_pairs)
71
bases, quotes = [json_dumps(x) for x in zip(*[pair.split("/") for pair in pairs])]
72
results = proxy.get_reference_data_bulk(bases, quotes)
73
​
74
if len(pairs) != len(results):
75
self.revert("LEN_PAIRS_MUST_EQUAL_TO_LEN_RESULTS")
76
​
77
for pair, result in zip(pairs, results):
78
self.prices[pair] = result["rate"]

Code Breakdown

The example code above can be broken down into two sections: defining the interface for the IStdReferenceProxy and the actual SimplePriceDB SCORE code itself.
IStdReferenceProxy Interface
This section consists of two functions, get_reference_data and get_reference_data_bulk. This is the interface that we’ll use to query price from Band oracle for the latest price of a token or a bunch of tokens.
SimplePriceDB class
The SimplePriceDB then contains the main logic of our SCORE. It’s purpose will be to store the latest price of tokens.
The actual price data query can then be called through the get_price function. Before we can call the method, however, we need to first set the address of the std_reference_proxy. This is done by calling the set_proxy method or constructor. After that the price should be set by calling set_single or set_multiple.
The set_single function will simply calling get_reference_data from std_reference_proxy with base symbol and quote symbol. It then extract the exchange rate from the result and save to the state.
The set_multiple function converts the input into an array of base symbol and quote symbol arrays. After that it will call get_reference_data_bulk from std_reference_proxy with base symbols and quote symbols. It then extract the exchange rates from the results and save all of them to the state.
The full source code for the SimplePriceDB score can be found in this repo along with the JSON for sending the set_proxy, set_single, set_multiple. The score itself is also deployed to the testnet at address cxd8b7e45dad9a111254f1d3168931e9ca562bc534.

Band Contracts

  • Band Testnet Proxy cx61a36e5d10412e03c907a507d1e8c6c3856d9964
  • Band Testnet Bridge cxc79c2120992356eac409d5cf5ff650f2780a6995
  • Band Testnet Relay cxb94de9090263f03c617d7e1ca767c23ca4efc6f2
  • Band Mainnet Proxy cxe647e0af68a4661566f5e9861ad4ac854de808a2
  • Band Mainnet Bridge cx087b4164a87fdfb7b714f3bafe9dfb050fd6b132
  • Band Mainnet Relay cx3ba7f485dca73db634f116c21995eb89e3994f36