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).

ProtocolFee ProtocolFee

Contents

IFactory

Git Source

Functions

getProtocolBeneficiary

function getProtocolBeneficiary() external view returns (address);

IPool

Git Source

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

Git Source

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

NameTypeDescription
token0addressFirst asset of the pool.
token1addressSecond asset of the pool.

Returns

NameTypeDescription
poolAddressaddressAddress 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

NameTypeDescription
token0addressFirst asset of the pool.
token1addressSecond asset of the pool.

Returns

NameTypeDescription
<none>addressAddress 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

Git Source

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

Git Source

Inherits: IPool, LP

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

NameTypeDescription
tokenaddressInput token.
amountuint256Input token amount.

Returns

NameTypeDescription
amountOutuint256Amount 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

NameTypeDescription
amount0uint256First token amount to stake
amount1uint256Second token amount to stake

Returns

NameTypeDescription
liquidityuint256Liquidity 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

NameTypeDescription
lpAmountuint256Shares amount to withdraw.

Returns

NameTypeDescription
amount0uint256Amount in token0 that was unstaked.
amount1uint256Amount 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

NameTypeDescription
tokenaddressBase token for which you want to quote the price.
amountuint256Amount to quote in "token".

Returns

NameTypeDescription
<none>uint256Relative 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();