Solidity Ether Units

In this article, we will explore Solidity Ether units in-depth and explain how they are used in the development of smart contracts on the Ethereum blockchain.

In blockchain cryptocurrency, a unit is a denomination, rather than a metric scale.

An ether unit is a denomination used to purchase computational processes in EVM.

Units in Solidity programming represent values or times involved in the execution of the code.

There are two types of units that exists in solidity programming:

  1. Solidity Ether.
  2. Solidity Time.


Solidity Ether

The suffix wei, finney, szabo or ether in solidity can be added to a literal to convert between ether based denominations. The smallest unit is wei and 1e12 stands for 1 x 10^12.

UnitsWei ValuesWei
wei1 wei1
Kwei (babbage)1e3 wei1,000
Mwei (lovelace)1e6 wei1,000,000
Gwei (shannon)1e9 wei1,000,000,000
microether (szabo)1e12 wei1,000,000,000,000
milliether (finney)1e15 wei1,000,000,000,000,000
ether1e18 wei1,000,000,000,000,000,000

The following code provides a good concept of ether conversions:

Example: 

// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest {function convert_Amount_to_Wei(uint Amount) public pure returns (uint256) { return Amount * 1 wei; }function convert_Amount_To_Ether(uint Amount) public pure returns (uint256) { return Amount * 1 ether; }function convert_Amount_To_Gwei(uint Amount) public pure returns (uint256) { return Amount * 1 gwei; }function convert_Amount_From_Wei_To_Ether(uint Amount) public pure returns (uint256) { return Amount * 1 wei / 1 ether; } function convert_Amount_From_Ether_To_Wei(uint Amount) public pure returns (uint256) { return Amount * 1 ether / 1 wei; } function convert_Amount_From_Ether_To_Gwei(uint Amount) public pure returns (uint256) { return Amount * 1 ether / 1 gwei; } function convert_Amount_From_Gwei_To_Ether(uint Amount) public pure returns (uint256) { return Amount * 1 gwei / 1 ether; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example Explanation

In above example we have apply a smart contract called SolidityTest that contains several functions to convert an input amount between different Ether units.

Specifically, the functions are:

FunctionsOverview
convert_Amount_to_WeiConverts an input Amount to Wei.
convert_Amount_To_EtherConverts an input Amount to Ether.
convert_Amount_To_GweiConverts an input Amount to Gwei.
convert_Amount_From_Wei_To_EtherConverts an input Amount from Wei to Ether.
convert_Amount_From_Ether_To_WeiConverts an input Amount from Ether to Wei.
convert_Amount_From_Ether_To_GweiConverts an input Amount from Ether to Gwei.
convert_Amount_From_Gwei_To_EtherConverts an input Amount from Gwei to Ether.

All functions are defined as public and pure, meaning they can be called from outside the contract and do not modify the contract’s state. Each function takes an input Amount and returns the converted amount in the desired unit.


Solidity Time

In contrast, time units are used to measure the duration of certain events in the blockchain, such as how long it takes for an action to take place.

You can use several time units in your code with Solidity, including:

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

The following program shows all types of time conversions:

Example: 

// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest {function convert_seconds_To_mins(uint _seconds) public pure returns(uint){return _seconds / 60;}function convert_seconds_To_Hours(uint _seconds) public pure returns(uint){return _seconds / 3600;} function convert_Mins_To_Seconds(uint _mins) public pure returns (uint256) { return _mins * 60; } function convert_Mins_To_Hours(uint _mins) public pure returns (uint256) { return _mins / 60; } function convert_Hours_To_Seconds(uint _hrs) public pure returns (uint256) { return _hrs * 3600; } function convert_Hours_To_Mins(uint _hrs) public pure returns (uint256) { return _hrs * 60; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example Explanation

In above example smart contract called SolidityTest includes several functions for time conversion between seconds, minutes, and hours.

The functions are:

 

FunctionsOverview
convert_seconds_To_minsThis function takes an input value in seconds _seconds and returns the equivalent value in minutes by dividing _seconds by 60.
convert_seconds_To_HoursThis function takes an input value in seconds _seconds and returns the equivalent value in hours by dividing _seconds by 3600.
convert_Mins_To_SecondsThis function takes an input value in minutes _mins and returns the equivalent value in seconds by multiplying _mins by 60.
convert_Mins_To_HoursThis function takes an input value in minutes _mins and returns the equivalent value in hours by dividing _mins by 60.
convert_Hours_To_SecondsThis function takes an input value in hours _hrs and returns the equivalent value in seconds by multiplying _hrs by 3600.
convert_Hours_To_MinsThis function takes an input value in hours _hrs and returns the equivalent value in minutes by multiplying _hrs by 60.

 

All functions are defined as public and pure, meaning they can be called from outside the contract and do not modify the contract’s state.


Conclusion

Understanding of solidity ether units and the ability to convert between them is an essential part of the Solidity development process in order to accomplish your tasks.

As the fundamental unit of currency in Solidity, ether is used for a wide range of functions, including paying for transactions, deploying contracts, and executing smart contract code.

Using ether amounts in smart contracts requires that the ether amounts can be converted between different ether units, such as wei, gwei, and ether. This is crucial in order to calculate ether amounts accurately and efficiently.

You can ensure their contracts handle ether amounts correctly by using the conversion codes provided in Solidity. This will avoid potential bugs and errors being introduced into their contracts.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *