Logo

dev-resources.site

for different kinds of informations.

Why Multiply and Divide by 1đť‘’18 in Solidity?

Published at
11/22/2024
Categories
defi
web3
ethereum
cryptocurrency
Author
prince_ad7e7c212e5c5ecf40
Categories
4 categories in total
defi
open
web3
open
ethereum
open
cryptocurrency
open
Author
25 person written this
prince_ad7e7c212e5c5ecf40
open
Why Multiply and Divide by 1đť‘’18 in Solidity?

When developing smart contracts for decentralized finance (DeFi), precision handling is crucial. A common pattern you’ll encounter is multiplying and then dividing by 1e18 in calculations involving token prices, amounts, and values. At first glance, this may seem redundant—but it serves a critical purpose.

-Ethereum and DeFi systems heavily rely on consistent precision across calculations:
Ethereum’s 18 Decimal Standard: Most tokens, including ERC20 tokens, use 18 decimal places for their values.
For example, 1 ETH is represented as 1 Ă— 10 ^18 . This standard ensures consistency across token operations.

-Chainlink Price Feed Precision: Chainlink, a popular oracle for fetching token prices, provides values with 8 decimal places (e.g., 1000 Ă— 10 ^8 ).
This mismatch in precision needs to be addressed when integrating Chainlink data with Ethereum-based systems.

The Problem: Mismatched Precisions Imagine you want to calculate the USD value of 1 ETH: Token Price from Chainlink = 1000 Ă— 10^8
Token Amount = 1Ă—10 ^18.
If you directly multiply these values:

USD Value = 1000 × 10^8 * 10^18

The result has 26 decimal places, far exceeding Ethereum’s standard of 18. Using this value in other parts of your contract—such as calculating collateralization or health factors—can lead to inconsistencies and errors.

The Solution:
1.Multiply the price by 1e10 to bring Chainlink's 8-decimal precision up to 18-decimal precision.

-Adjust price precision: 1000 10^8 * 10^10 = 1000 10^18

2.Multiply the result by the token amount

-Multiply by the token amount of 1ETH:
(1000*10^18) *(1*10^18) = 1000 * 10^36

  1. Divide by 1e18 to normalize the final value back to 18 decimals

Note 10e8 = 10^8 0r 10 raised to the power of 8.
10e18 = 10^18 or 10 raised to the power of 18.

An Example Let’s calculate the USD value of 1 ETH at $1,000:

  1. Inputs: -Price = 1000 Ă— 10^8 (from Chainlink) -Amount = 1 Ă— 10^18 (1 ETH).

Step-by-Step Calculation:

-Adjust price precision: 1000 10^8 * 10^10 = 1000 10^18
-Multiply by the amount:(1000*10^18) *(1*10^18) = 1000 *
10^36
-Normalize precision:
USD Value = 1000 * 10^36 / 10^18 = 1000 * 10^18

1000 * 10^18 represents $1,000 in 18-decimal precision.
NOTE:
Without dividing by 1e18, the result would have 36 decimals, making it incompatible with other values that adhere to Ethereum’s 18-decimal standard.

defi Article's
30 articles in total
Favicon
How to Recover Funds from Failed Polygon Bridge Transactions
Favicon
DEZILA: Innovative DeFi protocol leads the revolution of liquidity and price stability
Favicon
How DeFi Development is Reshaping the Financial Sector?
Favicon
DeFi Development Trends to Watch for in 2025
Favicon
Benefits of Choosing Outsourcing DeFi Application Development Services
Favicon
How to Start DeFi Platform Development? 10 Key Steps
Favicon
Decentralized finance development in 2025. Key use cases
Favicon
Essentials of DeFi Development Services for Startups
Favicon
Hiring DeFi Development Company 2025. Checklist for Founders
Favicon
Why Multiply and Divide by 1đť‘’18 in Solidity?
Favicon
How to Build a Secure and Profitable DeFi Staking Platform
Favicon
The Quickest Way to Create a DeFi App: MVP Tips
Favicon
Comprehensive Token Development Services: Your Gateway to Blockchain Success
Favicon
The End of Google's Monopoly: How Will It Affect Competitors and Technologies?
Favicon
Gem Wallet: A Multi-Chain Wallet for Crypto and DeFi
Favicon
03.DEFI ATTACKS - Falsche Slippagekalkulation
Favicon
02.DEFI ATTACKS - Keine Verfallsfrist/Deadline
Favicon
01.DEFI ATTACKS - Kein Slippage Parameter
Favicon
What Is an Ondo Finance Clone Script? A Beginner’s Guide
Favicon
What is DeFi and How Does it Work?
Favicon
The Future of Financial Compliance Supported by Blockchain
Favicon
Building Secure Smart Contracts for DeFi Applications
Favicon
XRPL Weekly Traction Report (2024/Sep/02-08)
Favicon
Exploring the Future of Decentralized Finance (DeFi)
Favicon
Building Secure Smart Contracts for DeFi
Favicon
21Shares Launches Wrapped Bitcoin on Ethereum
Favicon
The Future of DeFi: Trends and Predictions
Favicon
The largest airdrop in the history of cryptocurrencies!
Favicon
Avalanche Blockchain Development | Built for dApps and DeFi
Favicon
đź”— The Power of Cross-Chain Technology with SpookySwap

Featured ones: