Skip to content
Lattice

IConstantProduct

Git Source

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

NameTypeDescription
<none>address

token1

Returns the address of the higher-sorted pool token.

function token1() external view returns (address);

Returns

NameTypeDescription
<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

NameTypeDescription
reserve0uint256Reserve of token0.
reserve1uint256Reserve of token1.
blockTimestampLastuint32Unix timestamp (mod 2^32) of the last reserve update.

totalLpSupply

Returns the total supply of LP shares.

function totalLpSupply() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256

lpBalanceOf

Returns the LP share balance of account.

function lpBalanceOf(address account) external view returns (uint256);

Parameters

NameTypeDescription
accountaddressThe address to query.

Returns

NameTypeDescription
<none>uint256

feeBps

Returns the swap fee in basis points (always 30 = 0.3%).

function feeBps() external view returns (uint16);

Returns

NameTypeDescription
<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

NameTypeDescription
amount0Desireduint256The desired amount of token0 to deposit.
amount1Desireduint256The desired amount of token1 to deposit.
amount0Minuint256The minimum acceptable amount of token0 (slippage guard).
amount1Minuint256The minimum acceptable amount of token1 (slippage guard).
toaddressThe recipient of the minted LP shares.

Returns

NameTypeDescription
amount0uint256Actual amount of token0 deposited.
amount1uint256Actual amount of token1 deposited.
liquidityuint256LP 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

NameTypeDescription
liquidityuint256The amount of LP shares to burn.
amount0Minuint256The minimum acceptable amount of token0 (slippage guard).
amount1Minuint256The minimum acceptable amount of token1 (slippage guard).
toaddressThe recipient of the underlying tokens.

Returns

NameTypeDescription
amount0uint256Amount of token0 returned.
amount1uint256Amount 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

NameTypeDescription
amountInuint256The exact amount of input token to sell.
amountOutMinuint256The minimum acceptable output amount (slippage guard).
zeroForOneboolTrue if selling token0 for token1; false if selling token1 for token0.
toaddressThe recipient of the output tokens.

Returns

NameTypeDescription
amountOutuint256The 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

NameTypeDescription
amountInuint256The input amount.
reserveInuint256The reserve of the input token.
reserveOutuint256The reserve of the output token.

Returns

NameTypeDescription
<none>uint256The 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

NameTypeDescription
amountOutuint256The desired output amount.
reserveInuint256The reserve of the input token.
reserveOutuint256The reserve of the output token.

Returns

NameTypeDescription
<none>uint256The 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

NameTypeDescription
amountAuint256The amount of tokenA.
reserveAuint256The reserve of tokenA.
reserveBuint256The reserve of tokenB.

Returns

NameTypeDescription
<none>uint256The 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

NameTypeDescription
provideraddressThe address that called addLiquidity.
toaddressThe address that received the LP shares.
amount0uint256The amount of token0 deposited.
amount1uint256The amount of token1 deposited.
liquidityuint256The 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

NameTypeDescription
provideraddressThe address that called removeLiquidity.
toaddressThe address that received the underlying tokens.
amount0uint256The amount of token0 returned.
amount1uint256The amount of token1 returned.
liquidityuint256The 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

NameTypeDescription
senderaddressThe address that initiated the swap.
toaddressThe address that received the output tokens.
amount0Inuint256The amount of token0 sent in.
amount1Inuint256The amount of token1 sent in.
amount0Outuint256The amount of token0 sent out.
amount1Outuint256The amount of token1 sent out.

ReservesSync

Emitted when the pool reserves are synced to the actual balances.

event ReservesSync(uint256 reserve0, uint256 reserve1);

Parameters

NameTypeDescription
reserve0uint256The updated reserve of token0.
reserve1uint256The 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

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