Skip to main content

Validation

Git Source

SPDX-License-Identifier: GPL-3.0-only

State Variables​

MAX_BORROW_PERCENTAGE​

uint256 internal constant MAX_BORROW_PERCENTAGE = 90;

Functions​

getInputParams​

Get the input parameters for the validation

hasBorrow is set to true here, because we assume that the caller has verified there is a borrowed asset

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

Parameters

NameTypeDescription
userAssetsuint256[6]The user assets of the pool
activeLiquidityAssetsuint256The current active liquidity assets of the pool
reserveXAssetsuint256The reserve of the X asset
reserveYAssetsuint256The reserve of the Y asset
externalLiquidityuint256The external liquidity of the pool
minTickint16The min tick of the pool
maxTickint16The max tick of the pool

Returns

NameTypeDescription
inputParamsValidation.InputParamsThe input parameters for the validation

getCheckLtvParams​

Get the check LTV parameters for needed for validateLTVAndLeverage()

the sqrt prices are in the input params, but by passing them we allow for the ability to switch them as needed in liquidation and other cases.

function getCheckLtvParams(
uint256[6] memory userAssets,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) internal pure returns (CheckLtvParams memory checkLtvParams);

Parameters

NameTypeDescription
userAssetsuint256[6]User asset array
sqrtPriceMinInQ72uint256The minimum sqrt price in Q72
sqrtPriceMaxInQ72uint256The maximum sqrt price in Q72

validateBalanceAndLiqAndNotSameAssetsSuppliedAndBorrowed​

Verifies that debt is backed by liquidity from an independent provider and that users do not borrow the same underlying assets they supply as collateral.

function validateBalanceAndLiqAndNotSameAssetsSuppliedAndBorrowed(
uint256[6] memory userAssets,
uint256 activeLiquidityAssets
) internal pure;

Parameters

NameTypeDescription
userAssetsuint256[6]The account's assets by token type.
activeLiquidityAssetsuint256The pair's active liquidity assets.

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(
uint256[6] memory userAssets,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72,
uint256 activeLiquidityAssets
) external pure;

verifyNotSameAssetsSuppliedAndBorrowed​

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

verifyMaxBorrow​

function verifyMaxBorrow(
VerifyMaxBorrowParams memory params
) internal pure;

getDepositsInL​

function getDepositsInL(
uint256[6] memory userAssets,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) private pure returns (uint256 netDepositedXinLAssets, uint256 netDepositedYinLAssets);

getBorrowedInL​

function getBorrowedInL(
uint256[6] memory userAssets,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) private pure returns (uint256 netBorrowedXinLAssets, uint256 netBorrowedYinLAssets);

convertXToL​

Convert X assets to L assets: L = x / sqrt(p) amountLAssets = amountInXAssets * Q72 / sqrtPriceInXInQ72

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

convertLToX​

Convert L assets to X assets: x = L * sqrt(p) amountXAssets = amount * sqrtPriceQ72 / Q72

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

convertYToL​

Convert Y assets to L assets: L = y * sqrt(p) amountLAssets = amountInYAssets * sqrtPriceInXInQ72 / Q72

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

convertLToY​

Convert L assets to Y assets: y = L / sqrt(p) amountYAssets = amount * Q72 / sqrtPriceQ72

function convertLToY(
uint256 amount,
uint256 sqrtPriceQ72,
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​

Increases an L-denominated debt amount by the constant-product slippage needed to buy that debt against the pool's active liquidity. Let D be debtLiquidityAssets and L be activeLiquidityAssets. The function reverts when D >= L; otherwise it returns:

Din=⌈L⋅DL−D⌉D_{in}=\left\lceil\frac{L\cdot D}{L-D}\right\rceil

This is the amount of L assets that must enter the pool to remove D L assets of debt from the opposite reserve under the constant-product approximation.

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;
int16 minTick;
int16 maxTick;
uint256 sqrtPriceMinInQ72;
uint256 sqrtPriceMaxInQ72;
uint256 activeLiquidityAssets;
uint256 reservesXAssets;
uint256 reservesYAssets;
bool hasBorrow;
}

CheckLtvParams​

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

VerifyMaxBorrowParams​

struct VerifyMaxBorrowParams {
uint256 depositedAssets;
uint256 borrowedAssets;
uint256 reserve;
uint256 totalDepositedLAssets;
uint256 totalBorrowedLAssets;
}