Introducing ChainLinkResolver

Introducing ChainLinkResolver

Project
ChainLink
Oracle
4m

The ChainLinkResolver is a smart contract that serves as a registry for price feed contracts, designed to enhance decentralization, scalability, and gas efficiency for price computations on the blockchain. By leveraging multiple decentralized price sources, it enables the calculation of asset prices, even for pairs with different denominations. This opens new opportunities for DeFi applications, traders, and developers by providing flexible and reliable price data.

What is ChainLinkResolver?

At its core, ChainLinkResolver is a registry for ChainLink-like price feed contracts. The contract allows the owner to register price feeds for various asset pairs, and once registered, users can fetch prices directly or compute prices for pairs that are denominated differently. With ChainLinkResolver, you can:

  • Register price feeds (Owner only): The contract owner can register price feeds for different asset pairs (base and quote assets).
  • Fetch prices directly: Retrieve prices for an asset pair from an existing feed.
  • Compute derived prices: If a direct price feed is not available, compute the price for a pair by combining multiple feeds.

This contract supports multiple assets and can handle up to 255 unique assets, making it a flexible tool for projects that need to support various token pairs and pricing sources.

Core Components of ChainLinkResolver

1. ChainLinkResolver Contract

The ChainLinkResolver.sol contract is the central piece of the system, acting as the registry and management interface for all price feeds and asset configurations. It provides functions to register and deregister price feeds and manage the relationships between base and quote assets.

Key features include:

  • Price Feed Registration: Allows the contract owner to register price feeds for asset pairs (base and quote assets).
  • Price Querying: Enables querying of prices between assets, with the option to compute derived prices when direct feeds are unavailable.
  • Asset Management: Supports a registry of assets, with a unique ID assigned to each asset and the ability to track configurations for different pairs.

2. Price Computation with PriceConverter

The PriceConverter.sol library provides essential functions for price computation, ensuring accurate conversions between asset pairs that may have different decimal places. The library includes functions like derive, inverse, and scale that facilitate the calculation of derived prices by handling decimals efficiently.

  • derive: Computes derived prices between a base and quote asset, adjusting for different decimal places.
  • inverse: Inverts a price (i.e., computes the reciprocal), adjusting for decimal differences.
  • scale: Scales the price to ensure all assets are normalized to the same decimal places before performing calculations.

3. Bitmap: Efficient Asset Tracking

The Bitmap.sol library defines a custom Bitmap type for efficiently storing and managing asset configurations. Each bit in the bitmap represents an asset pairing, enabling quick lookups to see whether a price feed exists for a given pair.

4. Bytes32Lib: Packing and Unpacking Feed Data

The Bytes32Lib.sol library provides functions to pack and unpack price feed data into a bytes32 value. This compact data structure stores the feed address, asset IDs, and decimals, which can be easily retrieved and used for price calculations.

These libraries and contracts work together seamlessly to provide a robust system for handling price feeds, asset management, and price conversions, all designed with gas efficiency in mind.

Example: Price Querying

Let’s walk through how you can query prices using the ChainLinkResolver.

If there is a registered price feed for LINK and ETH, you can simply query the price directly.

If the direct feed is unavailable, you can compute the price using two intermediate prices:

  1. fetch LINK/ETH and BTC/ETH prices
  2. derive LINK/BTC price by combining LINK/ETH and BTC/ETH prices

Similarly, if you need the price of LINK in USDC, you can derive it from:

  1. fetch LINK/ETH and ETH/USD prices then invert ETH/USD to get USD/ETH price
  2. derive LINK/USD price, then fetch USDC/USD price
  3. derive LINK/USDC price by combining LINK/USD and USDC/USD prices

Conclusion

ChainLinkResolver provides a robust, decentralized solution for price feed management. With the ability to register, query, and compute derived prices across multiple asset pairs, it’s an essential tool for DeFi projects, traders, and developers. Its flexibility, scalability, and gas-efficient design make it an ideal choice for integrating decentralized price data into applications.

You can explore the source code and tests here.


Explore more posts