Skip to content
Lattice

ConstantProduct

Git Source

Inherits: IConstantProduct

Title: ConstantProduct

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from Uniswap V2 (https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol)

Stateless Diamond facet for a Uniswap V2-style constant-product AMM.

All logic lives in ConstantProductLib. This contract is a pure delegator. Deploy as a facet in a Diamond proxy. Initialize via ConstantProductLib.__ConstantProduct_init.

Notes:

  • lattice-version: 0.1.0
  • lattice-source: Uniswap V2

Functions

token0

Returns the address of the lower-sorted pool token.

function token0() public view virtual returns (address);

Returns

NameTypeDescription
<none>address

token1

Returns the address of the higher-sorted pool token.

function token1() public view virtual returns (address);

Returns

NameTypeDescription
<none>address

getReserves

Returns the current reserves and the block timestamp of the last update.

function getReserves() public view virtual 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() public view virtual returns (uint256);

Returns

NameTypeDescription
<none>uint256

lpBalanceOf

Returns the LP share balance of account.

function lpBalanceOf(address account) public view virtual 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() public pure virtual 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
    ) public virtual 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)
        public
        virtual
        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)
        public
        virtual
        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)
        public
        pure
        virtual
        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)
        public
        pure
        virtual
        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) public pure virtual returns (uint256);

Parameters

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

Returns

NameTypeDescription
<none>uint256The equivalent amount of tokenB.

exportSelectors

ERC-8153 selector export: this facet's cuttable selectors, tightly packed (4 bytes each).

Excludes exportSelectors() itself (0x0ef22643) - it is never cut into a diamond. Order matches forge inspect ConstantProduct methodIdentifiers (alphabetical by signature); kept in exact parity by ExportSelectorsParityTest. Chunks: addLiquidity(uint256,uint256,uint256,uint256,address) 0xe0ab0772 feeBps() 0x24a9d853 getAmountIn(uint256,uint256,uint256) 0x85f8c259 getAmountOut(uint256,uint256,uint256) 0x054d50d4 getReserves() 0x0902f1ac lpBalanceOf(address) 0x9c46665c quote(uint256,uint256,uint256) 0xad615dec removeLiquidity(uint256,uint256,uint256,address) 0xe39b0eb5 swapExactTokensForTokens(uint256,uint256,bool,address) 0xaa90d54f token0() 0x0dfe1681 token1() 0xd21220a7 totalLpSupply() 0x6aedea73

function exportSelectors() external pure virtual returns (bytes memory selectors);

Returns

NameTypeDescription
selectorsbytes