Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 52 additions & 25 deletions contracts/libraries/VolumeRestrictionLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,20 @@ library VolumeRestrictionLib {

using SafeMath for uint256;

function _checkLengthOfArray(
address[] _holders,
uint256[] _allowedTokens,
uint256[] _startTimes,
uint256[] _rollingPeriodInDays,
uint256[] _endTimes,
uint256[] _restrictionTypes
function deleteHolderFromList(
VolumeRestrictionTMStorage.RestrictedData storage data,
address _holder,
VolumeRestrictionTMStorage.TypeOfPeriod _typeOfPeriod
)
internal
pure
public
{
require(
_holders.length == _allowedTokens.length &&
_allowedTokens.length == _startTimes.length &&
_startTimes.length == _rollingPeriodInDays.length &&
_rollingPeriodInDays.length == _endTimes.length &&
_endTimes.length == _restrictionTypes.length,
"Length mismatch"
);
}

function deleteHolderFromList(VolumeRestrictionTMStorage.RestrictedData storage data, address _holder, uint8 _typeOfPeriod) public {
// Deleting the holder if holder's type of Period is `Both` type otherwise
// it will assign the given type `_typeOfPeriod` to the _holder typeOfPeriod
// `_typeOfPeriod` it always be contrary to the removing restriction
// if removing restriction is individual then typeOfPeriod is TypeOfPeriod.OneDay
// in uint8 its value is 1. if removing restriction is daily individual then typeOfPeriod
// is TypeOfPeriod.MultipleDays in uint8 its value is 0.
if (data.restrictedHolders[_holder].typeOfPeriod != uint8(VolumeRestrictionTMStorage.TypeOfPeriod.Both)) {
if (data.restrictedHolders[_holder].typeOfPeriod != VolumeRestrictionTMStorage.TypeOfPeriod.Both) {
uint128 index = data.restrictedHolders[_holder].index;
uint256 _len = data.restrictedAddresses.length;
if (index != _len) {
Expand All @@ -51,22 +36,64 @@ library VolumeRestrictionLib {
}
}

function addRestrictionData(VolumeRestrictionTMStorage.RestrictedData storage data, address _holder, uint8 _callFrom, uint256 _endTime) public {
function addRestrictionData(
VolumeRestrictionTMStorage.RestrictedData storage data,
address _holder,
VolumeRestrictionTMStorage.TypeOfPeriod _callFrom,
uint256 _endTime
)
public
{
uint128 index = data.restrictedHolders[_holder].index;
if (data.restrictedHolders[_holder].seen == 0) {
data.restrictedAddresses.push(_holder);
index = uint128(data.restrictedAddresses.length);
}
uint8 _type = _getTypeOfPeriod(data.restrictedHolders[_holder].typeOfPeriod, _callFrom, _endTime);
VolumeRestrictionTMStorage.TypeOfPeriod _type = _getTypeOfPeriod(data.restrictedHolders[_holder].typeOfPeriod, _callFrom, _endTime);
data.restrictedHolders[_holder] = VolumeRestrictionTMStorage.RestrictedHolder(uint8(1), _type, index);
}

function _getTypeOfPeriod(uint8 _currentTypeOfPeriod, uint8 _callFrom, uint256 _endTime) internal pure returns(uint8) {
function _getTypeOfPeriod(
VolumeRestrictionTMStorage.TypeOfPeriod _currentTypeOfPeriod,
VolumeRestrictionTMStorage.TypeOfPeriod _callFrom,
uint256 _endTime
)
internal
pure
returns(VolumeRestrictionTMStorage.TypeOfPeriod)
{
if (_currentTypeOfPeriod != _callFrom && _endTime != uint256(0))
return uint8(VolumeRestrictionTMStorage.TypeOfPeriod.Both);
return VolumeRestrictionTMStorage.TypeOfPeriod.Both;
else
return _callFrom;
}

function isValidAmountAfterRestrictionChanges(
uint256 _amountTradedLastDay,
uint256 _amount,
uint256 _sumOfLastPeriod,
uint256 _allowedAmount,
uint256 _lastTradedTimestamp
)
public
view
returns(bool)
{
// if restriction is to check whether the current transaction is performed within the 24 hours
// span after the last transaction performed by the user
if (BokkyPooBahsDateTimeLibrary.diffSeconds(_lastTradedTimestamp, now) < 86400) {
(,, uint256 lastTxDay) = BokkyPooBahsDateTimeLibrary.timestampToDate(_lastTradedTimestamp);
(,, uint256 currentTxDay) = BokkyPooBahsDateTimeLibrary.timestampToDate(now);
// This if statement is to check whether the last transaction timestamp (of `individualRestriction[_from]`
// when `_isDefault` is true or defaultRestriction when `_isDefault` is false) is comes within the same day of the current
// transaction timestamp or not.
if (lastTxDay == currentTxDay) {
// Not allow to transact more than the current transaction restriction allowed amount
if ((_sumOfLastPeriod.add(_amount)).add(_amountTradedLastDay) > _allowedAmount)
return false;
}
}
return true;
}

}
Loading