How to get the current price of ETH, BTC and other cryptocurrencies in Solidity

Get Price Data Using Chainlink's Price Feed
Get price data using an external API through Chainlink oracles
first level title
The Need for Accurate and Reliable Price Data in Smart Contracts
DeFi protocols have experienced explosive growth this year, and the total locked value of DeFi has surged from $680 million in January to $14 billion now. These DeFi protocols rely on external price data as a data source, because the blockchain cannot directly access external data locally. In addition, these applications need to provide data assurance to protect applications from exploits, such as price oracle attacks and other attacks.
Chainlink price feeds mitigate the risk of these attacks by providing data aggregated from multiple high-quality data providers and feeding the data on-chain through decentralized oracles on the Chainlink network. Chainlink's decentralized oracle mechanism ensures that the final price data reflects a broad coverage of market information, which means that the final price data is determined after aggregating various price data in the entire market, not just a small part Information, but also take into account other factors, such as volume and liquidity.
first level title
Using Chainlink to Feed Prices on Ethereum
first level title
Create a smart contract
The first step is to obtain the ETH of the testnet as the gas fee for the smart contract. After earning some ETH, the easiest way to build a smart contract that uses Chainlink to feed prices is to start with the standard price consumer contract. This is the base contract used to initiate price feed requests to Chainlink. This part is for demonstration purposes, we will use the ETH/USD price feed already defined in the standard price consumer contract, but we will explain this part of the code in detail so you can understand how it works.
First, we can see that the AggregatorV3Interface contract interface is imported. In this way, our smart contract can reference the price feed contract deployed on the Kovan test network. Next, we create a contract instance and store it in a local variable.

Next, we can see that the price feed reference contract is initialized in the constructor. The ETH/USD price feed reference contract on the Kovan testnet is deployed at address 0x9326BFA02ADD2366b30bacB125260Af641031331.

first level title

Deploy and test smart contracts
Now we are ready to deploy and test our contract. Compile the contract in Remix, then on the "Deploy" option, change the environment to "Injected Web3", and make sure that the wallet address below is the wallet address that you obtained ETH earlier in your MetaMask wallet, click the deploy button, and follow the steps Just operate. The end result is that you deployed your smart contract to the Kovan testnet. It is necessary to record the deployed contract address recorded through the transaction output in the Remix console.
image description
first level title
Get additional price data in Solidity
Chainlink Price Feed provides a large amount of price data on top DeFi price pairs. If you need asset price data in your smart contract that is not covered by existing price feed contracts, such as the price of a specific stock, you can customize the Chainlink oracle to call any external API. The easiest way to use a Chainlink oracle to get external price data is to start with the standard Chainlink APIConsumer contract. This is a standardized contract that initiates requests for external data through Chainlink oracles.
After creating the APIConsumer contract, you can find the API you want to get price data from. For example, you can use the Alpha Vantage API to find the current price of IBM stock.
Once you have an API endpoint, you just need to set the correct parameters in the contract and send the request to the Chainlink oracle. More information on this and how to use the standard APIConsumer contract can be found in the Chainlink documentation.
Summarize
Summarize
Chainlink price feeds provide a reliable way to get high-quality Bitcoin, Ethereum, and other cryptocurrency price data into Solidity smart contracts. In addition, Chainlink’s oracle framework provides the flexibility to quickly and easily obtain other price data, such as those of stocks, commodities, and other assets.


