How Chainlink Price Feeds Work (2024)

Chainlink price oracles are smart contracts with public view functions that return the price of a particular asset denominated in USD.

Off-chain nodes collect the prices from various sources like exchanges and write the price data to the smart contract.

Here is the smart contract for getting the price of ETH / USD: https://etherscan.io/address/0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419/advanced#readContract

When we call the function latestAnswer()we get the price of Ether. When we query decimals()we get the number of decimals to interpret the answer with.

How Chainlink Price Feeds Work (1)

Therefore, the current price of Ether is $2053.05552675 according to the oracle (true at the time of writing).

If you just want an idea of how Chainlink oracles work, you can stop here — that’s all a price oracle is!

What follows is important implementation details if you plan on using them in a project.

We will use ETH / USD as a running example, but Chainlink supports many more asset prices.

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.

In the green box below, we see the same price we got from latestAnswer()and in the blue box we see when it was last updated as a unix timestamp.

How Chainlink Price Feeds Work (2)

Smart contracts may want to set a threshold such that they use an alternative oracle or suspend critical decisions until the updatedAtfield is sufficiently recent.

Price Aggregation

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 suppliers of the ETH / USD price feedare screenshotted below.

The range of prices they supply at a given time can be seen in the small variation in reported price. Note that the “cents” portion of the price varies from 26 cents (top row) to 73 cents (bottom row).

How Chainlink Price Feeds Work (3)

transmit()

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.

How Chainlink Price Feeds Work (4)

Smart contract Architecture

The reader may have noticed that the latestRoundData()function is not in the same contract as transmit(). There are three smart contracts at play:

Price update transaction

During a price update, the signatures and prices of the nodes are batched together and sent to transmit()in the aggregator contract. The aggregator contract then calls the validate functionin the validator contract. Subject to the rules there, the price update might be rejected. The tenderly traceof such a transaction is screenshotted below. The purple call codes show the cross contract calls.

How Chainlink Price Feeds Work (5)

Viewing the price

How Chainlink Price Feeds Work (6)

Improving the gas efficiency of reading price oracles

Because viewing the price involves a cross-contract call, it is recommended to save 200 gas by “pre-warming” the aggregator call using an accessing list transaction.

Price update frequency

It isn’t practical to keep sending state-updating transactions to the blockchain every minute. Therefore, Chainlink updates the price under two circ*mstances:

  • When the "heartbeat" time passes (for ETH / USD this is one hour)

  • If the price changes by more than 0.5%

These parameters are highlighted in a screenshot of the chainlink ETH / USD dashboard below

How Chainlink Price Feeds Work (7)

Security considerations

Much has been written about smart contract securityissues that arise out of using Chainlink oracles incorrectly, so we won’t repeat it here. We refer the reader to an articleby Dacian which lists those potential issues.

Learn more with RareSkills

Please see our solidity bootcamp to learn smart contract development at a deep level!

How Chainlink Price Feeds Work (2024)
Top Articles
The 7 Levels of Financial Independence - MoneyGrower
Book Summary: Rich Dad’s Guide to Investing by Robert T. Kiyosaki
The Tribes and Castes of the Central Provinces of India, Volume 3
Nybe Business Id
Obor Guide Osrs
Craigslist Portales
Undergraduate Programs | Webster Vienna
Doublelist Paducah Ky
Wausau Marketplace
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Western Razor David Angelo Net Worth
United Dual Complete Providers
Osrs Blessed Axe
Ssefth1203
Purple Crip Strain Leafly
Craigslist Pets Southern Md
The Binding of Isaac
Uhcs Patient Wallet
Nail Salon Goodman Plaza
Richland Ecampus
Everything you need to know about Costco Travel (and why I love it) - The Points Guy
Palm Springs Ca Craigslist
Apple Original Films and Skydance Animation’s highly anticipated “Luck” to premiere globally on Apple TV+ on Friday, August 5
We Discovered the Best Snow Cone Makers for Carnival-Worthy Desserts
A Biomass Pyramid Of An Ecosystem Is Shown.Tertiary ConsumersSecondary ConsumersPrimary ConsumersProducersWhich
Selfservice Bright Lending
Melendez Imports Menu
[PDF] PDF - Education Update - Free Download PDF
Walgreens Bunce Rd
Airtable Concatenate
Suspiciouswetspot
Restaurants In Shelby Montana
Xpanas Indo
Vera Bradley Factory Outlet Sunbury Products
Myaci Benefits Albertsons
Craigslist Sf Garage Sales
Eaccess Kankakee
Teenbeautyfitness
Culver's Hartland Flavor Of The Day
Steven Batash Md Pc Photos
Cranston Sewer Tax
Www.craigslist.com Waco
Tgirls Philly
Luciane Buchanan Bio, Wiki, Age, Husband, Net Worth, Actress
Exploring the Digital Marketplace: A Guide to Craigslist Miami
Noh Buddy
Mail2World Sign Up
Sitka Alaska Craigslist
Overstock Comenity Login
What Are Routing Numbers And How Do You Find Them? | MoneyTransfers.com
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5967

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.