typestar

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

  1. Every call carries its sender and the chain's clock.
  2. block.timestamp is seconds; 1 days is sugar for 86400.
  3. Stamps store the context for later comparison.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in The environment, step 21 of 28 in Language basics.

← Previous Next →