Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/3.0.1' into feat/723-optimi…
Browse files Browse the repository at this point in the history
…zation
  • Loading branch information
eboadom committed Nov 14, 2022
2 parents bb8e2ac + cac49b4 commit bab0eab
Show file tree
Hide file tree
Showing 15 changed files with 644 additions and 320 deletions.
2 changes: 1 addition & 1 deletion contracts/interfaces/IACLManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ interface IACLManager {
function addFlashBorrower(address borrower) external;

/**
* @notice Removes an admin as FlashBorrower
* @notice Removes an address as FlashBorrower
* @param borrower The address of the FlashBorrower to remove
*/
function removeFlashBorrower(address borrower) external;
Expand Down
7 changes: 4 additions & 3 deletions contracts/interfaces/IPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ interface IPool {
event MintedToTreasury(address indexed reserve, uint256 amountMinted);

/**
* @dev Mints an `amount` of aTokens to the `onBehalfOf`
* @notice Mints an `amount` of aTokens to the `onBehalfOf`
* @param asset The address of the underlying asset to mint
* @param amount The amount to mint
* @param onBehalfOf The address that will receive the aTokens
Expand All @@ -226,16 +226,17 @@ interface IPool {
) external;

/**
* @dev Back the current unbacked underlying with `amount` and pay `fee`.
* @notice Back the current unbacked underlying with `amount` and pay `fee`.
* @param asset The address of the underlying asset to back
* @param amount The amount to back
* @param fee The amount paid in fees
* @return The backed amount
**/
function backUnbacked(
address asset,
uint256 amount,
uint256 fee
) external;
) external returns (uint256);

/**
* @notice Supplies an `amount` of underlying asset into the reserve, receiving in return overlying aTokens.
Expand Down
1 change: 0 additions & 1 deletion contracts/protocol/libraries/helpers/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ library Errors {
string public constant HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '45'; // 'Health factor is not below the threshold'
string public constant COLLATERAL_CANNOT_BE_LIQUIDATED = '46'; // 'The collateral chosen cannot be liquidated'
string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '47'; // 'User did not borrow the specified currency'
string public constant SAME_BLOCK_BORROW_REPAY = '48'; // 'Borrow and repay in same block is not allowed'
string public constant INCONSISTENT_FLASHLOAN_PARAMS = '49'; // 'Inconsistent flashloan parameters'
string public constant BORROW_CAP_EXCEEDED = '50'; // 'Borrow cap is exceeded'
string public constant SUPPLY_CAP_EXCEEDED = '51'; // 'Supply cap is exceeded'
Expand Down
6 changes: 5 additions & 1 deletion contracts/protocol/libraries/logic/BridgeLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,22 @@ library BridgeLogic {

/**
* @notice Back the current unbacked with `amount` and pay `fee`.
* @dev It is not possible to back more than the existing unbacked amount of the reserve
* @dev Emits the `BackUnbacked` event
* @param reserve The reserve to back unbacked for
* @param asset The address of the underlying asset to repay
* @param amount The amount to back
* @param fee The amount paid in fees
* @param protocolFeeBps The fraction of fees in basis points paid to the protocol
* @return The backed amount
**/
function executeBackUnbacked(
DataTypes.ReserveData storage reserve,
address asset,
uint256 amount,
uint256 fee,
uint256 protocolFeeBps
) external {
) external returns (uint256) {
DataTypes.ReserveCache memory reserveCache = reserve.cache();

reserve.updateState(reserveCache);
Expand All @@ -137,5 +139,7 @@ library BridgeLogic {
IERC20(asset).safeTransferFrom(msg.sender, reserveCache.aTokenAddress, added);

emit BackUnbacked(asset, msg.sender, backingAmount, fee);

return backingAmount;
}
}
16 changes: 8 additions & 8 deletions contracts/protocol/libraries/logic/LiquidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ library LiquidationLogic {
userConfig.setBorrowing(debtReserve.id, false);
}

// If the collateral being liquidated is equal to the user balance,
// we set the currency as not being used as collateral anymore
if (vars.actualCollateralToLiquidate == vars.userCollateralBalance) {
userConfig.setUsingAsCollateral(collateralReserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.collateralAsset, params.user);
}

_burnDebtTokens(params, vars);

debtReserve.updateInterestRates(
Expand Down Expand Up @@ -197,14 +204,7 @@ library LiquidationLogic {
vars.liquidationProtocolFeeAmount
);
}

// If the collateral being liquidated is equal to the user balance,
// we set the currency as not being used as collateral anymore
if (vars.actualCollateralToLiquidate == vars.userCollateralBalance) {
userConfig.setUsingAsCollateral(collateralReserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.collateralAsset, params.user);
}


// Transfers the debt asset being repaid to the aToken, where the liquidity is kept
IERC20(params.debtAsset).safeTransferFrom(
msg.sender,
Expand Down
30 changes: 17 additions & 13 deletions contracts/protocol/libraries/logic/ReserveLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ library ReserveLogic {
DataTypes.ReserveData storage reserve,
DataTypes.ReserveCache memory reserveCache
) internal {
//only cumulating if there is any income being produced
// Only cumulating on the supply side if there is any income being produced
// The case of Reserve Factor 100% is not a problem (currentLiquidityRate == 0),
// as liquidity index should not be updated
if (reserveCache.currLiquidityRate != 0) {
uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest(
reserveCache.currLiquidityRate,
Expand All @@ -302,19 +304,21 @@ library ReserveLogic {
reserveCache.currLiquidityIndex
);
reserve.liquidityIndex = reserveCache.nextLiquidityIndex.toUint128();
}

//as the liquidity rate might come only from stable rate loans, we need to ensure
//that there is actual variable debt before accumulating
if (reserveCache.currScaledVariableDebt != 0) {
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserveCache.currVariableBorrowRate,
reserveCache.reserveLastUpdateTimestamp
);
reserveCache.nextVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(
reserveCache.currVariableBorrowIndex
);
reserve.variableBorrowIndex = reserveCache.nextVariableBorrowIndex.toUint128();
}
// Variable borrow index only gets updated if there is any variable debt.
// reserveCache.currVariableBorrowRate != 0 is not a correct validation,
// because a positive base variable rate can be stored on
// reserveCache.currVariableBorrowRate, but the index should not increase
if (reserveCache.currScaledVariableDebt != 0) {
uint256 cumulatedVariableBorrowInterest = MathUtils.calculateCompoundedInterest(
reserveCache.currVariableBorrowRate,
reserveCache.reserveLastUpdateTimestamp
);
reserveCache.nextVariableBorrowIndex = cumulatedVariableBorrowInterest.rayMul(
reserveCache.currVariableBorrowIndex
);
reserve.variableBorrowIndex = reserveCache.nextVariableBorrowIndex.toUint128();
}
}

Expand Down
45 changes: 25 additions & 20 deletions contracts/protocol/libraries/logic/SupplyLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,32 +128,32 @@ library SupplyLogic {

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

bool isCollateral = userConfig.isUsingAsCollateral(reserve.id);

if (isCollateral && amountToWithdraw == userBalance) {
userConfig.setUsingAsCollateral(reserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.asset, msg.sender);
}

IAToken(reserveCache.aTokenAddress).burn(
msg.sender,
params.to,
amountToWithdraw,
reserveCache.nextLiquidityIndex
);

if (userConfig.isUsingAsCollateral(reserve.id)) {
if (userConfig.isBorrowingAny()) {
ValidationLogic.validateHFAndLtv(
reservesData,
reservesList,
eModeCategories,
userConfig,
params.asset,
msg.sender,
params.reservesCount,
params.oracle,
params.userEModeCategory
);
}

if (amountToWithdraw == userBalance) {
userConfig.setUsingAsCollateral(reserve.id, false);
emit ReserveUsedAsCollateralDisabled(params.asset, msg.sender);
}
if (isCollateral && userConfig.isBorrowingAny()) {
ValidationLogic.validateHFAndLtv(
reservesData,
reservesList,
eModeCategories,
userConfig,
params.asset,
msg.sender,
params.reservesCount,
params.oracle,
params.userEModeCategory
);
}

emit Withdraw(params.asset, msg.sender, params.to, amountToWithdraw);
Expand Down Expand Up @@ -264,7 +264,12 @@ library SupplyLogic {

if (useAsCollateral) {
require(
ValidationLogic.validateUseAsCollateral(reservesData, reservesList, userConfig, reserveCache.reserveConfiguration),
ValidationLogic.validateUseAsCollateral(
reservesData,
reservesList,
userConfig,
reserveCache.reserveConfiguration
),
Errors.USER_IN_ISOLATION_MODE
);

Expand Down
14 changes: 0 additions & 14 deletions contracts/protocol/libraries/logic/ValidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,6 @@ library ValidationLogic {
require(isActive, Errors.RESERVE_INACTIVE);
require(!isPaused, Errors.RESERVE_PAUSED);

uint256 variableDebtPreviousIndex = IScaledBalanceToken(reserveCache.variableDebtTokenAddress)
.getPreviousIndex(onBehalfOf);

uint40 stableRatePreviousTimestamp = IStableDebtToken(reserveCache.stableDebtTokenAddress)
.getUserLastUpdated(onBehalfOf);

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

require(
(stableDebt != 0 && interestRateMode == DataTypes.InterestRateMode.STABLE) ||
(variableDebt != 0 && interestRateMode == DataTypes.InterestRateMode.VARIABLE),
Expand Down
5 changes: 3 additions & 2 deletions contracts/protocol/pool/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ contract Pool is VersionedInitializable, PoolStorage, IPool {
address asset,
uint256 amount,
uint256 fee
) external virtual override onlyBridge {
BridgeLogic.executeBackUnbacked(_reserves[asset], asset, amount, fee, _bridgeProtocolFee);
) external virtual override onlyBridge returns (uint256) {
return
BridgeLogic.executeBackUnbacked(_reserves[asset], asset, amount, fee, _bridgeProtocolFee);
}

/// @inheritdoc IPool
Expand Down
4 changes: 2 additions & 2 deletions contracts/protocol/tokenization/AToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ contract AToken is VersionedInitializable, ScaledBalanceTokenBase, EIP712Base, I
}

/// @inheritdoc IAToken
function mintToTreasury(uint256 amount, uint256 index) external override onlyPool {
function mintToTreasury(uint256 amount, uint256 index) external virtual override onlyPool {
if (amount == 0) {
return;
}
Expand All @@ -119,7 +119,7 @@ contract AToken is VersionedInitializable, ScaledBalanceTokenBase, EIP712Base, I
address from,
address to,
uint256 value
) external override onlyPool {
) external virtual override onlyPool {
// Being a normal transfer, the Transfer() and BalanceTransfer() are emitted
// so no need to emit a specific event here
_transfer(from, to, value, false);
Expand Down
1 change: 0 additions & 1 deletion helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export enum ProtocolErrors {
HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '45', // 'Health factor is not below the threshold'
COLLATERAL_CANNOT_BE_LIQUIDATED = '46', // 'The collateral chosen cannot be liquidated'
SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '47', // 'User did not borrow the specified currency'
SAME_BLOCK_BORROW_REPAY = '48', // 'Borrow and repay in same block is not allowed'
INCONSISTENT_FLASHLOAN_PARAMS = '49', // 'Inconsistent flashloan parameters'
BORROW_CAP_EXCEEDED = '50', // 'Borrow cap is exceeded'
SUPPLY_CAP_EXCEEDED = '51', // 'Supply cap is exceeded'
Expand Down
Loading

0 comments on commit bab0eab

Please sign in to comment.