|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +import { Initializable } from "@openzeppelin/contracts/proxy/utils/Initializable.sol"; |
| 5 | +import { AccessControlEnumerable } from "@openzeppelin/contracts/access/AccessControlEnumerable.sol"; |
| 6 | +import { INSCommission } from "./interfaces/INSCommission.sol"; |
| 7 | +import { RONTransferHelper } from "./libraries/transfers/RONTransferHelper.sol"; |
| 8 | + |
| 9 | +contract RNSCommission is Initializable, AccessControlEnumerable, INSCommission { |
| 10 | + /// @dev Constant representing the maximum percentage value (100%). |
| 11 | + uint256 public constant MAX_PERCENTAGE = 100_00; |
| 12 | + /// @dev Role for accounts that can send RON for this contract. |
| 13 | + bytes32 public constant SENDER_ROLE = keccak256("SENDER_ROLE"); |
| 14 | + |
| 15 | + /// @dev Gap for upgradability. |
| 16 | + uint256[50] private ____gap; |
| 17 | + /// @dev Array of `Commission` structs that store commissions infomation. |
| 18 | + Commission[] internal _commissionInfos; |
| 19 | + |
| 20 | + constructor() { |
| 21 | + _disableInitializers(); |
| 22 | + } |
| 23 | + |
| 24 | + receive() external payable { |
| 25 | + _fallback(); |
| 26 | + } |
| 27 | + |
| 28 | + function initialize(address admin, Commission[] calldata commissionInfos, address[] calldata allowedSenders) |
| 29 | + external |
| 30 | + initializer |
| 31 | + { |
| 32 | + _setupRole(DEFAULT_ADMIN_ROLE, admin); |
| 33 | + |
| 34 | + uint256 length = allowedSenders.length; |
| 35 | + for (uint256 i; i < length; ++i) { |
| 36 | + _setupRole(SENDER_ROLE, allowedSenders[i]); |
| 37 | + } |
| 38 | + |
| 39 | + _setCommissions(commissionInfos); |
| 40 | + } |
| 41 | + |
| 42 | + /// @inheritdoc INSCommission |
| 43 | + function getCommissions() external view returns (Commission[] memory commissionInfos) { |
| 44 | + return _commissionInfos; |
| 45 | + } |
| 46 | + |
| 47 | + /// @inheritdoc INSCommission |
| 48 | + function setCommissions(Commission[] calldata commissionInfos) external onlyRole(DEFAULT_ADMIN_ROLE) { |
| 49 | + _setCommissions(commissionInfos); |
| 50 | + } |
| 51 | + |
| 52 | + /// @inheritdoc INSCommission |
| 53 | + function setCommissionInfo(uint256 commissionIdx, address payable newRecipient, string calldata newName) |
| 54 | + external |
| 55 | + onlyRole(DEFAULT_ADMIN_ROLE) |
| 56 | + { |
| 57 | + if (commissionIdx >= _commissionInfos.length) revert InvalidArrayLength(); |
| 58 | + |
| 59 | + _commissionInfos[commissionIdx].recipient = newRecipient; |
| 60 | + _commissionInfos[commissionIdx].name = newName; |
| 61 | + emit CommissionInfoUpdated(msg.sender, commissionIdx, newRecipient, newName); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @dev Helper method to allocate commission and take fee into recipient address. |
| 66 | + */ |
| 67 | + function _allocateCommissionAndTransferToRecipient(uint256 ronAmount) internal { |
| 68 | + if (ronAmount == 0) revert InvalidAmountOfRON(); |
| 69 | + |
| 70 | + uint256 length = _commissionInfos.length; |
| 71 | + if (length == 0) revert InvalidArrayLength(); |
| 72 | + |
| 73 | + uint256 lastIdx = length - 1; |
| 74 | + uint256 sumValue; |
| 75 | + |
| 76 | + for (uint256 i; i < lastIdx; ++i) { |
| 77 | + uint256 commissionAmount = _computePercentage(ronAmount, _commissionInfos[i].ratio); |
| 78 | + sumValue += commissionAmount; |
| 79 | + |
| 80 | + RONTransferHelper.safeTransfer(_commissionInfos[i].recipient, commissionAmount); |
| 81 | + emit Distributed(_commissionInfos[i].recipient, commissionAmount); |
| 82 | + } |
| 83 | + |
| 84 | + // This code send the remaining RON to the last recipient. |
| 85 | + if (sumValue < ronAmount) { |
| 86 | + RONTransferHelper.safeTransfer(_commissionInfos[lastIdx].recipient, ronAmount - sumValue); |
| 87 | + emit Distributed(_commissionInfos[lastIdx].recipient, ronAmount - sumValue); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + function _setCommissions(Commission[] calldata commissionInfos) internal { |
| 92 | + uint256 length = commissionInfos.length; |
| 93 | + // commissionInfos[] can not be empty |
| 94 | + if (length == 0) revert InvalidArrayLength(); |
| 95 | + |
| 96 | + delete _commissionInfos; |
| 97 | + |
| 98 | + uint256 sum; |
| 99 | + |
| 100 | + for (uint256 i; i < length; ++i) { |
| 101 | + sum += commissionInfos[i].ratio; |
| 102 | + _commissionInfos.push(commissionInfos[i]); |
| 103 | + } |
| 104 | + |
| 105 | + if (sum != MAX_PERCENTAGE) revert InvalidRatio(); |
| 106 | + |
| 107 | + emit CommissionsUpdated(msg.sender, commissionInfos); |
| 108 | + } |
| 109 | + |
| 110 | + // Calculate amount of money based on commission's ratio |
| 111 | + function _computePercentage(uint256 value, uint256 percentage) internal pure virtual returns (uint256) { |
| 112 | + return (value * percentage) / MAX_PERCENTAGE; |
| 113 | + } |
| 114 | + |
| 115 | + function _fallback() internal { |
| 116 | + if (hasRole(SENDER_ROLE, msg.sender)) { |
| 117 | + _allocateCommissionAndTransferToRecipient(msg.value); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments