Skip to content
Verified contractExactMIT

RootstakeDelegate

Address0x2297d4e7f8f2ad4bf99602a810e7b751ab00d2b1
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.26

 

Same code

2 more contracts

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
Language
solidity
Optimization
Yes, 800 runs
EVM version
cancun
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,646,836
Creation transaction
0x1221e81aca…0ff3523495
Main copy (most transactions)
0x080adc98…98cbe18f

src/RootstakeDelegate.sol

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

import {YieldStream} from "./YieldStream.sol";

/// @title  RootstakeDelegate — the EIP-7702 delegate implementation.
/// @notice An EOA sets its account code to this implementation (via an EIP-7702 authorization).
///         The account then executes Rootstake logic *in place* — its ROOT never moves. Calls are
///         gated to the account itself (`msg.sender == address(this)`, i.e. a self-call from the
///         delegated EOA), so nobody but the account owner can act. `stake`/`unstake`/`claim`
///         forward to the YieldStream as the account, registering the wallet's live balance.
///         Reference implementation — non-custodial; revoke by resetting delegation to 0x0.
contract RootstakeDelegate {
    YieldStream public immutable stream;

    event Staked(uint8 lockIdx);
    event Unstaked();
    event Claimed();

    constructor(address _stream) { stream = YieldStream(payable(_stream)); }

    modifier onlySelf() { require(msg.sender == address(this), "only account"); _; }

    /// @notice Opt the delegated wallet into stake-in-place at `lockIdx` (0=flexible..3=180d).
    function stake(uint8 lockIdx) external onlySelf { stream.delegate(lockIdx); emit Staked(lockIdx); }

    /// @notice Stop staking. Before the lock term, accrued reward is forfeited to the network.
    function unstake() external onlySelf { stream.revoke(); emit Unstaked(); }

    /// @notice Claim ROOT rewards to this wallet without un-delegating.
    function claim() external onlySelf { stream.claim(); emit Claimed(); }

    /// @notice Generic account execution, gated to the account itself.
    function execute(address to, uint256 value, bytes calldata data) external onlySelf returns (bytes memory) {
        (bool ok, bytes memory ret) = to.call{value: value}(data);
        require(ok, "call failed");
        return ret;
    }

    receive() external payable {}
}

lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC20/IERC20.sol)

pragma solidity >=0.4.16;

/**
 * @dev Interface of the ERC-20 standard as defined in the ERC.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

src/YieldStream.sol

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

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/// @title  YieldStream — Rootstake's reflexive stake-in-place ledger + reward treasury.
/// @notice Users DELEGATE (opt-in) instead of depositing: the contract snapshots the caller's
///         live ROOT balance and streams rewards to it — the tokens never leave the user's wallet.
///         The APR is reflexive: the base rate scales with total delegated supply. Locked positions
///         that exit before term FORFEIT their accrued (unclaimed) reward, which stays in the pool
///         and is redistributed to the accounts still rooted. Funded by protocol emissions + the
///         Uniswap v4 hook (swap-flow fees). Non-custodial: only rewards move, never principal.
contract YieldStream {
    IERC20  public immutable root;
    address public owner;

    // lock tiers: multiplier (bps) and duration (seconds)
    uint16[4] public LOCK_MULT = [10000, 16000, 22000, 30000]; // 1.0x / 1.6x / 2.2x / 3.0x
    uint32[4] public LOCK_DUR  = [0, 30 days, 90 days, 180 days];

    uint256 public constant SUPPLY = 1_000_000_000 ether;
    uint256 public aprBaseBps = 50_000;     // 500% base APR (reflexive; owner-tunable, capped)
    uint256 public constant APR_CAP_BPS = 500_000; // hard ceiling 5000%

    struct Delegation { uint8 lockIdx; uint64 since; uint128 snapshot; uint128 settled; bool active; }
    mapping(address => Delegation) public deleg;
    uint256 public totalDelegated;   // sum of active snapshots
    uint256 public totalPaid;        // lifetime ROOT rewards paid

    event Delegated(address indexed who, uint8 lockIdx, uint256 snapshot);
    event Revoked(address indexed who, bool forfeited, uint256 forfeitedAmount);
    event Claimed(address indexed who, uint256 amount);

    modifier onlyOwner() { require(msg.sender == owner, "only owner"); _; }

    constructor(address _root, address _owner) { root = IERC20(_root); owner = _owner; }

    receive() external payable {} // accepts ETH swap-flow fees from the hook

    // ---- reflexive APR ----
    /// @notice Effective APR (bps): base rate lifted by network depth, up to 2x base, capped.
    function effAprBps() public view returns (uint256 a) {
        a = aprBaseBps + (aprBaseBps * totalDelegated) / SUPPLY; // +100% base at full delegation
        if (a > APR_CAP_BPS) a = APR_CAP_BPS;
    }

    function _accrued(address u) internal view returns (uint256) {
        Delegation storage d = deleg[u];
        if (!d.active) return 0;
        uint256 elapsed = block.timestamp - d.since;
        // reward = snapshot * effApr * mult * elapsed / (year)
        return uint256(d.snapshot) * effAprBps() / 10_000 * LOCK_MULT[d.lockIdx] / 10_000 * elapsed / 365 days;
    }

    /// @notice Total claimable now (settled buffer + live accrual).
    function pending(address u) public view returns (uint256) { return deleg[u].settled + _accrued(u); }

    // ---- stake-in-place ----
    /// @notice Delegate the caller's wallet: snapshot its ROOT balance and start streaming rewards.
    function delegate(uint8 lockIdx) external {
        require(lockIdx < 4, "bad lock");
        Delegation storage d = deleg[msg.sender];
        if (d.active) { d.settled += uint128(_accrued(msg.sender)); totalDelegated -= d.snapshot; }
        uint128 bal = uint128(root.balanceOf(msg.sender));
        require(bal > 0, "no ROOT");
        d.lockIdx = lockIdx; d.since = uint64(block.timestamp); d.snapshot = bal; d.active = true;
        totalDelegated += bal;
        emit Delegated(msg.sender, lockIdx, bal);
    }

    /// @notice Revoke delegation. Before lock term, accrued (unclaimed) reward is FORFEITED to the pool.
    function revoke() external {
        Delegation storage d = deleg[msg.sender];
        require(d.active, "not delegated");
        bool matured = block.timestamp >= d.since + LOCK_DUR[d.lockIdx];
        uint256 acc = _accrued(msg.sender);
        uint256 forfeited = 0;
        if (matured || d.lockIdx == 0) d.settled += uint128(acc); else forfeited = acc; // forfeit stays in pool
        totalDelegated -= d.snapshot;
        d.active = false; d.since = 0; d.snapshot = 0;
        emit Revoked(msg.sender, forfeited > 0, forfeited);
    }

    /// @notice Claim ROOT rewards to the caller's wallet. Does NOT un-delegate.
    function claim() external {
        uint256 amt = pending(msg.sender);
        require(amt > 0, "nothing to claim");
        Delegation storage d = deleg[msg.sender];
        d.settled = 0; if (d.active) d.since = uint64(block.timestamp);
        uint256 pool = root.balanceOf(address(this));
        if (amt > pool) amt = pool;
        require(amt > 0, "empty pool");
        totalPaid += amt;
        require(root.transfer(msg.sender, amt), "transfer failed");
        emit Claimed(msg.sender, amt);
    }

    // ---- funding / admin ----
    /// @notice Fund the reward pool with ROOT (emissions). Pull from caller.
    function fund(uint256 amount) external { require(root.transferFrom(msg.sender, address(this), amount), "fund failed"); }
    function setAprBaseBps(uint256 bps) external onlyOwner { require(bps <= APR_CAP_BPS, "too high"); aprBaseBps = bps; }
    function transferOwnership(address n) external onlyOwner { require(n != address(0), "zero"); owner = n; }
    /// @notice Withdraw accumulated ETH swap-flow fees (protocol treasury).
    function withdrawEth(address to, uint256 amount) external onlyOwner { (bool ok,) = to.call{value: amount}(""); require(ok, "eth send failed"); }

    // ---- views for the dApp ----
    function delegationOf(address u) external view returns (uint8 lockIdx, uint64 since, uint256 snapshot, bool active, uint256 pendingRoot) {
        Delegation storage d = deleg[u];
        return (d.lockIdx, d.since, d.snapshot, d.active, pending(u));
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 800,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "solady/=lib/solady/",
    "@openzeppelin/=lib/openzeppelin-contracts/",
    "v4-core/=lib/v4-core/",
    "v4-periphery/=lib/v4-periphery/",
    "forge-std/=lib/forge-std/src/",
    "@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
    "@uniswap/v4-core/=lib/v4-periphery/lib/v4-core/",
    "ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "hardhat/=lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "permit2/=lib/v4-periphery/lib/permit2/",
    "solmate/=lib/v4-core/lib/solmate/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.