Skip to main content
The VaultV2Factory contract provides a deterministic deployment mechanism for creating Morpho Vault V2 instances. It uses CREATE2 for predictable vault addresses and maintains a registry of all deployed vaults.

Contract overview

The factory contract handles vault deployment and provides lookup functionality to verify vault authenticity and retrieve deployed vault addresses.

Key features

  • Deterministic deployment: Uses CREATE2 with salt for predictable vault addresses
  • Vault registry: Tracks all deployed vaults with isVaultV2 mapping
  • Address lookup: Query deployed vault addresses by owner, asset, and salt
  • Event emission: Emits CreateVaultV2 event for indexing and tracking

Factory deployment

The VaultV2Factory contract is a simple, immutable factory with no constructor parameters:
VaultV2Factory.sol
contract VaultV2Factory is IVaultV2Factory {
    mapping(address account => bool) public isVaultV2;
    mapping(address owner => mapping(address asset => mapping(bytes32 salt => address))) public vaultV2;

    function createVaultV2(address owner, address asset, bytes32 salt) external returns (address) {
        address newVaultV2 = address(new VaultV2{salt: salt}(owner, asset));

        isVaultV2[newVaultV2] = true;
        vaultV2[owner][asset][salt] = newVaultV2;
        emit CreateVaultV2(owner, asset, salt, newVaultV2);

        return newVaultV2;
    }
}

Creating vaults

Use the createVaultV2 function to deploy new vault instances:
function createVaultV2(
    address owner,
    address asset,
    bytes32 salt
) external returns (address)

Parameters

owner
address
The initial owner of the vault who will have administrative control
asset
address
The ERC20 token address that the vault will accept as deposits
salt
bytes32
A unique salt value for CREATE2 deployment to generate deterministic addresses

Returns

The address of the newly deployed VaultV2 contract.

Verifying vaults

The factory provides methods to verify vault authenticity and lookup deployed addresses:

Check if address is a factory-deployed vault

bool isFactoryVault = factory.isVaultV2(vaultAddress);

Lookup vault by parameters

address vault = factory.vaultV2(owner, asset, salt);
If no vault exists with the given parameters, this returns address(0).

Computing vault addresses

You can predict the vault address before deployment using CREATE2 address calculation:
bytes32 initCodeHash = keccak256(
    abi.encodePacked(
        type(VaultV2).creationCode,
        abi.encode(owner, asset)
    )
);

address predictedAddress = address(
    uint160(
        uint256(
            keccak256(
                abi.encodePacked(
                    bytes1(0xff),
                    address(factory),
                    salt,
                    initCodeHash
                )
            )
        )
    )
);
The predicted address calculation must use the exact same parameters (owner, asset, salt) that will be passed to createVaultV2.

Events

The factory emits the following event when a vault is created:
event CreateVaultV2(
    address indexed owner,
    address indexed asset,
    bytes32 salt,
    address indexed newVaultV2
);
This event allows you to:
  • Track all vault deployments
  • Index vaults by owner or asset
  • Monitor vault creation activity
  • Build off-chain registries of vaults

Example usage

function testDeployVault() public {
    address owner = address(this);
    address asset = address(usdc);
    bytes32 salt = keccak256("my-vault-v1");

    // Deploy vault through factory
    address vault = factory.createVaultV2(owner, asset, salt);

    // Verify deployment
    assertTrue(factory.isVaultV2(vault));
    assertEq(factory.vaultV2(owner, asset, salt), vault);

    // Verify vault parameters
    assertEq(VaultV2(vault).owner(), owner);
    assertEq(VaultV2(vault).asset(), asset);
}
Each combination of (owner, asset, salt) can only be used once. Attempting to deploy with the same parameters will cause the transaction to revert.

Next steps

Vault creation

Step-by-step guide to creating and initializing a vault

Configuration

Configure your vault with roles, fees, and parameters

Build docs developers (and LLMs) love