code refactor

This commit is contained in:
Richa-iitr 2022-06-13 09:08:01 +05:30
parent 7b002f9704
commit a715112abc
6 changed files with 39 additions and 45 deletions

View File

@ -52,5 +52,30 @@ contract DSMath {
function toRad(uint wad) internal pure returns (uint rad) { function toRad(uint wad) internal pure returns (uint rad) {
rad = mul(wad, 10 ** 27); 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);
}
} }

View File

@ -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( function getMsgValue(
uint16 currencyId, uint16 currencyId,
bool useUnderlying, bool useUnderlying,

View File

@ -2,21 +2,21 @@
pragma solidity ^0.7.6; pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
// import { Helpers } from "./helpers.sol"; import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.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 { TokenInterface } from "../../common/interfaces.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.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; using SafeERC20 for IERC20;
enum Protocol { enum Protocol {
AaveV2, AAVEV2,
AaveV3, AAVEV3,
Compound, COMPOUND,
Notional NOTIONAL
} }
uint256 internal constant LEND_TRADE = 0; uint256 internal constant LEND_TRADE = 0;
@ -116,26 +116,6 @@ contract Helpers is Basic {
underlying ? underlyingToken.tokenAddress : assetToken.tokenAddress; 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) function getAaveV2PaybackAmt(uint256 rateMode, address token)
internal internal
returns (uint256 bal) returns (uint256 bal)

View File

@ -3,9 +3,8 @@ pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
import { Helpers } from "../helpers.sol"; import { Helpers } from "../helpers.sol";
import { AaveV3DataProviderInterface, AaveV3PoolProviderInterface, AaveV3Interface } from "../interface.sol";
import { TokenInterface } from "../../../common/interfaces.sol"; import { TokenInterface } from "../../../common/interfaces.sol";
import "../interface.sol";
contract AaveV3Helpers is Helpers { contract AaveV3Helpers is Helpers {
// payback token to Aave V2, amts sent already checked for MAX // payback token to Aave V2, amts sent already checked for MAX

View File

@ -3,7 +3,7 @@ pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
import { Helpers } from "../helpers.sol"; import { Helpers } from "../helpers.sol";
import { Token, NotionalInterface, BalanceAction, BalanceActionWithTrades, DepositActionType, AaveV2DataProviderInterface } from "../interface.sol"; import "../interface.sol";
contract NotionalHelpers is Helpers { contract NotionalHelpers is Helpers {
function _notionalBorrowOne( function _notionalBorrowOne(
@ -52,12 +52,12 @@ contract NotionalHelpers is Helpers {
//calculating payback amounts for Aave v2 //calculating payback amounts for Aave v2
if (_amt == uint256(-1)) { if (_amt == uint256(-1)) {
if (data.source == Protocol.AaveV2) if (data.source == Protocol.AAVEV2)
_amt = getAaveV2PaybackAmt( _amt = getAaveV2PaybackAmt(
data.rateModes[i], data.rateModes[i],
data.tokens[i] data.tokens[i]
); );
else if(data.source == Protocol.AaveV3) else if (data.source == Protocol.AAVEV3)
_amt = getAaveV3PaybackAmt( _amt = getAaveV3PaybackAmt(
data.rateModes[i], data.rateModes[i],
data.tokens[i] data.tokens[i]

View File

@ -6,11 +6,11 @@ pragma experimental ABIEncoderV2;
* @title Refinance. * @title Refinance.
* @dev Refinancing among Notional, Aave v2, Aave v3. * @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 { TokenInterface } from "../../common/interfaces.sol";
import { AaveV2Helpers } from "./helpers/aaveV2.sol"; import { AaveV2Helpers } from "./helpers/aaveV2.sol";
import { AaveV3Helpers } from "./helpers/aaveV3.sol"; import { AaveV3Helpers } from "./helpers/aaveV3.sol";
import { NotionalHelpers } from "./helpers/notional.sol"; import { NotionalHelpers } from "./helpers/notional.sol";
import "./interface.sol";
contract RefinanceResolver is AaveV2Helpers, AaveV3Helpers, NotionalHelpers { contract RefinanceResolver is AaveV2Helpers, AaveV3Helpers, NotionalHelpers {
struct RefinanceData { struct RefinanceData {
@ -79,7 +79,7 @@ contract RefinanceResolver is AaveV2Helpers, AaveV3Helpers, NotionalHelpers {
// Aave v2 to Notional // Aave v2 to Notional
if ( if (
data.source == Protocol.AaveV2 && data.target == Protocol.Notional data.source == Protocol.AAVEV2 && data.target == Protocol.NOTIONAL
) { ) {
NotionalBorrowData memory _notionalBorrowData; NotionalBorrowData memory _notionalBorrowData;
@ -128,7 +128,7 @@ contract RefinanceResolver is AaveV2Helpers, AaveV3Helpers, NotionalHelpers {
} }
// Aave v3 to Notional // Aave v3 to Notional
else if ( else if (
data.source == Protocol.AaveV3 && data.target == Protocol.Notional data.source == Protocol.AAVEV3 && data.target == Protocol.NOTIONAL
) { ) {
NotionalBorrowData memory _notionalBorrowData; NotionalBorrowData memory _notionalBorrowData;