EIPs Explained

ERC-6372 · Review · ERC · 2023-01-25

ERC-6372: Contract clock

Two tiny view functions, clock() and CLOCK_MODE(), that let a contract say whether it measures time in block numbers or timestamps, so governance and voting tools stop guessing.

What problem does it solve?

Timelocks enforce delays, DAOs run voting periods, and voting tokens store checkpoints of past voting power. Some contracts measure these in block numbers, others in timestamps, and some use something else (for example an L1 block number on an L2). From the outside there was no way to tell which, so a governor, a frontend or an indexer had to assume, and a wrong assumption silently breaks deadlines and snapshots.

ERC-6372 adds a standard way for a contract to expose its clock.

How it works

interface IERC6372 {
    function clock() external view returns (uint48);
    function CLOCK_MODE() external view returns (string memory);
}
  • clock() returns the current timepoint in the contract's own unit. It must be non-decreasing, e.g. block.number or block.timestamp.
  • CLOCK_MODE() returns a machine-readable description, formatted as a URL query string (parseable with new URLSearchParams(...)):
Clock CLOCK_MODE()
block.number (the NUMBER opcode) mode=blocknumber&from=default
Another chain's block number mode=blocknumber&from=<CAIP-2 id>, e.g. from=eip155:1
block.timestamp mode=timestamp
Anything else a unique identifier in mode

Why uint48? It is enough for timestamps until the year 8921556 and for very fast block numbers for centuries, while being small enough to pack with other values in one storage slot. The spec recommends uint48 for timepoints and uint32 for durations.

Sample code

A timestamp-based voting token:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract TimestampVotes /* is IERC6372 */ {
    function clock() public view returns (uint48) {
        return uint48(block.timestamp);
    }

    // solhint-disable-next-line func-name-mixedcase
    function CLOCK_MODE() public pure returns (string memory) {
        return "mode=timestamp";
    }
}

A client adapting to whichever clock the contract uses (ethers v6):

import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const c = new Contract(process.env.GOVERNOR!, [
  "function clock() view returns (uint48)",
  "function CLOCK_MODE() view returns (string)",
], provider);

const params = new URLSearchParams(await c.CLOCK_MODE());
const now = await c.clock();
if (params.get("mode") === "timestamp") {
  console.log("now:", new Date(Number(now) * 1000).toISOString());
} else {
  console.log("now: block", now.toString(), "from", params.get("from"));
}

Things to know

  • Status is Review, though the interface is already used in practice: OpenZeppelin's Votes, ERC20Votes and Governor contracts implement clock() / CLOCK_MODE(), and ERC-5805 (voting with delegation) builds on it.
  • This ERC itself defines no fallback. ERC-5805 says a voting contract without ERC-6372 must behave as if CLOCK_MODE were mode=blocknumber&from=default.
  • Never mix units: a governor and its voting token must use the same clock, or proposal snapshots will point to the wrong moment.
  • On L2s, block.number may not mean what you expect; timestamp mode is often the safer choice.

#governance #time #interface #voting

Details

Authors
Hadrien Croubois (@Amxx), Francisco Giordano (@frangio)
Status
Review
Type
Standards Track / ERC
Created
2023-01-25
Required by

Machine-readable: Markdown · JSON