How Hedera calculates the HBAR / USD exchange rate for SDKs and applications
1516208829798
Jan 16, 2020
by Paul Madsen
Head of Identity, The HBAR Foundation

Having up-to-date and accurate HBAR to USD conversion values is an important aspect of network operations at Hedera. We utilize the current price of HBAR across various applications and services, including:

1. Within the Hedera Wallet for iOS or Android, you’ve likely noticed that the exchange rate for the number of HBARs to USD is displayed at the top, when you open the app or refresh your balance — this simple calculation is done from within the application itself.

2. By the network nodes, the exchange rate of HBAR is used to determine the cost of API calls (hbar prices for API calls on Hedera are calculated using the exchange rate so that the dollar equivalent of the price will change more slowly). The Exchange Rate Tool (ERT) is a Hedera hosted application that calculates and submits a new exchange rate value to the network every hour.

3. On the Hedera fee’s page, we have an interactive calculator to estimate your costs and a table which lists the most common API calls, with pricing converted from USD to HBAR based on current rates. This also utilizes the ERT calculated exchange rate.

If you’ve ever wondered how we do that conversion, look no further. This blog posting details our process for calculating the exchange rate between USD and HBAR and how it is used in the Hedera Wallet, by the network nodes, and the fee estimator.

Why didn’t we use an existing service for the latest price?

There exist free APIs which would allow us to pull the current exchange rate between USD & HBAR automatically, such as the CoinGecko API. But because we’re going to need this price output used inside the calculation of fees to ensure the right costs for every API call, this would have been too centralized a solution; it might have left Hedera open and vulnerable to attack if the price output were ever manipulated by a central authority.

That is why we decided to instead query the exchanges directly and perform our own determination of an appropriate exchange rate value within the platform. Our ERT (exchange rate tool) queries the exchange rate from a number of sources and calculates a rate that is utilized across the platform. Our Hedera Wallet queries the same exchanges in order to display an equivalent USD value of the hbars in the account.

Pulling the latest pricing from key exchanges

The value calculated from the current exchange rate between USD & HBAR is determined by querying three prominent exchanges on which HBAR trades. Both the ERT and the Hedera Wallet query the APIs for Bittrex (https://bittrex.github.io/api/... ), Liquid (https://api.liquid.com/product... ), and OKCoin (https://www.okcoin.com/docs/en... ) with the output for each appearing as:

Exchange Apis Output

These three exchanges are the ones that currently offer a USD-HBAR trading pair and so directly provide the desired exchange rate. Any new exchanges that offer the same pair will be added to the calculation. There are other exchanges on which HBAR lists that support other pairs, for instance USDT-HBAR. We are considering how we might include such a pair by taking a ratio of HBAR/USDT & USDT/USD pairs to determine the desired exchange rate. We’re also exploring the potential for a weighted median price in the ERT, where the volume of HBAR across each exchange is factored into the calculation, in order to get a more accurate representation of the current price.

Taking the median, not the mean, and smoothing

Every hour, the ERT takes the three exchange rate values and calculates a median. The ERT then compares that median rate to the one from the previous midnight (CST) and rejects any variation greater than a defined value. This price smoothing prevents hourly volatility in the exchange rate of hbars from having a disproportionally strong effect on the cost in hbars of using Hedera’s network and services. Once an exchange rate is calculated, the ERT submits that value to the Hedera network for the nodes to use in calculating fees. The fee estimator uses this same exchange rate value to ensure that the fees it projects accurately track the actual fees being charged on the network. This network exchange rate value is available to dapps in transaction records.

The Hedera Wallet also queries the same three exchanges and displays it to the end user upon every refresh of their balance. Currently, the Wallet calculates the mean of those three values. Recently, we noticed that the Liquid exchange had been exposing a higher value for the USD/HBAR pair than Bittrex and OKCoin. This resulted in the calculated average to also be higher. We’ve been in communication with the Liquid team, and they have confirmed that the higher value was indeed legitimate.

We’re now in the process of changing the Wallet calculation so that both the Wallet and the ERT utilize the median of exchange values (and not the mean), to better guard against outliers.