EIPs Explained

ERC-7615 · Draft · ERC · 2024-02-03

ERC-7615: Atomic Push-based Data Feed Among Contracts

A draft publish/subscribe pattern for contracts: when a publisher function runs, it calls exec() on every subscriber registered for that function selector in the same transaction, replacing off-chain keepers that watch events.

What problem does it solve?

When one contract needs to react to something that happens in another (a price update, an NFT transfer, a finished vote), the usual answer is an off-chain keeper: a bot watches events and sends a follow-up transaction. That adds trust in the bot, latency between the two steps, and a window where state is inconsistent.

ERC-7615 proposes that the source contract (the publisher) push the update to interested contracts (the subscribers) directly, inside the same call. Examples from the spec: an oracle pushing prices to a lending protocol that liquidates on the spot; an NFT contract telling a staking contract a token moved so it can unstake, letting users stake without transferring the NFT; a DAO contract executing a vote's result as soon as it closes.

How it works

  1. A publisher function is called and runs an internal _push(selector, data) hook.
  2. The publisher looks up the subscribers registered for that function's selector.
  3. It calls exec(selector, data) on each subscriber.
  4. The subscriber acts on it, optionally calling the publisher's inbox(selector) to fetch fuller data (the reference implementation keeps it in transient storage, EIP-1153).

Two kinds of subscription:

  • Unconditional (IPushForce): forceApprove(selector, target) makes every call to that selector push to target. Suited to price feeds.
  • Conditional (IPushFree): approve(selector, target, data) only pushes when the call matches data (for example, a specific tokenId). Saves gas for everyone else.

Subscribers implement IExec with exec and isLocked. Before removing a subscription (cancel / forceCancel), the publisher must check isLocked, so a subscriber that depends on the feed cannot be cut off unilaterally. The publisher can also permanently give up the power to add or remove unconditional subscribers with renounceForceApprove / renounceForceCancel.

Sample code

A subscriber that only trusts one publisher:

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

interface IExec {
    function isLocked(bytes4 selector, bytes calldata data) external returns (bool);
    function exec(bytes4 selector, bytes calldata data) external;
}

contract PriceSubscriber is IExec {
    address public immutable publisher;
    bytes4 public immutable priceSelector;
    uint256 public lastPrice;
    uint256 public openPositions;

    constructor(address pub, bytes4 sel) { publisher = pub; priceSelector = sel; }

    function exec(bytes4 selector, bytes calldata data) external {
        require(msg.sender == publisher, "untrusted caller"); // exec is public
        require(selector == priceSelector, "unexpected selector");
        lastPrice = abi.decode(data, (uint256));
        // ... check positions and liquidate if needed
    }

    // Refuse unsubscription while positions depend on the feed.
    function isLocked(bytes4, bytes calldata) external view returns (bool) {
        return openPositions > 0;
    }
}

Things to know

  • Status: Draft.
  • The caller of the publisher pays gas for every subscriber's exec. A long or expensive subscriber list can make the publisher's function costly or push it over the block gas limit, so approve and forceApprove need access control.
  • exec is callable by anyone; subscribers must verify msg.sender and validate data.
  • Calling untrusted subscribers opens reentrancy paths back into the publisher; use reentrancy guards and checks-effects-interactions.
  • A subscriber whose isLocked always returns true can never be removed; publishers should check this before subscribing it.

#oracle #pubsub #automation #hooks #defi

Details

Authors
Elaine Zhang (@lanyinzly) <[email protected]>, Jerry <[email protected]>, Amandafanny <[email protected]>, Shouhao Wong (@wangshouh) <[email protected]>, Doris Che (@Cheyukj) <[email protected]>, Henry Yuan (@onehumanbeing) <[email protected]>
Status
Draft
Type
Standards Track / ERC
Created
2024-02-03

Machine-readable: Markdown · JSON