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();