|
| 1 | +pragma solidity 0.5.17; |
| 2 | +pragma experimental ABIEncoderV2; |
| 3 | + |
| 4 | +import "./SchemeConstraints.sol"; |
| 5 | + |
| 6 | + |
| 7 | +contract DxDaoSchemeConstraints is SchemeConstraints { |
| 8 | + using SafeMath for uint256; |
| 9 | + |
| 10 | + uint256 public initialTimestamp; |
| 11 | + uint256 public periodSize; |
| 12 | + uint256 public periodLimitWei; |
| 13 | + |
| 14 | + mapping(address=>uint256) public periodLimitToken; |
| 15 | + mapping (uint256 => mapping(address => uint256)) public periodSpendingToken; |
| 16 | + mapping(uint256=>uint256) public periodSpendingWei; |
| 17 | + mapping(address=>bool) public contractsWhiteListMap; |
| 18 | + bytes4 private constant APPROVE_SIGNATURE = 0x095ea7b3;//approve(address,uint256) |
| 19 | + |
| 20 | + /* @dev initialize |
| 21 | + * @param _periodSize the time period to limit the tokens and eth spending |
| 22 | + * @param _periodLimitWei the limit of eth which can be sent per period |
| 23 | + * @param _periodLimitTokensAddresses tokens to limit |
| 24 | + * @param _periodLimitTokensAmounts the limit of token which can be sent per period |
| 25 | + * @param _contractsWhiteList the contracts the scheme is allowed to interact with |
| 26 | + */ |
| 27 | + function initialize( |
| 28 | + uint256 _periodSize, |
| 29 | + uint256 _periodLimitWei, |
| 30 | + address[] calldata _periodLimitTokensAddresses, |
| 31 | + uint256[] calldata _periodLimitTokensAmounts, |
| 32 | + address[] calldata _contractsWhiteList |
| 33 | + ) |
| 34 | + external { |
| 35 | + require(initialTimestamp == 0, "cannot initialize twice"); |
| 36 | + require(_periodSize > 0, "preriod size should be greater than 0"); |
| 37 | + require(_periodLimitTokensAddresses.length == _periodLimitTokensAmounts.length, |
| 38 | + "invalid length _periodLimitTokensAddresses"); |
| 39 | + periodSize = _periodSize; |
| 40 | + periodLimitWei = _periodLimitWei; |
| 41 | + // solhint-disable-next-line not-rely-on-time |
| 42 | + initialTimestamp = block.timestamp; |
| 43 | + for (uint i = 0; i < _contractsWhiteList.length; i++) { |
| 44 | + contractsWhiteListMap[_contractsWhiteList[i]] = true; |
| 45 | + } |
| 46 | + for (uint i = 0; i < _periodLimitTokensAmounts.length; i++) { |
| 47 | + periodLimitToken[_periodLimitTokensAddresses[i]] = _periodLimitTokensAmounts[i]; |
| 48 | + } |
| 49 | + contractsWhiteList = _contractsWhiteList; |
| 50 | + } |
| 51 | + |
| 52 | + /* |
| 53 | + * @dev isAllowedToCall should be called upon a proposal execution. |
| 54 | + * - check that the total spending of tokens within a 'periodSize' does not exceed the periodLimit per token |
| 55 | + * - check that the total sending of eth within a 'periodSize' does not exceed the periodLimit |
| 56 | + * @param _contractsToCall the contracts to be called |
| 57 | + * @param _callsData - The abi encode data for the calls |
| 58 | + * @param _values value(ETH) to transfer with the calls |
| 59 | + * @param _avatar avatar |
| 60 | + * @return bool value true-allowed false not allowed |
| 61 | + */ |
| 62 | + function isAllowedToCall( |
| 63 | + address[] calldata _contractsToCall, |
| 64 | + bytes[] calldata _callsData, |
| 65 | + uint256[] calldata _values, |
| 66 | + Avatar |
| 67 | + ) |
| 68 | + external |
| 69 | + returns(bool) |
| 70 | + { |
| 71 | + |
| 72 | + uint256 observervationIndex = observationIndex(); |
| 73 | + uint256 totalPeriodSpendingInWei; |
| 74 | + for (uint i = 0; i < _contractsToCall.length; i++) { |
| 75 | + // constraint eth transfer |
| 76 | + totalPeriodSpendingInWei = totalPeriodSpendingInWei.add(_values[i]); |
| 77 | + bytes memory callData = _callsData[i]; |
| 78 | + // constraint approve calls |
| 79 | + if (callData[0] == APPROVE_SIGNATURE[0] && |
| 80 | + callData[1] == APPROVE_SIGNATURE[1] && |
| 81 | + callData[2] == APPROVE_SIGNATURE[2] && |
| 82 | + callData[3] == APPROVE_SIGNATURE[3]) { |
| 83 | + uint256 amount; |
| 84 | + address contractToCall = _contractsToCall[i]; |
| 85 | + // solhint-disable-next-line no-inline-assembly |
| 86 | + assembly { |
| 87 | + amount := mload(add(callData, 68)) |
| 88 | + } |
| 89 | + periodSpendingToken[observervationIndex][contractToCall] = |
| 90 | + periodSpendingToken[observervationIndex][contractToCall].add(amount); |
| 91 | + require( |
| 92 | + periodSpendingToken[observervationIndex][contractToCall] <= periodLimitToken[contractToCall], |
| 93 | + "periodSpendingTokensExceeded"); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + periodSpendingWei[observervationIndex] = |
| 98 | + periodSpendingWei[observervationIndex].add(totalPeriodSpendingInWei); |
| 99 | + require(periodSpendingWei[observervationIndex] <= periodLimitWei, "periodSpendingWeiExceeded"); |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + /* |
| 104 | + * @dev isAllowedToPropose should be called upon a proposal submition. |
| 105 | + * allow only whitelisted target contracts or 'approve' calls which the 'spender' is whitelisted |
| 106 | + * @param _contractsToCall the contracts to be called |
| 107 | + * @param _callsData - The abi encode data for the calls |
| 108 | + * @param _values value(ETH) to transfer with the calls |
| 109 | + * @param _avatar avatar |
| 110 | + * @return bool value true-allowed false not allowed |
| 111 | + */ |
| 112 | + function isAllowedToPropose( |
| 113 | + address[] calldata _contractsToCall, |
| 114 | + bytes[] calldata _callsData, |
| 115 | + uint256[] calldata, |
| 116 | + Avatar) |
| 117 | + external |
| 118 | + returns(bool) |
| 119 | + { |
| 120 | + for (uint i = 0; i < _contractsToCall.length; i++) { |
| 121 | + if (!contractsWhiteListMap[_contractsToCall[i]]) { |
| 122 | + address spender; |
| 123 | + bytes memory callData = _callsData[i]; |
| 124 | + require( |
| 125 | + callData[0] == APPROVE_SIGNATURE[0] && |
| 126 | + callData[1] == APPROVE_SIGNATURE[1] && |
| 127 | + callData[2] == APPROVE_SIGNATURE[2] && |
| 128 | + callData[3] == APPROVE_SIGNATURE[3], |
| 129 | + "allow only approve call for none whitelistedContracts"); |
| 130 | + //in solidity > 6 this can be replaced by: |
| 131 | + //(spender,) = abi.descode(callData[4:], (address, uint)); |
| 132 | + // see https://github.com/ethereum/solidity/issues/9439 |
| 133 | + // solhint-disable-next-line no-inline-assembly |
| 134 | + assembly { |
| 135 | + spender := mload(add(callData, 36)) |
| 136 | + } |
| 137 | + require(contractsWhiteListMap[spender], "spender contract not whitelisted"); |
| 138 | + } |
| 139 | + } |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + function observationIndex() public view returns (uint256) { |
| 144 | + // solhint-disable-next-line not-rely-on-time |
| 145 | + return ((block.timestamp - initialTimestamp) / periodSize); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments