Skip to content

ChromiaProject/price-oracle

Repository files navigation

Price Oracle

Get it working

  1. Add rpc urls in config/sample.node-config.properties and remove the prefix "sample."
  2. run command chr node start --node-properties config/node-config.properties
  3. Wait for the node to pick up events and thus set the prices

Summary

This code is designed to listen to EVM events, calculate and broadcast the price of tokens based on their presence and activity within a decentralized finance (DeFi) platform, like Uniswap. It listens to smart contracts on a EVM with the help of Chromias bridge to update token prices, liquidity pools, and other related data. Here’s a breakdown of its key functionalities:

  • Fetching price route information specific to the token. For each price route, it calculates the price based on the liquidity pools' reserves and the total value locked (TVL) in the pools. Each price route can consist of multiple liquitidy pools, ending with the pool of the desired currency (presumably USDC), thus successfully valuating the initial tokens price in the end currency. The final price of the token is a weighted average, where weights are the TVLs in each pool. The less TVL a pool has, the less its price is weighed in onto the final token price.

  • Broadcasting Price Changes: After calculating the new price of a token, it broadcasts this information to the other chains in the network with a global ICMF message ("G_token_price_changed").

  • Handling Liquidity Pool Events: It updates the reserves of tokens in liquidity pools based on events like sync, swaps, burns, and mints. This ensures that the price calculation is based on the latest state of the liquidity pools. When a pools information is updated, all affected tokens price changes are calculated and also broadcasted.

  • Management of Price Routes and Tokens: The code supports operations for adding new price routes, pools, and tokens to the system. This allows it to be flexible and scalable, accommodating new tokens and liquidity pools as they become available in the DeFi ecosystem.

  • Event Handling for Different Exchanges: It can handle events from Uniswap protocol V2 and V3 (maybe even V4 but not verified).

The code provides functionality for administrators to specify in the config or dynamically add new liquidity pools, price routes, and tokens to the system. This is crucial for maintaining an up-to-date and expansive DeFi platform that can accommodate the rapidly changing landscape of tokens and liquidity pools in the blockchain space. Users can:

  • Add New Liquidity Pools: By specifying details such as network ID, contract addresses of the tokens in the pool, decimal precision for each token, and initial reserves, users can register new pools. This allows the system to include these pools in its price calculations and event handling.

  • Add New Price Routes: Users can define price routes for tokens, which are sequences of liquidity pools through which a token’s price is calculated relative to another token. This is essential for tokens that do not have direct trading pairs but can be valued through a series of swaps across multiple pools.

  • Add New Tokens: The system allows the addition of new tokens by specifying their name, symbol, and optionally, a price route for calculating its price relative to other tokens in the ecosystem. This ensures the platform remains relevant and comprehensive, covering a wide array of tokens available in the market.

  • Add Token Instances: For tokens that exist on multiple networks or have multiple instances, the system supports the addition of these instances, ensuring that price calculations and broadcasts accurately reflect the diverse environments in which these tokens operate.

Overall, this code serves as a comprehensive solution for managing and broadcasting token prices in a DeFi ecosystem, making it a valuable tool for developers building DeFi applications or platforms. These features ensure the system is adaptable and can grow with the DeFi ecosystem, allowing users to contribute to its accuracy and comprehensiveness. By empowering users to add new liquidity pools, price routes, and tokens, the platform can quickly adapt to new developments and ensure that its price information is as accurate and up-to-date as possible.

Example:

We can track the CHR tokens price on BSC and Ethereum, where we have the PancakeSwap DEX on BSC and Uniswap on Ethereum. Since PancakeSwap already has a CHR-USDC pair, we do not need a longer price route. But on Uniswap we must use the route CHR-ETH, ETH-USDC. Thus in total we will be monitoring 3 Pools. Everytime there is a change in any of the pools, the price will be broadcasted using ICMF to the network. The price can also be queried at any point through queries.

Packages

 
 
 

Contributors