Skip to main content

AmmalgamPair

Git Source

Inherits: IAmmalgamPair, TokenController

State Variables

ZERO_DEPOSIT_DUE_TO_NETTING

uint256 private constant ZERO_DEPOSIT_DUE_TO_NETTING = 0;

UNLOCKED

uint256 private constant UNLOCKED = 0;

LOCKED

uint256 private constant LOCKED = 1;

locked

uint256 private transient locked;

Functions

lock

modifier lock();

_lock

function _lock() private;

_unlock

function _unlock() private;

mint

function mint(
address to
) external virtual lock returns (uint256 liquidityShares);

burn

function burn(
address to
) external virtual lock returns (uint256 amountXAssets, uint256 amountYAssets);

swap

function swap(uint256 amountXOut, uint256 amountYOut, address to, bytes calldata data) external virtual lock;

calculateAmountIn

helper method to calculate amountIn for swap

Adds jump, saves on runtime size. Must check that reserve > amountOut, which happens in swap where function is called.

function calculateAmountIn(
uint256 amountOut,
uint256 balance,
uint256 reserve
) private pure returns (uint256 amountIn);

Parameters

NameTypeDescription
amountOutuint256the amount out
balanceuint256the balance
reserveuint256the reserve

calculateBalanceAfterFees

helper method to calculate balance after fees

Note that amountIn + reserve does not always equal balance if amountOut > 0. When assets are depleted, we should multiply (balance - missing) * BUFFER_NUMERATOR / INVERSE_BUFFER, but instead of divide here, we multiply the other side of the K comparison, see calculateReserveAdjustmentsForMissingAssets where we multiply by INVERSE_BUFFER. When not depleted, we multiply by INVERSE_BUFFER instead of dividing on the other side.

function calculateBalanceAfterFees(
uint256 amountIn,
uint256 balance,
uint256 reserve,
uint256 referenceReserve,
uint256 missing
) private pure returns (uint256 calculatedBalance);

Parameters

NameTypeDescription
amountInuint256the swap amount in
balanceuint256the balance
reserveuint256the reserve
referenceReserveuint256the reference reserve for the block
missinguint256the missing assets, zero if deposits > borrows of X or Y

calculateReserveAdjustmentsForMissingAssets

helper method to calculate balance adjustment for missing assets

For swap, when assets are depleted, we should multiply (reserve - missing) by BUFFER_NUMERATOR / INVERSE_BUFFER, but instead of divide here, we multiply the other side of the K comparison, see calculateBalanceAfterFees where we multiply by INVERSE_BUFFER. For updateObservation, different scaled buffer and bufferNumerator values are supplied so the adjusted reserve reflects observation-specific logic.

function calculateReserveAdjustmentsForMissingAssets(
uint256 reserve,
uint256 missing,
uint256 buffer,
uint256 bufferNumerator
) private pure returns (uint256 reserveAdjustment);

Parameters

NameTypeDescription
reserveuint256the starting reserve
missinguint256the missing assets, zero if deposits > borrows of X or Y
bufferuint256Scaling factor applied to the reserve for the depletion comparison.
bufferNumeratoruint256Scaling factor applied to the missing amount for the comparison and for computing the depleted-case adjusted reserve.

Returns

NameTypeDescription
reserveAdjustmentuint256The adjusted reserve value used for swap or updateObservation depends on the buffer, bufferNumerator to be passed in.

deposit

function deposit(
address to
) external virtual lock;

withdraw

withdraw X and/or Y

function withdraw(
address to
) external virtual lock;

updateWithdrawShares

function updateWithdrawShares(
address to,
uint256 depositedTokenType,
uint256 _reserve
) private returns (uint256 withdrawnAssets);

borrow

function borrow(address to, uint256 amountXAssets, uint256 amountYAssets, bytes calldata data) external virtual lock;

borrowHelper

function borrowHelper(
address to,
uint256 amountAssets,
uint256 reserve,
uint256 depositedTokenType,
uint256 borrowedTokenType
) private returns (uint256 amountShares);

updateBorrowOrDepositSharesHelper

function updateBorrowOrDepositSharesHelper(
address to,
uint256 tokenType,
uint256 amountAssets,
bool isRoundingUp
) private returns (uint256 amountShares);

borrowLiquidity

function borrowLiquidity(
address to,
uint256 borrowAmountLAssets,
bytes calldata data
) external virtual lock returns (uint256 borrowedLXAssets, uint256 borrowedLYAssets);

repay

function repay(
address onBehalfOf
) external virtual lock returns (uint256 repayXInXAssets, uint256 repayYInYAssets);

_repay

Internal version to allow for direct calls during liquidations

function _repay(
address onBehalfOf
) private returns (uint256 repayXInXAssets, uint256 repayYInYAssets);

repayHelper

function repayHelper(
address onBehalfOf,
uint256 repayInAssets,
uint256 borrowTokenType
) private returns (uint256 actualRepayInAssets);

repayLiquidity

function repayLiquidity(
address onBehalfOf
) external virtual lock returns (uint256 repaidLXInXAssets, uint256 repaidLYInYAssets, uint256 repayLiquidityAssets);

_repayLiquidity

function _repayLiquidity(
address onBehalfOf
) private returns (uint256 repaidLXInXAssets, uint256 repaidLYInYAssets, uint256 repayLiquidityAssets);

liquidate

LTV based liquidation. The LTV dictates the max premium that can be had by the liquidator.

function liquidate(
address borrower,
address to,
uint256 depositLToBeTransferredInLAssets,
uint256 depositXToBeTransferredInXAssets,
uint256 depositYToBeTransferredInYAssets,
uint256 repayLXInXAssets,
uint256 repayLYInYAssets,
uint256 repayXInXAssets,
uint256 repayYInYAssets,
uint256 liquidationType
) external virtual lock;

Parameters

NameTypeDescription
borroweraddressThe account being liquidated
toaddressThe account to send the liquidated deposit to
depositLToBeTransferredInLAssetsuint256The amount of L to be transferred to the liquidator.
depositXToBeTransferredInXAssetsuint256The amount of X to be transferred to the liquidator.
depositYToBeTransferredInYAssetsuint256The amount of Y to be transferred to the liquidator.
repayLXInXAssetsuint256The amount of LX to be repaid by the liquidator.
repayLYInYAssetsuint256The amount of LY to be repaid by the liquidator.
repayXInXAssetsuint256The amount of X to be repaid by the liquidator.
repayYInYAssetsuint256The amount of Y to be repaid by the liquidator.
liquidationTypeuint256The type of liquidation to be performed: HARD, SATURATION, LEVERAGE

liquidateHard

LTV based liquidation. The LTV dictates the max premium that can be had by the liquidator.

function liquidateHard(
address borrower,
address to,
Validation.InputParams memory inputParams,
Liquidation.HardLiquidationParams memory hardLiquidationParams
) private;

Parameters

NameTypeDescription
borroweraddressThe account being liquidated
toaddressThe account to send the liquidated deposit to
inputParamsValidation.InputParamsThe input parameters for the liquidation, including reserves and price limits.
hardLiquidationParamsLiquidation.HardLiquidationParamsThe parameters for the hard liquidation, including deposits and repayments.

repayCallback

function repayCallback(uint256 repayXAssets, uint256 repayYAssets) private;

verifyRepay

function verifyRepay(uint256 actualX, uint256 expectedX, uint256 actualY, uint256 expectedY) private pure;

resetSaturation

Liquidation based on change of saturation because of time.

function resetSaturation(
Validation.InputParams memory inputParams,
address borrower,
address to,
uint256 depositLToBeTransferredInLAssets,
uint256 depositXToBeTransferredInXAssets,
uint256 depositYToBeTransferredInYAssets
) private;

Parameters

NameTypeDescription
inputParamsValidation.InputParams
borroweraddressThe account being liquidated.
toaddressThe account to send the liquidated deposit to
depositLToBeTransferredInLAssetsuint256The amount of L to be transferred to the liquidator.
depositXToBeTransferredInXAssetsuint256The amount of X to be transferred to the liquidator.
depositYToBeTransferredInYAssetsuint256The amount of Y to be transferred to the liquidator.

liquidateLeverage

Liquidation based on leverage.

function liquidateLeverage(
Validation.InputParams memory inputParams,
address borrower,
address to,
bool depositL,
bool repayL
) private;

Parameters

NameTypeDescription
inputParamsValidation.InputParams
borroweraddressThe account being liquidated.
toaddressThe account to send the liquidated deposit to
depositLboolFlag indicating whether the deposit transferred to the liquidator is L xor X+Y.
repayLboolFlag indicating whether the repay by the liquidator is L xor X+Y.

finalizeLiquidation

function finalizeLiquidation(
address borrower,
address to,
uint256 depositLToBeTransferredInLAssets,
uint256 depositXToBeTransferredInXAssets,
uint256 depositYToBeTransferredInYAssets,
bool isBadDebt
) private;

liquidationTransfer

Transfer deposit to the liquidator from the borrower (==from).

function liquidationTransfer(
address from,
address to,
uint256 depositToTransferInAssets,
uint256 tokenType,
bool isBadDebt
) private;

Parameters

NameTypeDescription
fromaddressThe account the deposit is being transferred from.
toaddressThe account the deposit is being transferred to.
depositToTransferInAssetsuint256The amount being transferred to the liquidator.
tokenTypeuint256The deposit token type being transferred.
isBadDebtbool

skim

function skim(
address to
) external virtual lock;

sync

function sync() external virtual lock;

accrueSaturationPenaltiesAndInterest

function accrueSaturationPenaltiesAndInterest(
address affectedAccount,
uint256 minimumTimeBeforeUpdate
) private returns (uint256 _reserveXAssets, uint256 _reserveYAssets, uint256 balanceXAssets, uint256 balanceYAssets);

updateObservation

function updateObservation(
uint256 _reserveXAssets,
uint256 _reserveYAssets,
uint32 currentTimestamp,
uint32 deltaUpdateTimestamp
) private;

validateOnUpdate

function validateOnUpdate(address validate, address update, bool alwaysUpdate) public virtual;

validateSolvency

function validateSolvency(address validate, bool alwaysUpdate) private;

getInputParamsAndUpdateSaturation

function getInputParamsAndUpdateSaturation(address toUpdate, bool alwaysUpdate) private;

getInputParams

function getInputParams(
address toCheck,
bool includeLongTermPrice
) internal view returns (Validation.InputParams memory inputParams);

transferAssets

function transferAssets(address to, uint256 amountXAssets, uint256 amountYAssets) private;

calculateMinimumLiquidityAssets

function calculateMinimumLiquidityAssets(
uint256 amountXAssets,
uint256 amountYAssets,
uint256 _reserveXAssets,
uint256 _reserveYAssets,
uint256 liquidityAssetsNumerator,
bool isRoundingUp
) private pure returns (uint256 liquidityAssets);

checkMaxBorrowForLiquidity

function checkMaxBorrowForLiquidity(
uint256 reserveX,
uint256 reserveY,
uint256 totalDepositedLAssets,
uint256 totalBorrowedLAssets,
uint256 newBorrowLAssets
) private view;

checkMaxBorrow

function checkMaxBorrow(
uint256 depositedAssets,
uint256 borrowedAssets,
uint256 reserve,
uint256 totalDepositedLAssets,
uint256 totalBorrowedLiquidityAssets
) private pure;

Errors

Locked

error Locked();

InsufficientLiquidityMinted

error InsufficientLiquidityMinted();

InsufficientLiquidityBurned

error InsufficientLiquidityBurned();

InsufficientOutputAmount

error InsufficientOutputAmount();

InsufficientInputAmount

error InsufficientInputAmount();

InsufficientLiquidity

error InsufficientLiquidity();

InvalidToAddress

error InvalidToAddress();

K

error K();

InsufficientRepayLiquidity

error InsufficientRepayLiquidity();