Loading...
Hello, World!
Let's get started with the intro of the chainlink , basic defination of the data feed , assets and many more technical word .
What is Chainlink ?
Chainlink greatly expands the capabilities of smart contracts by enabling access to real-world data and off-chain computation while maintaining the security and reliability guarantees inherent to blockchain technology. In simple word's Chainlink provides you the real data feeds of the assets like ETH/USD , EUR/USD and many more , for the full and clear infromation u can also see their live docs avialable on the https://chain.link/
What is assets ?
Asset is basically digital currency in you can invest and hold some equity . In simple word assets are the digital money or currency .
What is Data feed ?
Data feed is basically the real value price of the assets we are getting through the chainlink.
Each data feed is updated by a decentralized oracle network. Each oracle operator is rewarded for publishing data. The number of oracles contributing to each feed varies. In order for an update to take place, the data feed aggregator contract must receive responses from a minimum number of oracles or the latest answer will not be updated. You can see the minimum number of oracles for the corresponding feed at chainlink.
For more you can read https://docs.chain.link/docs/architecture-decentralized-model/ .
Let's understand the baic code for the chainlink data feed of ETH/USD using remix .
/**
* Network: Rinkeby
* Aggregator: ETH/USD
* Address: 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceConsumerV3 {
AggregatorV3Interface internal priceFeed;
/**
* Network: Rinkeby
* Aggregator: ETH/USD
* Address: 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
*/
constructor() {
priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
}
/**
* Returns the latest price
*/
function getLatestPrice() public view returns (int) {
(
/*uint80 roundID*/,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData();
return price;
}
}
The above code is written in the remix ide , lets understand the line by line code of this .
Note : If u don't know how to use Remix ide you can read this https://remix-ide.readthedocs.io/en/latest/create_deploy.html
and if u are not familiar with the solidity then read from here https://dapp-world.com/smartbook/solidity-quick-guide-0Dm5
In the above code I am importing AggregatorV3Interface.sol
An aggregator is the contract that receives periodic data updates from the oracle network. Aggregators store aggregated data on-chain so that consumers can retrieve it and and act upon it within the same transaction. Which define the external functions implemented by Data Feeds.
and then in the contrcutor i am passing the address of the contract where the ET/USD deployed , remember in this we can't pass our own address beacuse this code on chainlink provide the real data of the assets which is already deployed with this address passed in the constructor .
By getLatestPrice( ) function we can get the lastest value of the ETH/USD in the integer form . By using the latestRoundData( ) function which is the method of the AggreatorV3Interface.sol .
When you deployed the code and click on the getLatestPrice you can see the latest value of the ETH/USD .
You can see on the left side below , getLatestPrice -> 0:int256: 114068290588
If you require a denomination other than what is provided, you can use two data feeds to derive the pair that you need. For example, if you needed a BTC / EUR price, you could take the BTC / USD feed and the EUR / USD feed and derive BTC / EUR using division. https://docs.chain.link/docs/get-the-latest-price/
/**
* THIS EXAMPLE USES UN-AUDITED CODE.
* Network: Rinkeby
* Base: BTC/USD
* Base Address: 0xECe365B379E1dD183B20fc5f022230C044d51404
* Quote: EUR/USD
* Quote Address: 0x78F9e60608bF48a1155b4B2A5e31F32318a1d85F
* Decimals: 8
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
/**
* THIS EXAMPLE USES UN-AUDITED CODE.
* Network: Rinkeby
* Base: BTC/USD
* Base Address: 0xECe365B379E1dD183B20fc5f022230C044d51404
* Quote: EUR/USD
* Quote Address: 0x78F9e60608bF48a1155b4B2A5e31F32318a1d85F
* Decimals: 8
*/
contract PriceConverter {
function getDerivedPrice(address _base, address _quote, uint8 _decimals)
public
view
returns (int256)
{
require(_decimals > uint8(0) && _decimals <= uint8(18), "Invalid _decimals");
int256 decimals = int256(10 ** uint256(_decimals));
( , int256 basePrice, , , ) = AggregatorV3Interface(_base).latestRoundData();
uint8 baseDecimals = AggregatorV3Interface(_base).decimals();
basePrice = scalePrice(basePrice, baseDecimals, _decimals);
( , int256 quotePrice, , , ) = AggregatorV3Interface(_quote).latestRoundData();
uint8 quoteDecimals = AggregatorV3Interface(_quote).decimals();
quotePrice = scalePrice(quotePrice, quoteDecimals, _decimals);
return basePrice * decimals / quotePrice;
}
function scalePrice(int256 _price, uint8 _priceDecimals, uint8 _decimals)
internal
pure
returns (int256)
{
if (_priceDecimals < _decimals) {
return _price * int256(10 ** uint256(_decimals - _priceDecimals));
} else if (_priceDecimals > _decimals) {
return _price / int256(10 ** uint256(_priceDecimals - _decimals));
}
return _price;
}
}