DEV Community

Cover image for Leveraging Hedera’s Architecture: Sirio’s Oracle-Free Price Feed Approach
Hedera Team
Hedera Team

Posted on • Originally published at hedera.com

Leveraging Hedera’s Architecture: Sirio’s Oracle-Free Price Feed Approach

By Mica Cerone & Maraglino Giacomo

Decentralized Finance (DeFi) is one of the fastest-growing sectors in the Web3 ecosystem, reshaping how we define financial systems. At the heart of its success are critical tools like oracles—data providers that bring real-world, off-chain information on-chain to be used in smart contracts. Oracles integrally power the creation of various decentralized applications (dApps). However, oracles have only become available on the Hedera network as of Q3 of 2024.

Oracles are crucial to Sirio Finance, a lending and borrowing protocol built on Hedera. To proceed with developing our protocol without access to oracles, we developed an alternative, oracle-free and fully on-chain mechanism, designed to fetch information that’s typically only provided by oracles. This allowed us to construct and test our application without oracle integrations, leading us to create a proprietary method that we will henceforth implement in our active platform as a backup to the oracle solution to enhance our protocol’s security.

This article will explore our strategy to bypass oracles in order to fetch token prices, which could previously only be done through oracles. The development of this method was only possible due to Hedera’s unique architecture. Let’s dig in!

How Do Oracles and Smart Contracts Work Together?

By design, smart contracts are isolated programs that are deployed to a network but that cannot access or verify data outside their native network. This limitation is where oracles come into play, serving as a bridge that connects smart contracts with data from the external world. They are decentralized entities that gather data from multiple sources and feed it to the smart contracts to be used for on-chain activities. The integration of oracles into smart contracts involves the following key steps:

  • Data Request: The smart contract generates a request for data; for example, the current price of a cryptocurrency, weather conditions, or any other real-world information.
  • Oracle Query: The request is then sent to an oracle or a network of oracles.
  • Data Verification: Once the data is retrieved, it undergoes a verification process.
  • Data Delivery: Next, the verified data is fed back to the smart contract.
  • Execution: With the necessary data as input, the smart contract automatically executes the predefined logic. The following diagram explains how oracles and smart contracts work together:

Image description

On-Chain Price Feeds System

When Sirio started developing its protocol, oracles were not yet available on the Hedera network, which prevented the ability to query token prices. Since this is an integral part of our protocol, we developed an alternative system that bypasses the need for oracles to obtain the price of tokens at market value. The mechanism is structured in three steps:

  1. Get the current price of the given token in HBAR.
  2. Calculate the mean of the historical price over a specific interval and frequency; this mechanism is also defined as TWAP (Time Weighted Average Price).
  3. Convert the price of the token from HBAR to USD. Using this mechanism, we could accurately implement token prices at market value within our protocol, allowing users to lend and borrow their assets. Let’s dive into the details!

1. Token-HBAR Price
The first step necessary to define the token’s market price is to extract its value from the liquidity pools (LPs) in which it’s paired. As seen in the analytics section on SaucerSwap, the LPs with the most liquidity have an HBAR pair. To explain how we defined the price in HBAR of each token, refer to SaucerSwap’s market-making architecture, which uses the constant product function defined as follows:

X * Y = K

Where:

  • X = Quantity of Asset X
  • Y = Quantity Asset Y
  • K = Constant (Quantity of Asset X * Quantity of Asset Y) The price of tokens in a SaucerSwap LP is determined by the ratio of the quantities of the two tokens in the pool. If you have a pool with Token A and Token B, the price of Token A in terms of Token B is calculated by dividing the quantity of Token B by the quantity of Token A, and vice versa.

Image description

Our smart contracts implement logic that first retrieves the latest Token/HBAR exchange rate for each token. Next, they convert the amount of HBARs needed to purchase one unit of the token into a time-weighted value using the TWAP system.

2. TWAP System
TWAP, or Time-Weighted Average Price, calculates the average price of an asset (e.g., a token) over a specific time period. This method ensures a more stable and reliable price estimate by considering multiple price points within the duration, rather than relying on a single snapshot.

One of the key advantages of TWAP is its effectiveness in reducing the risk of price manipulation by large traders, often referred to as "whales." By averaging prices over a specified timeframe, TWAP minimizes the impact of short-term price spikes or drops caused by large trades or market volatility.

For example, instead of using a single price point that could be artificially influenced, TWAP calculates an average using price data captured at regular intervals (e.g., hourly over 24 hours). This provides a more accurate price feed, which is essential for protocols managing lending, borrowing, and liquidation processes.

TWAP is calculated using the formula:

Token Price = (P1 × T1 + P2 × T2 + ... + Pn × Tn) / (T1 + T2 + ... + Tn)

Where:

P1​, P2​, …, P24​ represent the token prices captured at regular intervals (e.g., every hour over 24 hours).
T1, T2, …, T24​ represent the time intervals corresponding to each price observation.
Using this formula, we calculate the Time-Weighted Average Price (TWAP) for all our assets. This approach considers the prices (P) and time intervals (T) over the past 24 hours, with data points captured every 1 hour.

3. HBAR/USD Conversion
After calculating the time-weighted amount of HBAR required to purchase one unit of each token, we convert it into its dollar equivalent using the HIP-475 System Contract. HIPs are pre-compiled contracts designed to streamline interactions with Hedera through the Solidity programming language. HIP-475 specifically uses Hedera's Exchange Rate Tool (ERT) to determine the HBAR/USD exchange rate.

For each token, we call a HIP-475 function that takes an HBAR quantity as input and returns the corresponding value in USD.

We use the following function to make the conversion:
interface IExchangeRate {
function tinybarsToTinycents(uint256 tinybars) external returns (uint256);
}

The function's output is the Token/USD Exchange Rate, ready to be used in our smart contracts. We can resume the three steps mentioned above in the following diagram flow:

Image description

This approach ensures that token prices are accurately determined in a secure and decentralized manner, forming a reliable foundation for our protocol’s lending and borrowing functionalities.

Conclusion

The journey to building a decentralized and secure lending and borrowing protocol on Hedera has been both innovative and adaptive. Faced with the absence of oracles during the initial stages of development, Sirio Finance engineered an oracle-free, fully on-chain solution that reliably calculates token prices using TWAP and Hedera’s unique architecture. This system not only enabled the protocol's development and testing but also now serves as a robust backup mechanism to complement Hedera's newly available oracle integrations.

By combining oracle solutions with our proprietary price-fetching mechanism, Sirio Finance ensures enhanced reliability, resilience, and security for its users. This dual-layered approach underscores our commitment to leveraging Hedera's cutting-edge features while continuously optimizing our protocol to provide a seamless DeFi experience. With these innovations, we are proud to set new standards for transparency and robustness in DeFi finance.

About Sirio
Sirio Finance is a pioneering lending and borrowing protocol that leverages AI to revolutionize risk management. At its core, the team is developing an advanced machine-learning algorithm to mitigate liquidation events seamlessly and permissionlessly.

With a strong focus on user experience and cutting-edge risk management, Sirio's proprietary machine-learning model sets a new standard in the industry. The protocol's vision extends to decentralizing and open-sourcing this technology, reinforcing its commitment to advancing decentralized AI solutions.

This forward-thinking approach has fostered key partnerships across the ecosystem, including collaborations with Filecoin, HbarSuite, HashPack, SaucerSwap and Kabila. Sirio's innovation has also earned recognition through a grant from The HBAR Foundation, solidifying its position as a leader in the decentralized finance space.

Sirio aims to launch on mainnet by the beginning of 2025. Before we go live, we invite you to become familiar with our Testnet version.

To get involved in the Sirio community and stay in the loop on incoming opportunities, we invite you to join our Discord Server or follow us on X; you can also check more resources from Sirio (testnet, website, Medium, social media, etc.).

Top comments (0)