Validation
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
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(
uint112[6] memory currentAssets,
uint256[6] memory userAssets,
uint256 reserveXAssets,
uint256 reserveYAssets,
uint256 externalLiquidity,
int16 minTick,
int16 maxTick
) internal pure returns (Validation.InputParams memory inputParams);
Parameters
| Name | Type | Description |
|---|---|---|
currentAssets | uint112[6] | The current assets of the pool |
userAssets | uint256[6] | The user assets of the pool |
reserveXAssets | uint256 | The reserve of the X asset |
reserveYAssets | uint256 | The reserve of the Y asset |
externalLiquidity | uint256 | The external liquidity of the pool |
minTick | int16 | The min tick of the pool |
maxTick | int16 | The max tick of the pool |
Returns
| Name | Type | Description |
|---|---|---|
inputParams | Validation.InputParams | The 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 activeLiquidityScalerInQ72,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) internal pure returns (CheckLtvParams memory checkLtvParams);
Parameters
| Name | Type | Description |
|---|---|---|
userAssets | uint256[6] | User asset array |
activeLiquidityScalerInQ72 | uint256 | The active liquidity scaler in Q72 |
sqrtPriceMinInQ72 | uint256 | The minimum sqrt price in Q72 |
sqrtPriceMaxInQ72 | uint256 | The maximum sqrt price in Q72 |
validateBalanceAndLiqAndNotSameAssetsSuppliedAndBorrowed
function validateBalanceAndLiqAndNotSameAssetsSuppliedAndBorrowed(
uint256[6] memory userAssets,
uint256 activeLiquidityAssets
) 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(
uint256[6] memory userAssets,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72,
uint256 activeLiquidityScalerInQ72,
uint256 activeLiquidityAssets
) internal 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 activeLiquidityScalerInQ72,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) private pure returns (uint256 netDepositedXinLAssets, uint256 netDepositedYinLAssets);
getBorrowedInL
function getBorrowedInL(
uint256[6] memory userAssets,
uint256 activeLiquidityScalerInQ72,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) private pure returns (uint256 netBorrowedXinLAssets, uint256 netBorrowedYinLAssets);
convertXToL
The original math: L * activeLiquidityScalerInQ72 = x / sqrt(p) previous equation: amountLAssets = mulDiv(amount, Q72, sqrtPriceInXInQ72, rounding); adding activeLiquidityScalerInQ72: amountLAssets = (amount * Q72 / sqrtPriceInXInQ72) / (activeLiquidityScalerInQ72 / Q72); simplify to: (amount * Q72 * Q72) / (sqrtPriceInXInQ72 * activeLiquidityScalerInQ72) final equation: amountLAssets = mulDiv(mulDiv(amount, Q72, sqrtPriceInXInQ72, rounding), Q72, activeLiquidityScalerInQ72, rounding); or more simplified (failed for some tests) amountLAssets = mulDiv(amount, Q72 * Q72, 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) mulDiv(amount, sqrtPriceInXInQ72, rounding); amountLAssets = amount * sqrtPriceInXInQ72Scaled / Q72; sqrtPriceInXInQ72Scaled = sqrtPriceInXInQ72 / activeLiquidityScalerInQ72 / Q72; simplify to: amount * sqrtPriceInXInQ72 / activeLiquidityScalerInQ72 final equation: amountLAssets = mulDiv(amount, sqrtPriceInXInQ72, 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 . 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 , and price
The swap amount and can also be defined in terms of and
Starting with our swap equation we solve for
We now plug in liquidity values in place of , , , and .
Using described in our method as debtLiquidityAssets, or activeLiquidityAssets,
and our fee, we use the above equation to solve for the amount of liquidity that
must come into buy the debt.
function increaseForSlippage(
uint256 debtLiquidityAssets,
uint256 activeLiquidityAssets
) internal pure returns (uint256);
Parameters
| Name | Type | Description |
|---|---|---|
debtLiquidityAssets | uint256 | The amount of debt with units of L that will need to be purchased in case of liquidation. |
activeLiquidityAssets | uint256 | The 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 activeLiquidityScalerInQ72;
uint256 activeLiquidityAssets;
uint256 reservesXAssets;
uint256 reservesYAssets;
bool hasBorrow;
}
CheckLtvParams
struct CheckLtvParams {
uint256 netDepositedXinLAssets;
uint256 netDepositedYinLAssets;
uint256 netBorrowedXinLAssets;
uint256 netBorrowedYinLAssets;
uint256 depositedLAssets;
}
VerifyMaxBorrowParams
struct VerifyMaxBorrowParams {
uint256 depositedAssets;
uint256 borrowedAssets;
uint256 reserve;
uint256 totalDepositedLAssets;
uint256 totalBorrowedLAssets;
}