Chainlink Oracle
- Chainlink oracles are smart contracts with public view functions that return the price of a particular asset.
- Off-chain nodes collect the prices from various sources like exchanges and write the price data to the smart contract.
- When we call the function
latestAnswer()
we get the price of Ether. When we querydecimals()
we get the number of decimals to interpret the answer with.
- Using latestAnswer() is not recommended โ use latestRoundData() instead
- This function latestAnswer() does not tell us the last time the price updated. If price updates are delayed, the smart contract might make decisions based on outdated prices.
latestRoundData()
returns the price and and the timestamp it was last updated among other things.
- It would be unsafe to rely on a single node or data source to obtain prices, so Chainlink price feeds have several whitelisted nodes that supply prices.
- The off-chain prices enter the smart contract ecosystem via the transmit function. The function takes a list of (sorted) prices and a list of signatures from the nodes. The price reported on the oracle is the median of the prices. Below we show the relevant line of code from Etherscan.
- the
latestRoundData()
function is not in the same contract astransmit()
. There are three smart contracts at play.
- It isnโt practical to keep sending state-updating transactions to the blockchain every minute. Therefore, Chainlink updates the price under two circumstances:
- When the "heartbeat" time passes (for ETH / USD this is one hour)
- If the price changes by more than 0.5%