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