LCOV - code coverage report
Current view: top level - helpers - L2Encoder.sol (source / functions) Coverage Total Hit
Test: lcov.info.p Lines: 98.3 % 58 57
Test Date: 2024-09-24 09:34:24 Functions: 90.0 % 10 9
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: MIT
       2                 :             : pragma solidity ^0.8.10;
       3                 :             : 
       4                 :             : import {SafeCast} from '../dependencies/openzeppelin/contracts/SafeCast.sol';
       5                 :             : import {IPool} from '../interfaces/IPool.sol';
       6                 :             : import {DataTypes} from '../protocol/libraries/types/DataTypes.sol';
       7                 :             : 
       8                 :             : /**
       9                 :             :  * @title L2Encoder
      10                 :             :  * @author Aave
      11                 :             :  * @notice Helper contract to encode calldata, used to optimize calldata size in L2Pool for transaction cost reduction
      12                 :             :  * only indented to help generate calldata for uses/frontends.
      13                 :             :  */
      14                 :             : contract L2Encoder {
      15                 :             :   using SafeCast for uint256;
      16                 :             :   IPool public immutable POOL;
      17                 :             : 
      18                 :             :   /**
      19                 :             :    * @dev Constructor.
      20                 :             :    * @param pool The address of the Pool contract
      21                 :             :    */
      22                 :             :   constructor(IPool pool) {
      23                 :           0 :     POOL = pool;
      24                 :             :   }
      25                 :             : 
      26                 :             :   /**
      27                 :             :    * @notice Encodes supply parameters from standard input to compact representation of 1 bytes32
      28                 :             :    * @dev Without an onBehalfOf parameter as the compact calls to L2Pool will use msg.sender as onBehalfOf
      29                 :             :    * @param asset The address of the underlying asset to supply
      30                 :             :    * @param amount The amount to be supplied
      31                 :             :    * @param referralCode referralCode Code used to register the integrator originating the operation, for potential rewards.
      32                 :             :    *   0 if the action is executed directly by the user, without any middle-man
      33                 :             :    * @return compact representation of supply parameters
      34                 :             :    */
      35                 :             :   function encodeSupplyParams(
      36                 :             :     address asset,
      37                 :             :     uint256 amount,
      38                 :             :     uint16 referralCode
      39                 :             :   ) external view returns (bytes32) {
      40                 :           7 :     DataTypes.ReserveDataLegacy memory data = POOL.getReserveData(asset);
      41                 :             : 
      42                 :           7 :     uint16 assetId = data.id;
      43                 :           7 :     uint128 shortenedAmount = amount.toUint128();
      44                 :           7 :     bytes32 res;
      45                 :             : 
      46                 :             :     assembly {
      47                 :           7 :       res := add(assetId, add(shl(16, shortenedAmount), shl(144, referralCode)))
      48                 :             :     }
      49                 :           7 :     return res;
      50                 :             :   }
      51                 :             : 
      52                 :             :   /**
      53                 :             :    * @notice Encodes supplyWithPermit parameters from standard input to compact representation of 3 bytes32
      54                 :             :    * @dev Without an onBehalfOf parameter as the compact calls to L2Pool will use msg.sender as onBehalfOf
      55                 :             :    * @param asset The address of the underlying asset to supply
      56                 :             :    * @param amount The amount to be supplied
      57                 :             :    * @param referralCode referralCode Code used to register the integrator originating the operation, for potential rewards.
      58                 :             :    *   0 if the action is executed directly by the user, without any middle-man
      59                 :             :    * @param deadline The deadline timestamp that the permit is valid
      60                 :             :    * @param permitV The V parameter of ERC712 permit sig
      61                 :             :    * @param permitR The R parameter of ERC712 permit sig
      62                 :             :    * @param permitS The S parameter of ERC712 permit sig
      63                 :             :    * @return compact representation of supplyWithPermit parameters
      64                 :             :    * @return The R parameter of ERC712 permit sig
      65                 :             :    * @return The S parameter of ERC712 permit sig
      66                 :             :    */
      67                 :             :   function encodeSupplyWithPermitParams(
      68                 :             :     address asset,
      69                 :             :     uint256 amount,
      70                 :             :     uint16 referralCode,
      71                 :             :     uint256 deadline,
      72                 :             :     uint8 permitV,
      73                 :             :     bytes32 permitR,
      74                 :             :     bytes32 permitS
      75                 :             :   ) external view returns (bytes32, bytes32, bytes32) {
      76                 :        1000 :     DataTypes.ReserveDataLegacy memory data = POOL.getReserveData(asset);
      77                 :             : 
      78                 :        1000 :     uint16 assetId = data.id;
      79                 :        1000 :     uint128 shortenedAmount = amount.toUint128();
      80                 :        1000 :     uint32 shortenedDeadline = deadline.toUint32();
      81                 :             : 
      82                 :        1000 :     bytes32 res;
      83                 :             :     assembly {
      84                 :        1000 :       res := add(
      85                 :             :         assetId,
      86                 :             :         add(
      87                 :             :           shl(16, shortenedAmount),
      88                 :             :           add(shl(144, referralCode), add(shl(160, shortenedDeadline), shl(192, permitV)))
      89                 :             :         )
      90                 :             :       )
      91                 :             :     }
      92                 :             : 
      93                 :        1000 :     return (res, permitR, permitS);
      94                 :             :   }
      95                 :             : 
      96                 :             :   /**
      97                 :             :    * @notice Encodes withdraw parameters from standard input to compact representation of 1 bytes32
      98                 :             :    * @dev Without a to parameter as the compact calls to L2Pool will use msg.sender as to
      99                 :             :    * @param asset The address of the underlying asset to withdraw
     100                 :             :    * @param amount The underlying amount to be withdrawn
     101                 :             :    * @return compact representation of withdraw parameters
     102                 :             :    */
     103                 :             :   function encodeWithdrawParams(address asset, uint256 amount) external view returns (bytes32) {
     104                 :           2 :     DataTypes.ReserveDataLegacy memory data = POOL.getReserveData(asset);
     105                 :             : 
     106                 :           2 :     uint16 assetId = data.id;
     107                 :           2 :     uint128 shortenedAmount = amount == type(uint256).max ? type(uint128).max : amount.toUint128();
     108                 :             : 
     109                 :           2 :     bytes32 res;
     110                 :             :     assembly {
     111                 :           2 :       res := add(assetId, shl(16, shortenedAmount))
     112                 :             :     }
     113                 :           2 :     return res;
     114                 :             :   }
     115                 :             : 
     116                 :             :   /**
     117                 :             :    * @notice Encodes borrow parameters from standard input to compact representation of 1 bytes32
     118                 :             :    * @dev Without an onBehalfOf parameter as the compact calls to L2Pool will use msg.sender as onBehalfOf
     119                 :             :    * @param asset The address of the underlying asset to borrow
     120                 :             :    * @param amount The amount to be borrowed
     121                 :             :    * @param interestRateMode The interest rate mode at which the user wants to borrow: 2 for Variable, 1 is deprecated (changed on v3.2.0)
     122                 :             :    * @param referralCode The code used to register the integrator originating the operation, for potential rewards.
     123                 :             :    *   0 if the action is executed directly by the user, without any middle-man
     124                 :             :    * @return compact representation of withdraw parameters
     125                 :             :    */
     126                 :             :   function encodeBorrowParams(
     127                 :             :     address asset,
     128                 :             :     uint256 amount,
     129                 :             :     uint256 interestRateMode,
     130                 :             :     uint16 referralCode
     131                 :             :   ) external view returns (bytes32) {
     132                 :           3 :     DataTypes.ReserveDataLegacy memory data = POOL.getReserveData(asset);
     133                 :             : 
     134                 :           3 :     uint16 assetId = data.id;
     135                 :           3 :     uint128 shortenedAmount = amount.toUint128();
     136                 :           3 :     uint8 shortenedInterestRateMode = interestRateMode.toUint8();
     137                 :           3 :     bytes32 res;
     138                 :             :     assembly {
     139                 :           3 :       res := add(
     140                 :             :         assetId,
     141                 :             :         add(
     142                 :             :           shl(16, shortenedAmount),
     143                 :             :           add(shl(144, shortenedInterestRateMode), shl(152, referralCode))
     144                 :             :         )
     145                 :             :       )
     146                 :             :     }
     147                 :           3 :     return res;
     148                 :             :   }
     149                 :             : 
     150                 :             :   /**
     151                 :             :    * @notice Encodes repay parameters from standard input to compact representation of 1 bytes32
     152                 :             :    * @dev Without an onBehalfOf parameter as the compact calls to L2Pool will use msg.sender as onBehalfOf
     153                 :             :    * @param asset The address of the borrowed underlying asset previously borrowed
     154                 :             :    * @param amount The amount to repay
     155                 :             :    * - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `interestRateMode`
     156                 :             :    * @param interestRateMode The interest rate mode at of the debt the user wants to repay: 2 for Variable, 1 is deprecated (changed on v3.2.0)
     157                 :             :    * @return compact representation of repay parameters
     158                 :             :    */
     159                 :             :   function encodeRepayParams(
     160                 :             :     address asset,
     161                 :             :     uint256 amount,
     162                 :             :     uint256 interestRateMode
     163                 :             :   ) public view returns (bytes32) {
     164                 :           2 :     DataTypes.ReserveDataLegacy memory data = POOL.getReserveData(asset);
     165                 :             : 
     166                 :           2 :     uint16 assetId = data.id;
     167                 :           2 :     uint128 shortenedAmount = amount == type(uint256).max ? type(uint128).max : amount.toUint128();
     168                 :           2 :     uint8 shortenedInterestRateMode = interestRateMode.toUint8();
     169                 :             : 
     170                 :           2 :     bytes32 res;
     171                 :             :     assembly {
     172                 :           2 :       res := add(assetId, add(shl(16, shortenedAmount), shl(144, shortenedInterestRateMode)))
     173                 :             :     }
     174                 :           2 :     return res;
     175                 :             :   }
     176                 :             : 
     177                 :             :   /**
     178                 :             :    * @notice Encodes repayWithPermit parameters from standard input to compact representation of 3 bytes32
     179                 :             :    * @dev Without an onBehalfOf parameter as the compact calls to L2Pool will use msg.sender as onBehalfOf
     180                 :             :    * @param asset The address of the borrowed underlying asset previously borrowed
     181                 :             :    * @param amount The amount to repay
     182                 :             :    * - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
     183                 :             :    * @param interestRateMode The interest rate mode at of the debt the user wants to repay: 2 for Variable, 1 is deprecated (changed on v3.2.0)
     184                 :             :    * @param deadline The deadline timestamp that the permit is valid
     185                 :             :    * @param permitV The V parameter of ERC712 permit sig
     186                 :             :    * @param permitR The R parameter of ERC712 permit sig
     187                 :             :    * @param permitS The S parameter of ERC712 permit sig
     188                 :             :    * @return compact representation of repayWithPermit parameters
     189                 :             :    * @return The R parameter of ERC712 permit sig
     190                 :             :    * @return The S parameter of ERC712 permit sig
     191                 :             :    */
     192                 :             :   function encodeRepayWithPermitParams(
     193                 :             :     address asset,
     194                 :             :     uint256 amount,
     195                 :             :     uint256 interestRateMode,
     196                 :             :     uint256 deadline,
     197                 :             :     uint8 permitV,
     198                 :             :     bytes32 permitR,
     199                 :             :     bytes32 permitS
     200                 :             :   ) external view returns (bytes32, bytes32, bytes32) {
     201                 :        1000 :     DataTypes.ReserveDataLegacy memory data = POOL.getReserveData(asset);
     202                 :             : 
     203                 :        1000 :     uint16 assetId = data.id;
     204                 :        1000 :     uint128 shortenedAmount = amount == type(uint256).max ? type(uint128).max : amount.toUint128();
     205                 :        1000 :     uint8 shortenedInterestRateMode = interestRateMode.toUint8();
     206                 :        1000 :     uint32 shortenedDeadline = deadline.toUint32();
     207                 :             : 
     208                 :        1000 :     bytes32 res;
     209                 :             :     assembly {
     210                 :        1000 :       res := add(
     211                 :             :         assetId,
     212                 :             :         add(
     213                 :             :           shl(16, shortenedAmount),
     214                 :             :           add(
     215                 :             :             shl(144, shortenedInterestRateMode),
     216                 :             :             add(shl(152, shortenedDeadline), shl(184, permitV))
     217                 :             :           )
     218                 :             :         )
     219                 :             :       )
     220                 :             :     }
     221                 :        1000 :     return (res, permitR, permitS);
     222                 :             :   }
     223                 :             : 
     224                 :             :   /**
     225                 :             :    * @notice Encodes repay with aToken parameters from standard input to compact representation of 1 bytes32
     226                 :             :    * @param asset The address of the borrowed underlying asset previously borrowed
     227                 :             :    * @param amount The amount to repay
     228                 :             :    * - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
     229                 :             :    * @param interestRateMode The interest rate mode at of the debt the user wants to repay: 2 for Variable, 1 is deprecated  (changed on v3.2.0)
     230                 :             :    * @return compact representation of repay with aToken parameters
     231                 :             :    */
     232                 :             :   function encodeRepayWithATokensParams(
     233                 :             :     address asset,
     234                 :             :     uint256 amount,
     235                 :             :     uint256 interestRateMode
     236                 :             :   ) external view returns (bytes32) {
     237                 :           1 :     return encodeRepayParams(asset, amount, interestRateMode);
     238                 :             :   }
     239                 :             : 
     240                 :             :   /**
     241                 :             :    * @notice Encodes set user use reserve as collateral parameters from standard input to compact representation of 1 bytes32
     242                 :             :    * @param asset The address of the underlying asset borrowed
     243                 :             :    * @param useAsCollateral True if the user wants to use the supply as collateral, false otherwise
     244                 :             :    * @return compact representation of set user use reserve as collateral parameters
     245                 :             :    */
     246                 :             :   function encodeSetUserUseReserveAsCollateral(
     247                 :             :     address asset,
     248                 :             :     bool useAsCollateral
     249                 :             :   ) external view returns (bytes32) {
     250                 :           1 :     DataTypes.ReserveDataLegacy memory data = POOL.getReserveData(asset);
     251                 :           1 :     uint16 assetId = data.id;
     252                 :           1 :     bytes32 res;
     253                 :             :     assembly {
     254                 :           1 :       res := add(assetId, shl(16, useAsCollateral))
     255                 :             :     }
     256                 :           1 :     return res;
     257                 :             :   }
     258                 :             : 
     259                 :             :   /**
     260                 :             :    * @notice Encodes liquidation call parameters from standard input to compact representation of 2 bytes32
     261                 :             :    * @param collateralAsset The address of the underlying asset used as collateral, to receive as result of the liquidation
     262                 :             :    * @param debtAsset The address of the underlying borrowed asset to be repaid with the liquidation
     263                 :             :    * @param user The address of the borrower getting liquidated
     264                 :             :    * @param debtToCover The debt amount of borrowed `asset` the liquidator wants to cover
     265                 :             :    * @param receiveAToken True if the liquidators wants to receive the collateral aTokens, `false` if he wants
     266                 :             :    * to receive the underlying collateral asset directly
     267                 :             :    * @return First half ot compact representation of liquidation call parameters
     268                 :             :    * @return Second half ot compact representation of liquidation call parameters
     269                 :             :    */
     270                 :             :   function encodeLiquidationCall(
     271                 :             :     address collateralAsset,
     272                 :             :     address debtAsset,
     273                 :             :     address user,
     274                 :             :     uint256 debtToCover,
     275                 :             :     bool receiveAToken
     276                 :             :   ) external view returns (bytes32, bytes32) {
     277                 :           1 :     DataTypes.ReserveDataLegacy memory collateralData = POOL.getReserveData(collateralAsset);
     278                 :           1 :     uint16 collateralAssetId = collateralData.id;
     279                 :             : 
     280                 :           1 :     DataTypes.ReserveDataLegacy memory debtData = POOL.getReserveData(debtAsset);
     281                 :           1 :     uint16 debtAssetId = debtData.id;
     282                 :             : 
     283                 :           1 :     uint128 shortenedDebtToCover = debtToCover == type(uint256).max
     284                 :             :       ? type(uint128).max
     285                 :             :       : debtToCover.toUint128();
     286                 :             : 
     287                 :           1 :     bytes32 res1;
     288                 :           1 :     bytes32 res2;
     289                 :             : 
     290                 :             :     assembly {
     291                 :           1 :       res1 := add(add(collateralAssetId, shl(16, debtAssetId)), shl(32, user))
     292                 :           1 :       res2 := add(shortenedDebtToCover, shl(128, receiveAToken))
     293                 :             :     }
     294                 :           1 :     return (res1, res2);
     295                 :             :   }
     296                 :             : }
        

Generated by: LCOV version 2.1-1