|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; |
| 5 | +import "@openzeppelin/contracts-upgradeable/utils/structs/EnumerableSetUpgradeable.sol"; |
| 6 | +import "@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol"; |
| 7 | +import "./interfaces/IConfig.sol"; |
| 8 | + |
| 9 | +contract Config is IConfig, OwnableUpgradeable { |
| 10 | + using EnumerableSetUpgradeable for EnumerableSetUpgradeable.Bytes32Set; |
| 11 | + using SafeMathUpgradeable for uint256; |
| 12 | + |
| 13 | + EnumerableSetUpgradeable.Bytes32Set private _variables; |
| 14 | + |
| 15 | + /// @dev Mapping of a named Id to uint256 number |
| 16 | + mapping(bytes32 => uint256) private numbers; |
| 17 | + |
| 18 | + /// @dev Mapping of a named Id to address |
| 19 | + mapping(bytes32 => address) private addresses; |
| 20 | + |
| 21 | + /// @dev Mapping of en entity type on minimum deposit value |
| 22 | + mapping(bytes32 => uint256) private minDeposits; |
| 23 | + |
| 24 | + /// Events |
| 25 | + |
| 26 | + /// @dev Emitted when a variable is created/updated |
| 27 | + event ConfigNumbers(bytes32 name, uint256 value); |
| 28 | + |
| 29 | + /// @dev Emitted when a variable is created/updated |
| 30 | + event ConfigAddresses(bytes32 name, address value); |
| 31 | + |
| 32 | + /// @dev Emitted when an entity minimum deposit set/updates |
| 33 | + event ConfigMinDeposit(bytes32 kind, uint256 value); |
| 34 | + |
| 35 | + /// Errors |
| 36 | + |
| 37 | + /// @dev Thrown when a user attempts to provide an invalid config property value |
| 38 | + error InvalidConfig(); |
| 39 | + |
| 40 | + /** |
| 41 | + * @dev Initializes Config contract |
| 42 | + * @param _asset The address of the asset used by the protocol in tokenomics |
| 43 | + * @param _claimPeriod The default time period, in seconds, allowed for the supplier to claim the deal. |
| 44 | + * @param _protocolFee Protocol's fee in percents |
| 45 | + * @param _retailerFee Retailer's fee in percents |
| 46 | + * @param _feeRecipient he recipient of the protocol fee |
| 47 | + * @param _kinds Supported entity types |
| 48 | + * @param _minDeposits Minimum value of deposit |
| 49 | + */ |
| 50 | + function initialize( |
| 51 | + address _owner, |
| 52 | + address _asset, |
| 53 | + uint256 _claimPeriod, |
| 54 | + uint256 _protocolFee, |
| 55 | + uint256 _retailerFee, |
| 56 | + address _feeRecipient, |
| 57 | + bytes32[] memory _kinds, |
| 58 | + uint256[] memory _minDeposits |
| 59 | + ) external initializer { |
| 60 | + _transferOwnership(_owner); |
| 61 | + |
| 62 | + // Save an asset address |
| 63 | + config("asset", _asset); |
| 64 | + |
| 65 | + // The default time period, in seconds, allowed for the supplier to claim the deal. |
| 66 | + // The buyer is not able to cancel the deal during this period |
| 67 | + config("claim_period", _claimPeriod); |
| 68 | + |
| 69 | + // The recipient of the protocol fee |
| 70 | + config("fee_recipient", _feeRecipient); |
| 71 | + |
| 72 | + // In total, all the fees must not be greater than 100. |
| 73 | + // Of course, having 100% of the fees is absurd case. |
| 74 | + if (_protocolFee.add(_retailerFee) > 100) { |
| 75 | + revert InvalidConfig(); |
| 76 | + } |
| 77 | + |
| 78 | + // Protocol's fee in percents |
| 79 | + config("protocol_fee", _protocolFee); |
| 80 | + |
| 81 | + // Retailer's fee in percents |
| 82 | + config("retailer_fee", _retailerFee); |
| 83 | + |
| 84 | + // Save initial minimum deposits values |
| 85 | + _setMinDeposits(_kinds, _minDeposits); |
| 86 | + } |
| 87 | + |
| 88 | + /// @inheritdoc IConfig |
| 89 | + function getNumber(bytes32 _name) public view returns (uint256) { |
| 90 | + return numbers[_name]; |
| 91 | + } |
| 92 | + |
| 93 | + /// @inheritdoc IConfig |
| 94 | + function getAddress(bytes32 _name) public view returns (address) { |
| 95 | + return addresses[_name]; |
| 96 | + } |
| 97 | + |
| 98 | + /// @inheritdoc IConfig |
| 99 | + function getMinDeposit(bytes32 _name) public view returns (uint256) { |
| 100 | + return minDeposits[_name]; |
| 101 | + } |
| 102 | + |
| 103 | + /// @inheritdoc IConfig |
| 104 | + function variables() external view returns (bytes32[] memory) { |
| 105 | + return _variables.values(); |
| 106 | + } |
| 107 | + |
| 108 | + /// @inheritdoc IConfig |
| 109 | + function config(bytes32 name, uint256 value) public onlyOwner { |
| 110 | + numbers[name] = value; |
| 111 | + _variables.add(name); |
| 112 | + emit ConfigNumbers(name, value); |
| 113 | + } |
| 114 | + |
| 115 | + /// @inheritdoc IConfig |
| 116 | + function config(bytes32 name, address value) public onlyOwner { |
| 117 | + addresses[name] = value; |
| 118 | + _variables.add(name); |
| 119 | + emit ConfigAddresses(name, value); |
| 120 | + } |
| 121 | + |
| 122 | + /// @inheritdoc IConfig |
| 123 | + function setMinDeposits( |
| 124 | + bytes32[] memory _kinds, |
| 125 | + uint256[] memory _minDeposits |
| 126 | + ) external onlyOwner { |
| 127 | + _setMinDeposits(_kinds, _minDeposits); |
| 128 | + } |
| 129 | + |
| 130 | + /// Internal functions |
| 131 | + |
| 132 | + /// @dev See {IConfig.setMinDeposits} |
| 133 | + function _setMinDeposits( |
| 134 | + bytes32[] memory _kinds, |
| 135 | + uint256[] memory _minDeposits |
| 136 | + ) internal { |
| 137 | + // Entity types number must be equal to minimum deposits values number |
| 138 | + if (_kinds.length != _minDeposits.length) { |
| 139 | + revert InvalidConfig(); |
| 140 | + } |
| 141 | + |
| 142 | + // Save minimum deposit values |
| 143 | + for (uint256 i = 0; i < _minDeposits.length; i++) { |
| 144 | + minDeposits[_kinds[i]] = _minDeposits[i]; |
| 145 | + emit ConfigMinDeposit(_kinds[i], _minDeposits[i]); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments