ConstantProductLib
Title: ConstantProductLib
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)
Library implementing a Uniswap V2-style constant-product AMM (x*y=k) as a stateless Diamond facet.
All state lives in a single ERC-7201 namespaced slot. LP shares are tracked internally (not as a separate ERC-20 token) to avoid an extra facet dependency. 0.3% swap fee (30 bps).
Constants
FEE_BPS
Swap fee in basis points (0.3%).
uint16 internal constant FEE_BPS = 30;MINIMUM_LIQUIDITY
Minimum liquidity permanently locked on first deposit (Uniswap V2 convention).
uint256 internal constant MINIMUM_LIQUIDITY = 1000;Functions
constantProductStorage
Returns the ConstantProduct storage struct.
function constantProductStorage() internal pure returns (ConstantProductStorage storage $);Returns
| Name | Type | Description |
|---|---|---|
| $ | ConstantProductStorage |
__ConstantProduct_init
Initializes the ConstantProduct pool with two ERC-20 token addresses.
Must be called inside a pre/postInitializer block. Sorts tokens so token0 < token1.
function __ConstantProduct_init(address tokenA, address tokenB) internal;Parameters
| Name | Type | Description |
|---|---|---|
| tokenA | address | One of the two pool tokens. |
| tokenB | address | The other pool token. |
registerInterface
Registers support for IConstantProduct via ERC-165.
function registerInterface() internal;token0
Returns token0 address.
function token0() internal view returns (address);Returns
| Name | Type | Description |
|---|---|---|
| <none> | address |
token1
Returns token1 address.
function token1() internal view returns (address);Returns
| Name | Type | Description |
|---|---|---|
| <none> | address |
getReserves
Returns the current reserves and last-update timestamp.
function getReserves() internal view returns (uint256 reserve0, uint256 reserve1, uint32 blockTimestampLast);Returns
| Name | Type | Description |
|---|---|---|
| reserve0 | uint256 | |
| reserve1 | uint256 | |
| blockTimestampLast | uint32 |
totalLpSupply
Returns the total LP share supply.
function totalLpSupply() internal view returns (uint256);Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
lpBalanceOf
Returns the LP share balance of account.
function lpBalanceOf(address account) internal view returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| account | address |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
feeBps
Returns the swap fee in basis points (always 30).
function feeBps() internal pure 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
) internal returns (uint256 amount0, uint256 amount1, uint256 liquidity);Parameters
| Name | Type | Description |
|---|---|---|
| amount0Desired | uint256 | Desired deposit of token0. |
| amount1Desired | uint256 | Desired deposit of token1. |
| amount0Min | uint256 | Minimum acceptable token0 deposit (slippage guard). |
| amount1Min | uint256 | Minimum acceptable token1 deposit (slippage guard). |
| to | address | Recipient of minted LP shares. |
Returns
| Name | Type | Description |
|---|---|---|
| amount0 | uint256 | Actual token0 deposited. |
| amount1 | uint256 | Actual token1 deposited. |
| liquidity | uint256 | LP shares minted to to. |
removeLiquidity
Burns LP shares from the caller and returns underlying tokens to to.
function removeLiquidity(uint256 liquidity, uint256 amount0Min, uint256 amount1Min, address to)
internal
returns (uint256 amount0, uint256 amount1);Parameters
| Name | Type | Description |
|---|---|---|
| liquidity | uint256 | Amount of LP shares to burn. |
| amount0Min | uint256 | Minimum acceptable token0 returned (slippage guard). |
| amount1Min | uint256 | Minimum acceptable token1 returned (slippage guard). |
| to | address | Recipient of the returned tokens. |
Returns
| Name | Type | Description |
|---|---|---|
| amount0 | uint256 | Amount of token0 returned. |
| amount1 | uint256 | Amount of token1 returned. |
swapExactTokensForTokens
Swaps an exact input amount for output tokens.
function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, bool zeroForOne, address to)
internal
returns (uint256 amountOut);Parameters
| Name | Type | Description |
|---|---|---|
| amountIn | uint256 | Exact input token amount to sell. |
| amountOutMin | uint256 | Minimum acceptable output (slippage guard). |
| zeroForOne | bool | True = sell token0 for token1; False = sell token1 for token0. |
| to | address | Recipient of output tokens. |
Returns
| Name | Type | Description |
|---|---|---|
| amountOut | uint256 | Output token amount sent to to. |
_executeSwap
Internal helper that carries out the swap to avoid stack-too-deep in the public entry point.
Fee-on-transfer safe: the input is pulled first and the ACTUAL received
balance delta (effectiveAmountIn) drives the output, K-invariant,
slippage and reserve math. For standard tokens the delta equals the
requested amount, preserving existing behavior. The reentrancy guard in
the caller protects the pull-before-update ordering.
function _executeSwap(
ConstantProductStorage storage $,
uint256 amountIn,
uint256 amountOutMin,
bool zeroForOne,
address caller,
address to
) private returns (uint256 amountOut);Parameters
| Name | Type | Description |
|---|---|---|
| $ | ConstantProductStorage | |
| amountIn | uint256 | |
| amountOutMin | uint256 | |
| zeroForOne | bool | |
| caller | address | |
| to | address |
Returns
| Name | Type | Description |
|---|---|---|
| amountOut | uint256 |
_checkK
Validates that the constant-product invariant holds after a swap.
function _checkK(uint256 amountIn, uint256 amountOut, uint256 reserveIn, uint256 reserveOut) private pure;Parameters
| Name | Type | Description |
|---|---|---|
| amountIn | uint256 | |
| amountOut | uint256 | |
| reserveIn | uint256 | |
| reserveOut | uint256 |
getAmountOut
Computes the output amount given an input amount and pool reserves, applying the 0.3% fee.
function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut)
internal
pure
returns (uint256 amountOut);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 |
|---|---|---|
| amountOut | uint256 | The output amount after fee. |
getAmountIn
Computes the required input to receive a specific output amount, applying the 0.3% fee.
function getAmountIn(uint256 amountOut, uint256 reserveIn, uint256 reserveOut)
internal
pure
returns (uint256 amountIn);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 |
|---|---|---|
| amountIn | uint256 | The required input amount (rounded up). |
quote
Returns the equivalent amount of tokenB given amountA at the current pool ratio.
No fee is applied — this is used for liquidity proportion calculations.
function quote(uint256 amountA, uint256 reserveA, uint256 reserveB) internal pure returns (uint256 amountB);Parameters
| Name | Type | Description |
|---|---|---|
| amountA | uint256 | Amount of tokenA. |
| reserveA | uint256 | Reserve of tokenA. |
| reserveB | uint256 | Reserve of tokenB. |
Returns
| Name | Type | Description |
|---|---|---|
| amountB | uint256 | Equivalent amount of tokenB. |
_sqrt
Babylonian square-root used for first-liquidity LP share calculation.
function _sqrt(uint256 y) internal pure returns (uint256 z);Parameters
| Name | Type | Description |
|---|---|---|
| y | uint256 | The value to take the square root of. |
Returns
| Name | Type | Description |
|---|---|---|
| z | uint256 | The integer square root of y. |
_mintLp
Mints LP shares to account, updating total supply.
function _mintLp(ConstantProductStorage storage $, address account, uint256 amount) private;Parameters
| Name | Type | Description |
|---|---|---|
| $ | ConstantProductStorage | |
| account | address | |
| amount | uint256 |
_burnLp
Burns LP shares from account, updating total supply.
function _burnLp(ConstantProductStorage storage $, address account, uint256 amount) private;Parameters
| Name | Type | Description |
|---|---|---|
| $ | ConstantProductStorage | |
| account | address | |
| amount | uint256 |
_safeTransferFromReceived
Pulls amount of token from from into the pool and returns the
ACTUAL received amount (the pool balance delta).
Fee-on-transfer / deflationary tokens deliver less than amount; the
measured delta is the effective input used for reserve, LP and K math.
For standard tokens the delta equals amount, preserving existing
behavior. Reverts ConstantProductTransferFailed on a false return.
function _safeTransferFromReceived(address token, address from, uint256 amount) private returns (uint256 received);Parameters
| Name | Type | Description |
|---|---|---|
| token | address | The token to pull. |
| from | address | The address to pull from. |
| amount | uint256 | The requested transfer amount. |
Returns
| Name | Type | Description |
|---|---|---|
| received | uint256 | The actual amount credited to the pool (post-fee). |
_safeTransfer
Calls transfer on token, reverts with ConstantProductTransferFailed on false return.
function _safeTransfer(address token, address to, uint256 amount) private;Parameters
| Name | Type | Description |
|---|---|---|
| token | address | |
| to | address | |
| amount | uint256 |