Solidity Block Information
Block information is data about the current block, the batch of activity the network is processing, that any contract can read. block.timestamp gives the time in seconds and block.number gives the block's height.
A constructor can stamp the creation time
The constructor copies block.timestamp into a state variable. That stored copy keeps the creation time after later blocks arrive.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract TimeCapsule {
uint256 public createdAt;
constructor() {
createdAt = block.timestamp;
}
}
block.number stamps the creation block
The same trick works for the block height. The capsule now remembers both when and at which block it was made.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract TimeCapsule {
uint256 public createdAt;
uint256 public createdBlock;
constructor() {
createdAt = block.timestamp;
createdBlock = block.number;
}
}
Compare block.timestamp with a deadline
A deadline is just a stored number of seconds. The check reads the current block time and compares the two numbers.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract Deadline {
uint256 public deadline;
constructor(uint256 newDeadline) {
deadline = newDeadline;
}
function hasPassed() public view returns (bool) {
return block.timestamp >= deadline;
}
}
Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract GrantWindow {
uint256 public createdAt;
uint256 public createdBlock;
uint256 public deadline;
constructor(uint256 newDeadline) {
createdAt = block.timestamp;
createdBlock = block.number;
deadline = newDeadline;
}
function isOpen() public view returns (bool) {
return block.timestamp < deadline;
}
function hasPassed() public view returns (bool) {
return block.timestamp >= deadline;
}
}
Key points
- block.timestamp: the current block's time in seconds, counted from 1 January 1970.
- block.number: the current block's height, which grows by one with each new block.
- Creation stamp: a constructor can copy block.timestamp and block.number into state variables to record when a contract was deployed.
- Deadline check: comparing block.timestamp with a stored deadline tells whether that deadline has passed.
- Seconds, plainly: block.timestamp counts seconds, not milliseconds.
In this example, the GrantWindow contract stamps its creation time and creation block in the constructor and stores the deadline it was given. The isOpen function reports whether the current block time is still before the deadline. The hasPassed function reports the opposite, whether block.timestamp has reached the deadline. Both read the live block time at the moment they are called, while the stamps stay fixed at the creation values.
Note
block.timestamp is set when the block is produced, so it marks the block's time rather than the exact second a call was written.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Remix IDE
Exercise
A contract stores a deadline of 2000 and block.timestamp reads 2500. What is true?