Calculate Lending Power

Calculate Lending Power calculates an account's Collateral Power minus Debt Power. If the result is negative it means that the account is undercollaterized. These calculation is performed whenever an action on behalf of an account is performed that reduces account's lending power and during liquidation to check if the liquidated account is undercollaterized.

**'Internal' Inputs:**
- `market_rules`: reference to `MarketRules` the account is using
- `account_reserve_datas`: array reference of `AccountReserveData` structs
    in the order corresponding to AssetIds.
- `account_config`: reference to `AccountConfig` struct of the account
**'External' Inputs:**
- `deposit_fee_reduction_e6`: account deposit fee reduction
- `debt_fee_reduction_e6`: account debt fee reduction
- `prices_e18`: array reference of u128 representing prices
    in the order corresponding to AssetIds.

**Outputs:**
- (bool, u128) - (is lending power positive, absolute number)

**Pseudocode:**
1. Initialize `total_collateral_power_e6` and `total_debt_power_e6` as 0.
2. Iterate over all assets registered in the protocol:
    a. If asset is not used as collateral nor is borrowed 
    (based on account_config bitmaps): continue;
    b. Accumulate interest on `account_reserve_data`of the entry
     corresponding to the asset
        that is iterated on.
    c. If asset that is iterated on is used as collateral based on the `account_config`:
        i. Calculate `collateral_value_e8` based on 'prices_e18', 
         apropatiate 'account_reserve_data' and asset decimals.
        ii. Get `collateral_coefficient_e6` from `market_rule`.
        iii. Update `total_collateral_power_e6` by adding 
         `e8_mul_e6_to_e6_rdown(collateral_value_e8, collateral_coefficient_e6)`.
    f. If asset that is iterated on is borrowed based on the `account_config`:
        i. Calculate `debt_value_e8` based on 'prices_e18', 
         apropatiate 'account_reserve_data' and asset decimals.
        ii. Get `debt_coefficient_e6` from `market_rule` and handle errors.
        iii. Update `total_debt_power_e6` by adding 
         `e8_mul_e6_to_e6_rdown(debt_value_e8, debt_coefficient_e6)`.
3. If `total_collateral_power_e6` is greater than or equal to `total_debt_power_e6`:
    a. Return (true, `total_collateral_power_e6` minus `total_debt_power_e6`).
   Else:
    a. Return (false, `total_debt_power_e6` minus `total_collateral_power_e6`).

Last updated