How to Retrieve Price Data in Smart Contracts
Get crypto prices in a single function call

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 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:






