Withdraw

Learn how to withdraw a position from Morpho, on-chain via a Smart Contract & off-chain via ethers.js.

Withdraw a supply position, off-chain through Etherscan or with ethers.js, or on-chain using a Smart Contract. Here are concrete examples 👇

Morpho-AaveV2

// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.16;

import {IMorpho} from "./interfaces/IMorpho.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IWETH9 {
    function withdraw(uint256) external;
}

contract MorphoAaveV2Withdrawer {
    address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address public constant AWETH = 0x030bA81f1c18d280636F32af80b9AAd02Cf0854e;

    address public constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
    address public constant ADAI = 0x028171bCA77440897B824Ca71D1c56caC55b68A3;

    address public constant MORPHO = 0x777777c9898D384F785Ee44Acfe945efDFf5f3E0;

    function _withdrawERC20(address _aToken, uint256 _amount) internal {
        IMorpho(MORPHO).withdraw(_aToken, _amount);
    }

    function withdrawERC20(uint256 _amount) public {
        _withdrawERC20(
            ADAI, // the DAI market, represented by the aDAI ERC20 token
            _amount
        );
        // this contract now has _amount DAI: IERC20(DAI).balanceOf(address(this)) == _amount
    }

    function withdrawETH(uint256 _amount) public {
        _withdrawERC20(
            AWETH, // the ETH market, represented by the aWETH ERC20 token
            _amount
        );
        // this contract now has _amount WETH: IERC20(WETH).balanceOf(address(this)) == _amount
        IWETH9(WETH).withdraw(_amount);
        // this contract now has _amount ETH: address(this).balance == _amount
    }
}

Morpho-Compound

// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.16;

import {IMorpho} from "@morpho-dao/morpho-core-v1/contracts/compound/interfaces/IMorpho.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IWETH9 {
    function withdraw(uint256) external;
}

contract MorphoCompoundWithdrawer {
    address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address public constant CETH = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5;
    address public constant CDAI = 0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643;

    address public constant MORPHO = 0x8888882f8f843896699869179fB6E4f7e3B58888;

    function _withdrawERC20(address _cToken, uint256 _amount) internal {
        IMorpho(MORPHO).withdraw(_cToken, _amount);
    }

    function withdrawERC20(uint256 _amount) external {
        _withdrawERC20(
            CDAI, // the DAI market, represented by the cDAI ERC20 token
            _amount
        );
        // this contract now has _amount DAI: IERC20(DAI).balanceOf(address(this)) == _amount
    }

    function withdrawETH(uint256 _amount) external {
        _withdrawERC20(
            CETH, // the ETH market, represented by the cETH ERC20 token
            _amount
        );
        // this contract now has _amount WETH: IERC20(WETH).balanceOf(address(this)) == _amount
        IWETH9(WETH).withdraw(_amount);
        // this contract now has _amount ETH: address(this).balance == _amount
    }
}

ERC-4626 Vaults

Morpho's vaults are a new way to interact with Morpho according to the ERC-4626 standard. Learn more on the dedicated page.

// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.13;

import {ISupplyVault} from "@morpho-dao/morpho-tokenized-vaults/src/compound/interfaces/ISupplyVault.sol";
import {ISupplyHarvestVault} from "@morpho-dao/morpho-tokenized-vaults/src/compound/interfaces/ISupplyHarvestVault.sol";

contract MorphoCompoundVaultSupplier {
    address public constant MC_DAI = 0xd99D793B8FDaE42C1867293C172b9CBBD3ea49FF;
    address public constant MCH_DAI =
        0x5CBead740564A2173983E48f94F36357C1954EAE;

    function withdraw(uint256 _amount) internal {
        ISupplyVault(MC_DAI).withdraw(
            _amount,
            address(this), // the address of the receiver of the funds withdrawn
            address(this) // the address of the user you want to withdraw from (they must have approved this contract to spend their tokens)
        );
    }

    function withdrawHarvest(uint256 _amount) internal {
        ISupplyHarvestVault(MCH_DAI).withdraw(
            _amount,
            address(this), // the address of the receiver of the funds withdrawn
            address(this) // the address of the user you want to withdraw from (they must have approved this contract to spend their tokens)
        );
    }
}

Last updated

Logo

This documentation is provided by MorphoLabs, main contributor to Morpho DAO. For any question, reach out.