文件 1 的 3:CegaEntry.sol
pragma solidity ^0.8.10;
import { CegaEntryLib } from "./lib/CegaEntryLib.sol";
import { ICegaEntry } from "./interfaces/ICegaEntry.sol";
contract CegaEntry is ICegaEntry {
constructor(address _contractOwner) payable {
CegaEntryLib.setContractOwner(_contractOwner);
}
function diamondCut(
ProxyImplementation[] calldata _implementationParams,
address _init,
bytes calldata _calldata
) external override {
CegaEntryLib.enforceIsContractOwner();
CegaEntryLib.updateImplementation(
_implementationParams,
_init,
_calldata
);
}
fallback() external payable {
CegaEntryLib.ProxyStorage storage ds = CegaEntryLib.proxyStorage();
address implementation = ds
.selectorToImplAndPosition[msg.sig]
.implAddress;
require(
implementation != address(0),
"CegaEntry: Function does not exist"
);
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(
gas(),
implementation,
0,
calldatasize(),
0,
0
)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
receive() external payable {}
}
文件 2 的 3:CegaEntryLib.sol
pragma solidity ^0.8.10;
import { ICegaEntry } from "../interfaces/ICegaEntry.sol";
library CegaEntryLib {
bytes32 constant PROXY_STORAGE_POSITION =
bytes32(uint256(keccak256("cega.proxy.implementation.storage")) - 1);
struct ImplementationAddressAndPosition {
address implAddress;
uint96 functionSelectorPosition;
}
struct ImplementationFunctionSelectors {
bytes4[] functionSelectors;
uint256 implementationAddressPosition;
}
struct ProxyStorage {
mapping(bytes4 => ImplementationAddressAndPosition) selectorToImplAndPosition;
mapping(address => ImplementationFunctionSelectors) implementationFunctionSelectors;
address[] implementationAddresses;
mapping(bytes4 => bool) supportedInterfaces;
address contractOwner;
}
function proxyStorage() internal pure returns (ProxyStorage storage ds) {
bytes32 position = PROXY_STORAGE_POSITION;
assembly {
ds.slot := position
}
}
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
function setContractOwner(address _newOwner) internal {
ProxyStorage storage ds = proxyStorage();
address previousOwner = ds.contractOwner;
ds.contractOwner = _newOwner;
emit OwnershipTransferred(previousOwner, _newOwner);
}
function contractOwner() internal view returns (address contractOwner_) {
contractOwner_ = proxyStorage().contractOwner;
}
function enforceIsContractOwner() internal view {
require(
msg.sender == proxyStorage().contractOwner,
"CegaEntry: Must be contract owner"
);
}
event DiamondCut(
ICegaEntry.ProxyImplementation[] _implementationData,
address _init,
bytes _calldata
);
function updateImplementation(
ICegaEntry.ProxyImplementation[] memory _implementationData,
address _init,
bytes memory _calldata
) internal {
for (
uint256 implIndex;
implIndex < _implementationData.length;
implIndex++
) {
ICegaEntry.ProxyImplementationAction action = _implementationData[
implIndex
].action;
if (action == ICegaEntry.ProxyImplementationAction.Add) {
addFunctions(
_implementationData[implIndex].implAddress,
_implementationData[implIndex].functionSelectors
);
} else if (action == ICegaEntry.ProxyImplementationAction.Replace) {
replaceFunctions(
_implementationData[implIndex].implAddress,
_implementationData[implIndex].functionSelectors
);
} else if (action == ICegaEntry.ProxyImplementationAction.Remove) {
removeFunctions(
_implementationData[implIndex].implAddress,
_implementationData[implIndex].functionSelectors
);
} else {
revert("CegaEntry: Incorrect ProxyImplementationAction");
}
}
emit DiamondCut(_implementationData, _init, _calldata);
initializeImplementation(_init, _calldata);
}
function addFunctions(
address _implementationAddress,
bytes4[] memory _functionSelectors
) internal {
require(
_functionSelectors.length > 0,
"CegaEntry: No selectors in implementation to cut"
);
ProxyStorage storage ds = proxyStorage();
require(
_implementationAddress != address(0),
"CegaEntry: Add implementation can't be address(0)"
);
uint96 selectorPosition = uint96(
ds
.implementationFunctionSelectors[_implementationAddress]
.functionSelectors
.length
);
if (selectorPosition == 0) {
addFacet(ds, _implementationAddress);
}
for (
uint256 selectorIndex;
selectorIndex < _functionSelectors.length;
selectorIndex++
) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldImplementationAddress = ds
.selectorToImplAndPosition[selector]
.implAddress;
require(
oldImplementationAddress == address(0),
"CegaEntry: Can't add function that already exists"
);
addFunction(ds, selector, selectorPosition, _implementationAddress);
selectorPosition++;
}
}
function replaceFunctions(
address _implementationAddress,
bytes4[] memory _functionSelectors
) internal {
require(
_functionSelectors.length > 0,
"CegaEntry: No selectors in implementation to cut"
);
ProxyStorage storage ds = proxyStorage();
require(
_implementationAddress != address(0),
"CegaEntry: Replace implementation can't be address(0)"
);
uint96 selectorPosition = uint96(
ds
.implementationFunctionSelectors[_implementationAddress]
.functionSelectors
.length
);
if (selectorPosition == 0) {
addFacet(ds, _implementationAddress);
}
for (
uint256 selectorIndex;
selectorIndex < _functionSelectors.length;
selectorIndex++
) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldImplementationAddress = ds
.selectorToImplAndPosition[selector]
.implAddress;
require(
oldImplementationAddress != _implementationAddress,
"CegaEntry: Can't replace function with same function"
);
removeFunction(ds, oldImplementationAddress, selector);
addFunction(ds, selector, selectorPosition, _implementationAddress);
selectorPosition++;
}
}
function removeFunctions(
address _implementationAddress,
bytes4[] memory _functionSelectors
) internal {
require(
_functionSelectors.length > 0,
"CegaEntry: No selectors in implementation to cut"
);
ProxyStorage storage ds = proxyStorage();
require(
_implementationAddress == address(0),
"CegaEntry: Remove implementation address must be address(0)"
);
for (
uint256 selectorIndex;
selectorIndex < _functionSelectors.length;
selectorIndex++
) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldImplementationAddress = ds
.selectorToImplAndPosition[selector]
.implAddress;
removeFunction(ds, oldImplementationAddress, selector);
}
}
function addFacet(
ProxyStorage storage ds,
address _implementationAddress
) internal {
enforceHasContractCode(
_implementationAddress,
"CegaEntry: New implementation has no code"
);
ds
.implementationFunctionSelectors[_implementationAddress]
.implementationAddressPosition = ds.implementationAddresses.length;
ds.implementationAddresses.push(_implementationAddress);
}
function addFunction(
ProxyStorage storage ds,
bytes4 _selector,
uint96 _selectorPosition,
address _implementationAddress
) internal {
ds
.selectorToImplAndPosition[_selector]
.functionSelectorPosition = _selectorPosition;
ds
.implementationFunctionSelectors[_implementationAddress]
.functionSelectors
.push(_selector);
ds
.selectorToImplAndPosition[_selector]
.implAddress = _implementationAddress;
}
function removeFunction(
ProxyStorage storage ds,
address _implementationAddress,
bytes4 _selector
) internal {
require(
_implementationAddress != address(0),
"CegaEntry: Can't remove function that doesn't exist"
);
require(
_implementationAddress != address(this),
"CegaEntry: Can't remove immutable function"
);
uint256 selectorPosition = ds
.selectorToImplAndPosition[_selector]
.functionSelectorPosition;
uint256 lastSelectorPosition = ds
.implementationFunctionSelectors[_implementationAddress]
.functionSelectors
.length - 1;
if (selectorPosition != lastSelectorPosition) {
bytes4 lastSelector = ds
.implementationFunctionSelectors[_implementationAddress]
.functionSelectors[lastSelectorPosition];
ds
.implementationFunctionSelectors[_implementationAddress]
.functionSelectors[selectorPosition] = lastSelector;
ds
.selectorToImplAndPosition[lastSelector]
.functionSelectorPosition = uint96(selectorPosition);
}
ds
.implementationFunctionSelectors[_implementationAddress]
.functionSelectors
.pop();
delete ds.selectorToImplAndPosition[_selector];
if (lastSelectorPosition == 0) {
uint256 lastImplementationAddressPosition = ds
.implementationAddresses
.length - 1;
uint256 implementationAddressPosition = ds
.implementationFunctionSelectors[_implementationAddress]
.implementationAddressPosition;
if (
implementationAddressPosition !=
lastImplementationAddressPosition
) {
address lastImplementationAddress = ds.implementationAddresses[
lastImplementationAddressPosition
];
ds.implementationAddresses[
implementationAddressPosition
] = lastImplementationAddress;
ds
.implementationFunctionSelectors[lastImplementationAddress]
.implementationAddressPosition = implementationAddressPosition;
}
ds.implementationAddresses.pop();
delete ds
.implementationFunctionSelectors[_implementationAddress]
.implementationAddressPosition;
}
}
function initializeImplementation(
address _init,
bytes memory _calldata
) internal {
if (_init == address(0)) {
require(
_calldata.length == 0,
"CegaEntry: _init is address(0) but _calldata is not empty"
);
} else {
require(
_calldata.length > 0,
"CegaEntry: _calldata is empty but _init is not address(0)"
);
if (_init != address(this)) {
enforceHasContractCode(
_init,
"CegaEntry: _init address has no code"
);
}
(bool success, bytes memory error) = _init.delegatecall(_calldata);
if (!success) {
if (error.length > 0) {
assembly {
revert(add(error, 0x20), mload(error))
}
} else {
revert("CegaEntry: _init function reverted");
}
}
}
}
function enforceHasContractCode(
address _contract,
string memory _errorMessage
) internal view {
require(_contract.code.length > 0, _errorMessage);
}
}
文件 3 的 3:ICegaEntry.sol
pragma solidity ^0.8.10;
interface ICegaEntry {
enum ProxyImplementationAction {
Add,
Replace,
Remove
}
struct ProxyImplementation {
address implAddress;
ProxyImplementationAction action;
bytes4[] functionSelectors;
}
function diamondCut(
ProxyImplementation[] calldata _implementationParams,
address _init,
bytes calldata _calldata
) external;
event DiamondCut(
ProxyImplementation[] _diamondCut,
address _init,
bytes _calldata
);
}
{
"compilationTarget": {
"contracts/cega-entry/CegaEntry.sol": "CegaEntry"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"details": {
"constantOptimizer": true,
"cse": true,
"deduplicate": true,
"inliner": true,
"jumpdestRemover": true,
"orderLiterals": true,
"peephole": true,
"yul": true,
"yulDetails": {
"optimizerSteps": "u",
"stackAllocation": true
}
},
"runs": 200
},
"remappings": [],
"viaIR": true
}