// SPDX-License-Identifier: GPL-3.0 // // :+#####%%%%%%%%%%%%%%+ // .-*@@@%+.:+%@@@@@%%#***%@@%= // :=*%@@@#=. :#@@% *@@@%= // .-+*%@%*-.:+%@@@@@@+. -*+: .=#. :%@@@%- // :=*@@@@%%@@@@@@@@@%@@@- .=#@@@%@%= =@@@@#. // -=+#%@@%#*=:. :%@@@@%. -*@@#*@@@@@@@#=:- *@@@@+ // =@@%=:. :=: *@@@@@%#- =%*%@@@@#+-. =+ :%@@@%- // -@@%. .+@@@ =+=-. @@#- +@@@%- =@@@@%: // :@@@. .+@@#%: : .=*=-::.-%@@@+*@@= +@@@@#. // %@@: +@%%* =%@@@@@@@@@@@#. .*@%- +@@@@*. // #@@= .+@@@@%:=*@@@@@- :%@%: .*@@@@+ // *@@* +@@@#-@@%-:%@@* +@@#. :%@@@@- // -@@% .:-=++*##%%%@@@@@@@@@@@@*. :@+.@@@%: .#@@+ =@@@@#: // .@@@*-+*#%%%@@@@@@@@@@@@@@@@%%#**@@%@@@. *@=*@@# :#@%= .#@@@@#- // -%@@@@@@@@@@@@@@@*+==-:-@@@= *@# .#@*-=*@@@@%= -%@@@* =@@@@@%- // -+%@@@#. %@%%= -@@:+@: -@@* *@@*-:: -%@@%=. .*@@@@@# // *@@@* +@* *@@##@@- #@*@@+ -@@= . :+@@@#: .-+@@@%+- // +@@@%*@@:..=@@@@* .@@@* .#@#. .=+- .=%@@@*. :+#@@@@*=: // =@@@@%@@@@@@@@@@@@@@@@@@@@@@%- :+#*. :*@@@%=. .=#@@@@%+: // .%@@= ..... .=#@@+. .#@@@*: -*%@@@@%+. // +@@#+===---:::... .=%@@*- +@@@+. -*@@@@@%+. // -@@@@@@@@@@@@@@@@@@@@@@%@@@@= -@@@+ -#@@@@@#=. // ..:::---===+++***###%%%@@@#- .#@@+ -*@@@@@#=. // @@@@@@+. +@@*. .+@@@@@%=. // -@@@@@= =@@%: -#@@@@%+. // +@@@@@. =@@@= .+@@@@@*: // #@@@@#:%@@#. :*@@@@#- // @@@@@%@@@= :#@@@@+. // :@@@@@@@#.:#@@@%- // +@@@@@@-.*@@@*: // #@@@@#.=@@@+. // @@@@+-%@%= // :@@@#%@%= // +@@@@%- // :#%%= // /** * NOTICE * * The T-REX software is licensed under a proprietary license or the GPL v.3. * If you choose to receive it under the GPL v.3 license, the following applies: * T-REX is a suite of smart contracts implementing the ERC-3643 standard and * developed by Tokeny to manage and transfer financial assets on EVM blockchains * * Copyright (C) 2023, Tokeny sàrl. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ pragma solidity 0.8.17; import "../BasicCompliance.sol"; /** * this feature allows to put a limits on transfer volumes on a daily basis as well as on a monthly basis * Investors will not be allowed to send more tokens than the fixed limit per day/month */ abstract contract DayMonthLimits is BasicCompliance { /// Struct of transfer Counters struct TransferCounter { uint256 dailyCount; uint256 monthlyCount; uint256 dailyTimer; uint256 monthlyTimer; } /// Getter for Tokens dailyLimit uint256 public dailyLimit; /// Getter for Tokens monthlyLimit uint256 public monthlyLimit; /// Mapping for users Counters mapping(address => TransferCounter) public usersCounters; /** * this event is emitted whenever a DailyLimit has been updated. * the event is emitted by 'setDailyLimit' and by Compliance's constructor. * `_newDailyLimit` is the amount Limit of tokens to be transferred daily. */ event DailyLimitUpdated(uint _newDailyLimit); /** * this event is emitted whenever a MonthlyLimit has been updated. * the event is emitted by 'setMonthlyLimit' and by Compliance's constructor. * `_newMonthlyLimit` is the amount Limit of tokens to be transferred monthly. */ event MonthlyLimitUpdated(uint _newMonthlyLimit); /** * @dev Set the limit of tokens allowed to be transferred daily. * @param _newDailyLimit The new daily limit of tokens * Only the owner of the Compliance smart contract can call this function */ function setDailyLimit(uint256 _newDailyLimit) external onlyOwner { dailyLimit = _newDailyLimit; emit DailyLimitUpdated(_newDailyLimit); } /** * @dev Set the limit of tokens allowed to be transferred monthly. * @param _newMonthlyLimit The new monthly limit of tokens * Only the owner of the Compliance smart contract can call this function */ function setMonthlyLimit(uint256 _newMonthlyLimit) external onlyOwner { monthlyLimit = _newMonthlyLimit; emit MonthlyLimitUpdated(_newMonthlyLimit); } /** * @dev check on the compliance status of a transaction. * If the check returns TRUE, the transfer is allowed to be executed, if the check returns FALSE, the compliance * feature will block the transfer execution * The check will verify if the transfer is exceeding the limits (daily and/or monthly) * If the transfer exceeds the limits, the check returns false and the transfer is blocked * otherwise it returns true. Agents bypass this compliance feature * @param _from the address of the transfer sender * @param _value the amount of tokens that `_from` would send to `_to` */ function complianceCheckOnDayMonthLimits(address _from, address /*_to*/, uint256 _value) public view returns (bool) { address senderIdentity = _getIdentity(_from); if (!isTokenAgent(_from)) { if (_value > dailyLimit) { return false; } if (!_isDayFinished(senderIdentity) && ((usersCounters[senderIdentity].dailyCount + _value > dailyLimit) || (usersCounters[senderIdentity].monthlyCount + _value > monthlyLimit))) { return false; } if (_isDayFinished(senderIdentity) && _value + usersCounters[senderIdentity].monthlyCount > monthlyLimit) { return(_isMonthFinished(senderIdentity)); } } return true; } /** * @dev state update of the compliance feature post-transfer. * counters of daily and monthly transfers are updated post-transfer * @param _from the address of the transfer sender * @param _value the amount of tokens that `_from` sent to `_to` * internal function, can be called only from the functions of the Compliance smart contract */ function _transferActionOnDayMonthLimits(address _from, address /*_to*/, uint256 _value) internal { _increaseCounters(_from, _value); } /** * @dev state update of the compliance feature post-minting. * this compliance feature doesn't require state update post-minting * @param _to the address of the minting beneficiary * @param _value the amount of tokens minted on `_to` wallet * internal function, can be called only from the functions of the Compliance smart contract */ // solhint-disable-next-line no-empty-blocks function _creationActionOnDayMonthLimits(address _to, uint256 _value) internal {} /** * @dev state update of the compliance feature post-burning. * this compliance feature doesn't require state update post-burning * @param _from the wallet address on which tokens burnt * @param _value the amount of tokens burnt from `_from` wallet * internal function, can be called only from the functions of the Compliance smart contract */ // solhint-disable-next-line no-empty-blocks function _destructionActionOnDayMonthLimits(address _from, uint256 _value) internal {} /** * @dev Checks if daily and/or monthly cooldown must be reset, then check if _value sent has been exceeded, * if not increases user's OnchainID counters. * @param _userAddress, address on which counters will be increased * @param _value, value of transaction)to be increased */ function _increaseCounters(address _userAddress, uint256 _value) internal { address identity = _getIdentity(_userAddress); _resetDailyCooldown(identity); _resetMonthlyCooldown(identity); if ((usersCounters[identity].dailyCount + _value) <= dailyLimit) { usersCounters[identity].dailyCount += _value; } if ((usersCounters[identity].monthlyCount + _value) <= monthlyLimit) { usersCounters[identity].monthlyCount += _value; } } /** * @dev resets cooldown for the day if cooldown has reached time limit of 1 day * @param _identity ONCHAINID to be checked */ function _resetDailyCooldown(address _identity) internal { if (_isDayFinished(_identity)) { usersCounters[_identity].dailyTimer = block.timestamp + 1 days; usersCounters[_identity].dailyCount = 0; } } /** * @dev resets cooldown for the month if cooldown has reached the time limit of 30days * @param _identity ONCHAINID to be checked */ function _resetMonthlyCooldown(address _identity) internal { if (_isMonthFinished(_identity)) { usersCounters[_identity].monthlyTimer = block.timestamp + 30 days; usersCounters[_identity].monthlyCount = 0; } } /** * @dev checks if the day has finished since the cooldown has been triggered for this identity * @param _identity ONCHAINID to be checked */ function _isDayFinished(address _identity) internal view returns (bool) { return (usersCounters[_identity].dailyTimer <= block.timestamp); } /** * @dev checks if the month has finished since the cooldown has been triggered for this identity * @param _identity ONCHAINID to be checked */ function _isMonthFinished(address _identity) internal view returns (bool) { return (usersCounters[_identity].monthlyTimer <= block.timestamp); } }