Obtaining pricing data in Solidity smart contracts is a common requirement for DeFi applications. Solidity to get ETH, BTC or other encryption currency prices, you can be done in one of two ways:
Use the Chainlink price feed to get pricing data
Use external API to get price data via Chainlink oracle
In this technical tutorial, we will explain these two methods in detail, and share code samples to help you create, implement, and test smart contracts. First, let's quickly understand the importance of data quality and end-to-end decentralization when providing external data for smart contracts.
Smart contract request for accurate and reliable pricing data
This year, the DeFi agreement has exploded. DeFi's total locked value has jumped from USD 680 million in January to USD 14 billion now. The DeFi protocol relies on external price data as its data source, because the blockchain cannot directly access external data locally. In addition, these applications need to provide data protection to protect the application from vulnerabilities, such as oracle price attacks.
The Chainlink price feed can reduce the risk of these attacks by providing data collected from multiple data providers of high quality, and feed the data back in the chain via decentralized oracles on the Chainlink network. Chainlink's decentralized oracle mechanism can ensure that the final price data reflects a broad range of market information, which means that the final price data is determined after combining various price data across markets, not just a fraction of the information, while also considering other factors, such as volume. trade and liquidity.
Now that we understand the demand for accurate and reliable pricing data from Solidity smart contracts and the important role that the Chainlink price feed oracle plays, we will describe in detail the use cases of Chainlink's feed price to get the latest Ethereum price data. The following steps can also be applied to Bitcoin and other cryptocurrencies.
Use Chainlink to feed prices on Ethereum
Chainlink feed prices use a variety of high-quality data inputs, and combine this data through Chainlink's decentralized oracle network, and enter price data into reference contracts. The results of this data will be combined in the Aggregator smart contract. The most recent price data serves as a credible answer. By using multiple data sources collected by multiple nodes, we can ensure that our pricing data is of the highest quality and will not be exposed to vulnerabilities or oracle price attacks.
Create a smart contract
The first step is to get the ETH testnet as gas charge for the smart contract. After earning some ETH, the easiest way to create a smart contract that uses Chainlink to feed the price is to start with a standard price consumer contract. This is the base contract used to initiate a price feed request for Chainlink. This section is for demonstration purposes. We'll be using the ETH / USD feed price specified in a standard price consumer contract, but we'll explain the code in this section in detail so you can understand how it works.
First, we can see that the AggregatorV3Interface contract interface is imported. This way, our smart contracts can reference the price feed contracts used in the Kovan test network. Next, we instantiate the contract and store it in a local variable.
Next, we can see that a price feed reference contract is initialized in the constructor. The ETH / USD price feed reference contract on the Kovan testnet is implemented at the address 0x 9326BFA02ADD2366b30bacB125260Af641031331.
Then we look at predefined functions to get the most recent price data from the price feed aggregation contract, created in the constructor mentioned above. To get the price, we define a new function to call the latestRoundData function from the Aggregator contract. This is a function that returns the current status of the Aggregator contract. In this example, we are taking the current price and returning it to the consumption function.
Apply and test smart contracts
Now we are ready to implement 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 of your MetaMask wallet containing the previously acquired ETH, click the apply button, and follow the steps. Just do it. The end result is that you apply a smart contract to the Kovan testnet. You need to record the address of the applied contract which is recorded via the transaction output in the Remix console.
After implementation, we only need to run the "getLatestPrice" function. The result must be the most recent price returned by the function of the ETH / USD Aggregator contract, and then this data can be called to the chain in our smart contract. Note that we don't need to send any LINK tokens for the request, or use any ETH. This transaction is purely for reading data in the ETH-USD aggregator contract in the chain.
ETH / USD price feed results
Get more price data on Solidity
The Chainlink price feed provides bulk price data on DeFi price pairs located in the head. If the asset price data you need to acquire in your smart contract is not covered by an existing price feed contract, such as a specific share price, you can customize the Chainlink oracle to call any external API. The easiest way to use the Chainlink oracle to get external pricing data is to start with the standard APIC Chainlink consumer contract. This is to initiate standard contracts for external data requests via the Chainlink oracle.
After creating an APIC consumer contract, you can search for the API for which you want to get price data. For example, you can use the Alpha Vantage API to find the current IBM stock price.
With the API endpoint, you just need to set the correct parameters in the contract and send a request to the Chainlink oracle. You can find more information about this and how to use standard APIC consumer contracts in the Chainlink documentation.
Please note that in a production environment you will want to decentralize this usage method or get this specific data from the price feed contract itself.
To conclude
The Chainlink price feed provides a reliable way to get Bitcoin, Ethereum and other high-quality cryptocurrency price data into Solidity smart contracts. In addition, the Chainlink oracle framework also provides flexibility to obtain other price data quickly and easily, such as data on stock prices, commodities and other assets.