文件 1 的 1:Proxy.sol
pragma solidity ^0.8.7;
interface IDefaultImplementationBeacon {
function defaultImplementation() external view returns (address defaultImplementation_);
}
abstract contract SlotManipulatable {
function _getReferenceTypeSlot(bytes32 slot_, bytes32 key_) internal pure returns (bytes32 value_) {
return keccak256(abi.encodePacked(key_, slot_));
}
function _getSlotValue(bytes32 slot_) internal view returns (bytes32 value_) {
assembly {
value_ := sload(slot_)
}
}
function _setSlotValue(bytes32 slot_, bytes32 value_) internal {
assembly {
sstore(slot_, value_)
}
}
}
contract Proxy is SlotManipulatable {
bytes32 private constant FACTORY_SLOT = bytes32(0x7a45a402e4cb6e08ebc196f20f66d5d30e67285a2a8aa80503fa409e727a4af1);
bytes32 private constant IMPLEMENTATION_SLOT = bytes32(0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc);
constructor(address factory_, address implementation_) {
_setSlotValue(FACTORY_SLOT, bytes32(uint256(uint160(factory_))));
address implementation = implementation_ == address(0)
? IDefaultImplementationBeacon(factory_).defaultImplementation()
: implementation_;
require(implementation != address(0));
_setSlotValue(IMPLEMENTATION_SLOT, bytes32(uint256(uint160(implementation))));
}
fallback() payable external virtual {
bytes32 implementation = _getSlotValue(IMPLEMENTATION_SLOT);
require(address(uint160(uint256(implementation))).code.length != uint256(0));
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())
}
}
}
}
{
"compilationTarget": {
"Proxy.sol": "Proxy"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}