Block

Solidity

Définition

Variable globale Solidity donnant accès aux informations du bloc actuel : block.timestamp (horodatage), block.number (hauteur de bloc), block.chainid (ID de la chaîne), block.basefee (frais de base EIP-1559), block.prevrandao (anciennement difficulty). Utilisé pour la logique temporelle, les random seeds, ou la vérification du réseau.

Version anglaise

Solidity global variable providing access to current block information: block.timestamp, block.number, block.chainid, block.basefee (EIP-1559), block.prevrandao (formerly difficulty). Used for time-based logic, random seeds, or network verification.

Exemple de Code

contract TimeVault {
  uint256 public unlockTime;

  constructor(uint256 _duration) {
    unlockTime = block.timestamp + _duration; // Déverrouillage dans X secondes
  }

  function withdraw() external {
    require(block.timestamp >= unlockTime, "Locked");
    payable(msg.sender).transfer(address(this).balance);
  }

  function getCurrentBlock() public view returns (uint256) {
    return block.number; // Hauteur du bloc actuel
  }
}

Termes Liés

Pratique ce concept sur Solingo

Maîtrise Block avec des exercices interactifs et un IDE intégré.

Commencer gratuitement