Vault

Address:0x049c48104209adfad46c2133e6a0be4aee4d5a41f03ffffc602b438a00d8e760

Summary of the Vault Contract Code

The provided code defines a Vault contract for a StarkNet-based decentralized application. This contract manages collateral deposits, mints a synthetic asset (mUSD), and handles liquidation processes. Below is a detailed summary of the code:

Purpose

The Vault contract's primary functions are to manage user collateral, mint mUSD against deposited collateral, and ensure the system remains solvent through liquidation mechanisms.

Key Components

  1. Interfaces:

    • IstarUSD Interface:

      • Defines methods to mint and burn mUSD.

      • Methods:

        • mint(ref self: T, recipient: ContractAddress, amount: u128): Mints mUSD for a user.

        • burn(ref self: T, amount: u128, recipient: ContractAddress): Burns mUSD from a user.

    • IERC20 Interface:

      • Defines standard ERC20 token methods.

      • Methods:

        • name(self: @TContractState) -> felt252: Returns the token's name.

        • symbol(self: @TContractState) -> felt252: Returns the token's symbol.

        • decimals(self: @TContractState) -> u8: Returns the token's decimals.

        • total_supply(self: @TContractState) -> u128: Returns the total supply of the token.

        • balance_of(self: @TContractState, account: ContractAddress) -> u128: Returns the balance of a specific account.

        • allowance(self: @TContractState, owner: ContractAddress, spender: ContractAddress) -> u128: Returns the allowance of a spender.

        • transfer(ref self: TContractState, recipient: ContractAddress, amount: u128) -> bool: Transfers tokens to a recipient.

        • transfer_from(ref self: TContractState, sender: ContractAddress, recipient: ContractAddress, amount: u128) -> bool: Transfers tokens from a sender to a recipient.

        • approve(ref self: TContractState, spender: ContractAddress, amount: u128) -> bool: Approves a spender to spend tokens.

    • IAggregatorPriceConsumer Interface:

      • Defines methods to get the latest price from a price feed.

      • Methods:

        • get_latest_price(self: @TContractState) -> u128: Returns the latest price from the price feed.

    • IVault Interface:

      • Defines methods to get the total value of the vault and the collateral value for a specific user.

      • Methods:

        • getTotalValue(ref self: TContractState) -> u128: Returns the total value of the vault.

        • getAccountCollateralValue(ref self: TContractState, user: ContractAddress) -> u128: Returns the collateral value for a specific user.

  2. Vault Contract:

    • The main contract module implementing the Vault functionalities.

    • Storage Structure:

      • liquidation_threshold: u128: The threshold for liquidation.

      • liquidation_bonus: u128: The bonus awarded during liquidation.

      • liquidation_precision: u128: The precision used for liquidation calculations.

      • priceFeeds: LegacyMap<ContractAddress, ContractAddress>: Maps collateral tokens to their respective price feed addresses.

      • collateralDeposited: LegacyMap<(ContractAddress, ContractAddress), u128>: Maps user collateral deposits.

      • mUSDminted: LegacyMap<ContractAddress, u128>: Maps the amount of mUSD minted by users.

      • collaterals: List<ContractAddress>: List of acceptable collateral tokens.

      • mUSDAddr: ContractAddress: Address of the mUSD contract.

      • totalValue: u128: Total value of mUSD in the platform.

      • collateralValue: LegacyMap<ContractAddress, u128>: Maps collateral tokens to their total value.

      • reentrancy_guard: ReentrancyGuardComponent::Storage: Storage for reentrancy guard.

    • Events:

      • CollateralDeposit: Emitted when collateral is deposited.

      • CollateralRedeemed: Emitted when collateral is redeemed.

    • Constructor:

      • Initializes the contract with a list of acceptable collateral tokens and the mUSD contract address.

    • External Functions:

      • set_price_feeds(ref self: ContractState, collateral: ContractAddress, priceConsumerAddr: ContractAddress): Sets the price feed for a collateral token.

      • depositCollaterlAndMintMusd(ref self: ContractState, collateralToken: ContractAddress, amountCollateral: u128, amountMusdMint: u128): Deposits collateral and mints mUSD for the user.

      • redeemCollateral(ref self: ContractState, collateralToken: ContractAddress, amount: u128, amountMusdBurn: u128): Redeems collateral and burns mUSD from the user.

      • liquidateInVault(ref self: ContractState, collateralToken: ContractAddress, debtToConver: u128): Liquidates collateral in the vault to cover a user's debt.

    • ABI Implementation:

      • Implements the IVault interface to provide the total value of the vault and the collateral value for a specific user.

  3. Helper Functions:

    • _getAmontFromUsd(ref self: ContractState, amount: u128, collateralToken: ContractAddress) -> u128: Converts a USD amount to the collateral token amount using the price feed.

    • _getUsdValue(ref self: ContractState, amount: u128, collateralToken: ContractAddress) -> u128: Converts a collateral token amount to USD using the price feed.

    • _reedemCollateral(ref self: ContractState, user: ContractAddress, collateralToken: ContractAddress, amount: u128): Redeems collateral for a user.

    • _burnMUSD(ref self: ContractState, user: ContractAddress, amount: u128): Burns mUSD from a user.

Key Points

  • The Vault contract securely manages user collateral deposits and mints mUSD against them.

  • It ensures system solvency through liquidation mechanisms, which are triggered when the user's collateral falls below a certain threshold.

  • The contract interacts with various other contracts (ERC20, starUSD, price feeds) to perform its operations.

  • Reentrancy guards are implemented to prevent reentrancy attacks.

  • Price conversions and collateral calculations are handled using helper functions, ensuring accurate and fair assessments of collateral value.

This design ensures that users can securely deposit collateral, mint mUSD, and manage their positions, while the platform maintains overall system stability and solvency.

Last updated