The global context in Solidity
msg.sender, block.timestamp, block.number: the environment.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// The globals: msg.sender, block.timestamp, block.number.
contract WorkbookStamp {
struct Entry {
address who;
uint256 time;
uint256 blockNo;
}
Entry[] public entries;
function stamp() external {
entries.push(Entry(msg.sender, block.timestamp, block.number));
}
function isRecent(uint256 i) external view returns (bool) {
return block.timestamp - entries[i].time < 1 days;
}
}
How it works
- Every call carries its sender and the chain's clock.
block.timestampis seconds;1 daysis sugar for 86400.- Stamps store the context for later comparison.
Keywords and builtins used here
addressboolcontractexternalfunctionpublicreturnreturnsstructuint256
The run, in numbers
- Lines
- 21
- Characters to type
- 438
- Tokens
- 77
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 77 seconds.
Step 1 of 3 in The environment, step 21 of 28 in Language basics.