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: theowner
, thecurator
, and anyallocator
. - 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
-
Prepare the batch transaction
Create a batch transaction that will execute the following steps in order: -
Open the supply queue
CallsetSupplyQueue
with the ID of the market you want to allow deposits into:function setSupplyQueue(Id[] calldata newSupplyQueue) external onlyAllocatorRole;
Example:
vault.setSupplyQueue([marketId]);
-
Deposit into the vault
Call thedeposit
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);
-
Close the supply queue
CallsetSupplyQueue
again with an empty array to prevent further deposits:vault.setSupplyQueue([]);
-
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:
-
Approve token spending
Call theapprove
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, andamountToDeposit
with the amount you want to deposit. -
Add to batch transaction
Include this approval step at the beginning of your batch transaction, before thesetSupplyQueue
anddeposit
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.