Skip to content

Commit

Permalink
feat: simplify implementation
Browse files Browse the repository at this point in the history
Signed-off-by: GopherJ <[email protected]>
  • Loading branch information
GopherJ committed Sep 12, 2023
1 parent e1e87ff commit 1e40d46
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 177 deletions.
33 changes: 0 additions & 33 deletions contracts/interfaces/IPoolParameters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ interface IPoolParameters {
**/
event ClaimApeForYieldIncentiveUpdated(uint256 oldValue, uint256 newValue);

/**
* @notice TimeLockWhiteListEvents
* @dev This event is emitted when the time lock whitelist is updated. It provides information about
* addresses that were added to and removed from the whitelist during the update.
*
* @param added An array of addresses that were added to the time lock whitelist.
* @param removed An array of addresses that were removed from the time lock whitelist.
*/
event TimeLockWhitelistUpdated(address[] added, address[] removed);

/**
* @notice Initializes a reserve, activating it, assigning an xToken and debt tokens and an
* interest rate strategy
Expand Down Expand Up @@ -154,29 +144,6 @@ interface IPoolParameters {
DataTypes.ApeCompoundStrategy calldata strategy
) external;

/**
* @dev Updates the time lock whitelist by adding and/or removing multiple addresses.
* @param toAdd An array of addresses to be added to the whitelist.
* @param toRemove An array of addresses to be removed from the whitelist.
*/
function updateTimeLockWhiteList(
address[] calldata toAdd,
address[] calldata toRemove
) external;

/**
* @notice TimeLockWhiteList
* @dev This function allows external callers to check whether an array of addresses are
* on the time lock whitelist, indicating whether they have permission for specific actions.
*
* @param users An array of addresses to check for whitelist membership.
* @return isWhiteListed An array of boolean values indicating whether each provided address
* is on the time lock whitelist (true if whitelisted, false otherwise).
*/
function isTimeLockWhiteListed(
address[] calldata users
) external view returns (bool[] memory);

/**
* @notice get user ape compound strategy
* @param user The user address
Expand Down
33 changes: 33 additions & 0 deletions contracts/interfaces/ITimeLock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ interface ITimeLock {
*/
event TimeLockFrozen(bool value);

/**
* @notice TimeLockWhiteListEvents
* @dev This event is emitted when the time lock whitelist is updated. It provides information about
* addresses that were added to and removed from the whitelist during the update.
*
* @param added An array of addresses that were added to the time lock whitelist.
* @param removed An array of addresses that were removed from the time lock whitelist.
*/
event TimeLockWhitelistUpdated(address[] added, address[] removed);

/** @dev Function to create a new time-lock agreement
* @param assetType Type of the asset involved
* @param actionType Type of action for the time-lock
Expand Down Expand Up @@ -118,4 +128,27 @@ interface ITimeLock {
* @notice This function can only be called by an authorized user
*/
function unfreezeAllAgreements() external;

/**
* @dev Updates the time lock whitelist by adding and/or removing multiple addresses.
* @param toAdd An array of addresses to be added to the whitelist.
* @param toRemove An array of addresses to be removed from the whitelist.
*/
function updateTimeLockWhiteList(
address[] calldata toAdd,
address[] calldata toRemove
) external;

/**
* @notice TimeLockWhiteList
* @dev This function allows external callers to check whether an array of addresses are
* on the time lock whitelist, indicating whether they have permission for specific actions.
*
* @param users An array of addresses to check for whitelist membership.
* @return isWhiteListed An array of boolean values indicating whether each provided address
* is on the time lock whitelist (true if whitelisted, false otherwise).
*/
function isTimeLockWhiteListed(
address[] calldata users
) external view returns (bool[] memory);
}
38 changes: 38 additions & 0 deletions contracts/misc/TimeLock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ contract TimeLock is ITimeLock, ReentrancyGuardUpgradeable, IERC721Receiver {
uint248 public agreementCount;
bool public frozen;

// TimeLock whitelist
mapping(address => bool) _whiteList;

IPool private immutable POOL;
IACLManager private immutable ACL_MANAGER;
address private immutable weth;
address private immutable wpunk;
address private immutable Punk;

uint48 private constant MIN_WAIT_TIME = 12;

modifier onlyXToken(address asset) {
require(
msg.sender == POOL.getReserveXToken(asset),
Expand Down Expand Up @@ -83,6 +88,10 @@ contract TimeLock is ITimeLock, ReentrancyGuardUpgradeable, IERC721Receiver {
uint48 releaseTime
) external onlyXToken(asset) returns (uint256) {
require(beneficiary != address(0), "Beneficiary cant be zero address");
if (_whiteList[beneficiary]) {
releaseTime = uint48(block.timestamp) + MIN_WAIT_TIME;
}

require(releaseTime > block.timestamp, "Release time not valid");

uint256 agreementId = agreementCount++;
Expand Down Expand Up @@ -265,4 +274,33 @@ contract TimeLock is ITimeLock, ReentrancyGuardUpgradeable, IERC721Receiver {
) external virtual override returns (bytes4) {
return this.onERC721Received.selector;
}

/// @inheritdoc ITimeLock
function updateTimeLockWhiteList(
address[] calldata toAdd,
address[] calldata toRemove
) external onlyPoolAdmin {
for (uint256 i = 0; i < toAdd.length; i++) {
if (!_whiteList[toAdd[i]]) {
_whiteList[toAdd[i]] = true;
}
}
for (uint256 i = 0; i < toRemove.length; i++) {
if (_whiteList[toRemove[i]]) {
_whiteList[toRemove[i]] = false;
}
}
emit TimeLockWhitelistUpdated(toAdd, toRemove);
}

/// @inheritdoc ITimeLock
function isTimeLockWhiteListed(
address[] calldata users
) external view returns (bool[] memory) {
bool[] memory res = new bool[](users.length);
for (uint256 i = 0; i < users.length; i++) {
res[i] = _whiteList[users[i]];
}
return res;
}
}
20 changes: 12 additions & 8 deletions contracts/protocol/libraries/logic/BorrowLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,21 @@ library BorrowLogic {
);

if (params.releaseUnderlying) {
DataTypes.TimeLockParams memory timeLockParams = GenericLogic
.calculateTimeLockParams(
reserve,
DataTypes.TimeLockFactorParams({
assetType: DataTypes.AssetType.ERC20,
asset: params.asset,
amount: params.amount
})
);
timeLockParams.actionType = DataTypes.TimeLockActionType.BORROW;

IPToken(reserveCache.xTokenAddress).transferUnderlyingTo(
params.user,
params.amount,
GenericLogic.calculateTimeLockParams(
reserve,
DataTypes.AssetType.ERC20,
params.asset,
params.amount,
DataTypes.TimeLockActionType.BORROW,
params.isWhiteListed
)
timeLockParams
);
}

Expand Down
45 changes: 6 additions & 39 deletions contracts/protocol/libraries/logic/GenericLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {DataTypes} from "../types/DataTypes.sol";
import {ReserveLogic} from "./ReserveLogic.sol";
import {INonfungiblePositionManager} from "../../../dependencies/uniswapv3-periphery/interfaces/INonfungiblePositionManager.sol";
import {XTokenType, IXTokenType} from "../../../interfaces/IXTokenType.sol";
import {Helpers} from "../../libraries/helpers/Helpers.sol";

/**
* @title GenericLogic library
Expand All @@ -31,8 +32,6 @@ library GenericLogic {
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using UserConfiguration for DataTypes.UserConfigurationMap;

uint48 internal constant MIN_WAIT_TIME = 12;

struct CalculateUserAccountDataVars {
uint256 assetPrice;
uint256 assetUnit;
Expand Down Expand Up @@ -536,48 +535,16 @@ library GenericLogic {
);
}

/**
* @notice TimeLockParamsCalculator
* @dev This internal function is used to calculate the parameters required for a time-locked action
* on a specific asset within a reserve. It computes and returns a `TimeLockParams` structure containing
* essential information for executing the action.
*
* @param reserve The storage reference to the reserve data.
* @param assetType The type of the asset (e.g., collateral or debt).
* @param asset The address of the asset being affected.
* @param amount The amount of the asset involved in the time-locked action.
* @param actionType The type of time-locked action being performed (e.g., deposit or withdraw).
*
* @return timeLockParams A `TimeLockParams` structure with all the necessary parameters computed for
* executing the time-locked action.
*/
function calculateTimeLockParams(
DataTypes.ReserveData storage reserve,
DataTypes.AssetType assetType,
address asset,
uint256 amount,
DataTypes.TimeLockActionType actionType,
bool isWhiteListed
DataTypes.TimeLockFactorParams memory params
) internal returns (DataTypes.TimeLockParams memory) {
DataTypes.TimeLockParams memory timeLockParams;
if (!isWhiteListed) {
address timeLockStrategyAddress = reserve.timeLockStrategyAddress;
if (timeLockStrategyAddress != address(0)) {
timeLockParams = ITimeLockStrategy(timeLockStrategyAddress)
.calculateTimeLockParams(
DataTypes.TimeLockFactorParams({
assetType: assetType,
asset: asset,
amount: amount
})
);
}
} else {
timeLockParams.releaseTime =
uint48(block.timestamp) +
MIN_WAIT_TIME;
address timeLockStrategyAddress = reserve.timeLockStrategyAddress;
if (timeLockStrategyAddress != address(0)) {
timeLockParams = ITimeLockStrategy(timeLockStrategyAddress)
.calculateTimeLockParams(params);
}
timeLockParams.actionType = actionType;

return timeLockParams;
}
Expand Down
3 changes: 1 addition & 2 deletions contracts/protocol/libraries/logic/MarketplaceLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,7 @@ library MarketplaceLogic {
releaseUnderlying: false,
reservesCount: params.reservesCount,
oracle: params.oracle,
priceOracleSentinel: params.priceOracleSentinel,
isWhiteListed: false
priceOracleSentinel: params.priceOracleSentinel
})
);
}
Expand Down
6 changes: 2 additions & 4 deletions contracts/protocol/libraries/logic/PositionMoverLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ library PositionMoverLogic {
reservesCount: ps._reservesCount,
oracle: poolAddressProvider.getPriceOracle(),
priceOracleSentinel: poolAddressProvider
.getPriceOracleSentinel(),
isWhiteListed: false
.getPriceOracleSentinel()
})
);
}
Expand Down Expand Up @@ -402,8 +401,7 @@ library PositionMoverLogic {
releaseUnderlying: false,
reservesCount: params.reservesCount,
oracle: params.priceOracle,
priceOracleSentinel: params.priceOracleSentinel,
isWhiteListed: false
priceOracleSentinel: params.priceOracleSentinel
})
);
}
Expand Down
32 changes: 19 additions & 13 deletions contracts/protocol/libraries/logic/SupplyLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,14 @@ library SupplyLogic {
DataTypes.TimeLockParams memory timeLockParams = GenericLogic
.calculateTimeLockParams(
reserve,
DataTypes.AssetType.ERC20,
params.asset,
amountToWithdraw,
DataTypes.TimeLockActionType.WITHDRAW,
params.isWhiteListed
DataTypes.TimeLockFactorParams({
assetType: DataTypes.AssetType.ERC20,
asset: params.asset,
amount: amountToWithdraw
})
);
timeLockParams.actionType = DataTypes.TimeLockActionType.WITHDRAW;

IPToken(reserveCache.xTokenAddress).burn(
msg.sender,
params.to,
Expand Down Expand Up @@ -443,19 +445,23 @@ library SupplyLogic {
DataTypes.ReserveData storage reserve,
DataTypes.ExecuteWithdrawERC721Params memory params
) internal returns (uint64, uint64) {
DataTypes.TimeLockParams memory timeLockParams = GenericLogic
.calculateTimeLockParams(
reserve,
DataTypes.TimeLockFactorParams({
assetType: DataTypes.AssetType.ERC721,
asset: params.asset,
amount: params.tokenIds.length
})
);
timeLockParams.actionType = DataTypes.TimeLockActionType.WITHDRAW;

return
INToken(xTokenAddress).burn(
msg.sender,
params.to,
params.tokenIds,
GenericLogic.calculateTimeLockParams(
reserve,
DataTypes.AssetType.ERC721,
params.asset,
params.tokenIds.length,
DataTypes.TimeLockActionType.WITHDRAW,
params.isWhiteListed
)
timeLockParams
);
}

Expand Down
5 changes: 0 additions & 5 deletions contracts/protocol/libraries/types/DataTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ library DataTypes {
uint256 reservesCount;
address oracle;
address priceOracleSentinel;
bool isWhiteListed;
}

struct ExecuteRepayParams {
Expand All @@ -187,7 +186,6 @@ library DataTypes {
address to;
uint256 reservesCount;
address oracle;
bool isWhiteListed;
}

struct ExecuteWithdrawERC721Params {
Expand All @@ -196,7 +194,6 @@ library DataTypes {
address to;
uint256 reservesCount;
address oracle;
bool isWhiteListed;
}

struct ExecuteDecreaseUniswapV3LiquidityParams {
Expand Down Expand Up @@ -409,8 +406,6 @@ library DataTypes {
uint16 _apeCompoundFee;
// Map of user's ape compound strategies
mapping(address => ApeCompoundStrategy) _apeCompoundStrategies;
// TimeLock whitelist
mapping(address => bool) _timeLockWhiteList;
}

struct ReserveConfigData {
Expand Down
3 changes: 1 addition & 2 deletions contracts/protocol/pool/PoolApeStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@ contract PoolApeStaking is
reservesCount: ps._reservesCount,
oracle: ADDRESSES_PROVIDER.getPriceOracle(),
priceOracleSentinel: ADDRESSES_PROVIDER
.getPriceOracleSentinel(),
isWhiteListed: false
.getPriceOracleSentinel()
})
);
}
Expand Down
3 changes: 1 addition & 2 deletions contracts/protocol/pool/PoolBorrowAndStake.sol
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ contract PoolBorrowAndStake is
reservesCount: ps._reservesCount,
oracle: ADDRESSES_PROVIDER.getPriceOracle(),
priceOracleSentinel: ADDRESSES_PROVIDER
.getPriceOracleSentinel(),
isWhiteListed: false
.getPriceOracleSentinel()
})
);
}
Expand Down
Loading

0 comments on commit 1e40d46

Please sign in to comment.