Overview
Morpho vaults pool deposited assets into lending markets where borrowers pay interest. That interest flows back to depositors automatically. This guide walks through integrating Morpho vaults into a Next.js app with Dynamic embedded wallets. For the final code, see the GitHub repository.How it works
When a user deposits, they receive vault tokens representing their share of the pool. As borrowers pay interest, each vault token becomes redeemable for more of the underlying asset. When the user withdraws, they get back their original deposit plus accrued yield. No claiming or compounding is required. Example: A user deposits 1,000 USDC when each vault token is worth 1.00 USDC. When the token value rises to 1.05 USDC, those tokens are worth 1,050 USDC. APY is variable and adjusts based on borrower demand and the curator’s allocation strategy.Setup
Project setup
Follow the JS SDK Quickstart to initialize a Next.js app with Dynamic. Scaffold a Next.js app withcreate-next-app and mirror the provider wiring from the quickstart or the GitHub repository linked above.
In the Dynamic dashboard, enable Ethereum under Chains & Networks, enable Embedded wallets under Wallets, and add your app’s origin under Security → Allowed Origins.
Install dependencies
Environment variables
.env.local
Initialize Dynamic
Createsrc/lib/dynamic.ts:
src/lib/dynamic.ts
Configure providers
Createsrc/lib/providers.tsx. DynamicProvider owns the reactive auth state; InnerProviders reads it via useUser() and useGetWalletAccounts() from @dynamic-labs-sdk/react-hooks:
src/lib/providers.tsx
Configure networks and constants
The example uses a per-chain network config to support Base, Ethereum mainnet, Arbitrum, Optimism, and Polygon. Createsrc/lib/networks.ts:
src/lib/networks.ts
Set up contract ABIs
Create the ABI files insrc/lib/ABIs/:
- ERC20_ABI.ts — standard token interface (
balanceOf,approve,allowance) - ERC4626_ABI.ts — vault interface (
deposit,withdraw,balanceOf,convertToAssets) - REWARDS_ABI.ts — Morpho rewards distributor (
getUserRewardBalance,getRewardToken) - MORPHO_MARKETS_ABI.ts — core Morpho markets contract
Fetching vault data
Vault list
Createsrc/lib/hooks/useVaultsList.ts to load all available vaults. The hook reads the active chain from useWallet() and queries the Morpho API:
src/lib/hooks/useVaultsList.ts
Deposit and withdraw
Deposits require a two-step flow: the user must first approve the vault to spend their tokens, then deposit. Use viem directly withcreateWalletClientForWalletAccount:
src/lib/hooks/useVaultOperations.ts
Enable transaction simulation
Dynamic’s embedded wallets include built-in transaction previews. To enable, go to Developer Settings → Embedded Wallets → Dynamic in the dashboard and toggle on Show Confirmation UI and Transaction Simulation. Users will see the exact assets being transferred, estimated fees, and the vault contract before confirming.Run the app
http://localhost:3000 to your allowed origins in the Dynamic dashboard under Developer Settings → CORS Origins.