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:
- Solidity Ether.
- Solidity Time.
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.
|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|
The following code provides a good concept of ether conversions:
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:
|convert_Amount_to_Wei||Converts an input Amount to Wei.|
|convert_Amount_To_Ether||Converts an input Amount to Ether.|
|convert_Amount_To_Gwei||Converts an input Amount to Gwei.|
|convert_Amount_From_Wei_To_Ether||Converts an input Amount from Wei to Ether.|
|convert_Amount_From_Ether_To_Wei||Converts an input Amount from Ether to Wei.|
|convert_Amount_From_Ether_To_Gwei||Converts an input Amount from Ether to Gwei.|
|convert_Amount_From_Gwei_To_Ether||Converts 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.
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:
In above example smart contract called SolidityTest includes several functions for time conversion between seconds, minutes, and hours.
The functions are:
|convert_seconds_To_mins||This function takes an input value in seconds _seconds and returns the equivalent value in minutes by dividing _seconds by 60.|
|convert_seconds_To_Hours||This function takes an input value in seconds _seconds and returns the equivalent value in hours by dividing _seconds by 3600.|
|convert_Mins_To_Seconds||This function takes an input value in minutes _mins and returns the equivalent value in seconds by multiplying _mins by 60.|
|convert_Mins_To_Hours||This function takes an input value in minutes _mins and returns the equivalent value in hours by dividing _mins by 60.|
|convert_Hours_To_Seconds||This function takes an input value in hours _hrs and returns the equivalent value in seconds by multiplying _hrs by 3600.|
|convert_Hours_To_Mins||This 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.
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.