|
| 1 | +/* |
| 2 | + Copyright 2023 Index Coop |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +
|
| 16 | + SPDX-License-Identifier: Apache License, Version 2.0 |
| 17 | +*/ |
| 18 | + |
| 19 | +pragma solidity 0.6.10; |
| 20 | +import { AddressArrayUtils } from "./AddressArrayUtils.sol"; |
| 21 | + |
| 22 | +/** |
| 23 | + * @title AssetAllowList |
| 24 | + * @author Index Coop |
| 25 | + * |
| 26 | + * Abstract contract that allows inheriting contracts to restrict the assets that can be traded to, wrapped to, or claimed |
| 27 | + */ |
| 28 | +abstract contract AssetAllowList { |
| 29 | + using AddressArrayUtils for address[]; |
| 30 | + |
| 31 | + /* ============ Events ============ */ |
| 32 | + |
| 33 | + event AllowedAssetAdded( |
| 34 | + address indexed _asset |
| 35 | + ); |
| 36 | + |
| 37 | + event AllowedAssetRemoved( |
| 38 | + address indexed _asset |
| 39 | + ); |
| 40 | + |
| 41 | + event UseAssetAllowlistUpdated( |
| 42 | + bool _status |
| 43 | + ); |
| 44 | + |
| 45 | + /* ============ State Variables ============ */ |
| 46 | + |
| 47 | + // Boolean indicating wether to use asset allow list |
| 48 | + bool public useAssetAllowlist; |
| 49 | + |
| 50 | + // Mapping keeping track of allowed assets |
| 51 | + mapping(address => bool) public assetAllowlist; |
| 52 | + |
| 53 | + // List of allowed assets |
| 54 | + address[] internal allowedAssets; |
| 55 | + |
| 56 | + /* ============ Modifiers ============ */ |
| 57 | + |
| 58 | + modifier onlyAllowedAssets(address[] memory _assets) { |
| 59 | + require( |
| 60 | + _areAllowedAssets(_assets), |
| 61 | + "Invalid asset" |
| 62 | + ); |
| 63 | + _; |
| 64 | + } |
| 65 | + |
| 66 | + /* ============ Constructor ============ */ |
| 67 | + |
| 68 | + /** |
| 69 | + * Set state variables and map asset pairs to their oracles |
| 70 | + * |
| 71 | + * @param _allowedAssets Array of allowed assets |
| 72 | + * @param _useAssetAllowlist Bool indicating whether to use asset allow list |
| 73 | + */ |
| 74 | + constructor(address[] memory _allowedAssets, bool _useAssetAllowlist) public { |
| 75 | + _addAllowedAssets(_allowedAssets); |
| 76 | + _updateUseAssetAllowlist(_useAssetAllowlist); |
| 77 | + } |
| 78 | + |
| 79 | + /* ============ External Functions ============ */ |
| 80 | + |
| 81 | + function getAllowedAssets() external view returns(address[] memory) { |
| 82 | + return allowedAssets; |
| 83 | + } |
| 84 | + |
| 85 | + /* ============ Internal Functions ============ */ |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Add new assets that can be traded to, wrapped to, or claimed |
| 90 | + * |
| 91 | + * @param _assets New asset to add |
| 92 | + */ |
| 93 | + function _addAllowedAssets(address[] memory _assets) internal { |
| 94 | + for (uint256 i = 0; i < _assets.length; i++) { |
| 95 | + address asset = _assets[i]; |
| 96 | + |
| 97 | + require(!assetAllowlist[asset], "Asset already added"); |
| 98 | + |
| 99 | + allowedAssets.push(asset); |
| 100 | + |
| 101 | + assetAllowlist[asset] = true; |
| 102 | + |
| 103 | + emit AllowedAssetAdded(asset); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Remove asset(s) so that it/they can't be traded to, wrapped to, or claimed |
| 109 | + * |
| 110 | + * @param _assets Asset(s) to remove |
| 111 | + */ |
| 112 | + function _removeAllowedAssets(address[] memory _assets) internal { |
| 113 | + for (uint256 i = 0; i < _assets.length; i++) { |
| 114 | + address asset = _assets[i]; |
| 115 | + |
| 116 | + require(assetAllowlist[asset], "Asset not already added"); |
| 117 | + |
| 118 | + allowedAssets.removeStorage(asset); |
| 119 | + |
| 120 | + assetAllowlist[asset] = false; |
| 121 | + |
| 122 | + emit AllowedAssetRemoved(asset); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Toggle useAssetAllowlist on and off. When false asset allowlist is ignored |
| 128 | + * when true it is enforced. |
| 129 | + * |
| 130 | + * @param _useAssetAllowlist Bool indicating whether to use asset allow list |
| 131 | + */ |
| 132 | + function _updateUseAssetAllowlist(bool _useAssetAllowlist) internal { |
| 133 | + useAssetAllowlist = _useAssetAllowlist; |
| 134 | + |
| 135 | + emit UseAssetAllowlistUpdated(_useAssetAllowlist); |
| 136 | + } |
| 137 | + |
| 138 | + /// @notice Check that all assets in array are allowed to be treated |
| 139 | + /// @dev ca be bypassed by setting the useAssetAllowlist to false (default) |
| 140 | + function _areAllowedAssets(address[] memory _assets) internal view returns(bool) { |
| 141 | + if (!useAssetAllowlist) { return true; } |
| 142 | + for (uint256 i = 0; i < _assets.length; i++) { |
| 143 | + if (!assetAllowlist[_assets[i]]) { return false; } |
| 144 | + } |
| 145 | + return true; |
| 146 | + } |
| 147 | +} |
0 commit comments