Skip to main content

IButtonswapPair

Inherits: IButtonswapPairErrors, IButtonswapPairEvents, IButtonswapERC20

Functions

MINIMUM_LIQUIDITY

The smallest value that {IButtonswapERC20-totalSupply} can be.

After the first mint the total liquidity (represented by the liquidity token total supply) can never drop below this value. This is to protect against an attack where the attacker mints a very small amount of liquidity, and then donates pool tokens to skew the ratio. This results in future minters receiving no liquidity tokens when they deposit. By enforcing a minimum liquidity value this attack becomes prohibitively expensive to execute.

function MINIMUM_LIQUIDITY() external pure returns (uint256 MINIMUM_LIQUIDITY);

Returns

NameTypeDescription
MINIMUM_LIQUIDITYuint256The MINIMUM_LIQUIDITY value

movingAverageWindow

The duration for which the moving average is calculated for.

function movingAverageWindow() external view returns (uint32 _movingAverageWindow);

Returns

NameTypeDescription
_movingAverageWindowuint32The value of movingAverageWindow

setMovingAverageWindow

Updates the movingAverageWindow parameter of the pair. This can only be called by the Factory address. Refer to parameters.md for more detail.

function setMovingAverageWindow(uint32 newMovingAverageWindow) external;

Parameters

NameTypeDescription
newMovingAverageWindowuint32The new value for movingAverageWindow

maxVolatilityBps

Numerator (over 10_000) of the threshold when price volatility triggers maximum single-sided timelock duration.

function maxVolatilityBps() external view returns (uint16 _maxVolatilityBps);

Returns

NameTypeDescription
_maxVolatilityBpsuint16The value of maxVolatilityBps

setMaxVolatilityBps

Updates the maxVolatilityBps parameter of the pair. This can only be called by the Factory address. Refer to parameters.md for more detail.

function setMaxVolatilityBps(uint16 newMaxVolatilityBps) external;

Parameters

NameTypeDescription
newMaxVolatilityBpsuint16The new value for maxVolatilityBps

minTimelockDuration

How long the minimum singled-sided timelock lasts for.

function minTimelockDuration() external view returns (uint32 _minTimelockDuration);

Returns

NameTypeDescription
_minTimelockDurationuint32The value of minTimelockDuration

setMinTimelockDuration

Updates the minTimelockDuration parameter of the pair. This can only be called by the Factory address. Refer to parameters.md for more detail.

function setMinTimelockDuration(uint32 newMinTimelockDuration) external;

Parameters

NameTypeDescription
newMinTimelockDurationuint32The new value for minTimelockDuration

maxTimelockDuration

How long the maximum singled-sided timelock lasts for.

function maxTimelockDuration() external view returns (uint32 _maxTimelockDuration);

Returns

NameTypeDescription
_maxTimelockDurationuint32The value of maxTimelockDuration

setMaxTimelockDuration

Updates the maxTimelockDuration parameter of the pair. This can only be called by the Factory address. Refer to parameters.md for more detail.

function setMaxTimelockDuration(uint32 newMaxTimelockDuration) external;

Parameters

NameTypeDescription
newMaxTimelockDurationuint32The new value for maxTimelockDuration

maxSwappableReservoirLimitBps

Numerator (over 10_000) of the fraction of the pool balance that acts as the maximum limit on how much of the reservoir can be swapped in a given timeframe.

function maxSwappableReservoirLimitBps() external view returns (uint16 _maxSwappableReservoirLimitBps);

Returns

NameTypeDescription
_maxSwappableReservoirLimitBpsuint16The value of maxSwappableReservoirLimitBps

setMaxSwappableReservoirLimitBps

Updates the maxSwappableReservoirLimitBps parameter of the pair. This can only be called by the Factory address. Refer to parameters.md for more detail.

function setMaxSwappableReservoirLimitBps(uint16 newMaxSwappableReservoirLimitBps) external;

Parameters

NameTypeDescription
newMaxSwappableReservoirLimitBpsuint16The new value for maxSwappableReservoirLimitBps

swappableReservoirGrowthWindow

How much time it takes for the swappable reservoir value to grow from nothing to its maximum value.

function swappableReservoirGrowthWindow() external view returns (uint32 _swappableReservoirGrowthWindow);

Returns

NameTypeDescription
_swappableReservoirGrowthWindowuint32The value of swappableReservoirGrowthWindow

setSwappableReservoirGrowthWindow

Updates the swappableReservoirGrowthWindow parameter of the pair. This can only be called by the Factory address. Refer to parameters.md for more detail.

function setSwappableReservoirGrowthWindow(uint32 newSwappableReservoirGrowthWindow) external;

Parameters

NameTypeDescription
newSwappableReservoirGrowthWindowuint32The new value for swappableReservoirGrowthWindow

factory

The address of the {ButtonswapFactory} instance used to create this Pair.

Set to msg.sender in the Pair constructor.

function factory() external view returns (address factory);

Returns

NameTypeDescription
factoryaddressThe factory address

token0

The address of the first sorted token.

function token0() external view returns (address token0);

Returns

NameTypeDescription
token0addressThe token address

token1

The address of the second sorted token.

function token1() external view returns (address token1);

Returns

NameTypeDescription
token1addressThe token address

price0CumulativeLast

The time-weighted average price of the Pair. The price is of token0 in terms of token1.

The price is represented as a UQ112x112 to maintain precision. Consequently this value must be divided by 2^112 to get the actual price. Because of the time weighting, price0CumulativeLast must also be divided by the total Pair lifetime to get the average price over that time period.

function price0CumulativeLast() external view returns (uint256 price0CumulativeLast);

Returns

NameTypeDescription
price0CumulativeLastuint256The current cumulative token0 price

price1CumulativeLast

The time-weighted average price of the Pair. The price is of token1 in terms of token0.

The price is represented as a UQ112x112 to maintain precision. Consequently this value must be divided by 2^112 to get the actual price. Because of the time weighting, price1CumulativeLast must also be divided by the total Pair lifetime to get the average price over that time period.

function price1CumulativeLast() external view returns (uint256 price1CumulativeLast);

Returns

NameTypeDescription
price1CumulativeLastuint256The current cumulative token1 price

singleSidedTimelockDeadline

The timestamp for when the single-sided timelock concludes. The timelock is initiated based on price volatility of swaps over the last movingAverageWindow, and can be extended by new swaps if they are sufficiently volatile. The timelock protects against attempts to manipulate the price that is used to valuate the reservoir tokens during single-sided operations. It also guards against general legitimate volatility, as it is preferable to defer single-sided operations until it is clearer what the market considers the price to be.

function singleSidedTimelockDeadline() external view returns (uint120 singleSidedTimelockDeadline);

Returns

NameTypeDescription
singleSidedTimelockDeadlineuint120The current deadline timestamp

swappableReservoirLimitReachesMaxDeadline

The timestamp by which the amount of reservoir tokens that can be exchanged during a single-sided operation reaches its maximum value. This maximum value is not necessarily the entirety of the reservoir, instead being calculated as a fraction of the corresponding token's active liquidity.

function swappableReservoirLimitReachesMaxDeadline()
external
view
returns (uint120 swappableReservoirLimitReachesMaxDeadline);

Returns

NameTypeDescription
swappableReservoirLimitReachesMaxDeadlineuint120The current deadline timestamp

getSwappableReservoirLimit

Returns the current limit on the number of reservoir tokens that can be exchanged during a single-sided mint/burn operation.

function getSwappableReservoirLimit() external view returns (uint256 swappableReservoirLimit);

Returns

NameTypeDescription
swappableReservoirLimituint256The amount of reservoir token that can be exchanged

getIsPaused

Whether the Pair is currently paused

function getIsPaused() external view returns (bool _isPaused);

Returns

NameTypeDescription
_isPausedboolThe paused state

setIsPaused

Updates the pause state. This can only be called by the Factory address.

function setIsPaused(bool isPausedNew) external;

Parameters

NameTypeDescription
isPausedNewboolThe new value for isPaused

getLiquidityBalances

Get the current liquidity values.

function getLiquidityBalances()
external
view
returns (uint112 _pool0, uint112 _pool1, uint112 _reservoir0, uint112 _reservoir1, uint32 _blockTimestampLast);

Returns

NameTypeDescription
_pool0uint112The active token0 liquidity
_pool1uint112The active token1 liquidity
_reservoir0uint112The inactive token0 liquidity
_reservoir1uint112The inactive token1 liquidity
_blockTimestampLastuint32The timestamp of when the price was last updated

movingAveragePrice0

The current movingAveragePrice0 value, based on the current block timestamp.

This is the token0 price, time weighted to prevent manipulation. Refer to reservoir-valuation.md for more detail. The price is represented as a UQ112x112 to maintain precision. It is used to valuate the reservoir tokens that are exchanged during single-sided operations.

function movingAveragePrice0() external view returns (uint256 _movingAveragePrice0);

Returns

NameTypeDescription
_movingAveragePrice0uint256The current movingAveragePrice0 value

mint

Mints new liquidity tokens to to based on amountIn0 of token0 and amountIn1 oftoken1` deposited. Expects both tokens to be deposited in a ratio that matches the current Pair price.

The token deposits are deduced to be the delta between token balance before and after the transfers in order to account for unusual tokens. Refer to mint-math.md for more detail.

function mint(uint256 amountIn0, uint256 amountIn1, address to) external returns (uint256 liquidityOut);

Parameters

NameTypeDescription
amountIn0uint256The amount of token0 that should be transferred in from the user
amountIn1uint256The amount of token1 that should be transferred in from the user
toaddressThe account that receives the newly minted liquidity tokens

Returns

NameTypeDescription
liquidityOutuint256THe amount of liquidity tokens minted

mintWithReservoir

Mints new liquidity tokens to to based on how much token0 or token1 has been deposited. The token transferred is the one that the Pair does not have a non-zero inactive liquidity balance for. Expects only one token to be deposited, so that it can be paired with the other token's inactive liquidity.

The token deposits are deduced to be the delta between token balance before and after the transfers in order to account for unusual tokens. Refer to mint-math.md for more detail.

function mintWithReservoir(uint256 amountIn, address to) external returns (uint256 liquidityOut);

Parameters

NameTypeDescription
amountInuint256The amount of tokens that should be transferred in from the user
toaddressThe account that receives the newly minted liquidity tokens

Returns

NameTypeDescription
liquidityOutuint256THe amount of liquidity tokens minted

burn

Burns liquidityIn liquidity tokens to redeem to to the corresponding amountOut0 of token0 and amountOut1 of token1.

Refer to burn-math.md for more detail.

function burn(uint256 liquidityIn, address to) external returns (uint256 amountOut0, uint256 amountOut1);

Parameters

NameTypeDescription
liquidityInuint256The amount of liquidity tokens to burn
toaddressThe account that receives the redeemed tokens

Returns

NameTypeDescription
amountOut0uint256The amount of token0 that the liquidity tokens are redeemed for
amountOut1uint256The amount of token1 that the liquidity tokens are redeemed for

burnFromReservoir

Burns liquidityIn liquidity tokens to redeem to to the corresponding amountOut0 of token0 and amountOut1 of token1. Only returns tokens from the non-zero inactive liquidity balance, meaning one of amountOut0 and amountOut1 will be zero.

Refer to burn-math.md for more detail.

function burnFromReservoir(uint256 liquidityIn, address to) external returns (uint256 amountOut0, uint256 amountOut1);

Parameters

NameTypeDescription
liquidityInuint256The amount of liquidity tokens to burn
toaddressThe account that receives the redeemed tokens

Returns

NameTypeDescription
amountOut0uint256The amount of token0 that the liquidity tokens are redeemed for
amountOut1uint256The amount of token1 that the liquidity tokens are redeemed for

swap

Swaps one token for the other, taking amountIn0 of token0 and amountIn1 of token1 from the sender and sending amountOut0 of token0 and amountOut1 of token1 to to. The price of the swap is determined by maintaining the "K Invariant". A 0.3% fee is collected to distribute between liquidity providers and the protocol.

The token deposits are deduced to be the delta between the current Pair contract token balances and the last stored balances. Optional calldata can be passed to data, which will be used to confirm the output token transfer with to if to is a contract that implements the {IButtonswapCallee} interface. Refer to swap-math.md for more detail.

function swap(uint256 amountIn0, uint256 amountIn1, uint256 amountOut0, uint256 amountOut1, address to) external;

Parameters

NameTypeDescription
amountIn0uint256The amount of token0 that the sender sends
amountIn1uint256The amount of token1 that the sender sends
amountOut0uint256The amount of token0 that the recipient receives
amountOut1uint256The amount of token1 that the recipient receives
toaddressThe account that receives the swap output