Skip to content
Verified contractExactMIT

UpgradeFacet

Address0x509b6efb8630eb71ebfea3de70a6c983e5003516
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.38

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.38+commit.1835d247
Language
solidity
Optimization
Yes, 100,000 runs
EVM version
osaka
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,433,748
Creation transaction
0x6e8cd00ba4…76b1203a88

lib/ERC2535/facet/UpgradeFacet.sol

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

import {LibOwnable} from "../library/LibOwnable.sol";
import {LibProxy} from "../library/LibProxy.sol";

/// @title UpgradeFacet
/// @notice A core facet responsible for upgrading, managing, querying, and removing other facets within the proxy system.
contract UpgradeFacet {
    /// @notice Replaces a complete facet using its stable ID and compiler-derived selectors.
    function upgradeFacet(bytes32 facetId, bytes calldata creationCode, bytes4[] calldata selectors, bytes calldata initializeData) external returns (address) {
        LibOwnable.onlyOwnerCall();
        return LibProxy.upgradeFacet(facetId, creationCode, selectors, initializeData);
    }

    /// @notice Returns the implementation registered under an ID, or zero if it is absent.
    function getFacetById(bytes32 facetId) external view returns (address) {
        return LibProxy.store().facets[facetId].implementation;
    }

    /// @notice Retrieves the address of the facet that implements a specific function selector.
    /// @param selector The 4-byte function selector.
    /// @return The deployed address of the facet handling the selector, or the zero address if unregistered.
    function getFacetBySelector(bytes4 selector) external view returns (address) {
        return LibProxy.store().facetAddress[selector];
    }

    /// @notice Retrieves all function selectors associated with a specific facet.
    /// @param facetId The stable identifier generated by the deployment script.
    /// @return An array of 4-byte function selectors implemented by the facet.
    function getFacetSelectors(bytes32 facetId) external view returns (bytes4[] memory) {
        return LibProxy.store().facets[facetId].selectors;
    }

    /// @notice Completely removes a facet and deregisters all of its associated function selectors.
    /// @dev Can only be called by the contract owner.
    /// @param facetId The stable identifier of the facet to remove.
    function removeFacet(bytes32 facetId) external {
        LibOwnable.onlyOwnerCall();
        LibProxy.removeFacet(facetId);
    }
}

lib/ERC2535/library/LibOwnable.sol

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

/// @title LibOwnable
/// @notice A library for managing contract ownership using the Diamond storage pattern.
library LibOwnable {
    error OnlyOwnerCalls(address origin, address caller);

    /// @custom:storage-location erc7201:erc2535.LibOwnable
    struct Store {
        // The address of the current owner
        address owner;
    }

    // ERC-7201 base slot for this storage namespace.
    uint256 internal constant POS = erc7201("erc2535.LibOwnable");

    /// @notice Retrieves the storage pointer for the ownership state.
    /// @return $ The storage reference to the Store struct.
    function store() internal pure returns (Store storage $) {
        uint256 position = POS;
        assembly ("memory-safe") {
            $.slot := position
        }
    }

    /// @notice Enforces that the caller is the owner of the contract.
    /// @dev This should be called inside functions that require owner-only access control.
    function onlyOwnerCall() internal view {
        // Retrieve the current owner from Diamond storage
        address owner = store().owner;
        // Retrieve the address making the current function call
        address caller = msg.sender;

        // Revert with a custom error if the caller is not the owner
        require(owner == caller, OnlyOwnerCalls(owner, caller));
    }
}

lib/ERC2535/library/LibProxy.sol

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

/// @title LibProxy
/// @notice Stores selector routing and replaces complete facets identified by deployment-script IDs.
library LibProxy {
    error EmptySelectors();
    error DeployContractFailed();
    error SelectorConflictDetected(address facet, bytes4 selector);
    error InitializeFailed(address facet);

    struct FacetInfo {
        address implementation;
        bytes4[] selectors;
    }

    /// @custom:storage-location erc7201:erc2535.LibProxy
    struct Store {
        mapping(bytes4 => address) facetAddress;
        mapping(bytes32 => FacetInfo) facets;
    }

    uint256 internal constant POS = erc7201("erc2535.LibProxy");

    // Fixed CREATE2 salt; identical creation code reuses its existing address.
    bytes32 internal constant DEPLOY_SALT = 0x7611c5eb18af4f0ec42cab0f4d97be0e4e095bddbd6345447b169c31d8d5b710;

    function store() internal pure returns (Store storage $) {
        uint256 position = POS;
        assembly ("memory-safe") {
            $.slot := position
        }
    }

    /// @dev Selectors come from compiler artifacts. A failed install or initializer rolls back removal and deployment.
    function upgradeFacet(bytes32 facetId, bytes memory creationCode, bytes4[] memory selectors, bytes memory initializeData) internal returns (address newFacet) {
        require(selectors.length > 0, EmptySelectors());
        newFacet = deployFacet(creationCode);
        removeFacet(facetId);

        Store storage $ = store();
        for (uint256 i = 0; i < selectors.length; ++i) {
            bytes4 selector = selectors[i];
            address existing = $.facetAddress[selector];
            // This also rejects duplicates: the first occurrence has already been registered.
            require(existing == address(0), SelectorConflictDetected(existing, selector));
            $.facetAddress[selector] = newFacet;
        }
        $.facets[facetId] = FacetInfo(newFacet, selectors);

        if (initializeData.length > 0) {
            (bool success, bytes memory err) = newFacet.delegatecall(initializeData);
            if (!success) {
                if (err.length > 0) {
                    assembly ("memory-safe") {revert(add(err, 0x20), mload(err))}
                } else {
                    revert InitializeFailed(newFacet);
                }
            }
        }
    }

    /// @notice Removes every selector registered under an ID. Missing IDs are a no-op.
    function removeFacet(bytes32 facetId) internal {
        Store storage $ = store();
        bytes4[] storage selectors = $.facets[facetId].selectors;
        for (uint256 i = 0; i < selectors.length; ++i) {
            delete $.facetAddress[selectors[i]];
        }
        delete $.facets[facetId];
    }

    /// @dev Reuses existing CREATE2 code so previously installed implementations can be reinstalled or restored.
    function deployFacet(bytes memory creationCode) internal returns (address facet) {
        facet = address(uint160(uint256(keccak256(abi.encodePacked(bytes1(0xff), address(this), DEPLOY_SALT, keccak256(creationCode))))));
        if (facet.code.length > 0) return facet;

        assembly ("memory-safe") {facet := create2(0, add(creationCode, 0x20), mload(creationCode), DEPLOY_SALT)}
        require(facet != address(0) && facet.code.length > 0, DeployContractFailed());
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 100000,
    "enabled": true
  },
  "viaSSACFG": false,
  "evmVersion": "osaka",
  "remappings": [
    "@openzeppelin/=node_modules/@openzeppelin/",
    "ERC2535/=lib/ERC2535/",
    "forge-std/=node_modules/forge-std/"
  ],
  "experimental": false
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.