Just my clone of Uniswap V2. Code is untested and should not be used in any commercial project due to possible vulnerabilities.
Future docs: https://typicalhuman.github.io/mini-dex/src/Pool.sol/contract.Pool.html
Protocol Fee
The formula for (1/6 protocol fee).

Contents
IFactory
Functions
getProtocolBeneficiary
function getProtocolBeneficiary() external view returns (address);
IPool
Functions
swap
function swap(address token, uint256 amount) external returns (uint256);
deposit
function deposit(uint256 amount0, uint256 amount1) external returns (uint256);
withdraw
function withdraw(uint256 lpAmount) external returns (uint256, uint256);
quote
function quote(address token, uint256 amount) external view returns (uint256);
Factory
Inherits: IFactory
Author: typicalHuman.
Factory should be single instance for UX (finding pools for different tokens and for possible routing).
State Variables
s_owner
address immutable s_owner;
s_pools
mapping(address => mapping(address => address)) s_pools;
Functions
constructor
constructor();
createPool
Create new pool with 2 assets.
Pool should be unique (pool with these assets shouldn't be used on this factory yet).
function createPool(address token0, address token1) external returns (address poolAddress);
Parameters
| Name | Type | Description |
|---|---|---|
token0 | address | First asset of the pool. |
token1 | address | Second asset of the pool. |
Returns
| Name | Type | Description |
|---|---|---|
poolAddress | address | Address of newly created pool. |
getPool
Get pool address based on assets.
The order matters.
function getPool(address token0, address token1) external view returns (address);
Parameters
| Name | Type | Description |
|---|---|---|
token0 | address | First asset of the pool. |
token1 | address | Second asset of the pool. |
Returns
| Name | Type | Description |
|---|---|---|
<none> | address | Address of the pool, if not found - returns zero address. |
getProtocolBeneficiary
Get address which will receive protocol fees.
function getProtocolBeneficiary() external view returns (address);
Events
PoolCreated
event PoolCreated(address pool, address token0, address token1);
Errors
TOKENS_NOT_SORTED
error TOKENS_NOT_SORTED();
POOL_ALREADY_EXISTS
error POOL_ALREADY_EXISTS();
LP
Inherits: ERC20
Author: typicalHuman.
Represents pool shares.
State Variables
PREFIX
string constant PREFIX = "MINI-";
MINIMUM_LIQUIDITY
uint256 constant MINIMUM_LIQUIDITY = 10 ** 3;
s_name
string s_name;
Functions
constructor
constructor(string memory _name);
name
function name() public view override returns (string memory);
symbol
function symbol() public view override returns (string memory);
Pool
Author: typicalHuman
Contract that allows you to stake/swap 2 assets.
State Variables
LP_FEE
uint256 public constant LP_FEE = 30;
PROTOCOL_FEE
uint256 public constant PROTOCOL_FEE = 9;
factory
address public immutable factory;
kLast
uint256 public kLast;
s_token0
address immutable s_token0;
s_token1
address immutable s_token1;
s_reserve0
uint256 public s_reserve0;
s_reserve1
uint256 public s_reserve1;
Functions
constructor
Initializes pool.
Tokens should be sorted in asc order.
constructor(address token0, address token1) LP(string.concat(ERC20(token0).symbol(), ERC20(token1).symbol()));
checkTokenInside
modifier checkTokenInside(address token);
swap
Swap 1 token to another.
Token amount shouldn't be bigger than half of pool reserve.
function swap(address token, uint256 amount) external checkTokenInside(token) returns (uint256 amountOut);
Parameters
| Name | Type | Description |
|---|---|---|
token | address | Input token. |
amount | uint256 | Input token amount. |
Returns
| Name | Type | Description |
|---|---|---|
amountOut | uint256 | Amount out in second token. |
deposit
Stake tokens in pool
If total supply is 0, it will mint MINIMUM_LIQUIDITY to prevent inflation attack
function deposit(uint256 amount0, uint256 amount1) external returns (uint256 liquidity);
Parameters
| Name | Type | Description |
|---|---|---|
amount0 | uint256 | First token amount to stake |
amount1 | uint256 | Second token amount to stake |
Returns
| Name | Type | Description |
|---|---|---|
liquidity | uint256 | Liquidity minted after stake. It will represent your share in this pool. |
withdraw
Unstake shares back to tokens.
You can't withdraw your shares into only 1 one of the tokens.
function withdraw(uint256 lpAmount) external returns (uint256 amount0, uint256 amount1);
Parameters
| Name | Type | Description |
|---|---|---|
lpAmount | uint256 | Shares amount to withdraw. |
Returns
| Name | Type | Description |
|---|---|---|
amount0 | uint256 | Amount in token0 that was unstaked. |
amount1 | uint256 | Amount in token1 that was unstaked. |
quote
Get relative price in the second token.
Pool can't be empty (totalSupply should be bigger than 0).
function quote(address token, uint256 amount) public view checkTokenInside(token) returns (uint256);
Parameters
| Name | Type | Description |
|---|---|---|
token | address | Base token for which you want to quote the price. |
amount | uint256 | Amount to quote in "token". |
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | Relative price in second token. |
getTokens
function getTokens() public view returns (address, address);
mintFee
Fee comes from swaps and not deposits
Takes 1/10 of swaps fee
function mintFee(uint256 _reserve0, uint256 _reserve1) private;
_quote
function _quote(address token, uint256 amount) private view returns (uint256);
p_transferFrom
function p_transferFrom(address token, address from, address to, uint256 amount) private;
p_transfer
function p_transfer(address token, address to, uint256 amount) private;
Events
Swap
event Swap(address token0, address token1, uint256 amount0, uint256 amount1);
Deposit
event Deposit(address pool, uint256 lpAmount, uint256 amount0, uint256 amount1);
Withdraw
event Withdraw(address pool, uint256 lpAmount, uint256 amount0, uint256 amount1);
Errors
ZERO_ADDRESS
error ZERO_ADDRESS();
TOKEN_NOT_INSIDE_POOL
error TOKEN_NOT_INSIDE_POOL();
AMOUNT_EQUALS_ZERO
error AMOUNT_EQUALS_ZERO();
INSUFFICIENT_AMOUNT
error INSUFFICIENT_AMOUNT();
INSUFFICIENT_BALANCE
error INSUFFICIENT_BALANCE();
TRANSFER_FAILED
error TRANSFER_FAILED();
NOT_ENOUGH_BALANCE
error NOT_ENOUGH_BALANCE();