avatarAlex Roan

Summary

Chainlink Price Feeds provide a fast, reliable, and easy way to retrieve off-chain price data in a single transaction for smart contracts in the DeFi space.

Abstract

The article discusses the need for real-time price data in decentralized finance (DeFi) protocols and how Chainlink Price Feeds can fill this gap. It explains the concept of locked-up value in DeFi and how it is dependent on the price of the underlying asset, using the example of SAI, a stablecoin backed by ETH. The article then introduces Chainlink Price Feeds as a solution, which are on-chain resources containing price data aggregated from multiple independent sources. The article provides an example of how to use the ETH/USD price feed on the Kovan testnet, including code snippets and explanations. It concludes by highlighting the benefits of Chainlink Price Feeds, such as speed, reliability, and ease of use, and encourages developers to utilize them in the Ethereum space.

Bullet points

  • DeFi protocols need real-time price data for their operations, such as minting stablecoins.
  • Chainlink Price Feeds provide a solution by aggregating price data from multiple sources and making it available on-chain.
  • Chainlink Price Feeds are faster and more reliable than other Oracle solutions.
  • The article provides an example of how to use the ETH/USD price feed on the Kovan testnet.
  • Chainlink Price Feeds are easy to use and underutilized in the Ethereum space.
  • Chainlink Price Feeds offer benefits such as speed, reliability, and ease of use.
  • Developers are encouraged to utilize Chainlink Price Feeds in their smart contracts.

How to Retrieve Price Data in Smart Contracts

Get crypto prices in a single function call

Photo by Miloslav Hamřík on Pixabay.

The price of a LINK token has all but doubled in the past month, moving from around a steady $4 to a whopping $8 in a very short time. This is in no small part down to the Chainlink Oracle network, which provides a platform to call any external API from within smart contracts as well as a Verifiable Random Function for Solidity.

While both of these products are extremely powerful tools, they follow the Request & Receive cycle, which is not instantaneous. This means that any smart contract using them has to request the data in one transaction, then wait to receive the response in another.

This is great, but there is an even faster way to retrieve off-chain data in a single transaction.

The Need for Speed

At the time of writing, the amount of value locked in the DeFi ecosystem is over $3 billion. But what does “locked up value” mean for those of us who are unfamiliar with the economics?

Essentially, it represents collateral for other assets, like stable coins. Any decentralised stable coin has other assets backing it, so if anyone wants to redeem their coin for fair value, they can.

How to print money

Let’s walk through the example of SAI.

SAI

SAI is backed by ETH. This means that if you want to obtain SAI, you need to deposit ETH into the MakerDAO protocol. Once deposited, the protocol will mint the equivalent value in SAI to you, depending on the price of ETH and the amount you deposit.

For example: If ETH is worth $200 at the time and you deposit 2 ETH, the protocol mints 400 SAI.

Deposit 1 ETH, mint 200 SAI.

If the price of ETH changes, the amount of SAI that gets minted will change too. So if ETH is worth $100 and you deposit 1 ETH, it only mints 100 SAI.

Since the protocol is dependent on the price of ETH, a reliable data source to track the price in real-time must be readily available to maintain equilibrium in the system. The Request and Receive cycle might be OK, but because it doesn’t retrieve the price in a single transaction, it can’t be part of the minting function. It would either have to be tracked by some other function (meaning that when SAI is minted, the protocol’s knowledge of the price of ETH might be slightly out of date) or the minting process would need to span over multiple transactions (meaning the depositor would have to wait to receive their SAI).

Filling the Gap

Fortunately, there is a solution, and it comes in the form of Chainlink Price Feeds.

Price feeds are on-chain resources containing price data that is aggregated from multiple independent sources. They’re fast, reliable, executed in a single transaction, and easier to implement than any of the other Oracle solutions discussed so far.

How to use price feeds

These feeds are curated especially for the DeFi space, and a full list of all the feeds that are currently available can be found in the Chainlink developer documentation.

For this example, we’re going to use the ETH/USD price feed on the Kovan testnet:

Let’s walk through this code:

  • Line 3: Chainlink provides a bunch of useful contracts in their contract library, and we’re importing the AggregatorInterface. This contract defines a few functions that enable our contract to request information from it. (Note: as of August 2020, AggregatorInterface is deprecated. Please use AggregatorV3Interface .)
  • Lines 14-16: In the constructor, we initialize our priceFeed state variable with a new AggregatorInterface and the address of the feed we want to use. In this case, it’s the ETH/USD feed on the Kovan testnet.
  • Lines 21-23: Here, we get the latest price by calling priceFeed.latestAnswer(). It returns the latest price aggregated from multiple sources.
  • Lines 28-30: We can also get the timestamp of when the price was last updated in the aggregator.

Conclusion

Chainlink price feeds are extremely easy to use and, in my opinion, underutilized in the Ethereum space at the moment. They provide the quickest, easiest, and most reliable price data that your smart contracts can access.

Learn more from the official Chainlink blog post:

Further Reading

If you’re interested in blockchain development, I write tutorials, walkthroughs, hints, and tips on how to get started and build a portfolio. Check out some of these resources:

Programming
Blockchain
Ethereum
Smart Contracts
Cryptocurrency
Recommended from ReadMedium
avatarTitania Research
MEV 101 : How to dive into MEV

10 min read