Use Bundlers for Complex Borrows
When building an application for borrowers, providing a seamless and efficient user experience is paramount. Standard borrow operations often require multiple steps: approving collateral, supplying it, and then borrowing. Each step is a separate onchain transaction, which is slow, expensive, and can be confusing for users.
Morpho Bundlers are designed to solve this. They allow you to combine multiple market operations into a single, atomic transaction, dramatically improving the user experience.
This tutorial will introduce the specific Bundler actions available for Morpho Markets and guide you to the main tutorials for a full implementation.
Why Use Bundlers for Borrowing?
- Superior User Experience: Enable complex, one-click actions like "supply collateral and borrow," "leverage up," or "repay debt and withdraw collateral" in a single transaction.
- Gas Savings: Combining multiple calls into one transaction significantly reduces the total gas cost for the user.
- Atomicity & Safety: If any part of the bundled sequence fails (e.g., the borrow fails due to a health check), the entire transaction reverts. This prevents users from getting stuck in an undesirable intermediate state, such as having supplied collateral without being able to borrow.
For any user-facing application, using the Bundler is the recommended approach for all market interactions.
Core Bundler Actions for Borrowers
The bundler-sdk-viem
package provide a set of InputBundlerOperation
types for interacting with Morpho Markets.
Blue_SupplyCollateral
This action handles supplying a collateral asset to a market. The bundler will automatically handle any required token approvals (approve
or permit
).
import { type InputBundlerOperation } from "@morpho-org/bundler-sdk-viem";
const supplyCollateralOp: InputBundlerOperation = {
type: "Blue_SupplyCollateral",
args: {
id: marketId,
assets: collateralAmount,
onBehalf: userAddress,
},
};
Blue_Borrow
This action allows a user to borrow the loan asset against their supplied collateral. When bundled with Blue_SupplyCollateral
, it enables one-click borrowing.
import { DEFAULT_SLIPPAGE_TOLERANCE } from "@morpho-org/blue-sdk";
const borrowOp: InputBundlerOperation = {
type: "Blue_Borrow",
args: {
id: marketId,
assets: borrowAmount,
onBehalf: userAddress,
receiver: userAddress,
slippage: DEFAULT_SLIPPAGE_TOLERANCE, // Important for handling interest accrual
},
};
Blue_Repay
This action handles repaying debt. For the best user experience, it should be bundled with an approve
so the user only signs one transaction.
const repayOp: InputBundlerOperation = {
type: "Blue_Repay",
args: {
id: marketId,
shares: userBorrowShares, // For full repayment
onBehalf: userAddress,
slippage: DEFAULT_SLIPPAGE_TOLERANCE,
},
};
Blue_WithdrawCollateral
This action allows a user to withdraw their collateral, provided their position remains healthy after the withdrawal.
const withdrawCollateralOp: InputBundlerOperation = {
type: "Blue_WithdrawCollateral",
args: {
id: marketId,
assets: collateralToWithdraw,
receiver: userAddress,
},
};
urdClaim
In addition to managing market positions, the bundler can also claim rewards from the Universal Rewards Distributor (URD) in the same transaction. This is perfect for users who want to manage their position and claim pending rewards at once.
const claimOperation: InputBundlerOperation = {
type: "urdClaim",
args: [
distributorAddress, // Address of the URD contract
userAddress, // Address to receive rewards
rewardTokenAddress, // The reward token
claimableAmount, // The cumulative claimable amount
merkleProof, // The Merkle proof for validation
],
};
Next Steps: Building Your Integration
Now that you understand the core market actions, the next step is to integrate them into a seamless flow for your users.
1. Combining Borrow Actions with Other Operations
Your users will often want to perform multi-step workflows. For example:
- Leverage: Supply WETH, borrow USDC, swap USDC for more WETH, and supply it again as collateral—all in one click.
- Deleverage: Withdraw some WETH collateral, swap it for USDC, and repay part of the USDC debt.
- Repay and Claim: Repay a loan while simultaneously claiming any outstanding
MORPHO
rewards.
Our Combine Actions tutorial shows you how to chain these operations together into a single, logical bundle.
Learn how to combine actions in the Bundlers: Combine Actions Tutorial2. Full SDK Integration
Once you know which actions to combine, you need to use the SDK to simulate, populate, and encode the final transaction. This tutorial provides the complete code flow for using bundler-sdk-viem
to prepare and send a bundled transaction.
This is the core implementation guide for building your integration.
Follow the complete guide in the Bundlers: App Integration Tutorial