// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import "./zeppelin/Pausable.sol";
abstract contract IController is Pausable {
event SetContractInfo(bytes32 id, address contractAddress, bytes20 gitCommitHash);
function setContractInfo(
bytes32 _id,
address _contractAddress,
bytes20 _gitCommitHash
) external virtual;
function updateController(bytes32 _id, address _controller) external virtual;
function getContract(bytes32 _id) public view virtual returns (address);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
interface IManager {
event SetController(address controller);
event ParameterUpdate(string param);
function setController(address _controller) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import "./IManager.sol";
import "./IController.sol";
contract Manager is IManager {
// Controller that contract is registered with
IController public controller;
// Check if sender is controller
modifier onlyController() {
_onlyController();
_;
}
// Check if sender is controller owner
modifier onlyControllerOwner() {
_onlyControllerOwner();
_;
}
// Check if controller is not paused
modifier whenSystemNotPaused() {
_whenSystemNotPaused();
_;
}
// Check if controller is paused
modifier whenSystemPaused() {
_whenSystemPaused();
_;
}
constructor(address _controller) {
controller = IController(_controller);
}
/**
* @notice Set controller. Only callable by current controller
* @param _controller Controller contract address
*/
function setController(address _controller) external onlyController {
controller = IController(_controller);
emit SetController(_controller);
}
function _onlyController() private view {
require(msg.sender == address(controller), "caller must be Controller");
}
function _onlyControllerOwner() private view {
require(msg.sender == controller.owner(), "caller must be Controller owner");
}
function _whenSystemNotPaused() private view {
require(!controller.paused(), "system is paused");
}
function _whenSystemPaused() private view {
require(controller.paused(), "system is not paused");
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import "./ManagerProxyTarget.sol";
/**
* @title ManagerProxy
* @notice A proxy contract that uses delegatecall to execute function calls on a target contract using its own storage context.
The target contract is a Manager contract that is registered with the Controller.
* @dev Both this proxy contract and its target contract MUST inherit from ManagerProxyTarget in order to guarantee
that both contracts have the same storage layout. Differing storage layouts in a proxy contract and target contract can
potentially break the delegate proxy upgradeability mechanism. Since this proxy contract inherits from ManagerProxyTarget which inherits
from Manager, it implements the setController() function. The target contract will also implement setController() since it also inherits
from ManagerProxyTarget. Thus, any transaction sent to the proxy that calls setController() will execute against the proxy instead
of the target. As a result, developers should keep in mind that the proxy will always execute the same logic for setController() regardless
of the setController() implementation on the target contract. Generally, developers should not add any additional functions to this proxy contract
because any function implemented on the proxy will always be executed against the proxy and the call **will not** be forwarded to the target contract
*/
contract ManagerProxy is ManagerProxyTarget {
/**
* @notice ManagerProxy constructor. Invokes constructor of base Manager contract with provided Controller address.
* Also, sets the contract ID of the target contract that function calls will be executed on.
* @param _controller Address of Controller that this contract will be registered with
* @param _targetContractId contract ID of the target contract
*/
constructor(address _controller, bytes32 _targetContractId) Manager(_controller) {
targetContractId = _targetContractId;
}
/**
* @notice Fallback function that delegates calls to target contract when there is no msg.data
*/
receive() external payable {
_fallback();
}
/**
* @notice Fallback function that delegates calls to target contract when there is msg.data
*/
fallback() external payable {
_fallback();
}
/**
* @dev Uses delegatecall to execute function calls on this proxy contract's target contract using its own storage context.
This fallback function will look up the address of the target contract using the Controller and the target contract ID.
It will then use the calldata for a function call as the data payload for a delegatecall on the target contract. The return value
of the executed function call will also be returned
*/
function _fallback() private {
address target = controller.getContract(targetContractId);
require(target != address(0), "target contract must be registered");
assembly {
// Solidity keeps a free memory pointer at position 0x40 in memory
let freeMemoryPtrPosition := 0x40
// Load the free memory pointer
let calldataMemoryOffset := mload(freeMemoryPtrPosition)
// Update free memory pointer to after memory space we reserve for calldata
mstore(freeMemoryPtrPosition, add(calldataMemoryOffset, calldatasize()))
// Copy calldata (method signature and params of the call) to memory
calldatacopy(calldataMemoryOffset, 0x0, calldatasize())
// Call method on target contract using calldata which is loaded into memory
let ret := delegatecall(gas(), target, calldataMemoryOffset, calldatasize(), 0, 0)
// Load the free memory pointer
let returndataMemoryOffset := mload(freeMemoryPtrPosition)
// Update free memory pointer to after memory space we reserve for returndata
mstore(freeMemoryPtrPosition, add(returndataMemoryOffset, returndatasize()))
// Copy returndata (result of the method invoked by the delegatecall) to memory
returndatacopy(returndataMemoryOffset, 0x0, returndatasize())
switch ret
case 0 {
// Method call failed - revert
// Return any error message stored in mem[returndataMemoryOffset..(returndataMemoryOffset + returndatasize)]
revert(returndataMemoryOffset, returndatasize())
}
default {
// Return result of method call stored in mem[returndataMemoryOffset..(returndataMemoryOffset + returndatasize)]
return(returndataMemoryOffset, returndatasize())
}
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import "./Manager.sol";
/**
* @title ManagerProxyTarget
* @notice The base contract that target contracts used by a proxy contract should inherit from
* @dev Both the target contract and the proxy contract (implemented as ManagerProxy) MUST inherit from ManagerProxyTarget in order to guarantee
that both contracts have the same storage layout. Differing storage layouts in a proxy contract and target contract can
potentially break the delegate proxy upgradeability mechanism
*/
abstract contract ManagerProxyTarget is Manager {
// Used to look up target contract address in controller's registry
bytes32 public targetContractId;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
constructor() {
owner = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import "./Ownable.sol";
/**
* @title Pausable
* @dev Base contract which allows children to implement an emergency stop mechanism.
*/
contract Pausable is Ownable {
event Pause();
event Unpause();
bool public paused;
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*/
modifier whenNotPaused() {
require(!paused);
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*/
modifier whenPaused() {
require(paused);
_;
}
/**
* @dev called by the owner to pause, triggers stopped state
*/
function pause() public onlyOwner whenNotPaused {
paused = true;
emit Pause();
}
/**
* @dev called by the owner to unpause, returns to normal state
*/
function unpause() public onlyOwner whenPaused {
paused = false;
emit Unpause();
}
}
{
"compilationTarget": {
"contracts/ManagerProxy.sol": "ManagerProxy"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_controller","type":"address"},{"internalType":"bytes32","name":"_targetContractId","type":"bytes32"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"param","type":"string"}],"name":"ParameterUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"controller","type":"address"}],"name":"SetController","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[],"name":"controller","outputs":[{"internalType":"contract IController","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_controller","type":"address"}],"name":"setController","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"targetContractId","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]