From a715112abc5f9da732f31da5301887f851bbf0ea Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Mon, 13 Jun 2022 09:08:01 +0530 Subject: [PATCH] code refactor --- contracts/mainnet/common/math.sol | 25 ++++++++++++++ .../mainnet/connectors/notional/helpers.sol | 10 ------ .../connectors/refinance-notional/helpers.sol | 34 ++++--------------- .../refinance-notional/helpers/aaveV3.sol | 3 +- .../refinance-notional/helpers/notional.sol | 6 ++-- .../connectors/refinance-notional/main.sol | 6 ++-- 6 files changed, 39 insertions(+), 45 deletions(-) diff --git a/contracts/mainnet/common/math.sol b/contracts/mainnet/common/math.sol index 72ccd542..9a2a010f 100644 --- a/contracts/mainnet/common/math.sol +++ b/contracts/mainnet/common/math.sol @@ -52,5 +52,30 @@ contract DSMath { function toRad(uint wad) internal pure returns (uint rad) { rad = mul(wad, 10 ** 27); } + + function toUint96(uint256 value) internal pure returns (uint96) { + require(value <= type(uint96).max, "uint96 value overflow"); + return uint96(value); + } + + function toUint88(uint256 value) internal pure returns (uint88) { + require(value <= type(uint88).max, "uint88-overflow"); + return uint88(value); + } + + function toUint32(uint256 value) internal pure returns (uint32) { + require(value <= type(uint32).max, "uint32-overflow"); + return uint32(value); + } + + function toUint16(uint256 value) internal pure returns (uint16) { + require(value <= type(uint16).max, "uint16-overflow"); + return uint16(value); + } + + function toUint8(uint256 value) internal pure returns (uint8) { + require(value <= type(uint8).max, "uint8-overflow"); + return uint8(value); + } } diff --git a/contracts/mainnet/connectors/notional/helpers.sol b/contracts/mainnet/connectors/notional/helpers.sol index b551eb27..1cac54fc 100644 --- a/contracts/mainnet/connectors/notional/helpers.sol +++ b/contracts/mainnet/connectors/notional/helpers.sol @@ -71,16 +71,6 @@ abstract contract Helpers is DSMath, Basic { } } - function toUint96(uint256 value) internal pure returns (uint96) { - require(value <= type(uint96).max, "uint96 value overflow"); - return uint96(value); - } - - function toUint88(uint256 value) internal pure returns (uint88) { - require(value <= type(uint88).max, "uint88 value overflow"); - return uint88(value); - } - function getMsgValue( uint16 currencyId, bool useUnderlying, diff --git a/contracts/mainnet/connectors/refinance-notional/helpers.sol b/contracts/mainnet/connectors/refinance-notional/helpers.sol index 2b44a63c..f1a3e2ac 100644 --- a/contracts/mainnet/connectors/refinance-notional/helpers.sol +++ b/contracts/mainnet/connectors/refinance-notional/helpers.sol @@ -2,21 +2,21 @@ pragma solidity ^0.7.6; pragma experimental ABIEncoderV2; -// import { Helpers } from "./helpers.sol"; +import { DSMath } from "../../common/math.sol"; import { Basic } from "../../common/basic.sol"; -import { Token, NotionalInterface, BalanceAction, BalanceActionWithTrades, DepositActionType, AaveV2LendingPoolProviderInterface, AaveV2DataProviderInterface, AaveV2Interface, AaveV3PoolProviderInterface, AaveV3Interface, AaveV3DataProviderInterface } from "./interface.sol"; import { TokenInterface } from "../../common/interfaces.sol"; import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import "./interface.sol"; -contract Helpers is Basic { +contract Helpers is DSMath, Basic { using SafeERC20 for IERC20; enum Protocol { - AaveV2, - AaveV3, - Compound, - Notional + AAVEV2, + AAVEV3, + COMPOUND, + NOTIONAL } uint256 internal constant LEND_TRADE = 0; @@ -116,26 +116,6 @@ contract Helpers is Basic { underlying ? underlyingToken.tokenAddress : assetToken.tokenAddress; } - function toUint88(uint256 value) internal pure returns (uint88) { - require(value <= type(uint88).max, "uint88 value overflow"); - return uint88(value); - } - - function toUint32(uint256 value) internal pure returns (uint32) { - require(value <= type(uint32).max, "uint32 value overflow"); - return uint32(value); - } - - function toUint16(uint256 value) internal pure returns (uint16) { - require(value <= type(uint16).max, "uint16 value overflow"); - return uint16(value); - } - - function toUint8(uint256 value) internal pure returns (uint8) { - require(value <= type(uint8).max, "uint8 value overflow"); - return uint8(value); - } - function getAaveV2PaybackAmt(uint256 rateMode, address token) internal returns (uint256 bal) diff --git a/contracts/mainnet/connectors/refinance-notional/helpers/aaveV3.sol b/contracts/mainnet/connectors/refinance-notional/helpers/aaveV3.sol index ef967c35..87cf5cf3 100644 --- a/contracts/mainnet/connectors/refinance-notional/helpers/aaveV3.sol +++ b/contracts/mainnet/connectors/refinance-notional/helpers/aaveV3.sol @@ -3,9 +3,8 @@ pragma solidity ^0.7.6; pragma experimental ABIEncoderV2; import { Helpers } from "../helpers.sol"; - -import { AaveV3DataProviderInterface, AaveV3PoolProviderInterface, AaveV3Interface } from "../interface.sol"; import { TokenInterface } from "../../../common/interfaces.sol"; +import "../interface.sol"; contract AaveV3Helpers is Helpers { // payback token to Aave V2, amts sent already checked for MAX diff --git a/contracts/mainnet/connectors/refinance-notional/helpers/notional.sol b/contracts/mainnet/connectors/refinance-notional/helpers/notional.sol index 10c0bbd6..2a7bcede 100644 --- a/contracts/mainnet/connectors/refinance-notional/helpers/notional.sol +++ b/contracts/mainnet/connectors/refinance-notional/helpers/notional.sol @@ -3,7 +3,7 @@ pragma solidity ^0.7.6; pragma experimental ABIEncoderV2; import { Helpers } from "../helpers.sol"; -import { Token, NotionalInterface, BalanceAction, BalanceActionWithTrades, DepositActionType, AaveV2DataProviderInterface } from "../interface.sol"; +import "../interface.sol"; contract NotionalHelpers is Helpers { function _notionalBorrowOne( @@ -52,12 +52,12 @@ contract NotionalHelpers is Helpers { //calculating payback amounts for Aave v2 if (_amt == uint256(-1)) { - if (data.source == Protocol.AaveV2) + if (data.source == Protocol.AAVEV2) _amt = getAaveV2PaybackAmt( data.rateModes[i], data.tokens[i] ); - else if(data.source == Protocol.AaveV3) + else if (data.source == Protocol.AAVEV3) _amt = getAaveV3PaybackAmt( data.rateModes[i], data.tokens[i] diff --git a/contracts/mainnet/connectors/refinance-notional/main.sol b/contracts/mainnet/connectors/refinance-notional/main.sol index 13b36d1e..b21eaf71 100644 --- a/contracts/mainnet/connectors/refinance-notional/main.sol +++ b/contracts/mainnet/connectors/refinance-notional/main.sol @@ -6,11 +6,11 @@ pragma experimental ABIEncoderV2; * @title Refinance. * @dev Refinancing among Notional, Aave v2, Aave v3. */ -import { AaveV2Interface, AaveV2DataProviderInterface, AaveV2LendingPoolProviderInterface, AaveV3Interface, AaveV3DataProviderInterface, AaveV3PoolProviderInterface } from "./interface.sol"; import { TokenInterface } from "../../common/interfaces.sol"; import { AaveV2Helpers } from "./helpers/aaveV2.sol"; import { AaveV3Helpers } from "./helpers/aaveV3.sol"; import { NotionalHelpers } from "./helpers/notional.sol"; +import "./interface.sol"; contract RefinanceResolver is AaveV2Helpers, AaveV3Helpers, NotionalHelpers { struct RefinanceData { @@ -79,7 +79,7 @@ contract RefinanceResolver is AaveV2Helpers, AaveV3Helpers, NotionalHelpers { // Aave v2 to Notional if ( - data.source == Protocol.AaveV2 && data.target == Protocol.Notional + data.source == Protocol.AAVEV2 && data.target == Protocol.NOTIONAL ) { NotionalBorrowData memory _notionalBorrowData; @@ -128,7 +128,7 @@ contract RefinanceResolver is AaveV2Helpers, AaveV3Helpers, NotionalHelpers { } // Aave v3 to Notional else if ( - data.source == Protocol.AaveV3 && data.target == Protocol.Notional + data.source == Protocol.AAVEV3 && data.target == Protocol.NOTIONAL ) { NotionalBorrowData memory _notionalBorrowData;