Skip to content
Verified contractPartialMIT

GovernanceSpell_09_18_2026

Address0xee6066df6e27f142b921fd38e6766a9d6af35c2f
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.28

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,342,734
Creation transaction
0xf5b55ecf26…50194b0bb3

98 files

The source code is large: one file is shown at a time. Choose a file in the list.

contracts/spells/GovernanceSpell_09_18_2026.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import { IAccessControlEnumerable } from "@openzeppelin/contracts/access/extensions/IAccessControlEnumerable.sol";
import { IGovernor } from "@openzeppelin/contracts/governance/IGovernor.sol";
import { TimelockController } from "@openzeppelin/contracts/governance/TimelockController.sol";
import { IERC5805 } from "@openzeppelin/contracts/interfaces/IERC5805.sol";
import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

import { IReserveOptimisticGovernorDeployer } from "@reserve-protocol/reserve-governor/contracts/interfaces/IDeployer.sol";
import { IOptimisticSelectorRegistry } from "@reserve-protocol/reserve-governor/contracts/interfaces/IOptimisticSelectorRegistry.sol";
import { IReserveOptimisticGovernor } from "@reserve-protocol/reserve-governor/contracts/interfaces/IReserveOptimisticGovernor.sol";
import { IRewardTokenRegistry } from "@reserve-protocol/reserve-governor/contracts/interfaces/IRewardTokenRegistry.sol";

import { IFolio, Folio } from "@src/Folio.sol";
import { FolioProxyAdmin } from "@folio/FolioProxy.sol";
import { DEFAULT_ADMIN_ROLE, REBALANCE_MANAGER, BRAND_MANAGER, AUCTION_LAUNCHER, MAX_FEE_RECIPIENTS } from "@utils/Constants.sol";

bytes32 constant VERSION_1_1_0 = keccak256("1.1.0");
bytes32 constant VERSION_4_0_0 = keccak256("4.0.0");
bytes32 constant VERSION_5_0_0 = keccak256("5.0.0");
bytes4 constant START_REBALANCE_4_0_0 = 0x235d7142;
bytes4 constant START_REBALANCE_5_0_0 = 0x207c8eed;
bytes32 constant PROPOSER_ROLE = keccak256("PROPOSER_ROLE");
bytes32 constant CANCELLER_ROLE = keccak256("CANCELLER_ROLE");

interface IFolioGovernor is IGovernor {
    function token() external view returns (address);
    function timelock() external view returns (address);
}

interface IVersioned {
    function version() external view returns (string memory);
}

interface IStakingVault is IERC5805, IERC4626, IVersioned {}

// trusted-fillers GenericTokenJar-compatible interface
interface IGenericTokenJar {
    function token() external view returns (address);
    function destination() external view returns (address);
}

interface IRewardedStakingVault is IStakingVault {
    function getAllRewardTokens() external view returns (address[] memory);
}

// old staking vault model
interface IOwnableStakingVault is IStakingVault {
    function owner() external view returns (address);
    function transferOwnership(address newOwner) external;
    function renounceOwnership() external;
    function setUnstakingDelay(uint256 unstakingDelay) external;
    function setRewardRatio(uint256 rewardHalfLife) external;
}

/**
 * @title GovernanceSpell_09_18_2026
 * @author akshatmittal, julianmrodri, tbrent
 * @notice Optimistic governance upgrade spell for DTFs
 *
 * Upgrade spell to move a StakingVault + Folio onto the optimistic governance system.
 *
 * Each StakingVault and Folio has its own governor/timelock system. A single StakingVault can be used
 *   as the governance token in multiple Folios, but always has its own governor/timelock for its own governance.
 *
 * Upgrade flow:
 *   1. deploySuccessorStakingVault: Permissionlessly deploy a NEW StakingVault with its own NEW
 *      governor/timelock system, isolated from the old vault. No permissions required.
 *   2. upgradeFolio: Deploy NEW Folio governance system on the successor StakingVault; rotate Folio roles,
 *      and rotate fee recipients from old StakingVault to new StakingVault. Wait for new stake before calling.
 *        Caller: old timelock of Folio
 *   3. retireOldStakingVault: After every dependent Folio has completed step 2, permanently seal the
 *      old StakingVault (zero unstaking delay, fast reward handout, renounce ownership).
 *        Caller: timelock of old StakingVault
 *
 */
contract GovernanceSpell_09_18_2026 {
    error UpgradeError(uint256 code);

    event NewGovernanceDeployment(NewDeployment newDeployment);
    event StakingVaultRetired(address oldStakingVault);

    struct NewDeployment {
        address stakingVault;
        address newGovernor;
        address newTimelock;
        address newSelectorRegistry;
    }

    IReserveOptimisticGovernorDeployer public immutable governorDeployer;

    constructor(IReserveOptimisticGovernorDeployer _governorDeployer) {
        require(keccak256(bytes(IVersioned(address(_governorDeployer)).version())) == VERSION_1_1_0, UpgradeError(0));

        governorDeployer = _governorDeployer;
    }

    /// Deploy a successor StakingVault with a fresh optimistic governance system
    /// @dev Permissionless: does not require or change any ownership on the old staking vault
    /// @param guardians Must be a subset of the old staking vault timelock's CANCELLER_ROLE members
    function deploySuccessorStakingVault(
        IFolioGovernor stakingVaultGovernor,
        IReserveOptimisticGovernor.OptimisticGovernanceParams calldata optimisticParams,
        address[] calldata guardians,
        address[] calldata rewardTokens,
        bytes32 deploymentNonce
    ) public returns (NewDeployment memory newDeployment) {
        IStakingVault oldStakingVault = IStakingVault(stakingVaultGovernor.token());
        address newUnderlying = oldStakingVault.asset();
        require(newUnderlying != address(0), UpgradeError(21));

        IReserveOptimisticGovernorDeployer.BaseDeploymentParams memory baseParams = _baseDeploymentParams(
            stakingVaultGovernor,
            optimisticParams,
            new address[](0),
            guardians
        );
        IReserveOptimisticGovernorDeployer.NewStakingVaultParams
            memory newStakingVaultParams = IReserveOptimisticGovernorDeployer.NewStakingVaultParams({
                underlying: IERC20Metadata(newUnderlying),
                rewardTokens: rewardTokens,
                rewardHalfLife: 1 weeks,
                unstakingDelay: 1 weeks
            });

        (
            newDeployment.stakingVault,
            newDeployment.newGovernor,
            newDeployment.newTimelock,
            newDeployment.newSelectorRegistry
        ) = governorDeployer.deployWithNewStakingVault(
            baseParams,
            newStakingVaultParams,
            keccak256(abi.encode(deploymentNonce, msg.sender))
        );

        require(newDeployment.newTimelock != address(0), UpgradeError(22));
        require(IStakingVault(newDeployment.stakingVault).asset() == newUnderlying, UpgradeError(23));
        require(newDeployment.stakingVault != address(oldStakingVault), UpgradeError(24));
        require(
            IAccessControlEnumerable(newDeployment.stakingVault).getRoleMemberCount(DEFAULT_ADMIN_ROLE) == 1,
            UpgradeError(26)
        );
        require(
            IAccessControlEnumerable(newDeployment.stakingVault).getRoleMember(DEFAULT_ADMIN_ROLE, 0) ==
                newDeployment.newTimelock,
            UpgradeError(27)
        );

        emit NewGovernanceDeployment(newDeployment);
    }

    /// Deploy a new Folio governor/timelock on an existing staking vault and transfer Folio ownership/roles
    /// @dev Requirements:
    ///      - Caller is old Folio timelock
    ///      - Folio is exactly version 4.0.0 or 5.0.0
    ///      - Self is Folio admin
    ///      - Self is FolioProxyAdmin owner
    /// @dev IMPORTANT: Must atomically grant ownerships to the spell just before calling `upgradeFolio()`
    /// @dev Assumptions:
    ///      1. Folio governors SHOULD vote down new staking vaults with proposals in their governor
    ///      2. The Guardian emergency council SHOULD cancel malicious proposals proposed to new StakingVault
    ///                  governors while a `upgradeFolio()` proposal is in-flight.
    ///      3. The Guardian emergency council SHOULD cancel malicious proposals proposed to new Folio
    ///                  governors directly after `upgradeFolio()` execution, when new stake is still populating.
    /// @dev New Governance system will use standard 2-3-2 day voting independent of previous voting settings
    /// @param newStakingVault New staking vault to use for the new governor
    /// @param oldFolioGovernor Governor currently attached to the Folio being upgraded
    /// @param tradingGovernor Governor currently managing Folio trading operations
    /// @param optimisticProposers Use empty set to disable optimistic governance altogether
    /// @param guardians Must be a subset of the old Folio timelock's CANCELLER_ROLE members
    ///                  The shared Guardian contract will be included as a CANCELLER_ROLE member by default
    /// @param newFeeRecipient Optional GenericTokenJar address to intermediate new StakingVault revenue
    function upgradeFolio(
        Folio folio,
        FolioProxyAdmin folioProxyAdmin,
        IStakingVault newStakingVault,
        IFolioGovernor oldFolioGovernor,
        IFolioGovernor tradingGovernor,
        IReserveOptimisticGovernor.OptimisticGovernanceParams calldata optimisticParams,
        address[] calldata optimisticProposers,
        address[] calldata guardians,
        address newFeeRecipient,
        bytes32 deploymentNonce
    ) public returns (NewDeployment memory newDeployment) {
        // included for readability, root of security is folioProxyAdmin + folio role checks
        require(oldFolioGovernor.timelock() == msg.sender, UpgradeError(1));
        bytes32 folioVersion = keccak256(bytes(IVersioned(address(folio)).version()));
        require(folioVersion == VERSION_4_0_0 || folioVersion == VERSION_5_0_0, UpgradeError(28));

        // newStakingVault must not be the old immmutable kind, must be new and upgradeable
        require(keccak256(bytes(IVersioned(address(newStakingVault)).version())) == VERSION_1_1_0, UpgradeError(3));

        {
            IReserveOptimisticGovernorDeployer.BaseDeploymentParams memory baseParams = _baseDeploymentParams(
                oldFolioGovernor,
                optimisticParams,
                optimisticProposers,
                guardians
            );
            baseParams.selectorData = _startRebalanceSelectorData(folio, folioVersion);

            newDeployment.stakingVault = address(newStakingVault);
            (newDeployment.newGovernor, newDeployment.newTimelock, newDeployment.newSelectorRegistry) = governorDeployer
                .deployWithExistingStakingVault(baseParams, address(newStakingVault), deploymentNonce);
            require(newDeployment.newTimelock != address(0), UpgradeError(2));
        }

        // confirm Folio admins are self + old timelock
        require(folio.getRoleMemberCount(DEFAULT_ADMIN_ROLE) == 2, UpgradeError(4));
        require(folio.hasRole(DEFAULT_ADMIN_ROLE, address(this)), UpgradeError(5));
        require(folio.hasRole(DEFAULT_ADMIN_ROLE, msg.sender), UpgradeError(6));

        // main timelock should only be carrying admin ownership
        require(!folio.hasRole(BRAND_MANAGER, msg.sender), UpgradeError(29));
        require(!folio.hasRole(AUCTION_LAUNCHER, msg.sender), UpgradeError(30));

        {
            address tradingTimelock = tradingGovernor.timelock();

            // validate old trading governance
            require(folio.getRoleMemberCount(REBALANCE_MANAGER) == 1, UpgradeError(7));
            require(folio.getRoleMember(REBALANCE_MANAGER, 0) == tradingTimelock, UpgradeError(8));
            require(
                TimelockController(payable(tradingTimelock)).hasRole(PROPOSER_ROLE, address(tradingGovernor)),
                UpgradeError(34)
            );
            require(!folio.hasRole(AUCTION_LAUNCHER, tradingTimelock), UpgradeError(29));
            require(!folio.hasRole(BRAND_MANAGER, tradingTimelock), UpgradeError(30));
        }

        // rotate Folio fee recipients from old staking vault to new staking vault
        address[4] memory invalidFeeRecipients = [
            address(oldFolioGovernor),
            msg.sender,
            address(tradingGovernor),
            tradingGovernor.timelock()
        ];

        // direct revenue to StakingVault if no GenericTokenJar is provided
        if (newFeeRecipient == address(0)) {
            newFeeRecipient = address(newStakingVault);

            _validateFolioRewardToken(folio, newStakingVault);
        } else {
            _validateGenericTokenJar(newFeeRecipient, newStakingVault);
        }

        _rotateFeeRecipients(folio, oldFolioGovernor.token(), newFeeRecipient, invalidFeeRecipients);

        // rotate Folio REBALANCE_MANAGER
        folio.revokeRole(REBALANCE_MANAGER, tradingGovernor.timelock());
        folio.grantRole(REBALANCE_MANAGER, newDeployment.newTimelock);
        require(folio.getRoleMemberCount(REBALANCE_MANAGER) == 1, UpgradeError(7));
        require(folio.getRoleMember(REBALANCE_MANAGER, 0) == newDeployment.newTimelock, UpgradeError(8));

        // transfer Folio proxy admin ownership
        require(folioProxyAdmin.owner() == address(this), UpgradeError(9));
        folioProxyAdmin.transferOwnership(newDeployment.newTimelock);
        require(folioProxyAdmin.owner() == newDeployment.newTimelock, UpgradeError(10));

        // rotate Folio DEFAULT_ADMIN_ROLE
        folio.revokeRole(DEFAULT_ADMIN_ROLE, msg.sender);
        folio.grantRole(DEFAULT_ADMIN_ROLE, newDeployment.newTimelock);
        folio.renounceRole(DEFAULT_ADMIN_ROLE, address(this));
        require(folio.getRoleMemberCount(DEFAULT_ADMIN_ROLE) == 1, UpgradeError(11));
        require(folio.getRoleMember(DEFAULT_ADMIN_ROLE, 0) == newDeployment.newTimelock, UpgradeError(12));

        emit NewGovernanceDeployment(newDeployment);
    }

    /// Permanently retire an old StakingVault after every dependent Folio has upgraded
    /// @dev Requirements:
    ///      - Spell is owner of `oldStakingVault`
    /// @dev Enumeration of dependent Folios is off-chain: current governance is responsible for confirming
    ///      no Folio governor still uses this StakingVault as its voting token before transferring ownership.
    /// @dev No new Folios should be added to the old StakingVault after `retireOldStakingVault()` is proposed.
    function retireOldStakingVault(IOwnableStakingVault oldStakingVault) public {
        require(oldStakingVault.owner() == address(this), UpgradeError(13));

        oldStakingVault.setUnstakingDelay(0);
        oldStakingVault.setRewardRatio(1 days);
        oldStakingVault.renounceOwnership();
        require(oldStakingVault.owner() == address(0), UpgradeError(14));

        emit StakingVaultRetired(address(oldStakingVault));
    }

    // === Internal ===

    function _baseDeploymentParams(
        IFolioGovernor oldGovernor,
        IReserveOptimisticGovernor.OptimisticGovernanceParams calldata optimisticParams,
        address[] memory optimisticProposers,
        address[] calldata guardians
    ) internal view returns (IReserveOptimisticGovernorDeployer.BaseDeploymentParams memory baseParams) {
        // Optimistic governance params
        baseParams.optimisticParams = optimisticParams;

        // Standard governance params
        baseParams.standardParams.votingDelay = 2 days;
        baseParams.standardParams.votingPeriod = 3 days;
        baseParams.standardParams.voteExtension = 2 days;
        baseParams.standardParams.proposalThreshold = 0.01e18; // 1%
        baseParams.standardParams.quorumNumerator = 0.1e18; // 10%
        // hard-coded long standard governance params to unify across DTFs

        // Optimistic whitelists
        baseParams.selectorData = new IOptimisticSelectorRegistry.SelectorData[](0);
        baseParams.optimisticProposers = optimisticProposers;

        // Guardians
        _validateGuardians(oldGovernor, guardians);
        baseParams.additionalGuardians = guardians;

        // Timelock delay
        baseParams.timelockDelay = 2 days;

        // Proposal throttle
        baseParams.proposalThrottleCapacity = 3;
    }

    function _startRebalanceSelectorData(
        Folio folio,
        bytes32 folioVersion
    ) internal pure returns (IOptimisticSelectorRegistry.SelectorData[] memory selectorData) {
        selectorData = new IOptimisticSelectorRegistry.SelectorData[](1);
        bytes4[] memory selectors = new bytes4[](folioVersion == VERSION_4_0_0 ? 2 : 1);
        selectors[0] = START_REBALANCE_5_0_0;
        if (folioVersion == VERSION_4_0_0) selectors[1] = START_REBALANCE_4_0_0;
        selectorData[0] = IOptimisticSelectorRegistry.SelectorData({ target: address(folio), selectors: selectors });
    }

    function _validateFolioRewardToken(Folio folio, IStakingVault newStakingVault) internal view {
        require(
            IRewardTokenRegistry(governorDeployer.rewardTokenRegistry()).isRegistered(address(folio)),
            UpgradeError(33)
        );

        address[] memory rewardTokens = IRewardedStakingVault(address(newStakingVault)).getAllRewardTokens();
        for (uint256 i; i < rewardTokens.length; i++) {
            if (rewardTokens[i] == address(folio)) return;
        }

        revert UpgradeError(34);
    }

    function _validateGenericTokenJar(address newFeeRecipient, IStakingVault newStakingVault) internal view {
        IGenericTokenJar jar = IGenericTokenJar(newFeeRecipient);

        require(jar.token() == newStakingVault.asset(), UpgradeError(41));
        require(jar.destination() == address(newStakingVault), UpgradeError(42));
    }

    /// Require `guardians` is a subset of the old timelock's CANCELLER_ROLE members (excl old governor)
    /// @dev Does NOT confirm `guardians` is the complete set of CANCELLER_ROLE members
    function _validateGuardians(IFolioGovernor oldGovernor, address[] memory guardians) internal view {
        TimelockController oldTimelock = TimelockController(payable(oldGovernor.timelock()));

        for (uint256 i; i < guardians.length; i++) {
            require(guardians[i] != address(0) && guardians[i] != address(oldGovernor), UpgradeError(17));
            require(oldTimelock.hasRole(CANCELLER_ROLE, guardians[i]), UpgradeError(18));
        }
    }

    /// Rotate the fee recipient entry for the old fee recipient to the new fee recipient
    function _rotateFeeRecipients(
        Folio folio,
        address oldFeeRecipient,
        address newFeeRecipient,
        address[4] memory invalidFeeRecipients
    ) internal {
        require(newFeeRecipient != invalidFeeRecipients[0], UpgradeError(37));
        require(newFeeRecipient != invalidFeeRecipients[1], UpgradeError(38));
        require(newFeeRecipient != invalidFeeRecipients[2], UpgradeError(39));
        require(newFeeRecipient != invalidFeeRecipients[3], UpgradeError(40));

        IFolio.FeeRecipient[] memory recipients = _feeRecipients(folio);
        uint256 oldFeeRecipientCount;
        uint256 oldFeeRecipientIndex;

        for (uint256 i; i < recipients.length; i++) {
            address recipient = recipients[i].recipient;

            if (recipient == oldFeeRecipient) {
                oldFeeRecipientCount++;
                oldFeeRecipientIndex = i;
            }

            require(recipient != newFeeRecipient, UpgradeError(19));
            require(recipient != invalidFeeRecipients[0], UpgradeError(31));
            require(recipient != invalidFeeRecipients[1], UpgradeError(32));
            require(recipient != invalidFeeRecipients[2], UpgradeError(35));
            require(recipient != invalidFeeRecipients[3], UpgradeError(36));
        }

        require(oldFeeRecipientCount == 1, UpgradeError(20));

        recipients[oldFeeRecipientIndex].recipient = newFeeRecipient;
        _sortFeeRecipients(recipients);
        folio.setFeeRecipients(recipients);
    }

    function _feeRecipients(Folio folio) internal view returns (IFolio.FeeRecipient[] memory recipients) {
        // no accessor for feeRecipients.length
        uint256 length;
        for (; length < MAX_FEE_RECIPIENTS; length++) {
            try folio.feeRecipients(length) returns (address, uint96) {
                // no-op
            } catch {
                break;
            }
        }

        recipients = new IFolio.FeeRecipient[](length);
        for (uint256 i; i < length; i++) {
            (address recipient, uint96 portion) = folio.feeRecipients(i);
            recipients[i] = IFolio.FeeRecipient({ recipient: recipient, portion: portion });
        }
    }

    function _sortFeeRecipients(IFolio.FeeRecipient[] memory recipients) internal pure {
        for (uint256 i = 1; i < recipients.length; i++) {
            IFolio.FeeRecipient memory recipient = recipients[i];
            uint256 j = i;
            while (j > 0 && uint160(recipients[j - 1].recipient) > uint160(recipient.recipient)) {
                recipients[j] = recipients[j - 1];
                j--;
            }
            recipients[j] = recipient;
        }
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=node_modules/forge-std/src/",
    "@openzeppelin/contracts/=node_modules/@openzeppelin/contracts/",
    "@reserve-protocol/trusted-fillers/=node_modules/@reserve-protocol/trusted-fillers/",
    "@reserve-protocol/reserve-governor/=node_modules/@reserve-protocol/reserve-governor/",
    "@openzeppelin/contracts-upgradeable/=node_modules/@openzeppelin/contracts-upgradeable/",
    "@prb/math/=node_modules/@prb/math/",
    "utils/=test/utils/",
    "@src/=contracts/",
    "@utils/=contracts/utils/",
    "@interfaces/=contracts/interfaces/",
    "@folio/=contracts/folio/",
    "@gov/=contracts/governance/",
    "@staking/=contracts/staking/",
    "@deployer/=contracts/deployer/",
    "@spells/=contracts/spells/",
    "@periphery/=contracts/periphery/",
    "@openzeppelin/=node_modules/@openzeppelin/",
    "@prb/=node_modules/@prb/",
    "@reserve-protocol/=node_modules/@reserve-protocol/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.