EIPs Explained

ERC-5902 · Stagnant · ERC · 2022-11-09

ERC-5902: Smart Contract Event Hooks

A stagnant pub/sub design where publisher contracts emit Hook events and paid relayers call subscriber contracts, letting contracts react to other contracts' events without running their own bots.

What problem does it solve?

Contracts cannot listen to events. If a lending position should top up when an oracle price changes, or a DAO action should execute when a vote passes, someone has to run an off-chain process that watches logs, decides, and sends a transaction. That means an always-on server, a funded hot key, and often a third-party webhook service.

ERC-5902 proposes a shared, permissionless alternative: contracts publish hook events, contracts subscribe through an on-chain registry, and independent relayers deliver the events in exchange for a fee paid by the subscriber.

How it works

  • Registry. A single, deterministically deployed registry (in the spirit of ERC-1820) records publishers and subscribers. registerHook(publisher, threadId, signingKey) asks the publisher to confirm via verifyEventHookRegistration. A publisher can expose many "threads" (independent streams).
  • Subscribing. registerSubscriber records the publisher, subscriber, thread, the per-update fee, optional maxGas / maxGasPrice caps for gas rebates, a chain ID and a fee token (zero address for the native coin).
  • Publishing. The publisher emits Hook(threadId, nonce, digest, payload, checksum). nonce strictly increases; digest = keccak256(payload) and checksum = keccak256(digest, block.number).
  • Relaying. Relayers watch the registry and hook events, simulate the subscriber call, and if it would pay, send a transaction to the subscriber's verifyHook(publisher, payload, threadId, nonce, blockheight). The subscriber may revert to decline an update.
  • Verification. Subscribers should verify authenticity, either by calling verifyEventHook on the publisher (same chain) or by checking a signature over the payload, preferably EIP-712 typed data, and should record nonces to block replays.

Sample code

Publisher and subscriber skeleton (illustrative, no fee logic):

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

contract PricePublisher {
    event Hook(uint256 indexed threadId, uint256 indexed nonce,
               bytes32 digest, bytes payload, bytes32 checksum);
    uint256 public nonce = 1;
    mapping(bytes32 => bool) private fired;

    function publish(uint256 threadId, uint256 price) external /* onlyOracle */ {
        bytes memory payload = abi.encode(price);
        bytes32 digest = keccak256(payload);
        nonce++;                                   // first hook uses nonce 2
        fired[keccak256(abi.encode(digest, threadId, nonce, block.number))] = true;
        emit Hook(threadId, nonce, digest, payload,
                  keccak256(abi.encodePacked(digest, block.number)));
    }

    function verifyEventHook(bytes32 digest, uint256 threadId, uint256 n, uint256 bh)
        external view returns (bool)
    {
        return fired[keccak256(abi.encode(digest, threadId, n, bh))];
    }
}

contract Subscriber {
    uint256 public lastNonce;

    function verifyHook(address publisher, bytes calldata payload,
                        uint256 threadId, uint256 nonce, uint256 blockheight) external {
        require(nonce > lastNonce, "replay");
        require(PricePublisher(publisher).verifyEventHook(
            keccak256(payload), threadId, nonce, blockheight), "bad hook");
        lastNonce = nonce;
        uint256 price = abi.decode(payload, (uint256));
        // ...react to price, then pay the relayer (msg.sender) its fee
    }
}

Things to know

  • Status is Stagnant: no registry deployment or relayer network is standardised, and the interfaces in the spec are not fully consistent (for example updateSubscriber takes fewer parameters than the text describes). Treat it as a design sketch.
  • A malicious or spammy publisher can drain a subscriber's fee balance; only subscribe to publishers you trust and cap gas rebates.
  • Relayers can front-run the trigger transaction by lifting a signed payload from the mempool. Checking with verifyEventHook on the publisher prevents acting on events that never landed.
  • Fee setting is hard: relayer competition and volatile gas make it easy to overpay. The spec discusses auctions and minimum-fee checks but leaves them out of scope.
  • Signed hooks should use EIP-712 domains with a chain ID to prevent cross-chain replay.

#events #relayers #automation #pubsub #stagnant

Details

Authors
Simon Brown (@orbmis)
Status
Stagnant
Type
Standards Track / ERC
Created
2022-11-09
Requires

Machine-readable: Markdown · JSON