Docs/Examples/Auto Counter

AutoCounter

The simplest AutoLoop contract — a counter that increments at a fixed interval.


Overview

AutoCounter is the "hello world" of AutoLoop. It increments a number at a configurable time interval. No VRF, no complexity — just pure automation.

  • Base: AutoLoopCompatible
  • VRF: No
  • Gas: ~90,000 per tick
  • Use case: Periodic tasks, heartbeats, scheduled operations

Contract

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

import {AutoLoopCompatible} from
    "autoloop/AutoLoopCompatible.sol";

contract AutoCounter is AutoLoopCompatible {
    uint256 public number;
    uint256 public interval;
    uint256 public lastTimeStamp;
    uint256 private _loopID;

    constructor(uint256 _interval) {
        interval = _interval;
        lastTimeStamp = block.timestamp;
    }

    function shouldProgressLoop()
        external view override
        returns (
            bool loopIsReady,
            bytes memory progressWithData
        )
    {
        loopIsReady =
            (block.timestamp - lastTimeStamp) > interval;
        progressWithData = abi.encode(_loopID);
    }

    function progressLoop(
        bytes calldata progressWithData
    ) external override {
        uint256 loopID =
            abi.decode(progressWithData, (uint256));
        require(loopID == _loopID, "stale loop");
        lastTimeStamp = block.timestamp;
        ++number;
        ++_loopID;
    }
}

Deploy

forge create examples/AutoCounter.sol:AutoCounter \
  --constructor-args 30 \
  --rpc-url $RPC_URL \
  --private-key $PRIVATE_KEY

The constructor argument is the interval in seconds (30 = increment every 30 seconds).