Integrating Supra Oracles into Bonzo Finance on Hedera

Bonzo Finance Labs's picture
Bonzo Finance Labs
March 22, 2024

In the rapidly evolving world of decentralised finance (DeFi), the Bonzo Finance team is thrilled to work with some of the best protocols in the Hedera ecosystem. Our journey of building Bonzo has led us to a pivotal integration with Supra Oracles, marking a significant milestone in our quest to deliver a robust and reliable lending protocol on a platform known for its high throughput and low fees.

This post delves into the essence of our integration, focusing on how we've adapted Supra oracles to replace Chainlink's oracle services, given their unavailability on Hedera.

Background: The Need for Oracle Services

A Smart Contract written on a blockchain network is a standalone software with two limitations. Firstly, it cannot interact on its own with anything outside the blockchain. Secondly, it cannot PULL information elsewhere; you always need to PUSH information.

Oracles bridge blockchains and external data sources, enabling smart contracts to interact with off-chain data. A lending protocol like ours heavily depends on the current, real-world prices of assets (tokens) that the users are lending and borrowing. Hence, oracles are crucial for fetching real-time asset prices, ensuring that loans are issued and managed based on accurate market values.

This functionality underpins the security and efficiency of lending operations. If we don’t use a reliable Oracle service, there is a risk of undue liquidations. On the other hand, there is also a problem of insufficient collateral being available to borrow.

Why Supra Oracles?

On alternative networks, such as Ethereum and related L2s, Chainlink oracles have a leading market share and have become a defacto option for many Decentralised Applications. They are not an option on Hedera today, but Supra oracles has just launched on the network.

Supra offers a decentralised infrastructure for providing secure, reliable, and timely data to smart contracts. Their Push oracle model, detailed in Supra's documentation, allows us to seamlessly incorporate live asset price data into our protocol, ensuring our lending services remain competitive and trustworthy. Notably, the PUSH model opens the door to many protocols for Hedera (other than Bonzo) to receive the data and opens up even more avenues for collaboration.

To overcome the limitations of current Oracle solution implementations, the Supra team has developed an innovative solution dubbed DORA (Distributed Oracle Agreement). According to their documentation, this is how it works -

DORA natively employs the most performant consensus algorithm in to the oracle space, and aggregates representative price data (S-value) for a wide array of assets and commodities. To do this, the protocol validates and reports prices derived from up to 21 data sources with Byzantine Fault Tolerant algorithms. Employing a unique Tribe-Clan architecture, DORA is further strengthened and safeguarded by anti-collusion randomness algos (via our decentralised Verifiable Random Function, or dVRF) to ensure the decentralisation qualities of the network persist over time.

Supra supports a wide range of assets, including Hedera Token Service (HTS) tokens native to projects in the Hedera ecosystem. The HTS assets supported by Supra today include:

  • $HBAR
  • $HBARX
  • $SAUCE
  • $XSAUCE
  • $JAM
  • $HST
  • $DOVU
  • $HELI
  • $CLXY

Implementation Overview

Our integration with Supra oracles is encapsulated within our bespoke `SupraOracle` contract. Here's a breakdown of its core components and functionalities:

  • ISupraSValueFeed Interface: At the heart of our oracle implementation lies the `ISupraSValueFeed` interface, enabling our contract to interact with Supra's data feeds. This interface is crucial for fetching the price data of supported assets.
  • Contract Initialization: Our constructor initialises the `SupraOracle` with a specific Supra value feed, setting the stage for real-time data retrieval. This value feed depends on the chain and the market from which you want to fetch the prices.
function getAssetPrice(address _asset) public view returns (uint256 price) {

    uint16 priceIndex = getPriceIndex(_asset);

    ISupraSValueFeed.priceFeed memory priceFeed = sValueFeed.getSvalue(priceIndex);

    return priceFeed.price;

}
  • Asset Price Retrieval: The `getAssetPrice` function is designed to fetch the current price of a supported asset. It uses a predefined index to query the Supra value feed, translating the asset's address to its corresponding price index on Supra's platform. This function embodies our protocol's adaptability, catering to various assets, including CLXY, HBARX, SAUCE, DAI, and USDC. It is important to note that the price returned by this function is denominated in the protocol's base currency, HBAR.
  • Price Conversion: Recognizing the need to translate raw price data into usable formats, our implementation includes functions like `getAssetPriceInUSD` and `convertTinybarsToTinycents`, which convert blockchain-specific values into USD and other relevant units. These conversions are vital for maintaining the protocol's relevance to users and external systems.

Technical Highlights

Use of the Hedera precompile contract

function tinybarsToTinycents(uint256 tinybars) internal returns (uint256 tinycents) {
  
  (bool success, bytes memory result) = PRECOMPILE_ADDRESS.call(
  abi.encodeWithSelector(IExchangeRate.tinybarsToTinycents.selector, tinybars));
  
  require(success);
  
  tinycents = abi.decode(result, (uint256));
}

The Supra Oracles provide the price in terms of HBAR, which is the native currency of the Hedera blockchain. We have written a helper contract with a function that converts tinybarsToTinycents, which converts the price from HBAR to USD. This function then returns the price for an asset in USD, which can be used in the DAPP UI. This function calls the Hedera precompile address at 0x168 on the Testnet to fetch the exchange rate.

Looking Ahead: Pyth Integration

As Bonzo Finance continues to evolve and to have more redundancy in our protocol, we're also working on integrating with the Pyth oracle network to diversify our data sources and enhance our service offerings. This upcoming phase signifies our commitment to innovation and adaptability in the DeFi space.

Conclusion

Integrating Supra oracles into Bonzo Finance represents a significant stride towards realising our vision of a highly efficient, secure, and user-friendly lending protocol on Hedera Hashgraph.

By leveraging Supra Oracles' capabilities, we're enhancing our platform's functionality and paving the way for future expansions and integrations. As we continue to innovate and evolve, the Bonzo Finance team remains dedicated to contributing to the DeFi ecosystem's growth and accessibility.

Share this post on