VaultUtils
The VaultUtils contract provides utility functions for the Vault, handling fee calculations, funding rate updates, and position validation. It serves as a companion contract to the main Vault, allowing for modular upgrades and improved code organization.Contract Overview
The VaultUtils handles:- Validation of position liquidations
- Calculation of position fees
- Calculation of funding fees
- Dynamic fee adjustments based on pool utilization
- Swap fee calculations
The VaultUtils contract is designed to be upgradable separately from the main Vault, allowing for improvements to fee calculation and validation logic without changing the core Vault contract.
Key Functions
Liquidation Validation
Fee Calculations
Integration with Vault
The VaultUtils is tightly integrated with the Vault contract:- The Vault calls VaultUtils for fee calculations and position validations
- VaultUtils accesses the Vault’s state to perform its calculations
- Only the Vault can set itself as the vault address in VaultUtils
- More modular code organization
- Easier upgradeability for fee calculations
- Reduced complexity in the main Vault contract
Liquidation Mechanics
ThevalidateLiquidation
function is a critical component for the protocol’s position liquidation system:
-
It checks if a position has sufficient collateral based on:
- Current leverage vs. maximum allowed leverage
- Position size and collateral
- Current market price
- Accrued funding fees
-
It returns liquidation values including:
- Liquidation state (0 = cannot liquidate, 1 = can liquidate, 2 = can liquidate with high leverage)
- Margin fee amount
The liquidation validation logic is crucial for the protocol’s security. It ensures positions are liquidated when they pose a risk to the protocol, while protecting users from unfair liquidations.
Dynamic Fee System
The VaultUtils implements a dynamic fee system that adjusts fees based on pool utilization:- Balance token pools by incentivizing balancing actions
- Discourage actions that would imbalance pools
- Provide price stability for token swaps
Security Considerations
The VaultUtils implements several security features:- Access Control: Only the Vault contract can set itself as the vault address
- Parameter Validation: Checks for reasonable values in fee calculations
- Liquidation Safety: Validates liquidations based on multiple criteria
- Dynamic Fees: Adjusts fees to protect the protocol from imbalances
The VaultUtils contract cannot be directly accessed by users. All interactions flow through the main Vault contract.
Example: Fee Calculation Flow
1
Position Increase Request
When a user requests to increase a position, the Vault calls VaultUtils for fee calculation.
2
Fee Calculation
The VaultUtils calculates the fee based on the position size and fee configuration.
3
Funding Fee Calculation
When a position is decreased, the Vault calls VaultUtils to calculate funding fees.