Units and Globally Available Variables in Solidity 0.8.20

ยท

Ether Units

Literals in Solidity can include suffixes like wei, gwei, or ether to specify Ethereum denominations. Unsuffixed numbers default to wei.

Example:

assert(1 wei == 1);
assert(1 gwei == 1e9);
assert(1 ether == 1e18);

Note: Suffixes act as multipliers (e.g., 1 ether = 1e18 wei).

๐Ÿ‘‰ Learn more about Ethereum units


Time Units

Time suffixes (seconds, minutes, hours, days, weeks) convert literals into seconds-based values:

Warning: Avoid using these for calendar calculations due to unpredictable leap seconds.


Special Variables and Functions

Block and Transaction Properties

Caution: block.timestamp and blockhash are miner-influenced and unsuitable for randomness.


ABI Encoding/Decoding


Error Handling


Cryptographic Functions

๐Ÿ‘‰ Explore Solidity security practices


Type Information

Use type(X) to inspect:


Reserved Keywords

Future syntax may include: after, let, static, etc.


FAQ

Why avoid block.timestamp for randomness?

Miners can marginally influence timestamps, making them predictable.

How does abi.encodePacked differ from abi.encode?

encodePacked tightly packs arguments (may lead to hash collisions).

What replaces selfdestruct?

As of EIP-6049, selfdestruct is deprecated; redesign contracts to avoid it.

How to handle leap seconds in time calculations?

Use oracle services for precise calendar computations.