Skip to content
Verified contractExactMIT

PBS1Wrapper

Address0xd4007f3c899fdb8a98a891173766db8929ae82ef
Balance

0 ETH

 

Transactions

5

 

Compiler

solc 0.8.34

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.34+commit.80d5c536
Language
solidity
Optimization
No
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,346,981
Creation transaction
0xc62210db8c…93b1563067

contracts/PBS1Wrapper.sol

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

/// @title PBS1Wrapper v0 - Patronage Bundle Protocol, owner-wrapper model
/// @notice A bundle = one Signature Stake NFT + one membership NFT (e.g. a CryptoKitty)
///         held by this wrapper, with an explicit ON-CHAIN entitlement record
///         binding them and defining yield allocation. Validated direction per
///         Vitalik Buterin, Oct 31 2026: a wrapper that OWNS the Signature Stake
///         NFT can call scrapeInterest; scraped WISE is paid to the wrapper.
///
/// !!! v0 STATUS: FORK-VALIDATED TEST BUILD (Nov 5 2026). Unit suite 10/10 plus
/// 2/2 mainnet-fork tests against the REAL WISE engine, real Signature Stakes
/// collection and real CryptoKitties. Still NOT audited. Limit real exposure
/// to the tiny designated test stake; never wrap high-value stakes in v0.
contract PBS1Wrapper {
    // ---------------------------------------------------------------
    // On-chain entitlement record (Vitalik checklist item 2: metadata
    // describes, but is NOT the sole proof).
    // ---------------------------------------------------------------
    struct Entitlement {
        uint256 chainId;                    // checklist 1: chain
        address stakeCollection;            // checklist 1: collection
        uint256 stakeId;                    // checklist 1: underlying stake ID
        address yieldToken;                 // token scrapeInterest pays in
        address membershipCollection;       // checklist 1: membership NFT collection
        uint256 membershipTokenId;          // checklist 1: membership tokenId
        address patron;                     // who receives the patron share
        uint16 patronAllocationBps;         // checklist 2: allocation (basis pts, 10000 = 100%)
        uint64 createdAt;
        bool active;
    }

    address public owner;
    uint256 public nextBundleId = 1;
    mapping(uint256 => Entitlement) public entitlements;
    /// @dev PERMANENT pair bindings (Vitalik checklist: a membership NFT must
    /// never attach to a different underlying stake, even after closing).
    mapping(bytes32 => bool) public pairUsed;
    mapping(bytes32 => uint256) public bundleIdByStakeKey; // stake pairKey -> last bundleId
    mapping(bytes32 => uint256) public bundleIdByMembershipKey;

    event BundleCreated(
        uint256 indexed bundleId,
        address indexed patron,
        address stakeCollection,
        uint256 indexed stakeId,
        address yieldToken,
        address membershipCollection,
        uint256 membershipTokenId,
        uint16 patronAllocationBps,
        uint256 chainId
    );
    event Scraped(uint256 indexed bundleId, uint256 amount, uint256 toPatron, uint256 toOwner);
    event BundleClosed(uint256 indexed bundleId, address to);

    error NotOwner();
    error NotScrapeCaller();
    error InactiveBundle();
    error BadAllocation();
    error AssetAlreadyBundled();
    error StakeReboundToDifferentMembership();

    modifier onlyOwner() {
        if (msg.sender != owner) revert NotOwner();
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    /// @notice Create a bundle: wrapper takes custody of the stake NFT and the
    ///         membership NFT and writes the entitlement record first.
    /// @param patronAllocationBps share of scraped yield for the patron, in
    ///        basis points (7000 = 70%). Owner keeps the rest.
    function createBundle(
        address stakeCollection,
        uint256 stakeId,
        address yieldToken,
        address membershipCollection,
        uint256 membershipTokenId,
        address patron,
        uint16 patronAllocationBps
    ) external onlyOwner returns (uint256 bundleId) {
        if (patronAllocationBps > 10000) revert BadAllocation();

        bytes32 pairKey = keccak256(abi.encodePacked(stakeCollection, stakeId));
        if (stakeCollection == membershipCollection && stakeId == membershipTokenId) revert BadAllocation();

        bytes32 membershipKey = keccak256(abi.encodePacked(membershipCollection, membershipTokenId));
        // MEMBERSHIP FIRST: a membership NFT may only ever ride its original stake.
        if (pairUsed[membershipKey]) {
            Entitlement storage prevM = entitlements[bundleIdByMembershipKey[membershipKey]];
            if (prevM.stakeCollection != stakeCollection || prevM.stakeId != stakeId) {
                revert StakeReboundToDifferentMembership();
            }
        }
        if (pairUsed[pairKey]) {
            // stake already had a bundle: only legal if it is the SAME membership NFT again
            Entitlement storage prev = entitlements[bundleIdByStakeKey[pairKey]];
            if (prev.membershipCollection != membershipCollection ||
                prev.membershipTokenId != membershipTokenId) {
                revert StakeReboundToDifferentMembership();
            }
        }

        bundleId = nextBundleId++;

        // Record BEFORE moving assets, so the binding exists on-chain
        // even if a transfer reverts.
        entitlements[bundleId] = Entitlement({
            chainId: block.chainid,
            stakeCollection: stakeCollection,
            stakeId: stakeId,
            yieldToken: yieldToken,
            membershipCollection: membershipCollection,
            membershipTokenId: membershipTokenId,
            patron: patron,
            patronAllocationBps: patronAllocationBps,
            createdAt: uint64(block.timestamp),
            active: true
        });
        pairUsed[pairKey] = true;
        pairUsed[membershipKey] = true;
        bundleIdByStakeKey[pairKey] = bundleId;
        bundleIdByMembershipKey[membershipKey] = bundleId;

        // Wrapper becomes the owner of the stake NFT -> owner-only scrapeInterest works.
        IERC721(stakeCollection).transferFrom(msg.sender, address(this), stakeId);
        IERC721(membershipCollection).transferFrom(msg.sender, address(this), membershipTokenId);

        emit BundleCreated(
            bundleId, patron, stakeCollection, stakeId, yieldToken,
            membershipCollection, membershipTokenId, patronAllocationBps, block.chainid
        );
    }

    /// @notice Scrape accrued interest from the underlying stake and allocate it.
    ///         WHO can scrape (checklist 3): the wrapper owner or the bundle patron.
    ///         Shortfall behavior (checklist 2): a zero yield scrape is legal and
    ///         recorded on-chain via the Scraped event; nothing reverts.
    function scrape(uint256 bundleId, uint64 scrapeDays)
        external
        returns (uint256 amount, uint256 toPatron, uint256 toOwner)
    {
        Entitlement storage e = entitlements[bundleId];
        if (!e.active) revert InactiveBundle();
        if (msg.sender != owner && msg.sender != e.patron) revert NotScrapeCaller();

        // Delta accounting: measure exactly what this scrape paid in.
        uint256 balBefore = IERC20(e.yieldToken).balanceOf(address(this));
        IStakeCollection(e.stakeCollection).scrapeInterest(e.stakeId, scrapeDays);
        amount = IERC20(e.yieldToken).balanceOf(address(this)) - balBefore;

        toPatron = (amount * e.patronAllocationBps) / 10000;
        toOwner = amount - toPatron;

        if (toPatron > 0) IERC20(e.yieldToken).transfer(e.patron, toPatron);
        if (toOwner > 0) IERC20(e.yieldToken).transfer(owner, toOwner);

        emit Scraped(bundleId, amount, toPatron, toOwner);
    }

    /// @notice Close a bundle and return both NFTs. WHO can close (checklist 3):
    ///         wrapper owner only, in v0.
    function closeBundle(
        uint256 bundleId,
        address to
    ) external onlyOwner returns (bool) {
        Entitlement storage e = entitlements[bundleId];
        if (!e.active) revert InactiveBundle();
        e.active = false;
        // NOTE: pairUsed bindings are PERMANENT by design.

        // Legacy collections (e.g. CryptoKitties) CLEAR approvals on every
        // transfer, and their transferFrom has no owner exemption, so the
        // wrapper must re-approve itself. Modern OZ collections revert that
        // ("approval to current owner") - tolerate and continue.
        try IERC721(e.stakeCollection).approve(address(this), e.stakeId) {} catch {}
        try IERC721(e.membershipCollection).approve(address(this), e.membershipTokenId) {} catch {}
        IERC721(e.stakeCollection).transferFrom(address(this), to, e.stakeId);
        IERC721(e.membershipCollection).transferFrom(address(this), to, e.membershipTokenId);

        emit BundleClosed(bundleId, to);
        return true;
    }

    function isBundleActive(uint256 bundleId) external view returns (bool) {
        return entitlements[bundleId].active;
    }
}

interface IERC20 {
    function balanceOf(address) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
}
interface IERC721 {
    function approve(address spender, uint256 tokenId) external;
    function transferFrom(address from, address to, uint256 tokenId) external;
}
interface IStakeCollection {
    /// REAL Signature Stakes ABI (mainnet 0x48791...c1e1):
    /// scrapeInterest(tokenId, scrapeDays) -> amount, where scrapeDays is the
    /// number of days of accrued interest to scrape.
    function scrapeInterest(uint256 tokenId, uint64 scrapeDays) external returns (uint256);
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.