Skip to content
Verified contractExactMIT

ProjectEnumeration

Address0x2b2a84df4d62d18329899b66c744e6c643cdc065
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.24

 

Same code

2 more contracts

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 10,000 runs
EVM version
cancun
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,330,990
Creation transaction
0xf7a24cd30b…af80530fc0

src/ProjectEnumeration.sol

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

import {IAbxTransferHook} from "./interfaces/IAbxTransferHook.sol";
import {IOrgFactory} from "./interfaces/IOrgFactory.sol";

/// @title ProjectEnumeration — on-chain ownership enumeration for ABX FS project tokens
/// @author ABX FS
///
/// @notice ABX collections are not `ERC721Enumerable`, so without this hook "which projects does this
///         address own?" could only be answered by replaying every inbound `Transfer` log since the
///         collection's deploy block and confirming each candidate with `ownerOf`. Providers prune
///         logs, so that scan eventually fails outright, and its cost grows with chain age.
///
///         This hook moves that index on-chain. Ownership is recorded as it changes, so the question
///         becomes a state read whose cost depends on the size of the answer, never on the chain's
///         age, and which no provider can prune. It is the same move the configure hook makes for
///         version history.
///
///         It does not list a collection's whole token set: an org numbers its tokens `0, 1, 2, …` in
///         mint order and nothing in it can be burned, so its tokens are exactly `0 … nextTokenId() - 1`,
///         which the org itself answers.
///
/// @dev    **One cross-collection index, and it is gated.** Besides the per-collection lists, this
///         contract answers "which orgs does this address hold projects in?" — the one question the
///         per-collection lists cannot, and what lets a wallet find its orgs with no indexer. That
///         list is NOT namespaced by caller, so it admits only collections `factory` has registered;
///         otherwise any contract could write itself into a victim's list. The check is a raw
///         `staticcall` with a hand-decoded result and runs only when the recipient holds no other
///         counted token in the collection, so a transfer to an existing holder pays nothing for it.
///
///         **Everything else is namespaced by `msg.sender`.** One deployment serves every collection, and a
///         forged call can only corrupt the caller's own namespace — the same property
///         `ProjectHook` relies on. That is why this contract is NOT constructor-pinned to a
///         single token: pinning is the alternative ABX documents for hooks that key state some
///         other way, and namespacing is strictly more useful here.
///
///         **This hook must never revert.** It is a veto: ABX calls it from `_afterTokenTransfer`
///         with no try/catch, so a revert fails the transfer — and a mint is a transfer from
///         `address(0)`, so it fails minting too. Worse, `lockParamHooks()` would make that
///         permanent. Every branch below is therefore written to REPAIR a disagreement rather than
///         assert against it. A token leaves whoever the index RECORDS as its owner, whatever `from`
///         says, and a record that does not match the lists is treated as "nowhere to unlink", never
///         indexed blindly; the index converges on the truth and the collection is never stranded.
///
///         Accounting is O(1) per move (swap-and-pop), paid by whoever moves the token. That cost is the
///         price of the query being free and permanent, and it is disclosed rather than hidden.
contract ProjectEnumeration is IAbxTransferHook {
    // ──────────────────────────────────────────────────────────────────────────
    // Storage — namespaced by the calling collection, except the org-held index below
    // ──────────────────────────────────────────────────────────────────────────

    /// @dev What this hook records for one token, in one slot: the owner it believes holds the token and the
    ///      token's 1-based position in that owner's list (`_owned`). Both are set or both are clear when a transfer
    ///      returns.
    ///
    ///      The owner is deliberately not read from the token. The hook runs after the move is applied, so `ownerOf`
    ///      already returns `to`; the pre-image needed for removal exists only here. It is also what makes repair
    ///      possible when a reported `from` disagrees with what was recorded.
    struct Holding {
        address owner;
        uint96 indexPlusOne;
    }

    /// @dev What an owner holds in one registered org, in one slot: how many tokens (counted only for registered
    ///      orgs) and the org's 1-based position in `_orgsHeld[owner]`. A nonzero count implies the org passed the
    ///      registry check when the count left zero, so it is not re-checked while the count stays nonzero.
    struct OrgHolding {
        uint128 count;
        uint128 indexPlusOne;
    }

    /// @notice The factory whose orgs are admitted to the org-held index. Fixed at deployment.
    address public immutable factory;

    /// @dev owner => registered orgs in which they hold at least one counted token.
    mapping(address => address[]) internal _orgsHeld;
    /// @dev owner => org => what they hold there.
    mapping(address => mapping(address => OrgHolding)) internal _orgHolding;

    /// @dev collection => owner => tokens held. Order is not meaningful; swap-and-pop reorders it.
    mapping(address => mapping(address => uint256[])) internal _owned;
    /// @dev collection => tokenId => its record.
    mapping(address => mapping(uint256 => Holding)) internal _holding;

    constructor(address factory_) {
        factory = factory_;
    }

    // ──────────────────────────────────────────────────────────────────────────
    // The hook
    // ──────────────────────────────────────────────────────────────────────────

    /// @inheritdoc IAbxTransferHook
    ///
    /// @dev No access-control revert, by design. Authentication here is structural: every write is
    ///      keyed by `msg.sender` except the org-held index, which admits only registered orgs, so a
    ///      forged call writes only into the forger's own namespace and cannot touch a real
    ///      collection's index. Reverting on an unexpected caller would add a revert path to a veto
    ///      for no security benefit.
    function onTokenTransfer(
        uint256 tokenId,
        address from,
        address to,
        address, /* operator */
        uint256 /* amount */
    ) external {
        // A self-transfer changes no ownership set. ABX delivers these on ERC-721 (it filters them only
        // for ERC-1155), and remove-then-add would churn storage to reach the same state.
        if (from == to) return;

        address collection = msg.sender;
        Holding storage h = _holding[collection][tokenId];

        // The token leaves whoever the index RECORDS as holding it, which is not necessarily the `from` the
        // token reported. Trusting `from` would let one disagreement — a move that happened before this hook
        // was armed, say — desynchronize the index permanently, or revert and brick the collection. A mint of a
        // token that still has a record (only a caller that is not an org can produce one) unlinks it the same
        // way, so no token is ever listed under two owners.
        address recorded = h.owner;
        if (recorded != address(0)) _unlink(collection, tokenId, recorded, h.indexPlusOne);

        if (to == address(0)) {
            delete _holding[collection][tokenId];
        } else {
            uint256[] storage list = _owned[collection][to];
            list.push(tokenId);
            _holding[collection][tokenId] = Holding({owner: to, indexPlusOne: uint96(list.length)});
            _incHeld(collection, to);
        }
    }

    // ──────────────────────────────────────────────────────────────────────────
    // Views
    // ──────────────────────────────────────────────────────────────────────────

    /// @notice How many tokens of `collection` this hook records `owner` as holding.
    function balanceOfTracked(address collection, address owner) external view returns (uint256) {
        return _owned[collection][owner].length;
    }

    /// @notice Token at `index` of `owner`'s holdings. Order is arbitrary and unstable across moves. Reverts with an
    ///         array-bounds panic past the end.
    function tokenOfOwnerByIndex(address collection, address owner, uint256 index)
        external
        view
        returns (uint256)
    {
        return _owned[collection][owner][index];
    }

    /// @notice Every token `owner` holds in `collection`.
    /// @dev Unbounded. Fine for the sizes ABX FS projects realistically reach and for `eth_call`,
    ///      which is where it is meant to be used — but {tokensOfOwnerSlice} exists for callers that
    ///      cannot assume that, and nothing on-chain should call this.
    function tokensOfOwner(address collection, address owner)
        external
        view
        returns (uint256[] memory)
    {
        return _owned[collection][owner];
    }

    /// @notice A window of `owner`'s holdings, for paging past what one `eth_call` will return.
    /// @dev Clamps rather than reverting on an overrun: a paging caller that walks off the end
    ///      should get an empty tail, not an error it has to special-case.
    function tokensOfOwnerSlice(address collection, address owner, uint256 start, uint256 count)
        external
        view
        returns (uint256[] memory window)
    {
        uint256[] storage src = _owned[collection][owner];
        uint256 len = src.length;
        if (start >= len) return new uint256[](0);
        uint256 end = count > len - start ? len : start + count; // clamps; no overflow for any `count`
        window = new uint256[](end - start);
        for (uint256 i = start; i < end; ++i) {
            window[i - start] = src[i];
        }
    }

    /// @notice Registered orgs in which `owner` holds at least one token. Order is not stable.
    function orgsHeldBy(address owner) external view returns (address[] memory) {
        return _orgsHeld[owner];
    }

    function orgsHeldCount(address owner) external view returns (uint256) {
        return _orgsHeld[owner].length;
    }

    /// @notice A window of {orgsHeldBy}, clamping rather than reverting on an overrun.
    function orgsHeldSlice(address owner, uint256 start, uint256 count)
        external
        view
        returns (address[] memory window)
    {
        address[] storage src = _orgsHeld[owner];
        uint256 len = src.length;
        if (start >= len) return new address[](0);
        uint256 end = count > len - start ? len : start + count; // clamps; no overflow for any `count`
        window = new address[](end - start);
        for (uint256 i = start; i < end; ++i) {
            window[i - start] = src[i];
        }
    }

    /// @notice The owner this hook recorded for `tokenId`, or `address(0)` if it never saw the token or saw it burned.
    /// @dev The token contract remains the authority on ownership. This is what the index believes,
    ///      exposed so a disagreement is diagnosable rather than invisible.
    function trackedOwnerOf(address collection, uint256 tokenId) external view returns (address) {
        return _holding[collection][tokenId].owner;
    }

    // ──────────────────────────────────────────────────────────────────────────
    // Internals
    // ──────────────────────────────────────────────────────────────────────────

    /// @dev Take `tokenId` out of `recorded`'s list and count. The record's position is used only if the list
    ///      really holds the token there: a position that is zero, past the end or names another token cannot be
    ///      corrected without a scan, so nothing is popped (an array panic, or removing the wrong token, would be
    ///      worse). The token leaves `recorded` either way, so the org-held count drops either way. Through this
    ///      hook's own entry point the record always matches; only corrupted storage reaches the other case.
    function _unlink(address collection, uint256 tokenId, address recorded, uint256 indexPlusOne) internal {
        uint256[] storage list = _owned[collection][recorded];
        uint256 len = list.length;
        if (indexPlusOne != 0 && indexPlusOne <= len && list[indexPlusOne - 1] == tokenId) {
            uint256 last = len - 1;
            if (indexPlusOne - 1 != last) {
                uint256 moved = list[last];
                list[indexPlusOne - 1] = moved;
                _holding[collection][moved].indexPlusOne = uint96(indexPlusOne);
            }
            list.pop();
        }
        _decHeld(collection, recorded);
    }

    // ──────────────────────────────────────────────────────────────────────────
    // The org-held index
    // ──────────────────────────────────────────────────────────────────────────

    /// @dev Never reverts. The registry is consulted only while the count is zero; for a collection that is
    ///      not an org that is every time, since its count never leaves zero.
    function _incHeld(address collection, address owner) internal {
        OrgHolding storage o = _orgHolding[owner][collection];
        uint256 c = o.count;
        if (c != 0) {
            o.count = uint128(c + 1);
            return;
        }
        if (!_isOrg(collection)) return;
        address[] storage list = _orgsHeld[owner];
        list.push(collection);
        _orgHolding[owner][collection] = OrgHolding({count: 1, indexPlusOne: uint128(list.length)});
    }

    /// @dev Never reverts, and repairs rather than asserts: a zero count means the token was never
    ///      counted (the collection was not an org when it arrived), so there is nothing to undo, and a
    ///      position that does not name this org in the list is cleared without popping anything.
    function _decHeld(address collection, address owner) internal {
        OrgHolding storage o = _orgHolding[owner][collection];
        uint256 c = o.count;
        if (c == 0) return;
        if (c != 1) {
            o.count = uint128(c - 1);
            return;
        }
        uint256 indexPlusOne = o.indexPlusOne;
        delete _orgHolding[owner][collection];

        address[] storage list = _orgsHeld[owner];
        uint256 len = list.length;
        if (indexPlusOne != 0 && indexPlusOne <= len && list[indexPlusOne - 1] == collection) {
            uint256 last = len - 1;
            if (indexPlusOne - 1 != last) {
                address moved = list[last];
                list[indexPlusOne - 1] = moved;
                _orgHolding[owner][moved].indexPlusOne = uint128(indexPlusOne);
            }
            list.pop();
        }
    }

    /// @dev See the contract note. Anything other than exactly one canonical `true` word reads as "not an org"; a
    ///      call to an address with no code (`address(0)` included) returns nothing. At most 32 bytes of returndata
    ///      are copied, whatever the callee returns.
    function _isOrg(address collection) internal view returns (bool yes) {
        address f = factory;
        bytes4 selector = IOrgFactory.isOrg.selector;
        assembly ("memory-safe") {
            mstore(0x00, selector)
            mstore(0x04, shr(96, shl(96, collection)))
            let ok := staticcall(gas(), f, 0x00, 0x24, 0x00, 0x20)
            yes := and(and(ok, eq(returndatasize(), 0x20)), eq(mload(0x00), 1))
        }
    }
}

src/interfaces/IAbxTransferHook.sol

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

/// @title IAbxTransferHook — the ownership-change lifecycle callback on an ABX code collection
/// @notice Mirrors ABX's `IAbxTransferHook`. Declared locally because the collection calls it by
///         signature, and vendoring ABX's whole contract tree to compile against one interface would
///         couple this build to theirs.
///
/// @dev    Called from `SeriesCode._afterTokenTransfer`, so the move has ALREADY been applied when
///         the hook runs — `ownerOf(tokenId)` returns `to` during this call, not `from`.
///
///         **This is a VETO.** ABX calls it plainly: no try/catch, no gas cap, no swallowed
///         returndata. A revert here bubbles and fails the transfer — and because a mint is a
///         transfer from `address(0)`, a reverting hook also stops minting, through any minter. Any
///         revert path is therefore a liveness risk for the entire collection, which is why
///         `ProjectEnumeration` treats inconsistency as something to repair rather than something to
///         assert against.
///
///         **`msg.sender` is the collection**, and it is not passed as an argument. That is what
///         lets one deployment serve many collections safely: state keyed by `msg.sender` cannot be
///         written on another collection's behalf. This callback is otherwise an ordinary external
///         function — anyone may call it with forged arguments — so an implementation that keys
///         state by anything OTHER than `msg.sender` must authenticate the caller another way: pin the
///         token in its constructor, or ask a registry (as `ProjectEnumeration` asks the factory's
///         `isOrg`).
///
///         **On ERC-721, self-transfers ARE delivered.** `SeriesCode._notifyTransferHook` has no
///         `from == to` guard; only the ERC-1155 path filters self-transfers and zero amounts. A
///         hook that cares about ownership-set changes must skip that case itself.
interface IAbxTransferHook {
    /// @param tokenId  the ERC-721 token that moved.
    /// @param from     the previous owner. `address(0)` on mint.
    /// @param to       the new owner. `address(0)` on burn.
    /// @param operator the caller that initiated the move — the holder, an approved operator, or a
    ///                 minter. On ERC-721 `SeriesCode` passes `msg.sender`.
    /// @param amount   copies moved. Always 1 on ERC-721.
    function onTokenTransfer(
        uint256 tokenId,
        address from,
        address to,
        address operator,
        uint256 amount
    ) external;
}

src/interfaces/IOrgFactory.sol

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

/// @title IOrgFactory — the canonical entry point for ABX FS orgs
/// @notice An **org** is its own ABX collection, deployed by this factory, and projects are tokens in
///         it. The factory renounces ABX ownership of every org at creation, so nobody but a
///         project's owner (`ownerOf`) — not ABX FS, not the org's admin — holds any power over that
///         project. The admin is a record here, not an ABX owner, and its powers are exactly three:
///         mint into the org, open or close it (while open, anyone may mint), and hand the role on.
///
/// @dev    Org admin and project owner are different things. A project is governed by `ownerOf` and
///         nothing else; the admin only decides who can *create* projects in the org.
interface IOrgFactory {
    error InvalidName();
    error InvalidDescription();
    /// @dev `predictOrg(bytes32(0))`: a zero salt is a plain deploy, which has no predictable address.
    error NoDeterministicAddress();
    error SaltSenderMismatch();
    error NotAdmin(address org, address caller);
    error NotOrg(address org);
    error NotPendingAdmin(address org, address caller);
    /// @notice `createOrg` before all five components (hook, enumeration hook, minter, card, traits renderer) have been
    ///         deployed at the addresses this factory names. Anyone can deploy them; see `componentsDeployed`.
    error ComponentsNotDeployed();

    event OrgCreated(
        address indexed org, address indexed admin, uint256 indexed index, string name, uint64 createdBlock
    );
    event AdminTransferStarted(address indexed org, address indexed from, address indexed to);
    event AdminTransferred(address indexed org, address indexed from, address indexed to);
    event OpenSet(address indexed org, bool open);

    // ─── Creation ─────────────────────────────────────────────────────────────

    /// @notice Create an org administered by `msg.sender`, and mint its first project to them.
    /// @param name_ Display name: printable ASCII only (bytes 0x20 to 0x7e), 1-64 bytes, with at least one
    ///              non-space character, else `InvalidName`. Immutable: it becomes the ABX collection name.
    /// @param description_ The collection description marketplaces show: 0-1000 bytes of printable ASCII
    ///              (0x20 to 0x7e) and newlines (0x0a), not only whitespace unless empty, else
    ///              `InvalidDescription`. Immutable. Empty means none, and the collection document then
    ///              carries a standard text. It is also every project token's fallback `description`.
    /// @param salt  `bytes32(0)` for a plain deploy. Otherwise a deterministic address, and the first 20
    ///              bytes must be zero (anyone may use it) or `msg.sender` (only they may), else
    ///              `SaltSenderMismatch`.
    ///
    ///              SQUATTING: a zero-prefix salt is open to everyone, so anyone who sees your salt can
    ///              create an org at that address first, as its admin. A prefix-bound salt (the first 20
    ///              bytes equal your address) binds the org address to you only on chains where you
    ///              control your own address. The same salt names the same org address on every chain;
    ///              an EOA's key controls it everywhere, but a smart account (a Safe, say) exists only
    ///              where it was deployed, and on another chain that address may be unclaimed or someone
    ///              else's, who can then take the org address there. Only announce or pre-share a
    ///              predicted address for a prefix-bound salt, and only for chains where your address is
    ///              yours; a zero-prefix address is safe to rely on only once the org exists and
    ///              `adminOf` is you.
    function createOrg(string calldata name_, string calldata description_, bytes32 salt)
        external
        returns (address org);

    /// @notice The address `createOrg(_, _, salt)` would produce, if the salt is usable. Reverts with
    ///         `NoDeterministicAddress` for `bytes32(0)`: that is a plain deploy, with no predictable
    ///         address. It does not check the salt's creator prefix, so a prefix-bound salt of someone
    ///         else yields an address you cannot deploy to. See `createOrg` on squatting.
    function predictOrg(bytes32 salt) external view returns (address);

    /// @notice Whether every component exists at the address this factory computed for it. Each is deployed through
    ///         the keyless CREATE2 deployer with the factory's `COMPONENT_SALT` and its creation code with the
    ///         constructor arguments the factory used (both stores and this factory for the hook, this factory for the
    ///         enumeration hook and the minter, the hook for the card and the traits renderer), so only that exact
    ///         code can be there.
    function componentsDeployed() external view returns (bool);

    // ─── The registry ─────────────────────────────────────────────────────────

    function isOrg(address org) external view returns (bool);
    function orgCount() external view returns (uint256);
    /// @notice Org `index` in creation order. Append-only. Reverts with an array-bounds panic past the end.
    function orgAt(uint256 index) external view returns (address);
    /// @notice Orgs `start .. start + count` in creation order, clamped to the end (an overrun is a shorter or empty
    ///         window, never a revert).
    function orgsSlice(uint256 start, uint256 count) external view returns (address[] memory);
    function createdBlockOf(address org) external view returns (uint64);

    // ─── Admin ────────────────────────────────────────────────────────────────

    function adminOf(address org) external view returns (address);
    function pendingAdminOf(address org) external view returns (address);
    /// @notice Orgs `admin` currently administers. Order is not stable across transfers. Unbounded: use
    ///         {orgsAdministeredSlice} to page.
    function orgsAdministered(address admin) external view returns (address[] memory);
    function orgsAdministeredCount(address admin) external view returns (uint256);
    /// @notice A window of {orgsAdministered}, `start .. start + count`, clamped like {orgsSlice}. The order is not
    ///         stable across handovers, so a pager should read at one block.
    function orgsAdministeredSlice(address admin, uint256 start, uint256 count)
        external
        view
        returns (address[] memory);

    /// @notice Offer the admin role to `to`. Nothing changes until they accept.
    function transferAdmin(address org, address to) external;
    /// @notice Take the role offered to you (`NotOrg`; `NotPendingAdmin` unless an offer to you is pending). This also
    ///         closes the org: a handover is how an org recovers from a compromised admin key, and an open door the
    ///         old admin left must not outlive the old admin. The new admin reopens deliberately.
    function acceptAdmin(address org) external;

    // ─── Mint rules ───────────────────────────────────────────────────────────

    function isOpen(address org) external view returns (bool);
    /// @notice Whether `caller` may mint into `org` through the minter: true for a registered org when
    ///         `caller` is this factory (how `createOrg` mints the first project), the org's admin, or
    ///         anyone while the org is open; false for any address that is not an org.
    function mayMint(address org, address caller) external view returns (bool);
    /// @notice Open (`true`: anyone may mint) or close `org`. Admin only (`NotOrg`, `NotAdmin`).
    function setOpen(address org, bool open_) external;
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 10000,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=../contracts-core/lib/forge-std/src/",
    "solady/=../contracts-core/lib/solady/src/",
    "@abxfs/core/=../contracts-core/src/",
    "@abxfs/core-script/=../contracts-core/script/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.