Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Compound error return (Spearbit #33) #1550

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compound/MorphoGovernance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ abstract contract MorphoGovernance is MorphoUtils {
/// @notice Thrown when the address is the zero address.
error ZeroAddress();

/// @notice Throws back Compound errors.
error CompoundError(uint256 errorCode);

/// UPGRADE ///

/// @notice Initializes the Morpho contract.
Expand Down Expand Up @@ -440,7 +443,7 @@ abstract contract MorphoGovernance is MorphoUtils {
address[] memory marketToEnter = new address[](1);
marketToEnter[0] = _poolToken;
uint256[] memory results = comptroller.enterMarkets(marketToEnter);
if (results[0] != 0) revert MarketCreationFailedOnCompound();
if (results[0] != 0) revert CompoundError(results[0]);

// Same initial index as Compound.
uint256 initialIndex;
Expand Down
18 changes: 14 additions & 4 deletions src/compound/PositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ contract PositionsManager is IPositionsManager, MatchingEngine {
/// @notice Thrown when someone tries to liquidate but the liquidation with this asset as debt is paused.
error LiquidateBorrowIsPaused();

/// @notice Throws back Compound errors.
error CompoundError(uint256 errorCode);

/// STRUCTS ///

// Struct to avoid stack too deep.
Expand Down Expand Up @@ -926,7 +929,8 @@ contract PositionsManager is IPositionsManager, MatchingEngine {
ICEther(_poolToken).mint{value: _amount}();
} else {
_underlyingToken.safeApprove(_poolToken, _amount);
if (ICToken(_poolToken).mint(_amount) != 0) revert MintOnCompoundFailed();
uint256 errorCode = ICToken(_poolToken).mint(_amount);
if (errorCode != 0) revert CompoundError(errorCode);
}
}

Expand All @@ -936,15 +940,20 @@ contract PositionsManager is IPositionsManager, MatchingEngine {
function _withdrawFromPool(address _poolToken, uint256 _amount) internal {
// Withdraw only what is possible. The remaining dust is taken from the contract balance.
_amount = CompoundMath.min(ICToken(_poolToken).balanceOfUnderlying(address(this)), _amount);
if (ICToken(_poolToken).redeemUnderlying(_amount) != 0) revert RedeemOnCompoundFailed();

uint256 errorCode = ICToken(_poolToken).redeemUnderlying(_amount);
if (errorCode != 0) revert CompoundError(errorCode);

if (_poolToken == cEth) IWETH(address(wEth)).deposit{value: _amount}(); // Turn the ETH received in wETH.
}

/// @dev Borrows underlying tokens from Compound.
/// @param _poolToken The address of the pool token.
/// @param _amount The amount of token (in underlying).
function _borrowFromPool(address _poolToken, uint256 _amount) internal {
if ((ICToken(_poolToken).borrow(_amount) != 0)) revert BorrowOnCompoundFailed();
uint256 errorCode = ICToken(_poolToken).borrow(_amount);
if (errorCode != 0) revert CompoundError(errorCode);

if (_poolToken == cEth) IWETH(address(wEth)).deposit{value: _amount}(); // Turn the ETH received in wETH.
}

Expand All @@ -969,7 +978,8 @@ contract PositionsManager is IPositionsManager, MatchingEngine {
ICEther(_poolToken).repayBorrow{value: _amount}();
} else {
_underlyingToken.safeApprove(_poolToken, _amount);
if (ICToken(_poolToken).repayBorrow(_amount) != 0) revert RepayOnCompoundFailed();
uint256 errorCode = ICToken(_poolToken).repayBorrow(_amount);
if (errorCode != 0) revert CompoundError(errorCode);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/compound/TestGovernance.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract TestGovernance is TestSetup {
function testShouldRevertWhenCreatingMarketWithAnImproperMarket() public {
Types.MarketParameters memory marketParams = Types.MarketParameters(3_333, 0);

hevm.expectRevert(abi.encodeWithSignature("MarketCreationFailedOnCompound()"));
hevm.expectRevert(abi.encodeWithSignature("CompoundError(uint256)", 9));
morpho.createMarket(address(supplier1), marketParams);
}

Expand Down