Introducing ChainLinkResolver
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
.
Case 1) LINK/ETH
If there is a registered price feed for LINK
and ETH
, you can simply query the price directly.
Case 2) LINK/BTC
If the direct feed is unavailable, you can compute the price using two intermediate prices:
- fetch
LINK/ETH
andBTC/ETH
prices - derive
LINK/BTC
price by combiningLINK/ETH
andBTC/ETH
prices
Case 3) LINK/USDC
Similarly, if you need the price of LINK
in USDC
, you can derive it from:
- fetch
LINK/ETH
andETH/USD
prices then invertETH/USD
to getUSD/ETH
price - derive
LINK/USD
price, then fetchUSDC/USD
price - derive
LINK/USDC
price by combiningLINK/USD
andUSDC/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.