# Workings of Liquidation Engine [Source Code of `Liquidator.sol` from PR#13](https://github.com/arcadia-finance/lending-v2/blob/b4619258f5eedf8e2355fcd65f3faa7542321836/src/Liquidator.sol) ## Liquidation Engine ### Multipliers as Percentages: 1. **Start Price Multiplier:** - **Definition:** `startPriceMultiplier` is the multiplier that determines the initial price of the auction. - **Precision:** It is defined with 2 decimals precision. - **Usage:** It is used to increase the initial price of the auction. The auction starts at a multiplier of the open debt, and then it decreases rapidly (exponential decay). 2. **Minimum Price Multiplier:** - **Definition:** `minPriceMultiplier` sets the minimum price the auction converges to. - **Precision:** It is defined with 2 decimals precision. - **Usage:** It sets a lower bound to which the auction price converges. ### Calculation of the Base: - **Formula:** The base of the auction price curve is calculated using the provided `halfLifeTime` parameter. - The relationship between `halfLifeTime` and the base is given by: $\text{base} = \frac{1}{2^{1/\text{{halfLifeTime}}}}$ - It's based on the formula for exponential decay: $(N(t) = N(0) \cdot 2^{-t/\text{halfLifeTime}}$. - The base is derived to ensure a halving of the value in the specified `halfLifeTime`. ### Calculation of Asked Shares: - **Definition:** `askedShares` represent the total percentage of asset shares that the bidder wants to buy in the auction. - **Calculation:** It is calculated based on the distribution of asset shares and the amount of each asset that the bidder wants to buy. - $\text{askedShares} = \sum_{i=1}^{n} \left( \frac{{\text{{assetShares}}_i \times \text{askedAssetAmounts}_i}}{{\text{assetAmounts}_i}} \right)$ - It represents the total share of assets the bidder is interested in, considering the distribution in the auction. ### Calculation of Ask Price: - **Formula:** The ask price is calculated based on the exponential decay function and the bidder's requested asset shares. - $\text{askPrice}$\=$\frac{{\text{startPrice} \cdot \left( \text{LogExpMath.pow(base, timePassed)} \cdot (\text{startPriceMultiplier} - \text{minPriceMultiplier}) + 1e18 \cdot \text{uint256(minPriceMultiplier)} \right)}}{{1e20 \cdot \frac{{\text{{totalShares}}}}{{\text{askedShares}}}}}$ - Base is already in 18 decimals, hence timePassed here has been scaled to 18 decimals. Since the outpit of `pow` also yields to 18 decimals, hence the aguend in the numerator has not been scaled. - Since the numerator is percentage scaled to `1e18`, hence the denominator here is `1e20`. - It considers the decay over time, the initial price, and the bidder's interest in specific asset shares. ### Summary: - The `startPriceMultiplier` and `minPriceMultiplier` determine the initial and minimum prices of the auction. - The base is calculated to model exponential decay over time in the auction curve. - Asked shares represent the bidder's interest in specific assets, and their calculation considers the distribution of asset shares. - The ask price is then computed using the calculated base, time passed, and the bidder's requested asset shares. This approach ensures a dynamic and controlled liquidation process, taking into account the passage of time and bidder preferences for specific assets. ## How's the start price calculated? **Breakdown:** 1. **Base Debt Calculation:** - `auctionInformation_.startDebt`: This is the initial debt of the account that is being liquidated. 2. **Initiator Reward:** - `auctionInformation_.liquidationInitiatorReward`: This represents the reward for the liquidation initiator. 3. **Closer's Reward:** - `auctionInformation_.auctionClosingReward`: This represents the reward for the one who successfully closes the auction. 4. **Liquidation Penalty:** - `uint256(auctionInformation_.startDebt) * auctionInformation_.liquidationPenaltyWeight / 100`: This calculates the penalty applied to the initial debt. The `auctionInformation_.liquidationPenaltyWeight` is a percentage, and this line converts it to an absolute value by multiplying it with the initial debt and dividing by 100. Now, these components are summed up to calculate the total value that contributes to the starting price. This total value is then used in the `_calculateStartPrice` function along with the `auctionInformation_.startPriceMultiplier` to determine the actual `startPrice` for the auction. This calculation takes into account the initial debt, rewards for the initiator and closer, and the penalty applied to the debt. The `startPriceMultiplier` influences how these components are scaled to set the starting price for the auction.