How to Fetch the Current Price of Ethereum, Bitcoin, and Other Cryptocurrencies in Solidity

This article was first published on Chainlink

How to Fetch the Current Price of Ethereum, Bitcoin, and Other Cryptocurrencies in Solidity

Fetching price data into a Solidity smart contract is a common requirement of DeFi applications. To get the current price of Ethereum, Bitcoin, or other cryptocurrencies in Solidity, you can either:

  • Fetch price data using Chainlink Price Feeds
  • Fetch price data using an external API via a Chainlink oracle

In this technical tutorial, we’ll walk through both approaches and share code examples to help you build, deploy, and test your smart contract. First, let’s quickly cover the importance of data quality and end-to-end decentralization when feeding external inputs into your smart contracts.

The Need for Accurate and Reliable Prices in Smart Contracts

This year has seen an explosive growth in DeFi protocols, with the total value locked (TVL) in DeFi skyrocketing from $680M in January to now over $14B. These DeFi protocols rely on outside price data as their data source, because a blockchain cannot natively access external data. In addition to this, these applications need to provide data quality guarantees to protect their applications from exploits such as price oracle attacks.

Chainlink Price Feeds mitigate the risk of these attacks by providing aggregated data from various high quality data providers, fed on-chain by decentralized oracles on the Chainlink Network. Chainlink’s decentralized oracle mechanism ensures the final price values reflect broad market coverage, meaning the final price is determined after aggregating a diverse set of prices across the entire market, rather than just a small subset, while also taking into account other aspects such as volume and liquidity.

Now that we understand the need for accurate and reliable price data in Solidity smart contracts and the important role that Chainlink Price Feed oracles play, we’ll go through an example of using a Chainlink Price Feed to obtain the latest price of Ethereum in a Solidity smart contract. The steps below ...

To keep reading, please go to the original article at:

Comments (No)

Leave a Reply