AmmalgamPair
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
| Name | Type | Description |
|---|---|---|
amountOut | uint256 | the amount out |
balance | uint256 | the balance |
reserve | uint256 | the 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
| Name | Type | Description |
|---|---|---|
amountIn | uint256 | the swap amount in |
balance | uint256 | the balance |
reserve | uint256 | the reserve |
referenceReserve | uint256 | the reference reserve for the block |
missing | uint256 | the 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
| Name | Type | Description |
|---|---|---|
reserve | uint256 | the starting reserve |
missing | uint256 | the missing assets, zero if deposits > borrows of X or Y |
buffer | uint256 | Scaling factor applied to the reserve for the depletion comparison. |
bufferNumerator | uint256 | Scaling factor applied to the missing amount for the comparison and for computing the depleted-case adjusted reserve. |
Returns
| Name | Type | Description |
|---|---|---|
reserveAdjustment | uint256 | The 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
| Name | Type | Description |
|---|---|---|
borrower | address | The account being liquidated |
to | address | The account to send the liquidated deposit to |
depositLToBeTransferredInLAssets | uint256 | The amount of L to be transferred to the liquidator. |
depositXToBeTransferredInXAssets | uint256 | The amount of X to be transferred to the liquidator. |
depositYToBeTransferredInYAssets | uint256 | The amount of Y to be transferred to the liquidator. |
repayLXInXAssets | uint256 | The amount of LX to be repaid by the liquidator. |
repayLYInYAssets | uint256 | The amount of LY to be repaid by the liquidator. |
repayXInXAssets | uint256 | The amount of X to be repaid by the liquidator. |
repayYInYAssets | uint256 | The amount of Y to be repaid by the liquidator. |
liquidationType | uint256 | The 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
| Name | Type | Description |
|---|---|---|
borrower | address | The account being liquidated |
to | address | The account to send the liquidated deposit to |
inputParams | Validation.InputParams | The input parameters for the liquidation, including reserves and price limits. |
hardLiquidationParams | Liquidation.HardLiquidationParams | The 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
| Name | Type | Description |
|---|---|---|
inputParams | Validation.InputParams | |
borrower | address | The account being liquidated. |
to | address | The account to send the liquidated deposit to |
depositLToBeTransferredInLAssets | uint256 | The amount of L to be transferred to the liquidator. |
depositXToBeTransferredInXAssets | uint256 | The amount of X to be transferred to the liquidator. |
depositYToBeTransferredInYAssets | uint256 | The 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
| Name | Type | Description |
|---|---|---|
inputParams | Validation.InputParams | |
borrower | address | The account being liquidated. |
to | address | The account to send the liquidated deposit to |
depositL | bool | Flag indicating whether the deposit transferred to the liquidator is L xor X+Y. |
repayL | bool | Flag 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
| Name | Type | Description |
|---|---|---|
from | address | The account the deposit is being transferred from. |
to | address | The account the deposit is being transferred to. |
depositToTransferInAssets | uint256 | The amount being transferred to the liquidator. |
tokenType | uint256 | The deposit token type being transferred. |
isBadDebt | bool |
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();