Multicalls

Build and execute multicalls in Solidity.

For SDK multicall helpers, see Multicalls.

The MultiCall Structure

struct MultiCall {
    address target;   // CreditFacade or allowed Adapter
    bytes callData;   // Encoded function call
}

ICreditFacadeV3Multicall Operations

All multicall operations are defined in ICreditFacadeV3Multicall:

import {ICreditFacadeV3Multicall} from "@gearbox-protocol/core-v3/contracts/interfaces/ICreditFacadeV3Multicall.sol";

Protocol Operations

Function
Signature

addCollateral

(address token, uint256 amount)

addCollateralWithPermit

(address token, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s)

withdrawCollateral

(address token, uint256 amount, address to)

increaseDebt

(uint256 amount)

decreaseDebt

(uint256 amount)

updateQuota

(address token, int96 quotaChange, uint96 minQuota)

Safety Operations

Function
Signature

onDemandPriceUpdate

(address token, bool reserve, bytes data)

storeExpectedBalances

(BalanceDelta[] deltas)

compareBalances

()

setFullCheckParams

(uint256[] hints, uint16 minHF)

setBotPermissions

(address bot, uint192 permissions)

Encoding Multicalls

Use abi.encodeCall for type-safe encoding:

Adapter Calls

External protocol calls go through adapters. Get the adapter address from Credit Manager:

Complete Multicall Example

8-call strategy: price update, collateral, borrow, slippage setup, swap, deposit, slippage check, quota:

The "Diff" Pattern

Adapters implement *_diff functions for handling unknown amounts:

  • Standard function: Requires exact amountIn

  • Diff function: Calculates amountIn = currentBalance - leftoverAmount

This is essential when the exact output of a previous operation is unknown.

Best Practices

  1. Price updates first: Always put onDemandPriceUpdates at the start if using pull-based oracles

  2. Slippage protection: Always use storeExpectedBalances before swaps and compareBalances after

  3. Approve to Credit Manager: Token approvals go to Credit Manager, not Credit Facade

  4. Gas optimization: Use setFullCheckParams with hints for accounts with many tokens

  5. Dust management: Use type(uint256).max in withdrawCollateral to empty balances

Next Steps

For architectural background, see Multicall System.

Last updated