Verified contractExactMIT
Proxy
Address0x2d8e5d5c2dc86b88aa76388e8dc0a8df22c4aad0
- Balance
0 ETH
- Transactions
2
- 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
5 files
lib/ERC2535/Proxy.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.38;
import {OwnableFacet} from "./facet/OwnableFacet.sol";
import {UpgradeFacet} from "./facet/UpgradeFacet.sol";
import {LibOwnable} from "./library/LibOwnable.sol";
import {LibProxy} from "./library/LibProxy.sol";
/// @title Proxy
/// @notice The core entry point for the Diamond-like proxy system. It routes incoming function calls to the appropriate facets.
contract Proxy {
/// @notice Thrown when an incoming function call does not match any registered selector.
/// @param selector The 4-byte function signature that was not found in the proxy's registry.
error FunctionNotFound(bytes4 selector);
/// @notice Initializes the proxy contract, sets the owner, and bootstraps the essential facets.
constructor(bytes4[] memory upgradeSelectors, bytes4[] memory ownableSelectors) {
// Initialize the contract owner to the deployer's address
LibOwnable.store().owner = msg.sender;
// Deploy and register the UpgradeFacet to enable future facet modifications
LibProxy.upgradeFacet(keccak256(bytes(type(UpgradeFacet).name)), type(UpgradeFacet).creationCode, upgradeSelectors, "");
// Deploy and register the OwnableFacet to handle ownership queries and transfers
LibProxy.upgradeFacet(keccak256(bytes(type(OwnableFacet).name)), type(OwnableFacet).creationCode, ownableSelectors, "");
}
/// @notice Fallback function that triggers the delegatecall routing for any recognized function signatures.
/// @dev Marked as payable so the proxy can process transactions that send ETH along with calldata.
fallback() external payable {
// Retrieve the facet address associated with the incoming function selector (msg.sig)
address facet = LibProxy.store().facetAddress[msg.sig];
// Revert if the selector has not been registered to any facet
require(facet != address(0), FunctionNotFound(msg.sig));
assembly ("memory-safe") {
// Use temporary memory beyond the free pointer; this block always returns or reverts.
let data := mload(0x40)
calldatacopy(data, 0x00, calldatasize())
// Execute the delegatecall to the facet's implementation.
// Parameters: gas, target address, args memory offset, args size, return data offset, return data size.
// Return data offset and size are set to 0 initially because the exact return size is unknown at this point.
let result := delegatecall(gas(), facet, data, calldatasize(), 0x00, 0x00)
// Reuse the temporary region to forward the exact return or error data.
returndatacopy(data, 0x00, returndatasize())
// Evaluate the result of the delegatecall
switch result
// If delegatecall returns 0, an error or revert occurred in the facet
case 0 {
// Revert and pass along the exact error data returned by the facet
revert(data, returndatasize())
}
// If delegatecall returns 1, the execution was successful
default {
// Return the exact return data provided by the facet
return (data, returndatasize())
}
}
}
/// @notice Standard receive function to allow the proxy to accept plain ETH transfers (empty calldata).
receive() external payable {}
}
lib/ERC2535/facet/OwnableFacet.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.38;
import {LibOwnable} from "../library/LibOwnable.sol";
/// @title OwnableFacet
/// @notice A facet contract that provides ownership management functionalities for a Diamond-like proxy.
contract OwnableFacet {
error ZeroAddress();
/// @notice Transfers ownership of the contract to a new account.
/// @dev Can only be called by the current owner.
/// @param newOwner The address of the new owner.
function transferOwner(address newOwner) external {
// Prevent assigning the owner role to the zero address
require(newOwner != address(0), ZeroAddress());
// Ensure the caller is the current owner
LibOwnable.onlyOwnerCall();
// Update the owner address in the Diamond storage
LibOwnable.store().owner = newOwner;
}
/// @notice Retrieves the address of the current owner.
/// @return The address of the current owner.
function getOwner() external view returns (address) {
// Fetch the owner address directly from the Diamond storage
return LibOwnable.store().owner;
}
}
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.