code refactor

This commit is contained in:
Vaibhav Khanna 2022-04-14 18:32:25 +05:30
parent ba18bc271e
commit 6623b62ad3

View File

@ -31,7 +31,10 @@ contract Internals is Events {
*/ */
bytes32 internal constant _DUMMY_IMPLEMENTATION_SLOT = bytes32 internal constant _DUMMY_IMPLEMENTATION_SLOT =
0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
/**
* @dev Returns the storage slot which stores the sigs array set for the implementation.
*/
function _getSlotImplSigsSlot(address implementation_) function _getSlotImplSigsSlot(address implementation_)
internal internal
pure pure
@ -43,6 +46,9 @@ contract Internals is Events {
); );
} }
/**
* @dev Returns the storage slot which stores the implementation address for the function sig.
*/
function _getSlotSigsImplSlot(bytes4 sig_) internal pure returns (bytes32) { function _getSlotSigsImplSlot(bytes4 sig_) internal pure returns (bytes32) {
return keccak256(abi.encode("eip1967.proxy.implementation", sig_)); return keccak256(abi.encode("eip1967.proxy.implementation", sig_));
} }
@ -97,7 +103,7 @@ contract Internals is Events {
} }
/** /**
* @dev removes implementation and the mappings corresponding to it. * @dev Removes implementation and the mappings corresponding to it.
*/ */
function _removeImplementationSigs(address implementation_) internal { function _removeImplementationSigs(address implementation_) internal {
bytes32 slot_ = _getSlotImplSigsSlot(implementation_); bytes32 slot_ = _getSlotImplSigsSlot(implementation_);
@ -111,6 +117,9 @@ contract Internals is Events {
emit removeImplementationLog(implementation_); emit removeImplementationLog(implementation_);
} }
/**
* @dev Returns bytes4[] sigs from implementation address. If implemenatation is not registered then returns empty array.
*/
function _getImplementationSigs(address implementation_) function _getImplementationSigs(address implementation_)
internal internal
view view
@ -120,6 +129,9 @@ contract Internals is Events {
return getSigsSlot(slot_).value; return getSigsSlot(slot_).value;
} }
/**
* @dev Returns implementation address from bytes4 sig. If sig is not registered then returns address(0).
*/
function _getSigImplementation(bytes4 sig_) function _getSigImplementation(bytes4 sig_)
internal internal
view view
@ -223,20 +235,24 @@ contract Internals is Events {
} }
contract AdminStuff is Internals { contract AdminStuff is Internals {
/**
* @dev Only admin gaurd.
*/
modifier onlyAdmin() { modifier onlyAdmin() {
require(msg.sender == _getAdmin(), "not-the-admin"); require(msg.sender == _getAdmin(), "not-the-admin");
_; _;
} }
/** /**
* @dev sets new admin. * @dev Sets new admin.
*/ */
function setAdmin(address newAdmin_) external onlyAdmin { function setAdmin(address newAdmin_) external onlyAdmin {
_setAdmin(newAdmin_); _setAdmin(newAdmin_);
} }
/** /**
* @dev sets new dummy-implementation. * @dev Sets new dummy-implementation.
*/ */
function setDummyImplementation(address newDummyImplementation_) function setDummyImplementation(address newDummyImplementation_)
external external
@ -246,7 +262,7 @@ contract AdminStuff is Internals {
} }
/** /**
* @dev adds new implementation address. * @dev Adds new implementation address.
*/ */
function addImplementation(address implementation_, bytes4[] calldata sigs_) function addImplementation(address implementation_, bytes4[] calldata sigs_)
external external
@ -256,7 +272,7 @@ contract AdminStuff is Internals {
} }
/** /**
* @dev removes an existing implementation address. * @dev Removes an existing implementation address.
*/ */
function removeImplementation(address implementation_) external onlyAdmin { function removeImplementation(address implementation_) external onlyAdmin {
_removeImplementationSigs(implementation_); _removeImplementationSigs(implementation_);
@ -274,21 +290,21 @@ abstract contract Proxy is AdminStuff {
{} {}
/** /**
* @dev returns admin's address. * @dev Returns admin's address.
*/ */
function getAdmin() external view returns (address) { function getAdmin() external view returns (address) {
return _getAdmin(); return _getAdmin();
} }
/** /**
* @dev returns dummy-implementations's address. * @dev Returns dummy-implementations's address.
*/ */
function getDummyImplementation() external view returns (address) { function getDummyImplementation() external view returns (address) {
return _getDummyImplementation(); return _getDummyImplementation();
} }
/** /**
* @dev returns bytes4[] sigs from implementation address If not registered then returns empty array. * @dev Returns bytes4[] sigs from implementation address If not registered then returns empty array.
*/ */
function getImplementationSigs(address impl_) function getImplementationSigs(address impl_)
external external
@ -299,7 +315,7 @@ abstract contract Proxy is AdminStuff {
} }
/** /**
* @dev returns implementation address from bytes4 sig. If sig is not registered then returns address(0). * @dev Returns implementation address from bytes4 sig. If sig is not registered then returns address(0).
*/ */
function getSigsImplementation(bytes4 sig_) function getSigsImplementation(bytes4 sig_)
external external
@ -324,4 +340,4 @@ abstract contract Proxy is AdminStuff {
_fallback(msg.sig); _fallback(msg.sig);
} }
} }
} }