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;

ONE_HUNDRED_TIMES_N

uint256 private constant ONE_HUNDRED_TIMES_N = 2000;

TWO_Q64

uint256 private constant TWO_Q64 = 0x20000000000000000;

FIVE_Q64

uint256 private constant FIVE_Q64 = 0x50000000000000000;

NINE_Q64

uint256 private constant NINE_Q64 = 0x90000000000000000;

FIFTY_Q64

uint256 private constant FIFTY_Q64 = 0x320000000000000000;

TWO_TIMES_N_Q64

uint256 private constant TWO_TIMES_N_Q64 = 0x280000000000000000;

TWO_Q128

uint256 private constant TWO_Q128 = 0x200000000000000000000000000000000;

TWO_THOUSAND_FIVE_HUNDRED_Q128

uint256 private constant TWO_THOUSAND_FIVE_HUNDRED_Q128 = 0x9c400000000000000000000000000000000;

Functions

getInputParams

function getInputParams(
uint128[6] memory currentAssets,
uint256[6] memory userAssets,
uint256 reserveXAssets,
uint256 reserveYAssets,
uint256 externalLiquidity,
int16 minTick,
int16 maxTick
) internal pure returns (Validation.InputParams memory inputParams);

getCheckLtvParams

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

validateBalanceAndLiqAndNotSameAssetsSuppliedAndBorrowed

function validateBalanceAndLiqAndNotSameAssetsSuppliedAndBorrowed(
InputParams memory inputParams
) internal pure;

validateLTVAndLeverage

function validateLTVAndLeverage(CheckLtvParams memory checkLtvParams, uint256 activeLiquidityAssets) 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 * activeLiquidityScalerInQ72 = x / (2 * sqrt(p)) previous equation: amountLAssets = mulDiv(amount, Q72, 2 * sqrtPriceInXInQ72, rounding); adding activeLiquidityScalerInQ72: amountLAssets = (amount * Q72 / (2 * sqrtPriceInXInQ72)) / (activeLiquidityScalerInQ72 / Q72); simplify to: (amount * Q72 * Q72) / (2 * sqrtPriceInXInQ72 * activeLiquidityScalerInQ72) final equation: amountLAssets = mulDiv(mulDiv(amount, Q72, sqrtPriceInXInQ72, rounding), Q72, 2 * activeLiquidityScalerInQ72, rounding); or more simplified (failed for some tests) amountLAssets = mulDiv(amount, Q72 * Q72, 2 * sqrtPriceInQ72 * activeLiquidityScalerInQ72);

function convertXToL(
uint256 amountInXAssets,
uint256 sqrtPriceInXInQ72,
uint256 activeLiquidityScalerInQ72,
bool roundUp
) internal pure returns (uint256 amountLAssets);

convertLToX

function convertLToX(
uint256 amount,
uint256 sqrtPriceQ72,
uint256 activeLiquidityScalerInQ72,
bool roundUp
) internal pure returns (uint256 amountXAssets);

convertYToL

The simplified math: L = y * sqrt(p) / 2 mulDiv(amount, sqrtPriceInXInQ72, 2 * Q72, rounding); amountLAssets = amount * sqrtPriceInXInQ72Scaled / (2 * Q72) sqrtPriceInXInQ72Scaled = sqrtPriceInXInQ72 / activeLiquidityScalerInQ72 / Q72; simplify to: amount * sqrtPriceInXInQ72 / activeLiquidityScalerInQ72 / Q72 / (2 * Q72) simplify to: (amount * sqrtPriceInXInQ72 * Q56) / (activeLiquidityScalerInQ72 * 2) final equation: amountLAssets = mulDiv(amount, sqrtPriceInXInQ72 * Q56, 2 * activeLiquidityScalerInQ72, rounding);

function convertYToL(
uint256 amountInYAssets,
uint256 sqrtPriceInXInQ72,
uint256 activeLiquidityScalerInQ72,
bool roundUp
) internal pure returns (uint256 amountInLAssets);

convertLToY

function convertLToY(
uint256 amount,
uint256 sqrtPriceQ72,
uint256 activeLiquidityScalerInQ72,
bool roundUp
) internal pure returns (uint256 amountYAssets);

calcDebtAndCollateral

function calcDebtAndCollateral(
CheckLtvParams memory checkLtvParams
) internal pure returns (uint256 debtLiquidityAssets, uint256 collateralLiquidityAssets, bool netDebtX);

checkLtv

function checkLtv(
CheckLtvParams memory checkLtvParams,
uint256 activeLiquidityAssets
) 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. 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 + reserveIn)(reserveOut - out) &= reserveOut \cdot reserveIn \\ in &= \frac{reserveOut \cdot reserveIn} {reserveOut - out} - reserveIn \\ in &= reserveIn \cdot \left(\frac{reserveOut } {reserveOut - out} - 1 \right) \\ in &= reserveIn \cdot \frac{ reserveOut - (reserveOut - out) } { reserveOut - out } \\ in &= \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} &= \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} &= \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} } { (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
) internal 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 sqrtPriceMinInQ72;
uint256 sqrtPriceMaxInQ72;
uint256 activeLiquidityScalerInQ72;
uint256 activeLiquidityAssets;
uint256 reservesXAssets;
uint256 reservesYAssets;
}

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;
}