If you want to use off-chain data in a smart contract, you need an oracle.
Chainlink is the biggest Oracle with integrations with 89 Blockchains, including the biggest ones like Ethereum, Binance Smart Chain and Solana .
In this article, I will introduce Chainlink, and how you can use it in your Blockchain application .
Chainlink Data Feeds
The main product of Chainlink is Data feeds. With data feeds, you can get the prices of many on-chain and off-chain assets, inside your smart contract.
Data Feeds are aggregated from many data sources by a decentralized set of independent Node Operators.
Chainlink nodes are incentivized to produce accurate data because they have to stake some LINK tokens, and if their data is too far from the average their stake get slashed (the slashing is not implemented yet).
Then the Chainlink nodes talk to each other, reach consensus, and the data is sent to a smart contract of Chainlink, on the Blockchain, called a Feed smart contract.
There is one feed smart contract per pair of assets. The easiest way to consume these feeds for on-chain assets is with the Feed Registry contract.
To query the latest price, in your smart contract you call the latestRoundData()
function of the Feed Registry, with the asset denominations as argument.
In return, you will get the price, and details about when the price was last reported.
Every time the feed is updated, the roundId
is updated, and you also get the timestamps associated with this round.
And it’s also possible to consume the Chainlink price feeds from outside the Blockchain, by using web3 or ethers, and querying your own smart contract.
You can also get historical prices by using the getRoundData()
function of the Feed Registry contract.
Beside the asset denominations, you also have to provide the roundId
to identify the time for which you want the data.
You could get the roundId
by observing the rounds as they are submitted on-chain and recording them off-chain.
Chainlink VRF
Another product of Chainlink is Chainlink VRF, which allows you to get random numbers on the Blockchain.
To get a random number, you need to create a smart contract make it inherit from VRFConsumerBase .
You also need to implement 2 functions :
requestRandomness()
, which makes the initial request for randomness to a Chainlink oraclefulfillRandomness()
, which receives the random number provided by the Chainlink oracle
The contract also need to have enough LINK tokens to pay for the service.
## Conclusion
And if you need even more flexibility, you can also use Chainlink to initiate any API from your smart contract. Like for VRF, you have to request the data from your consumer contract, and pay Chainlink for the service. This is a little bit more advanced so I let you checkout the Chainlink documentation.
That’s it for today, have a great day!
Leave a Reply