IConstantProduct
Title: IConstantProduct
Author: Modified from Uniswap V2 (https://github.com/Uniswap/v2-core/blob/master/contracts/interfaces/IUniswapV2Pair.sol)
Interface for the Constant Product AMM (Uniswap V2 style) Diamond facet.
Manages a single x*y=k pool with two ERC-20 reserves and LP token accounting.
Functions
token0
Returns the address of the lower-sorted pool token.
function token0() external view returns (address);Returns
| Name | Type | Description |
|---|---|---|
| <none> | address |
token1
Returns the address of the higher-sorted pool token.
function token1() external view returns (address);Returns
| Name | Type | Description |
|---|---|---|
| <none> | address |
getReserves
Returns the current reserves and the block timestamp of the last update.
function getReserves() external view returns (uint256 reserve0, uint256 reserve1, uint32 blockTimestampLast);Returns
| Name | Type | Description |
|---|---|---|
| reserve0 | uint256 | Reserve of token0. |
| reserve1 | uint256 | Reserve of token1. |
| blockTimestampLast | uint32 | Unix timestamp (mod 2^32) of the last reserve update. |
totalLpSupply
Returns the total supply of LP shares.
function totalLpSupply() external view returns (uint256);Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
lpBalanceOf
Returns the LP share balance of account.
function lpBalanceOf(address account) external view returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| account | address | The address to query. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
feeBps
Returns the swap fee in basis points (always 30 = 0.3%).
function feeBps() external view returns (uint16);Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint16 |
addLiquidity
Adds liquidity to the pool and mints LP shares to to.
function addLiquidity(
uint256 amount0Desired,
uint256 amount1Desired,
uint256 amount0Min,
uint256 amount1Min,
address to
) external returns (uint256 amount0, uint256 amount1, uint256 liquidity);Parameters
| Name | Type | Description |
|---|---|---|
| amount0Desired | uint256 | The desired amount of token0 to deposit. |
| amount1Desired | uint256 | The desired amount of token1 to deposit. |
| amount0Min | uint256 | The minimum acceptable amount of token0 (slippage guard). |
| amount1Min | uint256 | The minimum acceptable amount of token1 (slippage guard). |
| to | address | The recipient of the minted LP shares. |
Returns
| Name | Type | Description |
|---|---|---|
| amount0 | uint256 | Actual amount of token0 deposited. |
| amount1 | uint256 | Actual amount of token1 deposited. |
| liquidity | uint256 | LP shares minted to to. |
removeLiquidity
Burns LP shares from the caller and returns the underlying tokens to to.
function removeLiquidity(uint256 liquidity, uint256 amount0Min, uint256 amount1Min, address to)
external
returns (uint256 amount0, uint256 amount1);Parameters
| Name | Type | Description |
|---|---|---|
| liquidity | uint256 | The amount of LP shares to burn. |
| amount0Min | uint256 | The minimum acceptable amount of token0 (slippage guard). |
| amount1Min | uint256 | The minimum acceptable amount of token1 (slippage guard). |
| to | address | The recipient of the underlying tokens. |
Returns
| Name | Type | Description |
|---|---|---|
| amount0 | uint256 | Amount of token0 returned. |
| amount1 | uint256 | Amount of token1 returned. |
swapExactTokensForTokens
Swaps an exact input amount for as many output tokens as possible.
function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, bool zeroForOne, address to)
external
returns (uint256 amountOut);Parameters
| Name | Type | Description |
|---|---|---|
| amountIn | uint256 | The exact amount of input token to sell. |
| amountOutMin | uint256 | The minimum acceptable output amount (slippage guard). |
| zeroForOne | bool | True if selling token0 for token1; false if selling token1 for token0. |
| to | address | The recipient of the output tokens. |
Returns
| Name | Type | Description |
|---|---|---|
| amountOut | uint256 | The amount of output tokens sent to to. |
getAmountOut
Computes the output amount given an input amount and pool reserves (with fee).
function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut) external pure returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| amountIn | uint256 | The input amount. |
| reserveIn | uint256 | The reserve of the input token. |
| reserveOut | uint256 | The reserve of the output token. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 | The output amount after the 0.3% fee. |
getAmountIn
Computes the required input amount to receive a specific output amount (with fee).
function getAmountIn(uint256 amountOut, uint256 reserveIn, uint256 reserveOut) external pure returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| amountOut | uint256 | The desired output amount. |
| reserveIn | uint256 | The reserve of the input token. |
| reserveOut | uint256 | The reserve of the output token. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 | The required input amount before fee. |
quote
Returns the equivalent amount of tokenB given an amount of tokenA at the current ratio.
Used to compute proportional token amounts for liquidity provision. No fee applied.
function quote(uint256 amountA, uint256 reserveA, uint256 reserveB) external pure returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| amountA | uint256 | The amount of tokenA. |
| reserveA | uint256 | The reserve of tokenA. |
| reserveB | uint256 | The reserve of tokenB. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 | The equivalent amount of tokenB. |
Events
LiquidityAdded
Emitted when liquidity is added to the pool.
event LiquidityAdded(
address indexed provider, address indexed to, uint256 amount0, uint256 amount1, uint256 liquidity
);Parameters
| Name | Type | Description |
|---|---|---|
| provider | address | The address that called addLiquidity. |
| to | address | The address that received the LP shares. |
| amount0 | uint256 | The amount of token0 deposited. |
| amount1 | uint256 | The amount of token1 deposited. |
| liquidity | uint256 | The amount of LP shares minted. |
LiquidityRemoved
Emitted when liquidity is removed from the pool.
event LiquidityRemoved(
address indexed provider, address indexed to, uint256 amount0, uint256 amount1, uint256 liquidity
);Parameters
| Name | Type | Description |
|---|---|---|
| provider | address | The address that called removeLiquidity. |
| to | address | The address that received the underlying tokens. |
| amount0 | uint256 | The amount of token0 returned. |
| amount1 | uint256 | The amount of token1 returned. |
| liquidity | uint256 | The amount of LP shares burned. |
Swap
Emitted on every swap.
event Swap(
address indexed sender,
address indexed to,
uint256 amount0In,
uint256 amount1In,
uint256 amount0Out,
uint256 amount1Out
);Parameters
| Name | Type | Description |
|---|---|---|
| sender | address | The address that initiated the swap. |
| to | address | The address that received the output tokens. |
| amount0In | uint256 | The amount of token0 sent in. |
| amount1In | uint256 | The amount of token1 sent in. |
| amount0Out | uint256 | The amount of token0 sent out. |
| amount1Out | uint256 | The amount of token1 sent out. |
ReservesSync
Emitted when the pool reserves are synced to the actual balances.
event ReservesSync(uint256 reserve0, uint256 reserve1);Parameters
| Name | Type | Description |
|---|---|---|
| reserve0 | uint256 | The updated reserve of token0. |
| reserve1 | uint256 | The updated reserve of token1. |
Errors
ConstantProductAlreadyInitialized
Raised when the pool is already initialized.
error ConstantProductAlreadyInitialized();ConstantProductInvalidTokens
Raised when the supplied token addresses are invalid (identical or zero).
error ConstantProductInvalidTokens();ConstantProductInsufficientLiquidityMinted
Raised when the computed LP liquidity to mint is zero.
error ConstantProductInsufficientLiquidityMinted();ConstantProductInsufficientLiquidityBurned
Raised when the computed token amounts to return on burn are zero.
error ConstantProductInsufficientLiquidityBurned();ConstantProductInsufficientInputAmount
Raised when the swap input amount is zero.
error ConstantProductInsufficientInputAmount();ConstantProductInsufficientOutputAmount
Raised when the swap output does not meet the caller's minimum.
error ConstantProductInsufficientOutputAmount();ConstantProductInsufficientLiquidity
Raised when a reserve is zero during a quote or swap calculation.
error ConstantProductInsufficientLiquidity();ConstantProductInvalidK
Raised when the k invariant check fails after a swap.
error ConstantProductInvalidK();ConstantProductTransferFailed
Raised when a token transfer (in or out) returns false.
error ConstantProductTransferFailed(address token);Parameters
| Name | Type | Description |
|---|---|---|
| token | address | The token address that failed to transfer. |
ConstantProductReserveOverflow
Raised when a reserve update would exceed the uint112 maximum (~5.19e33).
This is only reachable for tokens with astronomically large supplies.
error ConstantProductReserveOverflow();ConstantProductSlippageExceeded
Raised when the actual amounts deposited or received fall below the caller's minimums.
error ConstantProductSlippageExceeded();