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:
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:
Pool lends only to whitelisted Credit Managers
Credit Managers borrow on behalf of Credit Accounts
Credit Accounts hold all collateral and debt
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:
Deploy a fee collector contract
Configure the pool's treasury address to your collector
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:
State extension: Added
Positiontracking on top of base Credit ManagerCollateral override: Modified TWV calculation to include unrealized PnL
Protocol-specific logic: Market management and position tracking
Governance integration:
configuratorOnlymodifier for admin functions
For architectural background, see Credit Suite Architecture and Pool Architecture.
Security Considerations
When extending core contracts:
Preserve invariants - Never break core protocol assumptions (debt tracking, collateral checks)
Test extensively - Fork mainnet and test against real pools/Credit Managers
Audit thoroughly - Core extensions require professional audits
Consider upgrade paths - Plan for parameter changes and emergency controls
Monitor gas costs - Overrides affect every user operation
Validate inputs - Custom logic must not allow manipulation of debt/collateral calculations
Deployment Checklist
Related
Adapter Development - Standard integration path
Protocol Integration - Building on top of Credit Accounts
Credit Suite Architecture - Core architecture reference
Pool Architecture - Pool mechanics reference
Last updated
