Skip to content

Commit

Permalink
Remove defi Loan logic from CompoundManager
Browse files Browse the repository at this point in the history
  • Loading branch information
elenadimitrova committed May 6, 2020
1 parent 1ec717e commit 96bf7bf
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 756 deletions.
190 changes: 0 additions & 190 deletions contracts/modules/CompoundManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ contract CompoundManager is BaseModule, RelayerModule, OnlyOwnerModule {

event InvestmentAdded(address indexed _wallet, address _token, uint256 _invested, uint256 _period);
event InvestmentRemoved(address indexed _wallet, address _token, uint256 _fraction);
event LoanOpened(
address indexed _wallet,
bytes32 indexed _loanId,
address _collateral,
uint256 _collateralAmount,
address _debtToken,
uint256 _debtAmount);
event LoanClosed(address indexed _wallet, bytes32 indexed _loanId);
event CollateralAdded(address indexed _wallet, bytes32 indexed _loanId, address _collateral, uint256 _collateralAmount);
event CollateralRemoved(address indexed _wallet, bytes32 indexed _loanId, address _collateral, uint256 _collateralAmount);
event DebtAdded(address indexed _wallet, bytes32 indexed _loanId, address _debtToken, uint256 _debtAmount);
event DebtRemoved(address indexed _wallet, bytes32 indexed _loanId, address _debtToken, uint256 _debtAmount);

using SafeMath for uint256;

Expand All @@ -87,184 +75,6 @@ contract CompoundManager is BaseModule, RelayerModule, OnlyOwnerModule {
compoundRegistry = _compoundRegistry;
}

/* ********************************** Implementation of Loan ************************************* */

/**
* @dev Opens a collateralized loan.
* @param _wallet The target wallet.
* @param _collateral The token used as a collateral.
* @param _collateralAmount The amount of collateral token provided.
* @param _debtToken The token borrowed.
* @param _debtAmount The amount of tokens borrowed.
* @return bytes32(0) as Compound does not allow the creation of multiple loans.
*/
function openLoan(
BaseWallet _wallet,
address _collateral,
uint256 _collateralAmount,
address _debtToken,
uint256 _debtAmount
)
external
onlyWalletOwner(_wallet)
onlyWhenUnlocked(_wallet)
returns (bytes32 _loanId)
{
address[] memory markets = new address[](2);
markets[0] = compoundRegistry.getCToken(_collateral);
markets[1] = compoundRegistry.getCToken(_debtToken);
invokeWallet(address(_wallet), address(comptroller), 0, abi.encodeWithSignature("enterMarkets(address[])", markets));
mint(_wallet, markets[0], _collateral, _collateralAmount);
borrow(_wallet, markets[1], _debtAmount);
emit LoanOpened(address(_wallet), _loanId, _collateral, _collateralAmount, _debtToken, _debtAmount);
}

/**
* @dev Closes the collateralized loan in all markets by repaying all debts (plus interest). Note that it does not redeem the collateral.
* @param _wallet The target wallet.
* @param _loanId bytes32(0) as Compound does not allow the creation of multiple loans.
*/
function closeLoan(
BaseWallet _wallet,
bytes32 _loanId
)
external
onlyWalletOwner(_wallet)
onlyWhenUnlocked(_wallet)
{
address[] memory markets = comptroller.getAssetsIn(address(_wallet));
for (uint i = 0; i < markets.length; i++) {
address cToken = markets[i];
uint debt = ICToken(cToken).borrowBalanceCurrent(address(_wallet));
if (debt > 0) {
repayBorrow(_wallet, cToken, debt);
uint collateral = ICToken(cToken).balanceOf(address(_wallet));
if (collateral == 0) {
invokeWallet(address(_wallet), address(comptroller), 0, abi.encodeWithSignature("exitMarket(address)", address(cToken)));
}
}
}
emit LoanClosed(address(_wallet), _loanId);
}

/**
* @dev Adds collateral to a loan identified by its ID.
* @param _wallet The target wallet.
* @param _loanId bytes32(0) as Compound does not allow the creation of multiple loans.
* @param _collateral The token used as a collateral.
* @param _collateralAmount The amount of collateral to add.
*/
function addCollateral(
BaseWallet _wallet,
bytes32 _loanId,
address _collateral,
uint256 _collateralAmount
)
external
onlyWalletOwner(_wallet)
onlyWhenUnlocked(_wallet)
{
address cToken = compoundRegistry.getCToken(_collateral);
enterMarketIfNeeded(_wallet, cToken, address(comptroller));
mint(_wallet, cToken, _collateral, _collateralAmount);
emit CollateralAdded(address(_wallet), _loanId, _collateral, _collateralAmount);
}

/**
* @dev Removes collateral from a loan identified by its ID.
* @param _wallet The target wallet.
* @param _loanId bytes32(0) as Compound does not allow the creation of multiple loans.
* @param _collateral The token used as a collateral.
* @param _collateralAmount The amount of collateral to remove.
*/
function removeCollateral(
BaseWallet _wallet,
bytes32 _loanId,
address _collateral,
uint256 _collateralAmount
)
external
onlyWalletOwner(_wallet)
onlyWhenUnlocked(_wallet)
{
address cToken = compoundRegistry.getCToken(_collateral);
redeemUnderlying(_wallet, cToken, _collateralAmount);
exitMarketIfNeeded(_wallet, cToken, address(comptroller));
emit CollateralRemoved(address(_wallet), _loanId, _collateral, _collateralAmount);
}

/**
* @dev Increases the debt by borrowing more token from a loan identified by its ID.
* @param _wallet The target wallet.
* @param _loanId bytes32(0) as Compound does not allow the creation of multiple loans.
* @param _debtToken The token borrowed.
* @param _debtAmount The amount of token to borrow.
*/
function addDebt(
BaseWallet _wallet,
bytes32 _loanId,
address _debtToken,
uint256 _debtAmount
)
external
onlyWalletOwner(_wallet)
onlyWhenUnlocked(_wallet)
{
address dToken = compoundRegistry.getCToken(_debtToken);
enterMarketIfNeeded(_wallet, dToken, address(comptroller));
borrow(_wallet, dToken, _debtAmount);
emit DebtAdded(address(_wallet), _loanId, _debtToken, _debtAmount);
}

/**
* @dev Decreases the debt by repaying some token from a loan identified by its ID.
* @param _wallet The target wallet.
* @param _loanId bytes32(0) as Compound does not allow the creation of multiple loans.
* @param _debtToken The token to repay.
* @param _debtAmount The amount of token to repay.
*/
function removeDebt(
BaseWallet _wallet,
bytes32 _loanId,
address _debtToken,
uint256 _debtAmount
)
external
onlyWalletOwner(_wallet)
onlyWhenUnlocked(_wallet)
{
address dToken = compoundRegistry.getCToken(_debtToken);
repayBorrow(_wallet, dToken, _debtAmount);
exitMarketIfNeeded(_wallet, dToken, address(comptroller));
emit DebtRemoved(address(_wallet), _loanId, _debtToken, _debtAmount);
}

/**
* @dev Gets information about the loan status on Compound.
* @param _wallet The target wallet.
* @return a status [0: no loan, 1: loan is safe, 2: loan is unsafe and can be liquidated]
* and a value (in ETH) representing the value that could still be borrowed when status = 1; or the value of the collateral
* that should be added to avoid liquidation when status = 2.
*/
function getLoan(
BaseWallet _wallet,
bytes32 /* _loanId */
)
external
view
returns (uint8 _status, uint256 _ethValue)
{
(uint error, uint liquidity, uint shortfall) = comptroller.getAccountLiquidity(address(_wallet));
require(error == 0, "Compound: failed to get account liquidity");
if (liquidity > 0) {
return (1, liquidity);
}
if (shortfall > 0) {
return (2, shortfall);
}
return (0,0);
}

/* ********************************** Implementation of Invest ************************************* */

/**
Expand Down
Loading

0 comments on commit 96bf7bf

Please sign in to comment.