Skip to main content

Validation

Git Source

SPDX-License-Identifier: GPL-3.0-only

State Variables

MAX_BORROW_PERCENTAGE

uint256 private constant MAX_BORROW_PERCENTAGE = 90;

Functions

getCheckLtvParams

function getCheckLtvParams(
InputParams memory inputParams
) internal pure returns (CheckLtvParams memory checkLtvParams);

validateBalanceAndLiqAndNotSameAssetsSuppliedAndBorrowed

function validateBalanceAndLiqAndNotSameAssetsSuppliedAndBorrowed(
InputParams memory inputParams
) internal pure;

validateLTVAndLeverage

function validateLTVAndLeverage(InputParams memory inputParams, CheckLtvParams memory checkLtvParams) internal pure;

validateSolvency

Added TokenType and uint256s for amount, balance from, and balance to to enable to pass a value for the current balance of a token to avoid one check of a balance that can be done from within a token.

function validateSolvency(
InputParams memory inputParams
) internal pure;

verifyNotSameAssetsSuppliedAndBorrowed

function verifyNotSameAssetsSuppliedAndBorrowed(
uint256 depositedXAssets,
uint256 depositedYAssets,
uint256 borrowedXAssets,
uint256 borrowedYAssets
) internal pure;

verifyMaxBorrowXY

function verifyMaxBorrowXY(
VerifyMaxBorrowXYParams memory params
) internal pure;

verifyMaxBorrowL

function verifyMaxBorrowL(
VerifyMaxBorrowLParams memory params
) internal pure;

getDepositsInL

function getDepositsInL(
InputParams memory inputParams
) private pure returns (uint256 netDepositedXinLAssets, uint256 netDepositedYinLAssets);

getBorrowedInL

function getBorrowedInL(
InputParams memory inputParams
) private pure returns (uint256 netBorrowedXinLAssets, uint256 netBorrowedYinLAssets);

convertXToL

The original math: L * activeLiquidityScalerInQ128 = x / (2 * sqrt(p)) previous equation: amountLAssets = Math.mulDiv(amount, Q128, 2 * sqrtPriceInQ128Range, rounding); adding activeLiquidityScalerInQ128: amountLAssets = (amount * Q128 / (2 * sqrtPriceInQ128Range)) / (activeLiquidityScalerInQ128 / Q128); simplify to: (amount * Q128 * Q128) / (2 * sqrtPriceInQ128Range * activeLiquidityScalerInQ128) final equation: amountLAssets = Math.mulDiv(Math.mulDiv(amount, Q128, sqrtPriceInQ128Range, rounding), Q128, 2 * activeLiquidityScalerInQ128, rounding); or more simplified (failed for some tests) amountLAssets = Math.mulDiv(amount, Q128 * Q128, 2 * sqrtPriceInQ128Range * activeLiquidityScalerInQ128);

function convertXToL(
uint256 amount,
uint256 sqrtPriceInQ128Range,
uint256 activeLiquidityScalerInQ128,
Math.Rounding rounding
) private pure returns (uint256 amountLAssets);

convertYToL

The simplified math: L = y * sqrt(p) / 2 Math.mulDiv(amount, sqrtPriceInQ128Range, 2 * Q128, rounding); amountLAssets = amount * sqrtPriceInQ128RangeScaled / 2 * Q128 sqrtPriceInQ128RangeScaled = sqrtPriceInQ128Range / activeLiquidityScalerInQ128 / Q128; simplify to: amount * sqrtPriceInQ128Range / activeLiquidityScalerInQ128 / Q128 / 2 * Q128 simplify to: (amount * sqrtPriceInQ128Range) / (activeLiquidityScalerInQ128 * 2) final equation: amountLAssets = Math.mulDiv(amount, sqrtPriceInQ128Range, 2 * activeLiquidityScalerInQ128, rounding);

function convertYToL(
uint256 amount,
uint256 sqrtPriceInQ128Range,
uint256 activeLiquidityScalerInQ128,
Math.Rounding rounding
) private pure returns (uint256 amountLAssets);

calcDebtAndCollateral

function calcDebtAndCollateral(
CheckLtvParams memory checkLtvParams,
InputParams memory inputParams
) internal pure returns (uint256 debtLiquidityAssets, uint256 collateralLiquidityAssets);

checkLtv

function checkLtv(
CheckLtvParams memory checkLtvParams,
InputParams memory inputParams
) private pure returns (uint256 debtLiquidityAssets, uint256 collateralLiquidityAssets);

increaseForSlippage

Calculates the impact slippage of buying the debt in the dex using the currently available liquidity L=xyL = \sqrt{x \cdot y}. Uses a few formulas to simplify to not need reserves to calculate the required collateral to buy the debt. Let the fee be represented as

ϕ=1fee\phi = 1 - fee

The reserves available in and out for swapping can be defined in terms of LL, and price pp

reserveIn=LpreserveOut=Lp\begin{align*} reserveIn &= L \cdot \sqrt{p} \\ reserveOut &= \frac{L}{ \sqrt{p} } \\ \end{align*}

The swap amount inin and outout can also be defined in terms of LinL_{in} and LoutL_{out}

in=Lin2pout=2Loutp\begin{align*} in &= L_{in} \cdot 2 \cdot \sqrt{p} out &= \frac{ 2 \cdot L_{out} }{ \sqrt{p} } \end{align*}

Starting with our swap equation we solve for inin

(inϕ+reserveIn)(reserveOutout)=reserveOutreserveIninϕ=reserveOutreserveInreserveOutoutreserveIninϕ=reserveIn(reserveOutreserveOutout1)inϕ=reserveInreserveOut(reserveOutout)reserveOutoutinϕ=reserveInoutreserveOutout\begin{align*} (in \cdot \phi + reserveIn)(reserveOut - out) &= reserveOut \cdot reserveIn \\ in \cdot \phi &= \frac{reserveOut \cdot reserveIn} {reserveOut - out} - reserveIn \\ in \cdot \phi &= reserveIn \cdot \left(\frac{reserveOut } {reserveOut - out} - 1 \right) \\ in \cdot \phi &= reserveIn \cdot \frac{ reserveOut - (reserveOut - out) } { reserveOut - out } \\ in \cdot \phi &= \frac{ reserveIn \cdot out } { reserveOut - out } \\ \end{align*}

We now plug in liquidity values in place of reserveInreserveIn, reserveOutreserveOut, inin, and outout.

Lin2pϕ=Lp2LoutpLp2LoutpLinϕ=Lp2Loutp2p(Lp2Loutp)Lin=LLoutϕ(L2Lout)\begin{align*} L_{in} \cdot 2 \cdot \sqrt{p} \cdot \phi &= \frac{ L \cdot \sqrt{p} \cdot \frac{ 2 \cdot L_{out} }{ \sqrt{p} } } { \frac{L}{ \sqrt{p} } - \frac{ 2 \cdot L_{out} }{\sqrt{p} } } \\ L_{in} \cdot \phi &= \frac{ L \cdot \sqrt{p} \cdot \frac{ 2 \cdot L_{out} }{ \sqrt{p} } } { 2 \cdot \sqrt{p} \cdot \left(\frac{L}{ \sqrt{p} } - \frac{ 2 \cdot L_{out} }{\sqrt{p} }\right)} \\ L_{in} &= \frac { L \cdot L_{out} } { \phi \cdot (L - 2 \cdot L_{out}) } \\ \end{align*}

Using LoutL_{out} described in our method as debtLiquidityAssets, LL or activeLiquidityAssets, and our fee, we use the above equation to solve for the amount of liquidity that must come in to buy the debt.

function increaseForSlippage(
uint256 debtLiquidityAssets,
uint256 activeLiquidityAssets
) private pure returns (uint256);

Parameters

NameTypeDescription
debtLiquidityAssetsuint256The amount of debt with units of L that will need to be purchased in case of liquidation.
activeLiquidityAssetsuint256The amount of liquidity in the pool available to swap against.

checkLeverage

function checkLeverage(
CheckLtvParams memory checkLtvParams
) private pure;

Errors

InsufficientLiquidity

error InsufficientLiquidity();

AmmalgamCannotBorrowAgainstSameCollateral

error AmmalgamCannotBorrowAgainstSameCollateral();

AmmalgamMaxBorrowReached

error AmmalgamMaxBorrowReached();

AmmalgamDepositIsNotStrictlyBigger

error AmmalgamDepositIsNotStrictlyBigger();

AmmalgamLTV

error AmmalgamLTV();

AmmalgamMaxSlippage

error AmmalgamMaxSlippage();

AmmalgamTooMuchLeverage

error AmmalgamTooMuchLeverage();

AmmalgamTransferAmtExceedsBalance

error AmmalgamTransferAmtExceedsBalance();

Structs

InputParams

struct InputParams {
uint256[6] userAssets;
uint256 sqrtPriceMinInQ128;
uint256 sqrtPriceMaxInQ128;
uint256 activeLiquidityScalerInQ128;
uint256 activeLiquidityAssets;
}

CheckLtvParams

struct CheckLtvParams {
uint256 netDepositedXinLAssets;
uint256 netDepositedYinLAssets;
uint256 netBorrowedXinLAssets;
uint256 netBorrowedYinLAssets;
uint256 depositedLAssets;
}

VerifyMaxBorrowXYParams

struct VerifyMaxBorrowXYParams {
uint256 amount;
uint256 depositedAssets;
uint256 borrowedAssets;
uint256 reserve;
uint256 totalLiquidityAssets;
uint256 borrowedLiquidityAssets;
}

VerifyMaxBorrowLParams

struct VerifyMaxBorrowLParams {
uint256[6] totalAssets;
uint256 newBorrowedLAssets;
uint256 reserveXAssets;
uint256 reserveYAssets;
}