🎓 A1Learner
🔍
0 of 50 lessons completed

Solidity Ether and Wei

Ether is the money used on Ethereum, and wei is its smallest unit. Amounts in code are counted in wei unless a unit suffix says otherwise.

One ether equals a huge number of wei

The ether suffix stands for the full wei number. Both sides of this equality are the same value.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract EtherUnits {
    function etherEqualsWei() public pure returns (bool) {
        return 1 ether == 10 ** 18;
    }

    function oneEtherInWei() public pure returns (uint256) {
        return 1 ether;
    }
}

One gwei equals one billion wei

Gwei is a smaller middle unit. It keeps mid-size amounts short to write.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract GweiUnits {
    function gweiEqualsWei() public pure returns (bool) {
        return 1 gwei == 1e9;
    }

    function oneGweiInWei() public pure returns (uint256) {
        return 1 gwei;
    }
}

Multiplying by 1 ether converts to wei

A whole-ether amount becomes wei with one multiplication. The suffix supplies the big factor.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract EtherConverter {
    function etherToWei(uint256 wholeEther) public pure returns (uint256) {
        return wholeEther * 1 ether;
    }
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract UnitConverter {
    function etherToWei(uint256 wholeEther) public pure returns (uint256) {
        return wholeEther * 1 ether;
    }

    function gweiToWei(uint256 amount) public pure returns (uint256) {
        return amount * 1 gwei;
    }

    function etherEqualsWei() public pure returns (bool) {
        return 1 ether == 10 ** 18;
    }

    function gweiEqualsWei() public pure returns (bool) {
        return 1 gwei == 1e9;
    }
}

Key points

  • Wei: the smallest unit of ether, with no smaller part.
  • Ether: the main unit, equal to 10 to the power of 18 wei.
  • Gwei: a middle unit, equal to 1,000,000,000 wei.
  • Use a unit suffix after a number to write an amount, as in 2 ether.
  • A suffix is only a shortcut for the full number in wei.

In this example, etherToWei multiplies a whole-ether amount by 1 ether to get wei. The gweiToWei function does the same job with 1 gwei. The last two functions return the two unit equalities as true or false results. Every amount stays a plain number counted in wei.

Note

1e9 means 1 followed by 9 zeros.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

How many wei are in 1 gwei?