Skip to content

Commit

Permalink
fix: Replace rateMode with interestRateMode and precise dataype
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmtzinf committed Dec 17, 2021
1 parent 6a25662 commit 806f161
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 61 deletions.
42 changes: 23 additions & 19 deletions contracts/interfaces/IPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ interface IPool {
* initiator of the transaction on flashLoan()
* @param onBehalfOf The address that will be getting the debt
* @param amount The amount borrowed out
* @param borrowRateMode The rate mode: 1 for Stable, 2 for Variable
* @param borrowRate The numeric rate at which the user has borrowed
* @param interestRateMode The rate mode: 1 for Stable, 2 for Variable
* @param borrowRate The numeric rate at which the user has borrowed, expressed in ray
* @param referral The referral code used
**/
event Borrow(
address indexed reserve,
address user,
address indexed onBehalfOf,
uint256 amount,
uint256 borrowRateMode,
DataTypes.InterestRateMode interestRateMode,
uint256 borrowRate,
uint16 indexed referral
);
Expand All @@ -100,9 +100,13 @@ interface IPool {
* @notice Emitted on swapBorrowRateMode()
* @param reserve The address of the underlying asset of the reserve
* @param user The address of the user swapping his rate mode
* @param rateMode The current interest rate mode of the position being swapped.
* @param interestRateMode The current interest rate mode of the position being swapped: 1 for Stable, 2 for Variable
**/
event Swap(address indexed reserve, address indexed user, uint256 rateMode);
event Swap(
address indexed reserve,
address indexed user,
DataTypes.InterestRateMode interestRateMode
);

/**
* @notice Emitted on setUserUseReserveAsCollateral()
Expand Down Expand Up @@ -302,7 +306,7 @@ interface IPool {
function borrow(
address asset,
uint256 amount,
uint256 interestRateMode,
DataTypes.InterestRateMode interestRateMode,
uint16 referralCode,
address onBehalfOf
) external;
Expand All @@ -313,7 +317,7 @@ interface IPool {
* @param asset The address of the borrowed underlying asset previously borrowed
* @param amount The amount to repay
* - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
* @param rateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
* @param interestRateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
* @param onBehalfOf The address of the user who will get his debt reduced/removed. Should be the address of the
* user calling the function if he wants to reduce/remove his own debt, or the address of any other
* other borrower whose debt should be removed
Expand All @@ -322,7 +326,7 @@ interface IPool {
function repay(
address asset,
uint256 amount,
uint256 rateMode,
DataTypes.InterestRateMode interestRateMode,
address onBehalfOf
) external returns (uint256);

Expand All @@ -332,7 +336,7 @@ interface IPool {
* @param asset The address of the borrowed underlying asset previously borrowed
* @param amount The amount to repay
* - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
* @param rateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
* @param interestRateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
* @param onBehalfOf Address of the user who will get his debt reduced/removed. Should be the address of the
* user calling the function if he wants to reduce/remove his own debt, or the address of any other
* other borrower whose debt should be removed
Expand All @@ -345,7 +349,7 @@ interface IPool {
function repayWithPermit(
address asset,
uint256 amount,
uint256 rateMode,
DataTypes.InterestRateMode interestRateMode,
address onBehalfOf,
uint256 deadline,
uint8 permitV,
Expand All @@ -359,21 +363,21 @@ interface IPool {
* @param asset The address of the borrowed underlying asset previously borrowed
* @param amount The amount to repay
* - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
* @param rateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
* @param interestRateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
* @return The final amount repaid
**/
function repayWithATokens(
address asset,
uint256 amount,
uint256 rateMode
DataTypes.InterestRateMode interestRateMode
) external returns (uint256);

/**
* @notice Allows a borrower to swap his debt between stable and variable mode, or viceversa
* @param asset The address of the underlying asset borrowed
* @param rateMode The rate mode that the user wants to swap to
* @param interestRateMode The rate mode that the user wants to swap to: 1 for Stable, 2 for Variable
**/
function swapBorrowRateMode(address asset, uint256 rateMode) external;
function swapBorrowRateMode(address asset, DataTypes.InterestRateMode interestRateMode) external;

/**
* @notice Rebalances the stable interest rate of a user to the current stable rate defined on the reserve.
Expand Down Expand Up @@ -420,10 +424,10 @@ interface IPool {
* @param receiverAddress The address of the contract receiving the funds, implementing the IFlashLoanReceiver interface
* @param assets The addresses of the assets being flash-borrowed
* @param amounts The amounts of the assets being flash-borrowed
* @param modes Types of the debt to open if the flash loan is not returned:
* 0 -> Don't open any debt, just revert if funds can't be transferred from the receiver
* 1 -> Open debt at stable rate for the value of the amount flash-borrowed to the `onBehalfOf` address
* 2 -> Open debt at variable rate for the value of the amount flash-borrowed to the `onBehalfOf` address
* @param interestRateModes Types of the debt to open if the flash loan is not returned:
* 0: NONE -> Don't open any debt, just revert if funds can't be transferred from the receiver
* 1: STABLE -> Open debt at stable rate for the value of the amount flash-borrowed to the `onBehalfOf` address
* 2: VARIABLE -> Open debt at variable rate for the value of the amount flash-borrowed to the `onBehalfOf` address
* @param onBehalfOf The address that will receive the debt in the case of using on `modes` 1 or 2
* @param params Variadic packed params to pass to the receiver as extra information
* @param referralCode The code used to register the integrator originating the operation, for potential rewards.
Expand All @@ -433,7 +437,7 @@ interface IPool {
address receiverAddress,
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata modes,
DataTypes.InterestRateMode[] calldata interestRateModes,
address onBehalfOf,
bytes calldata params,
uint16 referralCode
Expand Down
2 changes: 1 addition & 1 deletion contracts/mocks/tests/FlashloanAttacker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract FlashloanAttacker is FlashLoanSimpleReceiverBase {
DataTypes.ReserveData memory config = _pool.getReserveData(asset);
IERC20 token = IERC20(asset);
uint256 avail = token.balanceOf(config.aTokenAddress);
_pool.borrow(asset, avail, 2, 0, address(this));
_pool.borrow(asset, avail, DataTypes.InterestRateMode.VARIABLE, 0, address(this));
}

function executeOperation(
Expand Down
27 changes: 14 additions & 13 deletions contracts/protocol/libraries/logic/BorrowLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ library BorrowLogic {
address user,
address indexed onBehalfOf,
uint256 amount,
uint256 borrowRateMode,
DataTypes.InterestRateMode interestRateMode,
uint256 borrowRate,
uint16 indexed referral
);
Expand All @@ -45,7 +45,11 @@ library BorrowLogic {
);

event RebalanceStableBorrowRate(address indexed reserve, address indexed user);
event Swap(address indexed reserve, address indexed user, uint256 rateMode);
event Swap(
address indexed reserve,
address indexed user,
DataTypes.InterestRateMode interestRateMode
);

/**
* @notice Implements the borrow feature. Borrowing allows users that provided collateral to draw liquidity from the
Expand Down Expand Up @@ -100,7 +104,7 @@ library BorrowLogic {
uint256 currentStableRate = 0;
bool isFirstBorrowing = false;

if (DataTypes.InterestRateMode(params.interestRateMode) == DataTypes.InterestRateMode.STABLE) {
if (params.interestRateMode == DataTypes.InterestRateMode.STABLE) {
currentStableRate = reserve.currentStableBorrowRate;

(
Expand Down Expand Up @@ -149,7 +153,7 @@ library BorrowLogic {
params.onBehalfOf,
params.amount,
params.interestRateMode,
DataTypes.InterestRateMode(params.interestRateMode) == DataTypes.InterestRateMode.STABLE
params.interestRateMode == DataTypes.InterestRateMode.STABLE
? currentStableRate
: reserve.currentVariableBorrowRate,
params.referralCode
Expand Down Expand Up @@ -177,7 +181,6 @@ library BorrowLogic {
DataTypes.ReserveCache memory reserveCache = reserve.cache();
reserve.updateState(reserveCache);

DataTypes.InterestRateMode interestRateMode = DataTypes.InterestRateMode(params.rateMode);
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(
params.onBehalfOf,
reserve
Expand All @@ -186,21 +189,21 @@ library BorrowLogic {
ValidationLogic.validateRepay(
reserveCache,
params.amount,
interestRateMode,
params.interestRateMode,
params.onBehalfOf,
stableDebt,
variableDebt
);

uint256 paybackAmount = interestRateMode == DataTypes.InterestRateMode.STABLE
uint256 paybackAmount = params.interestRateMode == DataTypes.InterestRateMode.STABLE
? stableDebt
: variableDebt;

if (params.amount < paybackAmount) {
paybackAmount = params.amount;
}

if (interestRateMode == DataTypes.InterestRateMode.STABLE) {
if (params.interestRateMode == DataTypes.InterestRateMode.STABLE) {
(reserveCache.nextTotalStableDebt, reserveCache.nextAvgStableBorrowRate) = IStableDebtToken(
reserveCache.stableDebtTokenAddress
).burn(params.onBehalfOf, paybackAmount);
Expand Down Expand Up @@ -292,22 +295,20 @@ library BorrowLogic {
* @param reserve The data of the reserve of the asset being repaid
* @param userConfig The user configuration mapping that tracks the supplied/borrowed assets
* @param asset The asset of the position being swapped
* @param rateMode The current interest rate mode of the position being swapped. If `rateMode == InterestRateMode.STABLE`, user must have stable debt
* @param interestRateMode The current interest rate mode of the position being swapped
*/
function executeSwapBorrowRateMode(
DataTypes.ReserveData storage reserve,
DataTypes.UserConfigurationMap storage userConfig,
address asset,
uint256 rateMode
DataTypes.InterestRateMode interestRateMode
) external {
DataTypes.ReserveCache memory reserveCache = reserve.cache();

reserve.updateState(reserveCache);

(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve);

DataTypes.InterestRateMode interestRateMode = DataTypes.InterestRateMode(rateMode);

ValidationLogic.validateSwapRateMode(
reserve,
reserveCache,
Expand Down Expand Up @@ -337,6 +338,6 @@ library BorrowLogic {

reserve.updateInterestRates(reserveCache, asset, 0, 0);

emit Swap(asset, msg.sender, rateMode);
emit Swap(asset, msg.sender, interestRateMode);
}
}
4 changes: 2 additions & 2 deletions contracts/protocol/libraries/logic/FlashLoanLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ library FlashLoanLogic {
vars.currentAsset = params.assets[vars.i];
vars.currentAmount = params.amounts[vars.i];

if (DataTypes.InterestRateMode(params.modes[vars.i]) == DataTypes.InterestRateMode.NONE) {
if (params.interestRateModes[vars.i] == DataTypes.InterestRateMode.NONE) {
vars.currentATokenAddress = vars.aTokenAddresses[vars.i];
vars.currentAmountPlusPremium = vars.currentAmount + vars.totalPremiums[vars.i];
vars.currentPremiumToProtocol = vars.currentAmount.percentMul(
Expand Down Expand Up @@ -176,7 +176,7 @@ library FlashLoanLogic {
user: msg.sender,
onBehalfOf: params.onBehalfOf,
amount: vars.currentAmount,
interestRateMode: params.modes[vars.i],
interestRateMode: params.interestRateModes[vars.i],
referralCode: params.referralCode,
releaseUnderlying: false,
maxStableRateBorrowSizePercent: params.maxStableRateBorrowSizePercent,
Expand Down
20 changes: 9 additions & 11 deletions contracts/protocol/libraries/logic/ValidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ library ValidationLogic {

//validate interest rate mode
require(
uint256(DataTypes.InterestRateMode.VARIABLE) == params.interestRateMode ||
uint256(DataTypes.InterestRateMode.STABLE) == params.interestRateMode,
params.interestRateMode == DataTypes.InterestRateMode.VARIABLE ||
params.interestRateMode == DataTypes.InterestRateMode.STABLE,
Errors.VL_INVALID_INTEREST_RATE_MODE_SELECTED
);

Expand Down Expand Up @@ -255,7 +255,7 @@ library ValidationLogic {
* 3. Users will be able to borrow only a portion of the total available liquidity
**/

if (params.interestRateMode == uint256(DataTypes.InterestRateMode.STABLE)) {
if (params.interestRateMode == DataTypes.InterestRateMode.STABLE) {
//check if the borrow mode is stable and if stable rate borrowing is enabled on this reserve

require(vars.stableRateBorrowingEnabled, Errors.VL_STABLE_BORROWING_NOT_ENABLED);
Expand Down Expand Up @@ -284,15 +284,15 @@ library ValidationLogic {
* @notice Validates a repay action
* @param reserveCache The cached data of the reserve
* @param amountSent The amount sent for the repayment. Can be an actual value or uint(-1)
* @param rateMode The interest rate mode of the debt being repaid
* @param interestRateMode The interest rate mode of the debt being repaid
* @param onBehalfOf The address of the user msg.sender is repaying for
* @param stableDebt The borrow balance of the user
* @param variableDebt The borrow balance of the user
*/
function validateRepay(
DataTypes.ReserveCache memory reserveCache,
uint256 amountSent,
DataTypes.InterestRateMode rateMode,
DataTypes.InterestRateMode interestRateMode,
address onBehalfOf,
uint256 stableDebt,
uint256 variableDebt
Expand All @@ -311,17 +311,15 @@ library ValidationLogic {

require(
(stableRatePreviousTimestamp < uint40(block.timestamp) &&
DataTypes.InterestRateMode(rateMode) == DataTypes.InterestRateMode.STABLE) ||
interestRateMode == DataTypes.InterestRateMode.STABLE) ||
(variableDebtPreviousIndex < reserveCache.nextVariableBorrowIndex &&
DataTypes.InterestRateMode(rateMode) == DataTypes.InterestRateMode.VARIABLE),
interestRateMode == DataTypes.InterestRateMode.VARIABLE),
Errors.VL_SAME_BLOCK_BORROW_REPAY
);

require(
(stableDebt > 0 &&
DataTypes.InterestRateMode(rateMode) == DataTypes.InterestRateMode.STABLE) ||
(variableDebt > 0 &&
DataTypes.InterestRateMode(rateMode) == DataTypes.InterestRateMode.VARIABLE),
(stableDebt > 0 && interestRateMode == DataTypes.InterestRateMode.STABLE) ||
(variableDebt > 0 && interestRateMode == DataTypes.InterestRateMode.VARIABLE),
Errors.VL_NO_DEBT_OF_SELECTED_TYPE
);

Expand Down
8 changes: 4 additions & 4 deletions contracts/protocol/libraries/types/DataTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ library DataTypes {
address user;
address onBehalfOf;
uint256 amount;
uint256 interestRateMode;
DataTypes.InterestRateMode interestRateMode;
uint16 referralCode;
bool releaseUnderlying;
uint256 maxStableRateBorrowSizePercent;
Expand All @@ -136,7 +136,7 @@ library DataTypes {
struct ExecuteRepayParams {
address asset;
uint256 amount;
uint256 rateMode;
DataTypes.InterestRateMode interestRateMode;
address onBehalfOf;
bool useATokens;
}
Expand Down Expand Up @@ -173,7 +173,7 @@ library DataTypes {
address receiverAddress;
address[] assets;
uint256[] amounts;
uint256[] modes;
DataTypes.InterestRateMode[] interestRateModes;
address onBehalfOf;
bytes params;
uint16 referralCode;
Expand Down Expand Up @@ -210,7 +210,7 @@ library DataTypes {
address asset;
address userAddress;
uint256 amount;
uint256 interestRateMode;
DataTypes.InterestRateMode interestRateMode;
uint256 maxStableLoanPercent;
uint256 reservesCount;
address oracle;
Expand Down
Loading

0 comments on commit 806f161

Please sign in to comment.