|
| 1 | +//SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity 0.6.12; |
| 4 | + |
| 5 | +import "../libraries/math/SafeMath.sol"; |
| 6 | +import "../libraries/token/IERC20.sol"; |
| 7 | +import "../libraries/token/ERC721/IERC721.sol"; |
| 8 | +import "../libraries/utils/ReentrancyGuard.sol"; |
| 9 | + |
| 10 | +import "../peripherals/interfaces/ITimelock.sol"; |
| 11 | + |
| 12 | +contract TokenManager is ReentrancyGuard { |
| 13 | + using SafeMath for uint256; |
| 14 | + |
| 15 | + bool public isInitialized; |
| 16 | + |
| 17 | + uint256 public actionsNonce; |
| 18 | + uint256 public minAuthorizations; |
| 19 | + |
| 20 | + address public admin; |
| 21 | + |
| 22 | + address[] public signers; |
| 23 | + mapping (address => bool) public isSigner; |
| 24 | + mapping (bytes32 => bool) public pendingActions; |
| 25 | + mapping (address => mapping (bytes32 => bool)) public signedActions; |
| 26 | + |
| 27 | + event SignalApprove(address token, address spender, uint256 amount, bytes32 action, uint256 nonce); |
| 28 | + event SignalApproveNFT(address token, address spender, uint256 tokenId, bytes32 action, uint256 nonce); |
| 29 | + event SignalApproveNFTs(address token, address spender, uint256[] tokenIds, bytes32 action, uint256 nonce); |
| 30 | + event SignalSetAdmin(address target, address admin, bytes32 action, uint256 nonce); |
| 31 | + event SignalPendingAction(bytes32 action, uint256 nonce); |
| 32 | + event SignAction(bytes32 action, uint256 nonce); |
| 33 | + event ClearAction(bytes32 action, uint256 nonce); |
| 34 | + |
| 35 | + constructor(uint256 _minAuthorizations) public { |
| 36 | + admin = msg.sender; |
| 37 | + minAuthorizations = _minAuthorizations; |
| 38 | + } |
| 39 | + |
| 40 | + modifier onlyAdmin() { |
| 41 | + require(msg.sender == admin, "TokenManager: forbidden"); |
| 42 | + _; |
| 43 | + } |
| 44 | + |
| 45 | + modifier onlySigner() { |
| 46 | + require(isSigner[msg.sender], "TokenManager: forbidden"); |
| 47 | + _; |
| 48 | + } |
| 49 | + |
| 50 | + function initialize(address[] memory _signers) public virtual onlyAdmin { |
| 51 | + require(!isInitialized, "TokenManager: already initialized"); |
| 52 | + isInitialized = true; |
| 53 | + |
| 54 | + signers = _signers; |
| 55 | + for (uint256 i = 0; i < _signers.length; i++) { |
| 56 | + address signer = _signers[i]; |
| 57 | + isSigner[signer] = true; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + function signersLength() public view returns (uint256) { |
| 62 | + return signers.length; |
| 63 | + } |
| 64 | + |
| 65 | + function signalApprove(address _token, address _spender, uint256 _amount) external nonReentrant onlyAdmin { |
| 66 | + actionsNonce++; |
| 67 | + uint256 nonce = actionsNonce; |
| 68 | + bytes32 action = keccak256(abi.encodePacked("approve", _token, _spender, _amount, nonce)); |
| 69 | + _setPendingAction(action, nonce); |
| 70 | + emit SignalApprove(_token, _spender, _amount, action, nonce); |
| 71 | + } |
| 72 | + |
| 73 | + function signApprove(address _token, address _spender, uint256 _amount, uint256 _nonce) external nonReentrant onlySigner { |
| 74 | + bytes32 action = keccak256(abi.encodePacked("approve", _token, _spender, _amount, _nonce)); |
| 75 | + _validateAction(action); |
| 76 | + require(!signedActions[msg.sender][action], "TokenManager: already signed"); |
| 77 | + signedActions[msg.sender][action] = true; |
| 78 | + emit SignAction(action, _nonce); |
| 79 | + } |
| 80 | + |
| 81 | + function approve(address _token, address _spender, uint256 _amount, uint256 _nonce) external nonReentrant onlyAdmin { |
| 82 | + bytes32 action = keccak256(abi.encodePacked("approve", _token, _spender, _amount, _nonce)); |
| 83 | + _validateAction(action); |
| 84 | + _validateAuthorization(action); |
| 85 | + |
| 86 | + IERC20(_token).approve(_spender, _amount); |
| 87 | + _clearAction(action, _nonce); |
| 88 | + } |
| 89 | + |
| 90 | + function signalApproveNFT(address _token, address _spender, uint256 _tokenId) external nonReentrant onlyAdmin { |
| 91 | + actionsNonce++; |
| 92 | + uint256 nonce = actionsNonce; |
| 93 | + bytes32 action = keccak256(abi.encodePacked("approveNFT", _token, _spender, _tokenId, nonce)); |
| 94 | + _setPendingAction(action, nonce); |
| 95 | + emit SignalApproveNFT(_token, _spender, _tokenId, action, nonce); |
| 96 | + } |
| 97 | + |
| 98 | + function signApproveNFT(address _token, address _spender, uint256 _tokenId, uint256 _nonce) external nonReentrant onlySigner { |
| 99 | + bytes32 action = keccak256(abi.encodePacked("approveNFT", _token, _spender, _tokenId, _nonce)); |
| 100 | + _validateAction(action); |
| 101 | + require(!signedActions[msg.sender][action], "TokenManager: already signed"); |
| 102 | + signedActions[msg.sender][action] = true; |
| 103 | + emit SignAction(action, _nonce); |
| 104 | + } |
| 105 | + |
| 106 | + function approveNFT(address _token, address _spender, uint256 _tokenId, uint256 _nonce) external nonReentrant onlyAdmin { |
| 107 | + bytes32 action = keccak256(abi.encodePacked("approveNFT", _token, _spender, _tokenId, _nonce)); |
| 108 | + _validateAction(action); |
| 109 | + _validateAuthorization(action); |
| 110 | + |
| 111 | + IERC721(_token).approve(_spender, _tokenId); |
| 112 | + _clearAction(action, _nonce); |
| 113 | + } |
| 114 | + |
| 115 | + function signalApproveNFTs(address _token, address _spender, uint256[] memory _tokenIds) external nonReentrant onlyAdmin { |
| 116 | + actionsNonce++; |
| 117 | + uint256 nonce = actionsNonce; |
| 118 | + bytes32 action = keccak256(abi.encodePacked("approveNFTs", _token, _spender, _tokenIds, nonce)); |
| 119 | + _setPendingAction(action, nonce); |
| 120 | + emit SignalApproveNFTs(_token, _spender, _tokenIds, action, nonce); |
| 121 | + } |
| 122 | + |
| 123 | + function signApproveNFTs(address _token, address _spender, uint256[] memory _tokenIds, uint256 _nonce) external nonReentrant onlySigner { |
| 124 | + bytes32 action = keccak256(abi.encodePacked("approveNFTs", _token, _spender, _tokenIds, _nonce)); |
| 125 | + _validateAction(action); |
| 126 | + require(!signedActions[msg.sender][action], "TokenManager: already signed"); |
| 127 | + signedActions[msg.sender][action] = true; |
| 128 | + emit SignAction(action, _nonce); |
| 129 | + } |
| 130 | + |
| 131 | + function approveNFTs(address _token, address _spender, uint256[] memory _tokenIds, uint256 _nonce) external nonReentrant onlyAdmin { |
| 132 | + bytes32 action = keccak256(abi.encodePacked("approveNFTs", _token, _spender, _tokenIds, _nonce)); |
| 133 | + _validateAction(action); |
| 134 | + _validateAuthorization(action); |
| 135 | + |
| 136 | + for (uint256 i = 0 ; i < _tokenIds.length; i++) { |
| 137 | + IERC721(_token).approve(_spender, _tokenIds[i]); |
| 138 | + } |
| 139 | + _clearAction(action, _nonce); |
| 140 | + } |
| 141 | + |
| 142 | + function signalSetAdmin(address _target, address _admin) external nonReentrant onlySigner { |
| 143 | + actionsNonce++; |
| 144 | + uint256 nonce = actionsNonce; |
| 145 | + bytes32 action = keccak256(abi.encodePacked("setAdmin", _target, _admin, nonce)); |
| 146 | + _setPendingAction(action, nonce); |
| 147 | + signedActions[msg.sender][action] = true; |
| 148 | + emit SignalSetAdmin(_target, _admin, action, nonce); |
| 149 | + } |
| 150 | + |
| 151 | + function signSetAdmin(address _target, address _admin, uint256 _nonce) external nonReentrant onlySigner { |
| 152 | + bytes32 action = keccak256(abi.encodePacked("setAdmin", _target, _admin, _nonce)); |
| 153 | + _validateAction(action); |
| 154 | + require(!signedActions[msg.sender][action], "TokenManager: already signed"); |
| 155 | + signedActions[msg.sender][action] = true; |
| 156 | + emit SignAction(action, _nonce); |
| 157 | + } |
| 158 | + |
| 159 | + function setAdmin(address _target, address _admin, uint256 _nonce) external nonReentrant onlySigner { |
| 160 | + bytes32 action = keccak256(abi.encodePacked("setAdmin", _target, _admin, _nonce)); |
| 161 | + _validateAction(action); |
| 162 | + _validateAuthorization(action); |
| 163 | + |
| 164 | + ITimelock(_target).setAdmin(_admin); |
| 165 | + _clearAction(action, _nonce); |
| 166 | + } |
| 167 | + |
| 168 | + function _setPendingAction(bytes32 _action, uint256 _nonce) private { |
| 169 | + pendingActions[_action] = true; |
| 170 | + emit SignalPendingAction(_action, _nonce); |
| 171 | + } |
| 172 | + |
| 173 | + function _validateAction(bytes32 _action) private view { |
| 174 | + require(pendingActions[_action], "TokenManager: action not signalled"); |
| 175 | + } |
| 176 | + |
| 177 | + function _validateAuthorization(bytes32 _action) private view { |
| 178 | + uint256 count = 0; |
| 179 | + for (uint256 i = 0; i < signers.length; i++) { |
| 180 | + address signer = signers[i]; |
| 181 | + if (signedActions[signer][_action]) { |
| 182 | + count++; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + if (count == 0) { |
| 187 | + revert("TokenManager: action not authorized"); |
| 188 | + } |
| 189 | + require(count >= minAuthorizations, "TokenManager: insufficient authorization"); |
| 190 | + } |
| 191 | + |
| 192 | + function _clearAction(bytes32 _action, uint256 _nonce) private { |
| 193 | + require(pendingActions[_action], "TokenManager: invalid _action"); |
| 194 | + delete pendingActions[_action]; |
| 195 | + emit ClearAction(_action, _nonce); |
| 196 | + } |
| 197 | +} |
0 commit comments