Open In App

Solidity – Ether Units

Last Updated : 27 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A unit is a metric scale but here in blockchain cryptocurrency, it refers to a denomination. Ether units are denominations that are used to pay for computational processes within EVM. In Solidity programming, a unit is a measurement of value or time that is used in the code. 

There are two types of units in Solidity

1. Ether

Ether units are used to represent value, such as the amount of money being transferred between accounts or the cost of a transaction. 

  • Ether, like many other cryptocurrencies, can be divided into smaller units of value. 
  • The smallest unit of ether is called a wei, and there are 1,000,000,000,000,000,000 (1 quintillion) Wei in one ether.

 Other units of ether include:

Unit Wei Value Wei
wei 1 wei 1
Kwei (babbage) 1e3 wei 1,000
Mwei (lovelace) 1e6 wei 1,000,000
Gwei (shannon) 1e9 wei 1,000,000,000
microether (szabo) 1e12 wei 1,000,000,000,000
milliether (finney) 1e15 wei 1,000,000,000,000,000
ether 1e18 wei 1,000,000,000,000,000,000

Here, 1en means 1 x 10n.

In Solidity, you can use these units to specify the amount of ether that is being transferred or used in a contract. For example:

   function sendEther(address _to, uint256 _value) public {

     require(_to != address(0));
     
     // send atleast 1 ether to the specified address
     require(_value >= 1 ether);

     payable(_to).transfer(_value);

 }

In this example, the transfer function is being called with value is ( >= 1 ether) as an argument, which will send (>= 1 ether) to the specified address.

Note: It’s important to note that the units are only a convenient way of specifying amounts of ether and do not affect the actual value of the ether being transferred. For example, 1 ether is always equal to 1,000,000,000,000,000,000 Wei, regardless of whether it is specified as 1 ether or 1,000,000,000,000,000,000 Wei.

2. Time Unit

Time units, on the other hand, are used to measure the duration of certain events in the blockchain, such as the amount of time that must pass before a certain action is allowed to occur. 

Solidity provides several time units that can be used in your code, including:

  • seconds (s).
  • minutes (min).
  • hours (h).
  • days (days).
  • weeks (weeks).

Example 1: Time unit can be used to specify a duration in the smart contract like this:

uint public lockPeriod = 1 week;

In this example, the lockPeriod variable is set to 1 week (7 days).

Example 2:  Time units can be used to specify the frequency at which an event should occur. 

event Heartbeat(uint timestamp);

// emit a Heartbeat event every 5 minutes

schedule Heartbeat(now + 5 minutes);

In this example, the Heartbeat event will be emitted every 5 minutes.

Note: It’s important to note that time units in Solidity are only approximate and are not intended to be precise. The actual duration of a time unit may vary due to factors such as network latency and block time.

Both Ether and Time units can be either local or global, with local units being accessible only within a specific function or contract, and global units being available throughout the entire program.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads