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:
1 minutes = 60 seconds1 hours = 3600 seconds1 weeks = 604800 seconds
Warning: Avoid using these for calendar calculations due to unpredictable leap seconds.
Special Variables and Functions
Block and Transaction Properties
block.timestamp: Current block timestamp (seconds since Unix epoch).block.number: Current block height.msg.sender: Address of the caller.msg.value: Wei sent with the call.
Caution: block.timestamp and blockhash are miner-influenced and unsuitable for randomness.
ABI Encoding/Decoding
abi.encode(...): Encodes arguments for external calls.abi.decode(...): Decodes ABI-encoded data.
Error Handling
assert(bool condition): Reverts on internal errors (e.g., overflow).require(bool condition, string message): Reverts with a message for invalid inputs.
Cryptographic Functions
keccak256(bytes): Computes Keccak-256 hash.ecrecover(bytes32 hash, ...): Recovers signer address from signatures.
๐ Explore Solidity security practices
Type Information
Use type(X) to inspect:
type(C).creationCode: Bytecode for contractCdeployment.type(uint).min/max: Min/max values for integer types.
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.