ConstantProduct
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
| Name | Type | Description |
|---|---|---|
| <none> | address |
token1
Returns the address of the higher-sorted pool token.
function token1() public view virtual returns (address);Returns
| Name | Type | Description |
|---|---|---|
| <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
| 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() public view virtual returns (uint256);Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
lpBalanceOf
Returns the LP share balance of account.
function lpBalanceOf(address account) public view virtual 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() public pure virtual 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
) public virtual 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)
public
virtual
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)
public
virtual
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)
public
pure
virtual
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)
public
pure
virtual
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) public pure virtual 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. |
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
| Name | Type | Description |
|---|---|---|
| selectors | bytes |