Credit Accounts interact with external protocols through adapters - whitelisted contracts that translate your calls into safe operations.
What
External calls flow through adapters:
You encode a call targeting an adapter address
Credit Facade routes the call to the adapter
Adapter builds the actual calldata for the external protocol
Adapter requests token approvals if needed
Credit Manager executes the call from the Credit Account
Credit Account acts as the "user" from the external protocol's perspective
Adapter returns which tokens to enable/disable based on the operation
Key insight: The Credit Account makes the actual call, so it receives the output tokens directly. You never touch the funds - they stay in the Credit Account.
How
Step 1: Get Adapter Address
Step 2: Encode the Adapter Call
Complete Example: Swap with Slippage Protection
Diff Functions
Many adapters have _diff variants that operate on "entire balance minus 1":
This is useful when you don't know the exact balance after previous operations.
Gotchas
Adapter ABIs Need Separate Import
SDK exports core ABIs, but adapter ABIs often need separate import:
Check what's available in @gearbox-protocol/integrations-v3.
Not All Protocols Have Adapters
An adapter must exist for each protocol you want to interact with. Check with contractToAdapter:
Recipient Parameter is Overridden
Many DEX functions have a recipient parameter. Adapters override this to ensure tokens go to the Credit Account, not an arbitrary address:
Always Use Slippage Protection
External calls are vulnerable to sandwich attacks. Always wrap swaps with slippage checks:
Adapter Function Signatures May Differ
Adapter functions may have slightly different signatures than the underlying protocol:
Read the adapter interface documentation for exact signatures.
Token Enable/Disable is Automatic
After adapter calls, tokens are automatically enabled/disabled based on balance changes:
Balance goes from 0 to non-zero: Token enabled
Balance goes from non-zero to 0: Token disabled
You usually don't need manual enableToken/disableToken after adapter calls.
import { getContract } from 'viem';
import { creditManagerAbi } from '@gearbox-protocol/sdk';
const creditManager = getContract({
address: cmAddress,
abi: creditManagerAbi,
client: publicClient,
});
// Get adapter for a protocol (e.g., Uniswap V3 Router)
const uniswapV3Adapter = await creditManager.read.contractToAdapter([
UNISWAP_V3_ROUTER,
]);
// Returns 0x0 if no adapter exists for this protocol
if (uniswapV3Adapter === '0x0000000000000000000000000000000000000000') {
throw new Error('No adapter for this protocol');
}
// Instead of specifying exact amount...
{ functionName: 'deposit', args: [exactAmount] }
// Use diff to deposit all USDC (minus 1 wei)
{ functionName: 'depositDiff', args: [1n] }
// Core ABIs from SDK
import { iCreditFacadeV300MulticallAbi } from '@gearbox-protocol/sdk';
// Adapter ABIs from integrations package
import { uniswapV3AdapterAbi } from '@gearbox-protocol/integrations-v3';
const adapter = await creditManager.read.contractToAdapter([protocolAddress]);
if (adapter === '0x0000000000000000000000000000000000000000') {
// No adapter - this protocol isn't integrated
throw new Error('Protocol not supported');
}
// You can pass any address here - adapter ignores it
const swapParams = {
recipient: '0x0000000000000000000000000000000000000000', // Will be overridden
// ...
};