Skip to content
Verified contractExactMIT

Proxy

Address0x66ddce67cf2b3b499c5f953eee49a25432ca3333
Balance

0.03618 ETH

 

Transactions

1,477

 

Compiler

solc 0.8.20

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.20+commit.a1b79de6
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,445,982
Creation transaction
0xea18c5f78c…772db3d9a1
Proxy implementation
0xab460099…051f0fc4HayekToken

proxy.sol

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

/// @title ERC1967 — 可升级代理的标准存储槽
/// @notice 使用 ERC1967 规定槽位保存实现合约地址,实现合约(P)与代理(Proxy)共享该常量,不会占用存储槽
abstract contract ERC1967 {
    /// @dev bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1)
    bytes32 internal constant IMPLEMENTATION_SLOT =
        0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;

    /// @dev bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1)
    bytes32 internal constant ADMIN_SLOT =
        0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;

    event Upgraded(address indexed implementation);
    event AdminChanged(address previousAdmin, address newAdmin);

    error NotContract();
    error NotProxyAdmin();
    error InitializeFailed();

    function _implementation() internal view returns (address impl) {
        assembly {
            impl := sload(IMPLEMENTATION_SLOT)
        }
    }

    function _admin() internal view returns (address adm) {
        assembly {
            adm := sload(ADMIN_SLOT)
        }
    }

    function _setImplementation(address newImplementation) internal {
        assembly {
            sstore(IMPLEMENTATION_SLOT, newImplementation)
        }
        emit Upgraded(newImplementation);
    }

    function _setAdmin(address newAdmin) internal {
        emit AdminChanged(_admin(), newAdmin);
        assembly {
            sstore(ADMIN_SLOT, newAdmin)
        }
    }
}

/// @title Proxy — 通用 delegatecall 代理
/// @notice 构造函数内可直接执行初始化调用(此时 tx.origin 仍为部署者),
///         后续调用全部通过 fallback 转发给实现合约
contract Proxy is ERC1967 {
    constructor(address implementation_, address admin_, bytes memory data) payable {
        if (implementation_.code.length == 0) revert NotContract();
        _setImplementation(implementation_);
        _setAdmin(admin_ == address(0) ? msg.sender : admin_);

        if (data.length > 0) {
            (bool success, bytes memory ret) = implementation_.delegatecall(data);
            if (!success) {
                if (ret.length > 0) {
                    assembly {
                        revert(add(ret, 0x20), mload(ret))
                    }
                }
                revert InitializeFailed();
            }
        }
    }

    /// @notice 代理自身实现地址(方便浏览器/工具读取)
    function implementation() external view returns (address) {
        return _implementation();
    }

    /// @notice 代理管理员(仅用于后台升级权限确认)
    function admin() external view returns (address) {
        return _admin();
    }

    fallback() external payable {
        _delegate();
    }

    receive() external payable {
        _delegate();
    }

    function _delegate() private {
        address impl = _implementation();
        assembly {
            calldatacopy(0, 0, calldatasize())
            let result := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
            returndatacopy(0, 0, returndatasize())
            switch result
            case 0 {
                revert(0, returndatasize())
            }
            default {
                return(0, returndatasize())
            }
        }
    }
}

/// @title UUPSUpgradeable — 实现合约侧升级能力
/// @notice implementation 地址写在 ERC1967 槽位,升级函数由实现合约提供(onlyOwner 控制)
abstract contract UUPSUpgradeable is ERC1967 {
    modifier onlyProxyAdmin() {
        if (_admin() != msg.sender) revert NotProxyAdmin();
        _;
    }

    /// @notice 升级实现合约(仅管理员)
    function upgradeTo(address newImplementation) external onlyProxyAdmin {
        if (newImplementation.code.length == 0) revert NotContract();
        _setImplementation(newImplementation);
    }

    /// @notice 转移代理管理员
    function changeProxyAdmin(address newAdmin) external onlyProxyAdmin {
        if (newAdmin == address(0)) revert NotProxyAdmin();
        _setAdmin(newAdmin);
    }

    /// @notice 当前实现合约地址
    function implementation() external view returns (address) {
        return _implementation();
    }

    /// @notice 当前代理管理员
    function proxyAdmin() external view returns (address) {
        return _admin();
    }
}

/// @title Reinitializable — 可升级合约的初始化保护
/// @notice 使用 ERC1967 后的普通存储位记录版本号,允许新版本实现合约再次初始化新字段
abstract contract Reinitializable {
    uint256 private _initVersion;

    error AlreadyInitialized();

    modifier initializer(uint256 version) {
        if (_initVersion >= version) revert AlreadyInitialized();
        _initVersion = version;
        _;
    }

    /// @dev 实现合约自身锁定初始化,只允许通过代理进行初始化
    function _disableInitializers() internal {
        _initVersion = type(uint256).max;
    }

    function initializedVersion() external view returns (uint256) {
        return _initVersion;
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris",
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.