dsa-connectors-2.0/contracts/mainnet/connectors/morpho-blue/helpers.sol

142 lines
4.6 KiB
Solidity
Raw Permalink Normal View History

2023-12-16 12:33:50 +00:00
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
2023-12-16 12:33:50 +00:00
2023-12-16 12:38:07 +00:00
import {Id, IMorpho, MarketParams, Position, Market} from "./interfaces/IMorpho.sol";
2023-12-16 12:33:50 +00:00
import "../../common/stores.sol";
import "../../common/basic.sol";
import "../../common/interfaces.sol";
2023-12-16 12:38:07 +00:00
import {MorphoBalancesLib} from "./libraries/periphery/MorphoBalancesLib.sol";
2024-01-10 18:04:11 +00:00
import {MorphoLib} from "./libraries/periphery/MorphoLib.sol";
2023-12-16 12:38:07 +00:00
import {UtilsLib} from "./libraries/UtilsLib.sol";
import {MarketParamsLib} from "./libraries/MarketParamsLib.sol";
2023-12-16 13:30:43 +00:00
import {SharesMathLib} from "./libraries/SharesMathLib.sol";
2023-12-16 12:33:50 +00:00
abstract contract Helpers is Stores, Basic {
2023-12-16 12:38:07 +00:00
using MorphoBalancesLib for IMorpho;
2024-01-10 18:04:11 +00:00
using MorphoLib for IMorpho;
2023-12-16 12:38:07 +00:00
using MarketParamsLib for MarketParams;
using UtilsLib for uint256;
2023-12-16 13:30:43 +00:00
using SharesMathLib for uint256;
2023-12-16 12:38:07 +00:00
IMorpho public constant MORPHO_BLUE =
2024-01-13 23:34:33 +00:00
IMorpho(0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb);
2023-12-16 12:38:07 +00:00
/// @notice Handles Eth to Weth conversion if assets are provided.
function _performEthToWethConversion(
MarketParams memory _marketParams,
uint256 _assets,
uint256 _getId,
2024-01-16 00:13:43 +00:00
bool _isModeCollateral
2023-12-16 13:30:43 +00:00
) internal returns (Id _id, MarketParams memory, uint256 _amt) {
2023-12-16 12:38:07 +00:00
_amt = getUint(_getId, _assets);
2023-12-16 15:52:19 +00:00
bool _isEth = _isModeCollateral
2023-12-16 12:38:07 +00:00
? _marketParams.collateralToken == ethAddr
: _marketParams.loanToken == ethAddr;
_marketParams = updateTokenAddresses(_marketParams);
2023-12-16 13:30:43 +00:00
_id = _marketParams.id();
2023-12-16 12:38:07 +00:00
// Check for max value
if (_assets == type(uint256).max) {
2024-01-16 00:13:43 +00:00
_amt = _isEth
2023-12-16 12:38:07 +00:00
? address(this).balance
2023-12-16 15:52:19 +00:00
: _isModeCollateral
2024-01-16 00:13:43 +00:00
? TokenInterface(_marketParams.collateralToken).balanceOf(
address(this)
)
: TokenInterface(_marketParams.loanToken).balanceOf(
address(this)
);
2023-12-16 12:38:07 +00:00
}
2023-12-16 15:52:19 +00:00
// Perform eth to weth conversion if necessary
convertEthToWeth(
2024-01-10 18:04:11 +00:00
_isEth,
2023-12-16 15:52:19 +00:00
_isModeCollateral
? TokenInterface(_marketParams.collateralToken)
: TokenInterface(_marketParams.loanToken),
_amt
);
2023-12-16 12:38:07 +00:00
2023-12-16 13:30:43 +00:00
return (_id, _marketParams, _amt);
2023-12-16 12:38:07 +00:00
}
/// @notice Handles Eth to Weth conversion if shares are provided.
function _performEthToWethSharesConversion(
MarketParams memory _marketParams,
uint256 _shares,
2024-01-16 00:13:43 +00:00
uint256 _getId
2023-12-16 13:30:43 +00:00
) internal returns (Id _id, MarketParams memory, uint256 _assets) {
2023-12-16 12:38:07 +00:00
uint256 _shareAmt = getUint(_getId, _shares);
bool _isEth = _marketParams.loanToken == ethAddr;
_marketParams = updateTokenAddresses(_marketParams);
2023-12-16 13:30:43 +00:00
_id = _marketParams.id();
2023-12-16 12:38:07 +00:00
// Handle the max share case
if (_shares == type(uint256).max) {
2024-01-16 00:13:43 +00:00
_assets = _isEth
2023-12-16 12:38:07 +00:00
? address(this).balance
: TokenInterface(_marketParams.loanToken).balanceOf(
address(this)
);
} else {
(
uint256 totalSupplyAssets,
uint256 totalSupplyShares,
2024-01-16 00:13:43 +00:00
,
2023-12-16 12:38:07 +00:00
) = MORPHO_BLUE.expectedMarketBalances(_marketParams);
2024-01-16 00:13:43 +00:00
_assets = _shareAmt.toAssetsUp(
totalSupplyAssets,
totalSupplyShares
);
2023-12-16 12:38:07 +00:00
}
// Perform ETH to WETH conversion if necessary
convertEthToWeth(
2024-01-10 18:04:11 +00:00
_isEth,
2023-12-16 12:38:07 +00:00
TokenInterface(_marketParams.loanToken),
_assets
);
2023-12-16 13:30:43 +00:00
return (_id, _marketParams, _assets);
2023-12-16 12:38:07 +00:00
}
2024-01-16 00:13:43 +00:00
/// @notice Returns the borrowed assets and shares of onBehalf.
2023-12-16 12:38:07 +00:00
function getPaybackBalance(
2023-12-16 13:30:43 +00:00
Id _id,
2023-12-16 12:38:07 +00:00
MarketParams memory _marketParams,
address _onBehalf
2024-01-16 00:13:43 +00:00
) internal view returns (uint256 _assets, uint256 _borrowedShareAmt) {
Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf);
_borrowedShareAmt = _pos.borrowShares;
2023-12-16 12:38:07 +00:00
2023-12-16 15:52:19 +00:00
(, , uint256 totalBorrowAssets, uint256 totalBorrowShares) = MORPHO_BLUE
2023-12-16 12:38:07 +00:00
.expectedMarketBalances(_marketParams);
2023-12-16 15:52:19 +00:00
_assets = _borrowedShareAmt.toAssetsUp(
totalBorrowAssets,
totalBorrowShares
);
2023-12-16 12:38:07 +00:00
}
function updateTokenAddresses(
MarketParams memory _marketParams
) internal pure returns (MarketParams memory) {
_marketParams.loanToken = _marketParams.loanToken == ethAddr
? wethAddr
: _marketParams.loanToken;
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
? wethAddr
: _marketParams.collateralToken;
return _marketParams;
}
2023-12-16 12:33:50 +00:00
}