Skip to main content
Coming Soon: The Developers section is under active development. Documentation will be available soon.

Overview

The CollateralManager handles all collateral operations including deposits, withdrawals, and liquidations.

Functions

deposit

function deposit(
    address asset,
    uint256 amount
) external
Deposit collateral to enable borrowing.

withdraw

function withdraw(
    address asset,
    uint256 amount
) external
Withdraw collateral (must maintain health factor > 1).

liquidate

function liquidate(
    address user,
    address debtAsset,
    uint256 debtAmount,
    address collateralAsset
) external returns (uint256 collateralSeized)
Liquidate an underwater position.

View Functions

getCollateralBalance

function getCollateralBalance(
    address user,
    address asset
) external view returns (uint256)

getCollateralValue

function getCollateralValue(
    address user
) external view returns (uint256)

getBorrowingPower

function getBorrowingPower(
    address user
) external view returns (uint256)

getHealthFactor

function getHealthFactor(
    address user
) external view returns (uint256)
Returns health factor with 18 decimals (1e18 = 1.0).

Collateral Parameters

getAssetConfig

function getAssetConfig(
    address asset
) external view returns (AssetConfig memory)

AssetConfig

struct AssetConfig {
    uint256 ltv;                    // Max loan-to-value (8000 = 80%)
    uint256 liquidationThreshold;   // Liquidation point (8500 = 85%)
    uint256 liquidationPenalty;     // Penalty (500 = 5%)
    address priceFeed;              // Oracle address
    bool enabled;                   // Is collateral enabled
}

Liquidation Mechanics

Health Factor Calculation

healthFactor = totalCollateralValue × weightedLiquidationThreshold
               ─────────────────────────────────────────────────────
                              totalDebtValue

Liquidation Process

// Example liquidation
function liquidate(
    address user,
    address debtAsset,
    uint256 debtAmount,
    address collateralAsset
) external {
    // 1. Verify user is liquidatable
    require(getHealthFactor(user) < 1e18, "Not liquidatable");

    // 2. Calculate collateral to seize
    uint256 debtValue = getAssetValue(debtAsset, debtAmount);
    uint256 penalty = getAssetConfig(collateralAsset).liquidationPenalty;
    uint256 collateralToSeize = debtValue * (10000 + penalty) / 10000;

    // 3. Transfer debt from liquidator
    IERC20(debtAsset).transferFrom(msg.sender, address(this), debtAmount);

    // 4. Reduce user's debt
    _reduceDebt(user, debtAsset, debtAmount);

    // 5. Transfer collateral to liquidator
    _transferCollateral(user, msg.sender, collateralAsset, collateralToSeize);
}

Events

event CollateralDeposited(
    address indexed user,
    address indexed asset,
    uint256 amount
);

event CollateralWithdrawn(
    address indexed user,
    address indexed asset,
    uint256 amount
);

event Liquidation(
    address indexed user,
    address indexed liquidator,
    address debtAsset,
    uint256 debtCovered,
    address collateralAsset,
    uint256 collateralSeized
);

Integration Example

const collateralManager = new ethers.Contract(
    COLLATERAL_MANAGER_ADDRESS,
    COLLATERAL_MANAGER_ABI,
    signer
);

// Deposit ETH as collateral
const weth = new ethers.Contract(WETH_ADDRESS, ERC20_ABI, signer);
await weth.approve(COLLATERAL_MANAGER_ADDRESS, amount);
await collateralManager.deposit(WETH_ADDRESS, amount);

// Check borrowing power
const power = await collateralManager.getBorrowingPower(userAddress);
console.log(`Can borrow up to: ${ethers.formatUnits(power, 6)} USDC`);

// Check health factor
const hf = await collateralManager.getHealthFactor(userAddress);
console.log(`Health factor: ${ethers.formatUnits(hf, 18)}`);