Complete guide: the units used in smart contracts (Uint256, BigNumber, Wei, Ether, Hex...)

In this tutorial, we are going to see the difference between the units used in smart contracts to understand what they correspond to. We'll talk about UInt256, BigNumber, Wei, Ether and Hex.

In this tutorial, we are going to see the difference between the units used in smart contracts to understand what they correspond to. We'll talk about UInt256, BigNumber, Wei, Ether and Hex.

Uint256

First, Uint256 if a type used to represent the amounts in smart contracts.

  • U stands for unsigned, which means it can only be positive
  • int stands for integer
  • 256 stands for 256 bits in size

Blockchains use 256 bits as their word sizes to communicate. The minimal value is 0 and the maximal value is 2 ^ 255.

If you want for example 1 ETH in this format, it won't be just a 1, it will be 1*10^18. 18 is usually the default number of decimals to represent numbers. 1 in smart contracts that use Uint256 will be a 1 followed by 18 zeros.

BigNumber

BigNumber is an underlying library used in web3 to deal with big numbers and make computations with such high numbers. We need that to make faster dApps.

As explained before, numbers in the blockchain are represented as uint256 which makes them large and it makes the computations harder. To limit the impact on the performances of our dApp, web3 uses this library.

You can use for sums, multiplications and all sorts of computations.

Wei

The Wei type is a unit for Ethereum. Ethereum and other blockchains have several units to represent quantities.

The Ether unit is the one we always use as humans because with this unit system,
1 = 1 ETH.

The wei unit is simply the smallest of Ethereum's units. Using this unit system, we have 1 ETH = 1000000000000000000 wei.

We use this type because floaters can create losses in accuracy because of how computers work. With integers, we don't have this loss. That's why we use uint256 and wei.

You may have already seen Gwei too when talking about gas fees for example. Gwei is just another unit for ether and it's a billion times bigger than Wei.

Hex

The hex type is for hexadecimal. It is used to represent addresses for example to make unique values.

Hexadecimal is a system like the binary or decimal system where instead of using 10 digits from 0 to 9, we use 16 digits because 1 digit can be stored in 1 byte.

There's not much to say about this type unless that it is used for addresses and to represent objects in a smaller format and in a unique way.

Thanks for reading this post!