Skip to main content

AmmalgamPair

Git Source

Inherits: IAmmalgamPair, TokenController

State Variables

BUFFER

uint256 private constant BUFFER = 95;

INVERSE_BUFFER

uint256 private constant INVERSE_BUFFER = 5;

INVERSE_BUFFER_SQUARED

uint256 private constant INVERSE_BUFFER_SQUARED = 25;

BUFFER_NUMERATOR

uint256 private constant BUFFER_NUMERATOR = 100;

unlocked

uint256 private unlocked = 1;

Functions

_lock

function _lock() private view;

lock

modifier lock();

mint

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

burn

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

swap

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

calculateAmountIn

helper method to calculate amountIn for swap

Adds jump, saves on runtime size

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

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.

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

Parameters

NameTypeDescription
reserveuint256the starting reserve
missinguint256the missing assets, zero if deposits > borrows of X or Y

deposit

function deposit(
address to
) external lock;

updateDepositShares

function updateDepositShares(
uint256 depositedTokenType,
uint256 amountAssets,
uint256 reserveAssets,
uint256 _missingAssets,
address to
) private returns (uint256 adjustReserves);

withdraw

withdraw X and/or Y

function withdraw(
address to
) external 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 lock;

borrowHelper

function borrowHelper(
Validation.VerifyMaxBorrowXYParams memory maxBorrowParams,
address to,
uint256 amountAssets,
uint256 reserve,
uint256 borrowedTokenType,
uint256 depositedTokenType
) 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 lock returns (uint256, uint256);

repay

function repay(
address onBehalfOf
) external 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 reserveInAssets,
uint256 missingInAssets,
uint256 borrowTokenType
) private returns (uint256 adjustedReservesInAssets, uint256 netRepayInAssets);

repayLiquidity

function repayLiquidity(
address onBehalfOf
) external 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 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, SOFT, 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;

liquidateSoft

Liquidation based on change of saturation because of time.

function liquidateSoft(
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.

liquidationTransferAll

function liquidationTransferAll(
address borrower,
address to,
uint256 depositLToBeTransferredInLAssets,
uint256 depositXToBeTransferredInXAssets,
uint256 depositYToBeTransferredInYAssets
) 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 lock;

sync

function sync() external lock;

_sync

function _sync() private;

depletionReserveAdjustmentWhenAssetIsAdded

When assets are depleted, a user can deposit the depleted asset and earn additional deposit credit for moving the swap curve from the adjusted amount due to assets being depleted to the original curve.

function depletionReserveAdjustmentWhenAssetIsAdded(
uint256 amountAssets,
uint256 reserveAssets,
uint256 _missingAssets
) private pure returns (uint256 adjustReserves_);

accrueSaturationPenaltiesAndInterest

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

updateObservation

function updateObservation(uint256 _reserveXAssets, uint256 _reserveYAssets) private;

updateObservation

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

validateOnUpdate

function validateOnUpdate(address validate, address update, bool isBorrow) external;

validateSolvency

function validateSolvency(address validate, bool isBorrow) private;

getInputParamsAndUpdateSaturation

function getInputParamsAndUpdateSaturation(address toUpdate, bool alwaysUpdate) private;

getInputParams

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

transferAssets

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

calcMinLiquidityConsideringDepletion

function calcMinLiquidityConsideringDepletion(
uint256 amountXAssets,
uint256 amountYAssets,
uint256 _reserveXAssets,
uint256 _reserveYAssets,
uint256 activeLiquidityAssets,
uint256 depositLiquidityAssets,
uint256 depositLiquidityShares,
bool isRoundingUp
) private view returns (uint256 liquidityAssets, uint256 liquidityShares);

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();

Overflow

error Overflow();