HomeCoinsChainlink (LINK)Get Index Prices in Solidity Smart Contracts

Get Index Prices in Solidity Smart Contracts

- Advertisement -

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.

Website | Twitter | Reddit | YouTube | Telegram | Events | GitHub | Price Feeds | DeFi



Source link

- Advertisement -
Mr Bitcointehttps://www.bitcointe.com/
“Fact You Need To Know About Cryptocurrency - The first Bitcoin purchase was for pizza.” ― Mohsin Jameel
462FansLike
76FollowersFollow
4,567FollowersFollow
5,261FollowersFollow
1,499FollowersFollow
2,230SubscribersSubscribe

Most Popular

bitcoin
Bitcoin (BTC) $ 62,634.00
ethereum
Ethereum (ETH) $ 3,880.38
tether
Tether (USDT) $ 1.00
bitcoin-cash
Bitcoin Cash (BCH) $ 617.95
litecoin
Litecoin (LTC) $ 187.06
eos
EOS (EOS) $ 4.51
okb
OKB (OKB) $ 16.11
tezos
Tezos (XTZ) $ 6.59
leo-token
LEO Token (LEO) $ 2.96
cardano
Cardano (ADA) $ 2.17
monero
Monero (XMR) $ 264.26
stellar
Stellar (XLM) $ 0.393915
chainlink
Chainlink (LINK) $ 26.57
huobi-token
Huobi Token (HT) $ 7.52
tron
TRON (TRX) $ 0.099322
usd-coin
USD Coin (USDC) $ 1.00
dash
Dash (DASH) $ 190.72
neo
NEO (NEO) $ 43.49
iota
IOTA (MIOTA) $ 1.24
nem
NEM (XEM) $ 0.169319
zcash
Zcash (ZEC) $ 138.51
maker
Maker (MKR) $ 2,533.76
paxos-standard
Pax Dollar (USDP) $ 1.00
ethereum-classic
Ethereum Classic (ETC) $ 53.35
vechain
VeChain (VET) $ 0.122626
true-usd
TrueUSD (TUSD) $ 1.00
ftx-token
FTX Token (FTT) $ 56.29
kucoin-shares
KuCoin Token (KCS) $ 13.45
waves
Waves (WAVES) $ 29.05