Get Index Prices in Solidity Smart Contracts

DeFi asset prices can experience large amounts of volatility, especially in illiquid markets. This is a major reason why it’s important to build smart contracts around aggregated price data that reflects broad market coverage and that is highly resistant to low liquidity price source manipulations. Volatility is an important consideration for both developers integrating an oracle into their dApps, as well as for consumers who wish to diversify their holdings to gain broader market exposure. For some, this is where index funds help.

Index funds or indices are financial products that track a group of assets rather than singular assets. By grouping together multiple assets, index funds offer consumers a simple and cheaper way to gain diversified exposure. With indices, consumers can place one purchase with one fee that instantly grants them ownership of an entire basket of assets. When compared to buying multiple assets individually or delegating funds to a manager, indices are both simpler and cheaper.

Just as in traditional finance, DeFi users want access to indices for easy diversification. As with all DeFi products, accurate and reliable pricing data is critical. If an index price is manipulated and artificial volatility is generated, the purpose of the index is defeated and it no longer offers a consistent haven for investors. An index could be composed of many highly liquid assets with low volatility but if the price feed for that index is illiquid or susceptible to manipulation, an attacker would only need to manipulate that price feed rather than the prices of the assets in the index. As such, the oracle mechanism feeding an index price becomes a potential single source of failure in DeFi indices.

Thankfully, Chainlink’s widely adopted Data Feeds solve this problem by providing wide market, pre-aggregated decentralized price oracles for traditional indices such as FTSE 100 (Financial Times Stock Exchange index), Nikkei 225 (Tokyo Stock Exchange index), sCEX (Synthetix exchange index), and DeFi indices such as sDeFi.

In this guide, we’ll cover how to quickly retrieve Chainlink’s tamper-proof index price feeds in your smart contract so you can build more secure DeFi applications around new on-chain datasets.

Initialize the Feeds

The first step with any Chainlink Data Feed is importing the aggregator interface and then initializing the feeds with the correct address in the constructor. In this example, we’re initializing with the address of the FTSE 100 feed on Ethereum but any other feed address could be used.

Note: this tutorial is using Remix syntax. For nodejs/python, refer to the Chainlink documentation.

pragma solidity ^0.6.7;

import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";

contract indexFeed{

    AggregatorV3Interface internal ftseFeed;

    /**
     * Aggregator: FTSE
     * Address: 0xE23FA0e8dd05D6f66a6e8c98cab2d9AE82A7550c
     */
    constructor() public {
        ftseFeed = AggregatorV3Interface(0xE23FA0e8dd05D6f66a6e8c98cab2d9AE82A7550c);
    }

Retrieve Values

Once the feed is initialized, we only need one function to retrieve the price data.

//Returns the latest Supply info
    function getLatestPrice() public view returns (int) {
        (
            uint80 roundID, 
            int price,
            uint startedAt,
            uint updatedAt,
            uint80 answeredInRound
        ) = ftseFeed.latestRoundData();
        return price;
    }
}

Since the data is already aggregated and committed on-chain by the network of Chainlink oracle nodes, we only need a simple view function to access the data so there is no gas cost. There are some other fields like the ID and time of the aggregation round in which the data was retrieved but in this case, it’s only the price we care about.

Conclusion

Index funds help investors easily and cheaply diversify, reducing volatility by grouping assets together. However, DeFi products that offer these indices are susceptible to manipulation if they use a centralized or low-quality source of pricing data for the overall index. Chainlink ensures both data quality and secure data delivery by making it exceptionally simple for smart contract developers to retrieve index price data that’s aggregated across hundreds of sources and validated by a decentralized network of security-reviewed oracle nodes. DeFi users demand a wide array of robust financial products and Chainlink is there to help developers provide the most secure and reliable version of them.

If you’re a developer and want to integrate Chainlink into your smart contract applications, check out the Chainlink developer documentation or reach out here.

Need Integration Support?
Talk to an expert
Faucets
Get testnet tokens
Read the Docs
Technical documentation