Collatinator Technical Details

The Collatinator Facet is a core component of the Vaultinator Protocol that provides collateralized lending functionality. This document outlines the technical implementation details, including the contract architecture, function specifications, storage layout, and security considerations.

Contract Architecture

The Collatinator Facet follows the Diamond Standard (EIP-2535) pattern and is implemented as a facet that can be added to the Vaultinator Diamond. It interacts with various lending protocols through a standardized adapter pattern.

Class Diagram

┌─────────────────┐      ┌───────────────────┐      ┌───────────────────┐
│  VaultinatorFacet│◄─────┤  CollatinatorFacet│─────►│  YieldinatorFacet │
└─────────────────┘      └───────────────────┘      └───────────────────┘
         ▲                         │                          ▲
         │                         │                          │
         │                         ▼                          │
┌─────────────────┐      ┌───────────────────┐      ┌───────────────────┐
│  DiamondStorage  │◄─────┤CollatinatorStorage│─────►│StakinatorFacet    │
└─────────────────┘      └───────────────────┘      └───────────────────┘


                          ┌───────────────────┐
                          │   AdapterRegistry  │
                          └───────────────────┘


                          ┌───────────────────┐
                          │  Lending Adapters  │
                          └───────────────────┘


                          ┌───────────────────┐
                          │ Lending Protocols  │
                          └───────────────────┘

Component Interaction

  1. Collatinator Facet: The main contract that implements the collateralized lending functionality

  2. Adapter Registry: A registry of lending protocol adapters that the Collatinator can use

  3. Lending Adapters: Standardized adapters for interacting with different lending protocols

  4. Lending Protocols: External protocols that provide collateralized lending services

Function Specifications

Protocol Management

Registers a new lending protocol adapter in the Collatinator.

  • Parameters:

    • adapter: The address of the lending protocol adapter

  • Returns: bool indicating success

  • Access Control: Only administrators can call this function

  • Events Emitted: ProtocolRegistered(address indexed adapter, string name)

Deregisters a lending protocol adapter from the Collatinator.

  • Parameters:

    • adapter: The address of the lending protocol adapter

  • Returns: bool indicating success

  • Access Control: Only administrators can call this function

  • Events Emitted: ProtocolDeregistered(address indexed adapter)

Gets a list of all registered lending protocol adapters.

  • Returns: An array of lending protocol adapter addresses

  • Access Control: Anyone can call this function

Position Management

Creates a new collateralized position.

  • Parameters:

    • protocol: The address of the lending protocol adapter

    • collateralAsset: The address of the collateral asset

    • collateralAmount: The amount of collateral to deposit

    • debtAsset: The address of the debt asset

    • debtAmount: The amount of debt to borrow

  • Returns: The ID of the created position

  • Access Control: Anyone can call this function

  • Events Emitted: PositionCreated(uint256 indexed positionId, address indexed owner, address protocol, address collateralAsset, uint256 collateralAmount, address debtAsset, uint256 debtAmount)

Adds collateral to an existing position.

  • Parameters:

    • positionId: The ID of the position

    • amount: The amount of collateral to add

  • Returns: bool indicating success

  • Access Control: Only the position owner can call this function

  • Events Emitted: CollateralAdded(uint256 indexed positionId, uint256 amount)

Removes collateral from an existing position.

  • Parameters:

    • positionId: The ID of the position

    • amount: The amount of collateral to remove

  • Returns: bool indicating success

  • Access Control: Only the position owner can call this function

  • Events Emitted: CollateralRemoved(uint256 indexed positionId, uint256 amount)

Increases the borrowed amount of an existing position.

  • Parameters:

    • positionId: The ID of the position

    • amount: The amount to increase the borrow by

  • Returns: bool indicating success

  • Access Control: Only the position owner can call this function

  • Events Emitted: BorrowIncreased(uint256 indexed positionId, uint256 amount)

Repays part or all of the borrowed amount of an existing position.

  • Parameters:

    • positionId: The ID of the position

    • amount: The amount to repay

  • Returns: bool indicating success

  • Access Control: Only the position owner can call this function

  • Events Emitted: BorrowRepaid(uint256 indexed positionId, uint256 amount)

Closes an existing position by repaying all debt and withdrawing all collateral.

  • Parameters:

    • positionId: The ID of the position

  • Returns: bool indicating success

  • Access Control: Only the position owner can call this function

  • Events Emitted: PositionClosed(uint256 indexed positionId)

Vault Integration

Creates a new collateralized position using assets from a vault.

  • Parameters:

    • vaultId: The ID of the vault

    • protocol: The address of the lending protocol adapter

    • collateralAsset: The address of the collateral asset

    • collateralAmount: The amount of collateral to deposit

    • debtAsset: The address of the debt asset

    • debtAmount: The amount of debt to borrow

  • Returns: The ID of the created position

  • Access Control: Only vault owners or authorized users can call this function

  • Events Emitted: VaultPositionCreated(uint256 indexed vaultId, uint256 indexed positionId, address protocol, address collateralAsset, uint256 collateralAmount, address debtAsset, uint256 debtAmount)

Adds collateral to an existing position using assets from a vault.

  • Parameters:

    • vaultId: The ID of the vault

    • positionId: The ID of the position

    • amount: The amount of collateral to add

  • Returns: bool indicating success

  • Access Control: Only vault owners or authorized users can call this function

  • Events Emitted: VaultCollateralAdded(uint256 indexed vaultId, uint256 indexed positionId, uint256 amount)

Repays part or all of the borrowed amount of an existing position and sends the collateral to a vault.

  • Parameters:

    • vaultId: The ID of the vault

    • positionId: The ID of the position

    • amount: The amount to repay

  • Returns: bool indicating success

  • Access Control: Only vault owners or authorized users can call this function

  • Events Emitted: VaultBorrowRepaid(uint256 indexed vaultId, uint256 indexed positionId, uint256 amount)

Information and Metrics

Gets information about a position.

  • Parameters:

    • positionId: The ID of the position

  • Returns: A Position struct containing information about the position

  • Access Control: Anyone can call this function

Gets the health factor of a position.

  • Parameters:

    • positionId: The ID of the position

  • Returns: The health factor of the position (scaled by 1e18)

  • Access Control: Anyone can call this function

Gets the liquidation threshold for a collateral asset in a protocol.

  • Parameters:

    • protocol: The address of the lending protocol adapter

    • collateralAsset: The address of the collateral asset

  • Returns: The liquidation threshold (scaled by 1e18)

  • Access Control: Anyone can call this function

Gets the maximum loan-to-value ratio for a collateral asset in a protocol.

  • Parameters:

    • protocol: The address of the lending protocol adapter

    • collateralAsset: The address of the collateral asset

  • Returns: The maximum LTV (scaled by 1e18)

  • Access Control: Anyone can call this function

Gets the borrow rate for a debt asset in a protocol.

  • Parameters:

    • protocol: The address of the lending protocol adapter

    • debtAsset: The address of the debt asset

  • Returns: The borrow rate in basis points (1/100 of a percent)

  • Access Control: Anyone can call this function

Gets the supply rate for a collateral asset in a protocol.

  • Parameters:

    • protocol: The address of the lending protocol adapter

    • collateralAsset: The address of the collateral asset

  • Returns: The supply rate in basis points (1/100 of a percent)

  • Access Control: Anyone can call this function

Emergency Operations

Performs an emergency repayment of a position to prevent liquidation.

  • Parameters:

    • positionId: The ID of the position

  • Returns: bool indicating success

  • Access Control: Only the position owner can call this function

  • Events Emitted: EmergencyRepaid(uint256 indexed positionId)

Pauses all Collatinator operations.

  • Returns: bool indicating success

  • Access Control: Only administrators can call this function

  • Events Emitted: CollatinatorPaused(address indexed admin)

Unpauses all Collatinator operations.

  • Returns: bool indicating success

  • Access Control: Only administrators can call this function

  • Events Emitted: CollatinatorUnpaused(address indexed admin)

Storage Layout

The Collatinator Facet uses the Diamond Storage pattern to store its state. The storage layout is defined in the LibCollatinatorStorage library.

Events

The Collatinator Facet emits the following events:

Security Considerations

Reentrancy Protection

The Collatinator Facet uses a reentrancy guard to prevent reentrancy attacks. All external functions that interact with other contracts are protected by the reentrancy guard.

Access Control

The Collatinator Facet uses role-based access control to restrict access to certain functions. The access control is implemented through the Vaultinator Facet.

Pause Mechanism

The Collatinator Facet includes a pause mechanism that allows administrators to pause all operations in case of an emergency.

Health Factor Monitoring

The Collatinator Facet includes health factor monitoring to prevent liquidations. When a position's health factor drops below a certain threshold, the contract can take action to prevent liquidation.

Slippage Protection

The Collatinator Facet includes slippage protection for token exchanges. When creating or modifying positions, users can specify a minimum amount to receive or a maximum amount to pay.

Emergency Repayment

The Collatinator Facet includes an emergency repayment function that allows users to repay their debt in case of an emergency, bypassing some of the normal checks.

Integration with Other Facets

The Collatinator Facet integrates with other facets in the Vaultinator Protocol:

  1. Vaultinator Facet: For vault management and access control

  2. Yieldinator Facet: For using yield-generating assets as collateral

  3. Stakinator Facet: For staking collateral assets

Vaultinator Integration

Yieldinator Integration

Stakinator Integration

Multi-Chain Support

The Collatinator Facet includes support for cross-chain collateral management, allowing users to create positions that span multiple blockchains.

Cross-Chain Position Creation

Cross-Chain Collateral Management

Cross-Chain Debt Management

Supported Lending Protocols

The Collatinator Facet supports multiple lending protocols through its adapter pattern:

  1. Sky.money (formerly MakerDAO): Decentralized stablecoin lending

  2. Aave: Liquidity protocol for lending and borrowing

  3. Compound: Algorithmic money market protocol

  4. Euler Finance: Permissionless lending protocol

  5. Benqi: Liquidity market protocol on Avalanche

  6. Solend: Lending protocol on Solana

Each protocol has its own adapter that implements the ILendingAdapter interface, providing a consistent API for interacting with different protocols.

Conclusion

The Collatinator Facet is a powerful component of the Vaultinator Protocol that enables users to create and manage collateralized positions across multiple lending protocols. Its modular design, security features, and integration with other facets make it a flexible and robust solution for collateralized lending.