Core Extension

Advanced patterns for extending Gearbox protocol contracts beyond standard adapter integration.

This is advanced material. For standard integrations, see Adapter Development or Protocol Integration.

When to Extend vs Integrate

Different levels of integration require different approaches:

Approach
Complexity
When to Use

Adapter

Low

Wrap existing DeFi protocols for Credit Account access

Protocol Integration

Medium

Build contracts that compose with Credit Accounts externally

Core Extension

High

Modify Credit Manager/Pool behavior, create protocol-specific spokes

Examples by approach:

Adapter: Wrapping Uniswap, Curve, Yearn for Credit Account users

Protocol Integration: Strategy vaults that open/manage Credit Accounts

Core Extension: Custom Credit Manager for protocol-specific accounting, custom interest rate models, pool hooks for fee distribution

Build a core extension when you need to:

  • Modify how debt/collateral calculations work

  • Change liquidity flow patterns between pools and Credit Managers

  • Implement protocol-specific Credit Manager variants ("spokes")

  • Create custom interest rate logic beyond linear models

  • Add lifecycle hooks to pool operations

Understanding Money Flows

Core extensions require deep understanding of how liquidity moves through the protocol.

The Fundamental Flow

Key invariants:

  1. Pool lends only to whitelisted Credit Managers

  2. Credit Managers borrow on behalf of Credit Accounts

  3. Credit Accounts hold all collateral and debt

  4. All value flows are tracked via indexed interest and quota systems

Detailed Liquidity Flow

When borrowing:

When repaying:

Critical detail: The Credit Manager never holds funds. It orchestrates transfers between Pool and Credit Account while maintaining accounting state.

Pool Extension Patterns

Custom Interest Rate Models

The pool queries an external IRM contract for interest rates. You can implement custom logic by deploying a new IRM.

IInterestRateModel interface:

Example: Time-weighted interest model

Deploying custom IRM:

Custom IRMs can be set via governance:

Pool Withdrawal Hooks

Pools in V3 support withdrawal fees. These are not "hooks" in the callback sense, but configurable parameters:

For custom fee distribution logic, you would typically:

  1. Deploy a fee collector contract

  2. Configure the pool's treasury address to your collector

  3. Implement distribution logic in your collector

Credit Manager Extension Patterns

Spoke Development Concept

A "spoke" is a specialized Credit Manager variant designed for protocol-specific use cases. Unlike standard Credit Managers, spokes extend core functionality for unique accounting or liquidity patterns.

When to build a spoke:

  • Your protocol needs custom collateral valuation logic

  • You're integrating Credit Accounts as a core protocol primitive

  • You need specialized debt/liquidation mechanics

  • You want to modify how positions are opened/closed

Example use case: A derivatives protocol where Credit Accounts are used as margin accounts with custom position tracking.

Spoke Architecture Pattern

Custom Collateral Valuation

Extend collateral calculations for protocol-specific assets:

Extending Liquidation Logic

Custom liquidation premiums based on collateral type:

Working with Configurators

The CreditConfigurator provides governance interface for Credit Manager parameters. When building spokes, you typically extend the configurator as well.

Standard Configurator Usage

Custom Configurator for Spokes

Governance Integration

For production deployments, configurator changes should go through governance:

Key patterns demonstrated:

  1. State extension: Added Position tracking on top of base Credit Manager

  2. Collateral override: Modified TWV calculation to include unrealized PnL

  3. Protocol-specific logic: Market management and position tracking

  4. Governance integration: configuratorOnly modifier for admin functions

For architectural background, see Credit Suite Architecture and Pool Architecture.

Security Considerations

When extending core contracts:

  1. Preserve invariants - Never break core protocol assumptions (debt tracking, collateral checks)

  2. Test extensively - Fork mainnet and test against real pools/Credit Managers

  3. Audit thoroughly - Core extensions require professional audits

  4. Consider upgrade paths - Plan for parameter changes and emergency controls

  5. Monitor gas costs - Overrides affect every user operation

  6. Validate inputs - Custom logic must not allow manipulation of debt/collateral calculations

Deployment Checklist

Last updated