Skip to main content

Risk Engine: Cross-Margining, Liquidation Logic

Ammalgam's Risk Engine secures the protocol through an integrated system of cross-margining, dynamic liquidation mechanics, and internal price safeguards. Users can borrow across multiple collateral types - tokens (XX, YY) and liquidity shares (LL) - all managed within a single unified pool. Loans are validated against a Loan-to-Value (LTV) framework that continuously adjusts to each user's position and the real-time conditions of the pools.

Loan to Value

Each loan must be worth less than 75% of collateral provided to secure it, and often much less depending on the depth of liquidity with respect to the size of the loan. When borrowing one asset against the other, the calculation of loan to value (LTV) is as expected, LTV=DebtValueCollateralValueL_{TV} = \frac{Debt Value}{ Collateral Value}. When we calculate the loan to value for market making positions, the calculation may not be as obvious. First we look at what we call LXL_X and LYL_Y, the individual quantity of XX and YY in the market making position and net it with the amount of XX and YY deposited and borrowed. If the net of XX or YY is negative, then that asset is treated as the debt and the other asset is treated as the collateral. We use the following equation to calculate LTV:

LTV={(X+LX)Y+LXif X+LX<0(Y+LY)X+LXif Y+LY<0L_{TV} = \begin{cases} \frac{-(X + L_X)}{Y + L_X} & \text{if } X + L_X < 0 \\ \frac{-(Y + L_Y)}{X + L_X} & \text{if } Y + L_Y < 0 \\ \end{cases}

Each variable XX, YY, LXL_X, & LYL_Y can be negative or positive depending if it represents debt or collateral. If both X+LX<0X + L_X < 0 and Y+LY<0Y + L_Y < 0, there is no collateral and the validation will fail and if both values are positive, there is no net debt and the validation of LTV passes.

Slippage added to LTV

To deter large unsustainable loans, we consider slippage when calculating the LTV. To consider the slippage created when needing to liquidate collateral to repay the debt, we consider the amount of collateral needed to buy the requested debt using the current reserves. The needed collateral is then compared to the total collateral provided to ensure it fits within the allowed LTV. This will have a negligible impact on small loans concerning available liquidity and more restrictive for large loans. Flash loan manipulation attacks like C.R.E.A.M Finance and Mango become impossible when the slippage would require all the liquidity in the protocol pushing the LTV price from its high manipulated price back down to zero (the price required to buy all of the debt from the AMM to fund liquidation when the debt is the size of all of that asset in the AMM). Similar to Tranche Limitations, these limits could be expanded by the same configuration for more liquid trading pairs in which arbitrageurs can be assumed.

NatSpec and Implementation ↗

Cost to liquidate X or Y