Protocol Integration

Build smart contracts that compose with Gearbox Credit Accounts.

Overview

Protocol integration means building contracts that:

  • Call Gearbox Credit Accounts from external contracts

  • Create automated strategies using multicalls

  • Compose Gearbox with your own protocol logic

Unlike adapters (which are called BY Credit Accounts), protocol integrations CALL Credit Accounts from outside.

Architecture

Your Contract -> CreditFacade -> Credit Account -> Adapters -> DeFi Protocols
                      |
                      +-> Multicall execution
                      +-> Collateral checks
                      +-> Access control

Basic Integration Pattern

1. Find the Right Market

2. Build and Execute Multicalls

Common Integration Patterns

Automated Strategy Contract

A contract that executes predefined strategies:

Strategy with Price Updates

When using Pyth or Redstone oracles, your contract must accept and forward price data:

Keeper/Bot Integration

For automated position management:

Access Control Considerations

Account Ownership

Only the account owner (or approved bots) can execute multicalls:

Bot Permissions

To allow external contracts to manage accounts:

Then the bot can use botMulticall:

Error Handling

Reading Account State Before Operations

Handling Reverts

Gas Optimization

Use Collateral Hints

For accounts with many tokens, provide hints to reduce oracle calls:

Batch Operations

Combine related operations in single multicalls rather than multiple transactions:

Testing

Foundry Setup

Security Considerations

  1. Reentrancy - Multicalls are atomic, but be careful with callbacks

  2. Slippage - Always use storeExpectedBalances/compareBalances for swaps

  3. Access control - Verify account ownership before operations

  4. Oracle manipulation - Use safe pricing for withdrawals

  5. Flash loan attacks - Consider same-block restrictions on debt changes

Last updated