Adding New Protocols to Collatinator

This document provides a comprehensive guide on how to add new CDP (Collateralized Debt Position) protocols to the Collatinator facet in the Diamond Vaultinator framework.

Table of Contents

  1. Overview

  2. Protocol Requirements

  3. Adding a Protocol

  4. Creating Protocol Adapters

  5. Cross-Chain Support

  6. Testing New Protocols

  7. Protocol Management

  8. Best Practices

  9. Example: Aave Protocol Integration

Overview

The Collatinator facet supports multiple CDP protocols like MakerDAO, Aave, Compound, and Liquity. Each protocol has its own rules for collateralization, liquidation thresholds, and other parameters. This guide explains how to extend the Collatinator to support additional protocols.

Protocol Requirements

Before adding a new protocol, ensure it meets these requirements:

  1. Collateralization Mechanism: The protocol must support collateralized debt positions

  2. Liquidation Process: Clear rules for liquidation thresholds and processes

  3. Token Standards: Compatible with ERC20 tokens for collateral and debt

  4. Public Interface: Well-documented public interface for interaction

  5. Chain Compatibility: Supported on the target blockchain network

Adding a Protocol

Step 1: Define Protocol ID

First, add a new protocol constant in the CollatinatiorFacet.sol file:

Step 2: Register the Protocol

Use the addProtocol function to register the new protocol. This can be done:

  1. During initialization: Add to the initializeCollatinatiorStorage function

  2. Post-deployment: Call the addProtocol function with admin rights

Parameters:

  • _protocolId: Unique identifier for the protocol (use the constant defined in Step 1)

  • _name: Human-readable name of the protocol

  • _liquidationThreshold: Default liquidation threshold (e.g., 150 means 150% collateralization required)

  • _adapter: Address of the protocol adapter (see next section)

Creating Protocol Adapters

Protocol adapters are contracts that handle the specific interactions with each CDP protocol.

Step 1: Create Adapter Interface

Step 2: Implement Protocol-Specific Adapter

Create a new adapter that implements the IProtocolAdapter interface:

Cross-Chain Support

The Collatinator facet can support protocols across different blockchain networks. Here's how to handle cross-chain protocol integration:

Chain-Specific Considerations

  1. Protocol Availability: Not all protocols are available on all chains

  2. Contract Addresses: The same protocol may have different contract addresses on different chains

  3. Gas Costs: Operations may have different gas costs across chains

  4. Block Confirmation Times: Consider different block confirmation times when designing liquidation mechanisms

Implementing Chain-Specific Adapters

For protocols that exist on multiple chains, create chain-specific adapters:

Chain ID Detection

Use the block.chainid to detect the current chain and apply chain-specific logic:

Cross-Chain Communication

For protocols that require cross-chain communication:

  1. Message Passing: Use cross-chain messaging protocols like LayerZero, Axelar, or Wormhole

  2. State Verification: Implement state verification to ensure data integrity across chains

  3. Fallback Mechanisms: Design fallback mechanisms for when cross-chain communication fails

Testing New Protocols

Before deploying to production, thoroughly test the new protocol integration:

  1. Unit Tests: Test each adapter function independently

  2. Integration Tests: Test the full flow from creating to closing positions

  3. Edge Cases: Test with minimum/maximum values and unusual scenarios

  4. Gas Optimization: Ensure operations are gas-efficient

Protocol Management

After adding a protocol, you can manage it using these functions:

Updating Liquidation Threshold

Removing a Protocol

Note: Removing a protocol only prevents new positions from being created. Existing positions remain active.

Best Practices

  1. Security First: Always prioritize security when integrating with external protocols

  2. Conservative Thresholds: Start with conservative liquidation thresholds and adjust based on protocol behavior

  3. Thorough Testing: Test all edge cases before deploying to production

  4. Monitoring: Implement monitoring for protocol health and position status

  5. Upgradeability: Design adapters to be upgradeable if protocol interfaces change

  6. Documentation: Document all protocol-specific behaviors and requirements

Example: Aave Protocol Integration

This section provides a detailed example of integrating Aave V3 as a protocol in the Collatinator facet.

Step 1: Define Aave Protocol ID

Step 2: Create Aave Adapter Interface

Step 3: Implement Aave Adapter

Step 4: Deploy and Register Aave Adapter

Step 5: Usage Example

This example demonstrates how to integrate Aave V3 with the Collatinator facet, including chain-specific considerations and a complete adapter implementation.