0
0
Blockchain / Solidityprogramming~5 mins

Gas and transaction fees in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Gas and transaction fees help pay for the work computers do to process and confirm your actions on a blockchain.

When you want to send cryptocurrency to a friend.
When you want to run a smart contract on the blockchain.
When you want to create or update digital assets like NFTs.
When you want to vote or participate in blockchain governance.
When you want to move tokens between different blockchain accounts.
Syntax
Blockchain / Solidity
transaction {
  gasLimit: number
  gasPrice: number
  data: bytes
  to: address
  value: number
}

gasLimit is the maximum amount of gas you allow for the transaction.

gasPrice is how much you pay per unit of gas, usually in a small currency unit like gwei.

Examples
This example shows a simple transfer of 1 ether with a standard gas limit and gas price.
Blockchain / Solidity
transaction {
  gasLimit: 21000
  gasPrice: 50 gwei
  to: 0xabc123...
  value: 1 ether
}
This example is for calling a smart contract function, which needs more gas and possibly no ether sent.
Blockchain / Solidity
transaction {
  gasLimit: 100000
  gasPrice: 100 gwei
  to: 0xdef456...
  data: smart contract call data
  value: 0
}
Sample Program

This simple JavaScript program calculates the total transaction fee in wei by multiplying the gas limit by the gas price in wei.

Blockchain / Solidity
const gasLimit = 21000;
const gasPriceGwei = 50;
const gasPriceWei = gasPriceGwei * 1e9;
const totalFeeWei = gasLimit * gasPriceWei;

console.log(`Gas limit: ${gasLimit}`);
console.log(`Gas price (gwei): ${gasPriceGwei}`);
console.log(`Total transaction fee (wei): ${totalFeeWei}`);
OutputSuccess
Important Notes

Gas fees can change depending on network demand.

If you set gasLimit too low, your transaction might fail but you still pay fees.

Higher gasPrice can make your transaction confirm faster.

Summary

Gas measures the work needed to process a transaction.

Transaction fees = gas used x gas price.

Fees pay miners or validators to include your transaction.