Skip to main content

Validation

Git Source

State Variables

POW2_96

uint256 public constant POW2_96 = 79_228_162_514_264_337_593_543_950_336;

Functions

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 depositedX,
uint256 depositedY,
uint256 borrowedX,
uint256 borrowedY
) 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 netDepositedXinL, uint256 netDepositedYinL);

getBorrowedInL

function getBorrowedInL(InputParams memory inputParams)
private
pure
returns (uint256 netBorrowedXinL, uint256 netBorrowedYinL);

convertXToL

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

function convertXToL(
uint256 amount,
uint256 sqrtPriceX96Range,
uint256 activeLiquidityScaler,
Math.Rounding rounding
) private pure returns (uint256 amountL);

convertYToL

The simplified math: L = y * sqrt(p) / 2 Math.mulDiv(amount, sqrtPriceX96Range, 2 * POW2_96, rounding); amountL = amount * sqrtPriceX96RangeScaled / 2 * POW2_96 sqrtPriceX96RangeScaled = sqrtPriceX96Range / activeLiquidityScaler / POW2_96; simplify to: amount * sqrtPriceX96Range / activeLiquidityScaler / POW2_96 / 2 * POW2_96 simplify to: (amount * sqrtPriceX96Range) / (activeLiquidityScaler * 2) final equation: amountL = Math.mulDiv(amount, sqrtPriceX96Range, 2 * activeLiquidityScaler, rounding);

function convertYToL(
uint256 amount,
uint256 sqrtPriceX96Range,
uint256 activeLiquidityScaler,
Math.Rounding rounding
) private pure returns (uint256 amountL);

checkLtv

function checkLtv(CheckLtvParams memory checkLtvParams, InputParams memory inputParams) private pure;

increaseForSlippage

Calculates the impact of buying debt in the dex with slippage simplified to only use liquidity and

*Uses a few formulas to simplify to not need reserves to calculate the required collateral to buy the debt.

phi = 1 - fee reserveIn = { L * sqrt(p) if Lx > Ly` { L / sqrt(p) otherwise reserveOut = { L / sqrt(p) if Lx > Ly { L * sqrt(p) otherwise in * phi = reserveOut * reserveIn / (reserveOut - out) - reserveIn in * phi = reserveIn * (reserveOut / (reserveOut - out) - 1) in * phi = reserveIn * (reserveOut - (reserveOut - out) / (reserveOut - out) in * phi = reserveIn * (out / (reserveOut - out))

if Lx > Ly: math ``` inL * 2 * sqrt(p) * phi = L * sqrt(p) * (outL * 2 / sqrt(p) / (L / sqrt(p) - outL * 2 / sqrt(p))) note in and out * 2 due to being half of the liquidity which is both x and y inL * phi = L / (L - 2 * outL)

otherwise case cancels in a similar manner resulting in the same formula.
```math
inL * phi = L * outL / (L - 2 * outL)

end*

function increaseForSlippage(uint256 debtL, uint256 activeLiquidity) private pure returns (uint256);

Parameters

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

checkLeverage

function checkLeverage(CheckLtvParams memory checkLtvParams, uint8 allowedLeverage) private pure;

Errors

AmmalgamTransferAmtExceedsBalance

error AmmalgamTransferAmtExceedsBalance();

AmmalgamInsufficientLiquidity

error AmmalgamInsufficientLiquidity();

AmmalgamCannotBorrowAgainstSameCollateral

error AmmalgamCannotBorrowAgainstSameCollateral();

AmmalgamMaxBorrowReached

error AmmalgamMaxBorrowReached();

AmmalgamDepositIsNotStrictlyBigger

error AmmalgamDepositIsNotStrictlyBigger();

AmmalgamLTV

error AmmalgamLTV();

AmmalgamMaxSlippage

error AmmalgamMaxSlippage();

AmmalgamTooMuchLeverage

error AmmalgamTooMuchLeverage();

Structs

InputParams

struct InputParams {
uint256 depositedX;
uint256 depositedY;
uint256 depositedL;
uint256 borrowedX;
uint256 borrowedY;
uint256 borrowedL;
TokenType tokenType;
address toCheck;
uint256 amount;
uint256 balanceFrom;
uint256 sqrtPriceX96Min;
uint256 sqrtPriceX96Max;
uint256 activeLiquidityScaler;
uint112 activeLiquidity;
uint8 ltv;
uint8 allowedLeverage;
}

CheckLtvParams

struct CheckLtvParams {
uint256 netDepositedXinL;
uint256 netDepositedYinL;
uint256 netBorrowedXinL;
uint256 netBorrowedYinL;
uint256 depositedL;
uint8 ltv;
}

VerifyMaxBorrowXYParams

struct VerifyMaxBorrowXYParams {
uint256 amount;
uint256 deposited;
uint256 borrowed;
uint112 reserve;
uint8 maxBorrow;
uint256 totalLiquidity;
uint256 borrowedLiquidity;
}

VerifyMaxBorrowLParams

struct VerifyMaxBorrowLParams {
uint256 amountL;
uint112 reserveX;
uint112 reserveY;
uint8 maxBorrow;
uint256 depositedX;
uint256 depositedY;
uint256 totalLiquidity;
uint256 borrowedX;
uint256 borrowedY;
uint256 borrowedL;
}