// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
/**
* @title UniswapV2Pair
* @dev Complete Uniswap V2 style AMM pair implementation for Neo blockchain
* Features: liquidity provision, token swapping, fee collection, price oracles
*/
contract UniswapV2Pair {
// Constants
uint256 public constant MINIMUM_LIQUIDITY = 1000;
bytes4 private constant SELECTOR = bytes4(keccak256(bytes('transfer(address,uint256)')));
// State variables
address public factory;
address public token0;
address public token1;
uint112 private reserve0; // uses single storage slot, accessible via getReserves
uint112 private reserve1; // uses single storage slot, accessible via getReserves
uint32 private blockTimestampLast; // uses single storage slot, accessible via getReserves
uint256 public price0CumulativeLast;
uint256 public price1CumulativeLast;
uint256 public kLast; // reserve0 * reserve1, as of immediately after the most recent liquidity event
uint256 private unlocked = 1;
// LP token state
string public name = 'Neo-Uniswap V2';
string public symbol = 'NEO-UNI-V2';
uint8 public constant decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
// Fee state
uint256 public constant FEE_DENOMINATOR = 1000;
uint256 public swapFee = 3; // 0.3%
address public feeTo;
// Events
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
event Mint(address indexed sender, uint256 amount0, uint256 amount1);
event Burn(address indexed sender, uint256 amount0, uint256 amount1, address indexed to);
event Swap(
address indexed sender,
uint256 amount0In,
uint256 amount1In,
uint256 amount0Out,
uint256 amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
event FeeToSet(address indexed feeTo);
event SwapFeeSet(uint256 swapFee);
// Custom errors
error Forbidden();
error InsufficientLiquidity();
error InsufficientAmount();
error InsufficientInputAmount();
error InsufficientOutputAmount();
error InvalidTo();
error Overflow();
error TransferFailed();
error ReentrancyGuard();
error IdenticalAddresses();
error ZeroAddress();
error InsufficientLiquidityMinted();
error InsufficientLiquidityBurned();
error InvalidK();
error InvalidFee();
// Modifiers
modifier lock() {
if (unlocked == 0) revert ReentrancyGuard();
unlocked = 0;
_;
unlocked = 1;
}
modifier onlyFactory() {
if (msg.sender != factory) revert Forbidden();
_;
}
/**
* @dev Constructor called once by factory at time of deployment
*/
constructor() {
factory = msg.sender;
}
/**
* @dev Initialize the pair with two token addresses (called once by factory)
*/
function initialize(address _token0, address _token1) external onlyFactory {
if (_token0 == _token1) revert IdenticalAddresses();
if (_token0 == address(0) || _token1 == address(0)) revert ZeroAddress();
token0 = _token0;
token1 = _token1;
// Update pair name and symbol based on token symbols
try IERC20(token0).symbol() returns (string memory symbol0) {
try IERC20(token1).symbol() returns (string memory symbol1) {
name = string(abi.encodePacked('Neo-Uniswap V2: ', symbol0, '-', symbol1));
symbol = string(abi.encodePacked(symbol0, '-', symbol1, ' LP'));
} catch {
// Keep default name and symbol
}
} catch {
// Keep default name and symbol
}
}
/**
* @dev Returns the reserves of token0 and token1 and timestamp of last update
*/
function getReserves() public view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast) {
_reserve0 = reserve0;
_reserve1 = reserve1;
_blockTimestampLast = blockTimestampLast;
}
/**
* @dev Safe token transfer function
*/
function _safeTransfer(address token, address to, uint256 value) private {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
if (!success || (data.length > 0 && !abi.decode(data, (bool)))) {
revert TransferFailed();
}
}
/**
* @dev Update reserves and timestamp on every liquidity change
*/
function _update(uint256 balance0, uint256 balance1, uint112 _reserve0, uint112 _reserve1) private {
if (balance0 > type(uint112).max || balance1 > type(uint112).max) {
revert Overflow();
}
uint32 blockTimestamp = uint32(block.timestamp % 2**32);
uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
// * never overflows, and + overflow is desired
price0CumulativeLast += uint256(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed;
price1CumulativeLast += uint256(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed;
}
reserve0 = uint112(balance0);
reserve1 = uint112(balance1);
blockTimestampLast = blockTimestamp;
emit Sync(reserve0, reserve1);
}
/**
* @dev If fee is on, mint liquidity equivalent to 1/6th of the growth in sqrt(k)
*/
function _mintFee(uint112 _reserve0, uint112 _reserve1) private returns (bool feeOn) {
address _feeTo = feeTo;
feeOn = _feeTo != address(0);
uint256 _kLast = kLast; // gas savings
if (feeOn) {
if (_kLast != 0) {
uint256 rootK = Math.sqrt(uint256(_reserve0) * _reserve1);
uint256 rootKLast = Math.sqrt(_kLast);
if (rootK > rootKLast) {
uint256 numerator = totalSupply * (rootK - rootKLast);
uint256 denominator = rootK * 5 + rootKLast;
uint256 liquidity = numerator / denominator;
if (liquidity > 0) _mint(_feeTo, liquidity);
}
}
} else if (_kLast != 0) {
kLast = 0;
}
}
/**
* @dev Mint LP tokens (called by router)
*/
function mint(address to) external lock returns (uint256 liquidity) {
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
uint256 balance0 = IERC20(token0).balanceOf(address(this));
uint256 balance1 = IERC20(token1).balanceOf(address(this));
uint256 amount0 = balance0 - _reserve0;
uint256 amount1 = balance1 - _reserve1;
bool feeOn = _mintFee(_reserve0, _reserve1);
uint256 _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
if (_totalSupply == 0) {
liquidity = Math.sqrt(amount0 * amount1) - MINIMUM_LIQUIDITY;
_mint(address(0xdead), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
} else {
liquidity = Math.min(amount0 * _totalSupply / _reserve0, amount1 * _totalSupply / _reserve1);
}
if (liquidity == 0) revert InsufficientLiquidityMinted();
_mint(to, liquidity);
_update(balance0, balance1, _reserve0, _reserve1);
if (feeOn) kLast = uint256(reserve0) * reserve1; // reserve0 and reserve1 are up-to-date
emit Mint(msg.sender, amount0, amount1);
}
/**
* @dev Burn LP tokens (called by router)
*/
function burn(address to) external lock returns (uint256 amount0, uint256 amount1) {
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
address _token0 = token0; // gas savings
address _token1 = token1; // gas savings
uint256 balance0 = IERC20(_token0).balanceOf(address(this));
uint256 balance1 = IERC20(_token1).balanceOf(address(this));
uint256 liquidity = balanceOf[address(this)];
bool feeOn = _mintFee(_reserve0, _reserve1);
uint256 _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
amount0 = liquidity * balance0 / _totalSupply; // using balances ensures pro-rata distribution
amount1 = liquidity * balance1 / _totalSupply; // using balances ensures pro-rata distribution
if (amount0 == 0 || amount1 == 0) revert InsufficientLiquidityBurned();
_burn(address(this), liquidity);
_safeTransfer(_token0, to, amount0);
_safeTransfer(_token1, to, amount1);
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
_update(balance0, balance1, _reserve0, _reserve1);
if (feeOn) kLast = uint256(reserve0) * reserve1; // reserve0 and reserve1 are up-to-date
emit Burn(msg.sender, amount0, amount1, to);
}
/**
* @dev Swap tokens (called by router)
*/
function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data) external lock {
if (amount0Out == 0 && amount1Out == 0) revert InsufficientOutputAmount();
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
if (amount0Out >= _reserve0 || amount1Out >= _reserve1) revert InsufficientLiquidity();
uint256 balance0;
uint256 balance1;
{ // scope for _token{0,1}, avoids stack too deep errors
address _token0 = token0;
address _token1 = token1;
if (to == _token0 || to == _token1) revert InvalidTo();
if (amount0Out > 0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokens
if (amount1Out > 0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokens
if (data.length > 0) IUniswapV2Callee(to).uniswapV2Call(msg.sender, amount0Out, amount1Out, data);
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
}
uint256 amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
uint256 amount1In = balance1 > _reserve1 - amount1Out ? balance1 - (_reserve1 - amount1Out) : 0;
if (amount0In == 0 && amount1In == 0) revert InsufficientInputAmount();
{ // scope for reserve{0,1}Adjusted, avoids stack too deep errors
uint256 balance0Adjusted = balance0 * FEE_DENOMINATOR - amount0In * swapFee;
uint256 balance1Adjusted = balance1 * FEE_DENOMINATOR - amount1In * swapFee;
if (balance0Adjusted * balance1Adjusted < uint256(_reserve0) * _reserve1 * (FEE_DENOMINATOR ** 2)) {
revert InvalidK();
}
}
_update(balance0, balance1, _reserve0, _reserve1);
emit Swap(msg.sender, amount0In, amount1In, amount0Out, amount1Out, to);
}
/**
* @dev Force balances to match reserves
*/
function skim(address to) external lock {
address _token0 = token0; // gas savings
address _token1 = token1; // gas savings
_safeTransfer(_token0, to, IERC20(_token0).balanceOf(address(this)) - reserve0);
_safeTransfer(_token1, to, IERC20(_token1).balanceOf(address(this)) - reserve1);
}
/**
* @dev Force reserves to match balances
*/
function sync() external lock {
_update(IERC20(token0).balanceOf(address(this)), IERC20(token1).balanceOf(address(this)), reserve0, reserve1);
}
// LP Token functions
/**
* @dev Approve spender to transfer tokens
*/
function approve(address spender, uint256 value) external returns (bool) {
_approve(msg.sender, spender, value);
return true;
}
/**
* @dev Transfer tokens
*/
function transfer(address to, uint256 value) external returns (bool) {
_transfer(msg.sender, to, value);
return true;
}
/**
* @dev Transfer tokens from one address to another
*/
function transferFrom(address from, address to, uint256 value) external returns (bool) {
if (allowance[from][msg.sender] != type(uint256).max) {
allowance[from][msg.sender] -= value;
}
_transfer(from, to, value);
return true;
}
/**
* @dev Internal transfer function
*/
function _transfer(address from, address to, uint256 value) private {
balanceOf[from] -= value;
balanceOf[to] += value;
emit Transfer(from, to, value);
}
/**
* @dev Internal approve function
*/
function _approve(address owner, address spender, uint256 value) private {
allowance[owner][spender] = value;
emit Approval(owner, spender, value);
}
/**
* @dev Internal mint function
*/
function _mint(address to, uint256 value) private {
totalSupply += value;
balanceOf[to] += value;
emit Transfer(address(0), to, value);
}
/**
* @dev Internal burn function
*/
function _burn(address from, uint256 value) private {
balanceOf[from] -= value;
totalSupply -= value;
emit Transfer(from, address(0), value);
}
// Admin functions (only factory can call)
/**
* @dev Set fee recipient address
*/
function setFeeTo(address _feeTo) external onlyFactory {
feeTo = _feeTo;
emit FeeToSet(_feeTo);
}
/**
* @dev Set swap fee (in basis points, max 1% = 10)
*/
function setSwapFee(uint256 _swapFee) external onlyFactory {
if (_swapFee > 10) revert InvalidFee(); // Max 1%
swapFee = _swapFee;
emit SwapFeeSet(_swapFee);
}
// View functions for external integrations
/**
* @dev Get amount out for exact amount in
*/
function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut)
external
view
returns (uint256 amountOut)
{
if (amountIn == 0) revert InsufficientAmount();
if (reserveIn == 0 || reserveOut == 0) revert InsufficientLiquidity();
uint256 amountInWithFee = amountIn * (FEE_DENOMINATOR - swapFee);
uint256 numerator = amountInWithFee * reserveOut;
uint256 denominator = reserveIn * FEE_DENOMINATOR + amountInWithFee;
amountOut = numerator / denominator;
}
/**
* @dev Get amount in for exact amount out
*/
function getAmountIn(uint256 amountOut, uint256 reserveIn, uint256 reserveOut)
external
view
returns (uint256 amountIn)
{
if (amountOut == 0) revert InsufficientAmount();
if (reserveIn == 0 || reserveOut == 0) revert InsufficientLiquidity();
uint256 numerator = reserveIn * amountOut * FEE_DENOMINATOR;
uint256 denominator = (reserveOut - amountOut) * (FEE_DENOMINATOR - swapFee);
amountIn = (numerator / denominator) + 1;
}
/**
* @dev Calculate current price of token0 in terms of token1
*/
function price0() external view returns (uint256) {
(uint112 _reserve0, uint112 _reserve1,) = getReserves();
if (_reserve0 == 0) return 0;
return (uint256(_reserve1) * 1e18) / uint256(_reserve0);
}
/**
* @dev Calculate current price of token1 in terms of token0
*/
function price1() external view returns (uint256) {
(uint112 _reserve0, uint112 _reserve1,) = getReserves();
if (_reserve1 == 0) return 0;
return (uint256(_reserve0) * 1e18) / uint256(_reserve1);
}
}
// Required interfaces
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function symbol() external view returns (string memory);
}
interface IUniswapV2Callee {
function uniswapV2Call(address sender, uint256 amount0, uint256 amount1, bytes calldata data) external;
}
// Math library for sqrt and min functions
library Math {
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = x < y ? x : y;
}
function sqrt(uint256 y) internal pure returns (uint256 z) {
if (y > 3) {
z = y;
uint256 x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
} else if (y != 0) {
z = 1;
}
}
}
// UQ112x112 library for price calculations
library UQ112x112 {
uint224 constant Q112 = 2**112;
// encode a uint112 as a UQ112x112
function encode(uint112 y) internal pure returns (uint224 z) {
z = uint224(y) * Q112; // never overflows since y < 2**112
}
// divide a UQ112x112 by a uint112, returning a UQ112x112
function uqdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
z = x / uint224(y);
}
}