Skip to main content

Gating a Morpho Vault

This tutorial explains how to gate a Morpho Vault by controlling access to deposits through the supply queue.

Prerequisites

  • You have the necessary permissions (onlyAllocatorRole) to call setSupplyQueue functions on the vault contract. The users allowed to do so are: the owner, the curator, and any allocator.
  • You already have approved the Morpho Vault to spend the ERC20-compliant token. Otherwise, you will have to add it into the batch of operations.
  • You have the ID of the market you want to allow deposits into.
  • The supply queue is empty.

Steps

  1. Prepare the batch transaction
    Create a batch transaction that will execute the following steps in order:

  2. Open the supply queue
    Call setSupplyQueue with the ID of the market you want to allow deposits into:

    function setSupplyQueue(Id[] calldata newSupplyQueue) external onlyAllocatorRole;

    Example:

    vault.setSupplyQueue([marketId]);
  3. Deposit into the vault
    Call the deposit function to add liquidity to the vault:

    function deposit(uint256 assets, address receiver) public override returns (uint256 shares);

    Example:

    uint256 sharesReceived = vault.deposit(amountToDeposit, receiverAddress);
  4. Close the supply queue
    Call setSupplyQueue again with an empty array to prevent further deposits:

    vault.setSupplyQueue([]);
  5. Execute the batch transaction
    Submit the batch transaction containing all three operations to the blockchain.

By following these steps, you can control access to deposits in your Morpho Vault, effectively gating it to desired participants or time windows.

Optional: Approving ERC20 Token Spending (if not done previously)

If you haven't already approved the Morpho Vault to spend your ERC20 tokens, you'll need to add this step to your batch transaction before the deposit. Here's how to do it:

  1. Approve token spending
    Call the approve function on the ERC20 token contract:

    function approve(address spender, uint256 amount) external returns (bool);

    Example:

    IERC20(tokenAddress).approve(vaultAddress, amountToDeposit);

    Make sure to replace tokenAddress with the address of the ERC20 token you're depositing, vaultAddress with the address of the Morpho Vault, and amountToDeposit with the amount you want to deposit.

  2. Add to batch transaction
    Include this approval step at the beginning of your batch transaction, before the setSupplyQueue and deposit calls.

Remember that approvals are typically persistent, so you only need to do this once for each token-vault pair unless you've set a specific allowance that has been exhausted.