The Ethereum blockchain, an integral part of the cryptocurrency world, operates with its own unique units of account. Understanding these units — Wei, Gwei, and ETH — is crucial for interacting with Ethereum, whether for executing smart contracts, making transactions, or developing decentralized applications.
Wei, Gwei, and ETH: The Basics
Wei: The smallest denomination of Ether (ETH), the native cryptocurrency of the Ethereum blockchain. Just like a cent is to a dollar, a Wei is to an Ether. Wei is commonly used when dealing with smart contracts and lower-level Ethereum protocols, as it allows for precise calculations without floating point numbers. When retrieving any value from the blockchain, especially in programming contexts, the value is often denoted in Wei to maintain accuracy and avoid rounding errors.
Gwei: Short for ‘Giga-Wei,’ it is equivalent to 1,000,000,000 Wei. Gwei is most commonly used in the context of gas fees, which are the fees paid to execute transactions and smart contracts on the Ethereum network. Gas fees are calculated in Gwei because it provides a good balance between granularity and practicality. Gwei effectively scales Wei to a more human-readable form, making it easier to understand and calculate transaction costs without dealing with excessively large numbers.
Eth: Represents one whole coin or token. When developers refer to “1 ETH” in Web 2.0-Web3 integrations, it doesn’t necessarily mean one Ether from the Ethereum blockchain. Instead, it represents a standard unit of any cryptocurrency or digital token involved in the integration. In a blockchain application where multiple types of tokens are used, each token, regardless of its underlying technology or value, could be abstractly referred to as “1 ETH” within the code or system design. See the code below when the “ether” is used as a conversion unit.
Calculating Gas Fees
To calculate the cost of a transaction on the Ethereum network, you need to consider both the gas price (in Gwei) and the amount of gas used. The formula is:
Total Cost (in ETH) = Gas Price (in Gwei) × Gas Used × Conversion Factor
The conversion factor is used to convert Gwei into ETH (since 1 ETH = 1,000,000,000 Gwei).
Code Example
Using Web3.js, a popular JavaScript library for Ethereum, you can calculate the cost of a transaction. Here’s a simple example:
const Web3 = require('web3');
// Initialize Web3
const web3 = new Web3('RPC_API_KEY');
// Function to calculate transaction cost in USD
async function calculateTransactionCostInUSD(gasPriceGwei, gasUsed, ethPriceUSD) {
// Convert gas price from Gwei to Wei
const gasPriceWei = web3.utils.toWei(gasPriceGwei.toString(), 'gwei');
// Calculate total cost in Wei
const totalCostWei = gasPriceWei * gasUsed;
// Convert total cost to ETH
const totalCostEth = web3.utils.fromWei(totalCostWei.toString(), 'ether');
// Convert total cost to USD
const totalCostUSD = totalCostEth * ethPriceUSD;
return totalCostUSD;
}
// Example usage
const gasPriceGwei = 17; // Example gas price in Gwei
const gasUsed = 21000; // Standard gas limit for a simple transaction
const ethPriceUSD = 2500; // Current price of ETH in USD
calculateTransactionCostInUSD(gasPriceGwei, gasUsed, ethPriceUSD)
.then(cost => console.log(`Total Transaction Cost: $${cost.toFixed(2)} USD`))
.catch(error => console.error(error));
Result:
Total Transaction Cost: $0.89 USD
You check a simple application to convert some values in DevWeb3.co. There, you can find more tools to help you better understand how to integrate your Web 2.0 applications or develop new ones using Solidity.
Top comments (0)