Validation
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 . 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
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 in to buy the debt.
function increaseForSlippage(
uint256 debtLiquidityAssets,
uint256 activeLiquidityAssets
) private 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;
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;
}