Making External Calls

Interact with external protocols (Uniswap, Curve, Yearn, etc.) from your Credit Account.

For Solidity implementation, see Making External Calls.

Why

You make external calls when:

  • Swapping tokens - Trade via Uniswap, Curve, or other DEXs

  • Depositing to vaults - Stake in Yearn, Lido, or yield strategies

  • Managing LP positions - Add/remove liquidity on various protocols

  • Executing complex strategies - Chain multiple protocol interactions

Credit Accounts interact with external protocols through adapters - whitelisted contracts that translate your calls into safe operations.

What

External calls flow through adapters:

  1. You encode a call targeting an adapter address

  2. Credit Facade routes the call to the adapter

  3. Adapter builds the actual calldata for the external protocol

  4. Adapter requests token approvals if needed

  5. Credit Manager executes the call from the Credit Account

  6. Credit Account acts as the "user" from the external protocol's perspective

  7. 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.

See Also

Last updated