morpho-import

This commit is contained in:
Richa-iitr 2022-10-29 19:09:25 +05:30
parent 0f9e04b0c9
commit 51821bf036
16 changed files with 692 additions and 6 deletions

View File

@ -2,9 +2,9 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
import "./interface.sol";
import "../../common/stores.sol";
import "../../common/basic.sol";
import "../../common/interfaces.sol";
import "../../../common/stores.sol";
import "../../../common/basic.sol";
import "../../../common/interfaces.sol";
abstract contract Helpers is Stores, Basic {
IMorphoCore public constant MORPHO_AAVE =

View File

@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2;
contract Events {
event LogMorphoCompoundImport(
address indexed user,
address[] ctokens,
string[] supplyIds,
string[] borrowIds,
uint256[] supplyAmts,
uint256[] borrowAmts
);
}

View File

@ -0,0 +1,202 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
import { DSMath } from "../../../common/math.sol";
import { Basic } from "../../../common/basic.sol";
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
import { ComptrollerInterface, CompoundMappingInterface, CETHInterface, CTokenInterface } from "./interface.sol";
abstract contract Helpers is DSMath, Basic {
/**
* @dev Compound CEth
*/
CETHInterface internal constant cEth =
CETHInterface(0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5);
/**
* @dev Compound Comptroller
*/
ComptrollerInterface internal constant troller =
ComptrollerInterface(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B);
/**
* @dev Compound Mapping
*/
IMorphoLens internal constant morphoLens =
IMorphoLens(0x930f1b46e1D081Ec1524efD95752bE3eCe51EF67);
IMorpho internal constant morpho = IMorpho(0x8888882f8f843896699869179fB6E4f7e3B58888)
struct ImportData {
uint256[] borrowAmts;
uint256[] supplyAmts;
address[] borrowTokens;
address[] supplyTokens;
CTokenInterface[] borrowCtokens;
CTokenInterface[] supplyCtokens;
address[] supplyCtokensAddr;
address[] borrowCtokensAddr;
}
struct ImportInputData {
address userAccount;
address[] supplyCTokens;
address[] borrowCTokens;
uint256[] flashLoanFees;
}
}
contract MorphoCompoundHelper is Helpers {
/**
* @notice fetch the borrow details of the user
* @dev approve the cToken to spend (borrowed amount of) tokens to allow for repaying later
* @param _importInputData the struct containing borrowIds of the users borrowed tokens
* @param data struct used to store the final data on which the CompoundHelper contract functions operate
* @return ImportData the final value of param data
*/
function getBorrowAmounts(
ImportInputData memory importInputData_,
ImportData memory data
) internal returns (ImportData memory) {
if (importInputData_.borrowCTokens.length > 0) {
// initialize arrays for borrow data
uint256 length_ = importInputData_.borrowCTokens.length;
data.borrowTokens = new address[](_length);
data.borrowCtokens = new CTokenInterface[](_length);
data.borrowCtokensAddr = new address[](_length);
data.borrowAmts = new uint256[](_length);
// populate the arrays with borrow tokens, cToken addresses and instances, and borrow amounts
for (uint256 i; i < _length; i++) {
address cToken_ = importInputData_.borrowCTokens[i];
CTokenInterface ctoken_ = CTokenInterface(cToken_);
address token_;
token_ = cToken_ == address(cEth) ? wethAddr : ctoken_.underlying();
require(token_ != address(0), "invalid-ctoken-address");
data.borrowTokens[i] = token_;
data.borrowCtokens[i] = ctoken_;
data.borrowCtokensAddr[i] = cToken_;
(, , data.borrowAmts[i]) = morphoLens
.getCurrentBorrowBalanceInOf(
cToken_,
importInputData_.userAccount
);
// give the morpho approval to spend tokens
if (token_ != ethAddr && data.borrowAmts[i] > 0) {
// will be required when repaying the borrow amount on behalf of the user
TokenInterface(token_).approve(address(morpho), data.borrowAmts[i]);
}
}
}
return data;
}
/**
* @notice fetch the supply details of the user
* @dev only reads data from blockchain hence view
* @param _importInputData the struct containing supplyIds of the users supplied tokens
* @param data struct used to store the final data on which the CompoundHelper contract functions operate
* @return ImportData the final value of param data
*/
function getSupplyAmounts(
ImportInputData memory importInputData_,
ImportData memory data
) internal view returns (ImportData memory) {
// initialize arrays for supply data
uint256 length_ = importInputData_.supplyCTokens.length;
data.supplyTokens = new address[](_length);
data.supplyCtokens = new CTokenInterface[](_length);
data.supplyCtokensAddr = new address[](_length);
data.supplyAmts = new uint256[](_length);
// populate arrays with supply data (supply tokens address, cToken addresses, cToken instances and supply amounts)
for (uint256 i; i < _length; i++) {
address cToken_ = importInputData_.supplyCTokens[i];
CTokenInterface ctoken_ = CTokenInterface(cToken_);
address token_ ;
token_ = cToken_ == address(cEth) ? wethAddr : ctoken_.underlying();
require(
token_ != address(0),
"invalid-ctoken-address"
);
data.supplyTokens[i] = token_;
data.supplyCtokens[i] = ctoken_;
data.supplyCtokensAddr[i] = (cToken_);
data.supplyAmts[i] = morpho.getCurrentSupplyBalanceInOf(
cToken_,
importInputData_.userAccount
);
}
return data;
}
/**
* @notice repays the debt taken by user on Compound on its behalf to free its collateral for transfer
* @dev uses the cEth contract for ETH repays, otherwise the general cToken interface
* @param _userAccount the user address for which debt is to be repayed
* @param _cTokenContracts array containing all interfaces to the cToken contracts in which the user has debt positions
* @param _borrowAmts array containing the amount borrowed for each token
*/
function _paybackDebt(
address userAccount_,
address[] memory cTokens_,
uint256[] memory borrowAmts_
) internal {
uint256 length_ = cTokens.length;
for (uint256 i; i < length; ++i) {
if (borrowAmts_[i] > 0) {
morpho.repay(cTokens[i], userAccount_, borrowAmts_[i]);
}
}
}
/**
* @notice used to transfer user's supply position on Compound to DSA
* @dev uses the transferFrom token in cToken contracts to transfer positions, requires approval from user first
* @param _userAccount address of the user account whose position is to be transferred
* @param _cTokenContracts array containing all interfaces to the cToken contracts in which the user has supply positions
* @param _amts array containing the amount supplied for each token
*/
function _transferTokensToDsa(
address userAccount_,
CTokenInterface[] memory cTokenContracts_,
uint256[] memory supplyAmts_
) internal {
uint256 length_ = cTokens_.length;
for (uint256 i; i < length_; ++i)
if (supplyAmts_[i] > 0)
require(
cTokenContracts_[i].transferFrom(
userAccount_,
address(this),
supplyAmts_[i]
),
"ctoken-transfer-failed-allowance?"
);
}
/**
* @notice borrows the user's debt positions from Compound via DSA, so that its debt positions get imported to DSA
* @dev actually borrow some extra amount than the original position to cover the flash loan fee
* @param _cTokenContracts array containing all interfaces to the cToken contracts in which the user has debt positions
* @param _amts array containing the amounts the user had borrowed originally from Compound plus the flash loan fee
* @param _flashLoanFees flash loan fee (in percentage and scaled up to 10**2)
*/
function _borrowDebtPosition(
address[] memory cTokens_,
uint256[] memory borrowAmts_,
uint256[] memory flashLoanFees_
) internal {
uint256 length_ = cTokens_.length;
for (uint256 i; i < length_; ++i)
if (borrowAmts_[i] > 0)
morpho.borrow(cTokens_[i], add(borrowAmts_[i], flashLoanFees_[i]));
}
}

View File

@ -0,0 +1,382 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
interface TokenInterface {
function balanceOf(address) external view returns (uint256);
function allowance(address, address) external view returns (uint256);
function approve(address, uint256) external;
function transfer(address, uint256) external returns (bool);
function transferFrom(
address,
address,
uint256
) external returns (bool);
}
interface CTokenInterface {
function underlying() external view returns (address);
function mint(uint256 mintAmount) external returns (uint256);
function redeem(uint256 redeemTokens) external returns (uint256);
function borrow(uint256 borrowAmount) external returns (uint256);
function repayBorrow(uint256 repayAmount) external returns (uint256);
function repayBorrowBehalf(address borrower, uint256 repayAmount)
external
returns (uint256); // For ERC20
function liquidateBorrow(
address borrower,
uint256 repayAmount,
address cTokenCollateral
) external returns (uint256);
function borrowBalanceCurrent(address account) external returns (uint256);
function redeemUnderlying(uint256 redeemAmount) external returns (uint256);
function exchangeRateCurrent() external returns (uint256);
function balanceOf(address owner) external view returns (uint256 balance);
function transferFrom(
address,
address,
uint256
) external returns (bool);
function allowance(address, address) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
}
interface IMorphoLens {
function MAX_BASIS_POINTS() external view returns (uint256);
function WAD() external view returns (uint256);
function morpho() external view returns (IMorpho);
function comptroller() external view returns (IComptroller);
function getTotalSupply()
external
view
returns (
uint256 p2pSupplyAmount,
uint256 poolSupplyAmount,
uint256 totalSupplyAmount
);
function getTotalBorrow()
external
view
returns (
uint256 p2pBorrowAmount,
uint256 poolBorrowAmount,
uint256 totalBorrowAmount
);
function isMarketCreated(address _poolToken) external view returns (bool);
function isMarketCreatedAndNotPaused(address _poolToken)
external
view
returns (bool);
function isMarketCreatedAndNotPausedNorPartiallyPaused(address _poolToken)
external
view
returns (bool);
function getAllMarkets()
external
view
returns (address[] memory marketsCreated_);
function getMainMarketData(address _poolToken)
external
view
returns (
uint256 avgSupplyRatePerBlock,
uint256 avgBorrowRatePerBlock,
uint256 p2pSupplyAmount,
uint256 p2pBorrowAmount,
uint256 poolSupplyAmount,
uint256 poolBorrowAmount
);
function getTotalMarketSupply(address _poolToken)
external
view
returns (uint256 p2pSupplyAmount, uint256 poolSupplyAmount);
function getTotalMarketBorrow(address _poolToken)
external
view
returns (uint256 p2pBorrowAmount, uint256 poolBorrowAmount);
function getCurrentP2PSupplyIndex(address _poolToken)
external
view
returns (uint256);
function getCurrentP2PBorrowIndex(address _poolToken)
external
view
returns (uint256);
function getCurrentPoolIndexes(address _poolToken)
external
view
returns (
uint256 currentPoolSupplyIndex,
uint256 currentPoolBorrowIndex
);
function getIndexes(address _poolToken, bool _computeUpdatedIndexes)
external
view
returns (
uint256 p2pSupplyIndex,
uint256 p2pBorrowIndex,
uint256 poolSupplyIndex,
uint256 poolBorrowIndex
);
function getEnteredMarkets(address _user)
external
view
returns (address[] memory enteredMarkets);
function getUserMaxCapacitiesForAsset(address _user, address _poolToken)
external
view
returns (uint256 withdrawable, uint256 borrowable);
function getUserHypotheticalBalanceStates(
address _user,
address _poolToken,
uint256 _withdrawnAmount,
uint256 _borrowedAmount
) external view returns (uint256 debtValue, uint256 maxDebtValue);
function getUserLiquidityDataForAsset(
address _user,
address _poolToken,
bool _computeUpdatedIndexes,
ICompoundOracle _oracle
) external view returns (AssetLiquidityData memory assetData);
function computeLiquidationRepayAmount(
address _user,
address _poolTokenBorrowed,
address _poolTokenCollateral,
address[] calldata _updatedMarkets
) external view returns (uint256 toRepay);
function getAverageSupplyRatePerBlock(address _poolToken)
external
view
returns (uint256);
function getAverageBorrowRatePerBlock(address _poolToken)
external
view
returns (uint256);
function getNextUserSupplyRatePerBlock(
address _poolToken,
address _user,
uint256 _amount
)
external
view
returns (
uint256 nextSupplyRatePerBlock,
uint256 balanceOnPool,
uint256 balanceInP2P,
uint256 totalBalance
);
function getNextUserBorrowRatePerBlock(
address _poolToken,
address _user,
uint256 _amount
)
external
view
returns (
uint256 nextBorrowRatePerBlock,
uint256 balanceOnPool,
uint256 balanceInP2P,
uint256 totalBalance
);
function getMarketConfiguration(address _poolToken)
external
view
returns (
address underlying,
bool isCreated,
bool p2pDisabled,
bool isPaused,
bool isPartiallyPaused,
uint16 reserveFactor,
uint16 p2pIndexCursor,
uint256 collateralFactor
);
function getRatesPerBlock(address _poolToken)
external
view
returns (
uint256 p2pSupplyRate,
uint256 p2pBorrowRate,
uint256 poolSupplyRate,
uint256 poolBorrowRate
);
function getAdvancedMarketData(address _poolToken)
external
view
returns (
uint256 p2pSupplyIndex,
uint256 p2pBorrowIndex,
uint256 poolSupplyIndex,
uint256 poolBorrowIndex,
uint32 lastUpdateBlockNumber,
uint256 p2pSupplyDelta,
uint256 p2pBorrowDelta
);
function getCurrentSupplyBalanceInOf(address _poolToken, address _user)
external
view
returns (
uint256 balanceOnPool,
uint256 balanceInP2P,
uint256 totalBalance
);
function getCurrentBorrowBalanceInOf(address _poolToken, address _user)
external
view
returns (
uint256 balanceOnPool,
uint256 balanceInP2P,
uint256 totalBalance
);
function getUserBalanceStates(
address _user,
address[] calldata _updatedMarkets
)
external
view
returns (
uint256 collateralValue,
uint256 debtValue,
uint256 maxDebtValue
);
function getAccruedSupplierComp(
address _supplier,
address _poolToken,
uint256 _balance
) external view returns (uint256);
function getAccruedBorrowerComp(
address _borrower,
address _poolToken,
uint256 _balance
) external view returns (uint256);
function getCurrentCompSupplyIndex(address _poolToken)
external
view
returns (uint256);
function getCurrentCompBorrowIndex(address _poolToken)
external
view
returns (uint256);
function getUserUnclaimedRewards(
address[] calldata _poolTokens,
address _user
) external view returns (uint256 unclaimedRewards);
function isLiquidatable(address _user, address[] memory _updatedMarkets)
external
view
returns (bool);
function getCurrentUserSupplyRatePerBlock(address _poolToken, address _user)
external
view
returns (uint256);
function getCurrentUserBorrowRatePerBlock(address _poolToken, address _user)
external
view
returns (uint256);
function getUserHealthFactor(
address _user,
address[] calldata _updatedMarkets
) external view returns (uint256);
}
interface CETHInterface {
function mint() external payable;
function repayBorrow() external payable;
function repayBorrowBehalf(address borrower) external payable;
function liquidateBorrow(address borrower, address cTokenCollateral)
external
payable;
}
interface ComptrollerInterface {
function enterMarkets(address[] calldata cTokens)
external
returns (uint256[] memory);
function exitMarket(address cTokenAddress) external returns (uint256);
function getAssetsIn(address account)
external
view
returns (address[] memory);
function getAccountLiquidity(address account)
external
view
returns (
uint256,
uint256,
uint256
);
}
interface CompoundMappingInterface {
function cTokenMapping(string calldata tokenId)
external
view
returns (address);
function getMapping(string calldata tokenId)
external
view
returns (address, address);
}

View File

@ -0,0 +1,88 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2;
/**
* @title Morpho-Compound-Import.
* @dev Lending & Borrowing.
*/
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
import { MorphoCompoundHelper } from "./helpers.sol";
import { Events } from "./events.sol";
contract MorphoCompoundImportResolver is MorphoCompoundHelper {
function _importMorphoCompound(
address userAccount_
ImportInputData memory importInputData_
) internal returns (string memory eventName_, bytes memory eventParam_) {
require(
AccountInterface(address(this)).isAuth(
userAccount_
),
"user-account-not-auth"
);
require(importInputData_.supplyCTokens.length > 0, "0-length-not-allowed");
ImportData memory data;
// get info about all borrowings and lendings by the user on Morpho-Compound
data = getBorrowAmounts(importInputData_, data);
data = getSupplyAmounts(importInputData_, data);
// pay back user's debt using flash loan funds
_paybackDebt(
importInputData_.userAccount,
data.borrowCtokens,
data.borrowAmts
);
// transfer user's tokens to DSA
_transferTokensToDsa(
importInputData_.userAccount,
data.supplyCtokens,
data.supplyAmts
);
// borrow the earlier position from Compound with flash loan fee added
_borrowDebtPosition(
data.borrowCtokens,
data.borrowAmts,
_flashLoanFees
);
_eventName = "LogCompoundImport(address,address[],address[],uint256[],uint256[])";
_eventParam = abi.encode(
importInputData_.userAccount,
importInputData_.supplyCTokens,
importInputData_.borrowCTokens,
data.supplyAmts,
data.borrowAmts
);
}
/**
* @notice import Morpho-Compound position of the address passed in as userAccount
* @dev Import EOA's morpho-compound position to DSA's morpho-compound position
* @param userAccount The address of the EOA from which aave position will be imported
* @param inputData The struct containing all the neccessary input data
*/
function importMorphoCompound(
address userAccount_,
ImportInputData memory inputData_
)
external
payable
returns (string memory eventName_, bytes memory eventParam_)
{
(_eventName, _eventParam) = _importMorphoCompound(
userAccount_,
inputData_
);
}
}
contract ConnectV2MorphoCompoundImport is MorphoCompoundImportResolver {
string public constant name = "Morpho-Compound-Import-v2";
}

View File

@ -2,9 +2,9 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
import "./interface.sol";
import "../../common/stores.sol";
import "../../common/basic.sol";
import "../../common/interfaces.sol";
import "../../../common/stores.sol";
import "../../../common/basic.sol";
import "../../../common/interfaces.sol";
abstract contract Helpers is Stores, Basic {
IMorphoCore public constant MORPHO_COMPOUND =