Manage Positions
This guide shows you how to manage your positions in Morpho Markets using Solidity snippets.
To use these snippets, you can either:
- Deploy the full MorphoMarketSnippet contract
- Copy the specific functions you need into your own contract
Imports
import {Id, IMorpho, MarketParams} from "@morpho-blue/interfaces/IMorpho.sol";
import {IIrm} from "@morpho-blue/interfaces/IIrm.sol";
import {IOracle} from "@morpho-blue/interfaces/IOracle.sol";
import {ERC20} from "@openzeppelin4/token/ERC20/ERC20.sol";
import {SafeERC20} from "@openzeppelin4/token/ERC20/utils/SafeERC20.sol";
import {MorphoBalancesLib} from "@morpho-blue/libraries/periphery/MorphoBalancesLib.sol";
import {MarketParamsLib} from "@morpho-blue/libraries/MarketParamsLib.sol";
import {MorphoLib} from "@morpho-blue/libraries/periphery/MorphoLib.sol";
import {SharesMathLib} from "@morpho-blue/libraries/SharesMathLib.sol";
Base Contract
contract MorphoBlueSnippets {
using MorphoLib for IMorpho;
using MorphoBalancesLib for IMorpho;
using MarketParamsLib for MarketParams;
using SafeERC20 for ERC20;
using SharesMathLib for uint256;
IMorpho public immutable morpho;
constructor(address morphoAddress) {
morpho = IMorpho(morphoAddress);
}
...
Functions
Supply Collateral
/// @notice Handles the supply of collateral by the caller to a specific market.
/// @param marketParams The parameters of the market.
/// @param amount The amount of collateral the user is supplying.
function supplyCollateral(MarketParams memory marketParams, uint256 amount) external {
ERC20(marketParams.collateralToken).forceApprove(address(morpho), type(uint256).max);
ERC20(marketParams.collateralToken).safeTransferFrom(msg.sender, address(this), amount);
address onBehalf = msg.sender;
morpho.supplyCollateral(marketParams, amount, onBehalf, hex"");
}