# Exchange

This is a smart contract to expand ERC-20 that is created with every liquidity pair. The Factory deploys this as new pool creation is requested. Although the Factory actually stores the two tokens of a pair and swapping takes place through it, the Exchange Contract must be called for liquidity to be provided or taken.

## Code

Github Link: (Will be updated after official launch)

## Address

Contract address after production is deployed&#x20;

## Events, Read-Only Functions, and State-Changing Functions&#x20;

{% tabs %}
{% tab title="Events" %}

## Events

#### Transfer

```solidity
event Transfer(address indexed from, address indexed to, uint amount);
```

* ERC-20 Standard
* Event log of Transfer/Mint/Burn

#### Approval

```solidity
event Approval(address indexed holder, address indexed spender, uint amount);
```

* ERC-20 Standard
* Event log of Approvals

#### ChangeMiningRate

```solidity
event ChangeMiningRate(uint _mining);
```

* Event log of mining rate change&#x20;

#### ChangeFee

```solidity
event ChangeFee(uint _fee);
```

* Event log of trading fee rate changes

#### ChangeRateNumerator

```solidity
event ChangeRateNumerator(uint rateNumerator);
```

* Event log of rateNumerator changes

#### UpdateMiningIndex

```solidity
event UpdateMiningIndex(uint lastMined, uint miningIndex);
```

* Event log of pairs’ mining index changes
* `lastMined` : Factory.mined() value at the time index is updated
* `miningIndex` : pairs’ miningIndex value at the time

#### GiveReward

```solidity
event GiveReward(address user, uint amount, uint lastIndex, uint rewardSum);
```

* Event log of when mined MESH is claimed and distributed
* `user` :  address of the user who claimed
* `amount` :  the amount of MESH claimed
* `lastIndex` :  index result of the pair of the wallet after claiming&#x20;
* `rewardSum` :  The amount of MESH that has been accrued so far

#### ExchangePos

```solidity
event ExchangePos(address token0, uint amount0, address token1, uint amount1);
```

* Event log of POS transactions
* `token0` : Token address delivered by the user
* `amount0` :  the amount of tokens delivered by the user
* `token1` : token address received by user&#x20;
* `amount1` : the amount of tokens received by the user

#### ExchangeNeg

```
event ExchangeNeg(address token0, uint amount0, address token1, uint amount1);
```

* Event log of NEG transactions
* `token0` :  token address delivered by the user
* `amount0` : the amount of tokens delivered by the user&#x20;
* `token1` : token address received by the user
* `amount1` : the amount of tokens received by the user

#### AddLiquidity

```
event AddLiquidity(address user, address token0, uint amount0, address token1, uint amount1, uint liquidity);
```

* Event log of liquidity additions
* `liquidity` :  the amount of LP tokens minted due to additional liquidity

#### RemoveLiquidity

```
event RemoveLiquidity(address user, address token0, uint amount0, address token1, uint amount1, uint liquidity);
```

* Event log of liquidity removals
* `liquidity` : the amount of LP tokens burned due to liquidity removal

**Sync**

```
event Sync(uint112 reserveA, uint112 reserveB);
```

* Emitted each time reserves are updated.
* Parameters
  * pool :  Pool contract address of target pool
  * reserveA : Token0 liquidity amount
  * reserveB : Token1 liquidity amount
    {% endtab %}

{% tab title="Read-Only Functions" %}

## Read-Only Functions

#### name

* Meshswap LP

#### symbol

* MSLP

#### decimals

* 18

#### totalSupply

* mint/burn depending on liquidity addition/removal

#### balanceOf

* Number of LP tokens held by each address

#### allowance

* Status of approval to spender for each address

#### factory

* The factory address where the LP smart contract is deployed

#### token0

* The first token address composed of the pair

#### token1

* The second token address composed of the pair

**reserve0**

* Returns the liquidity quantity of the token0 asset in the current pool

**reserve1**

* Returns the liquidity quantity of the token1 asset in the current pool

#### fee

* Transaction fee for the pair
* It is a value between 0 and 100, in units of 0.01%
* 1 == 0.01%, 100 == 1%

#### mining

* The mining rate of the pair
* It is a value between 0 and 100, in units of 1%
* e.g. If mining is 10, the pair has claim authority for 10% of the quantity mined per block in the factory.

#### lastMined

* Factory.mined() value at the last time the pair updated the index

#### miningIndex

* Index value of the last recorded pair

#### userLastIndex

* mapping(address => uint)&#x20;
* Stores the index value at the time of last reward for each user

#### userRewardSum

* mapping(address => uint )
* Stores the cumulatively claimed MESH quantity value for each user

#### getCurrentPool

```
function getCurrentPool() public view returns (uint, uint)
```

* Returns the liquidity quantity of the two assets in the current pool

#### estimatePos

```
function estimatePos(address token, uint amount) public view returns (uint)
```

* When a request is made to sell the amount of the corresponding token through a POS transaction, the expected receiving amount of the opposite token is returned.

#### estimateNeg

```
function estimateNeg(address token, uint amount) public view returns (uint)
```

* When a request is made to purchase the amount of the corresponding token through a NEG transaction, the expected payment amount of the opposite token is returned.
  {% endtab %}

{% tab title="State-Changing Functions" %}

## State-Changing Functions

#### transfer

```solidity
function transfer(address _to, uint _value) public returns (bool)
```

* ERC-20 Standard

#### transferFrom

```solidity
function transferFrom(address _from, address _to, uint _value) public returns (bool)
```

* ERC-20 Standard
* Method to transfer tokens on behalf of the approved wallet

#### approve

```solidity
function approve(address _spender, uint _value) public returns (bool)
```

* ERC-20 Standard
* Method to approve transfer as much as value to the spender

#### updateMiningIndex

```solidity
function updateMiningIndex() public
```

* Method to update the value of the pair contract based on the current Factory.mined() value
* Anyone can call in real time. Even if no one calls it, it calls itself if necessary.

#### claimReward

```
function claimReward() public
```

* Method that a user calls to claim the claimable MESH that has accumulated for the pair
* When called, MESH is claimed from the Factory and paid to `msg.sender`
* Even if the method is not called directly, it is automatically called when the LP token balance of the user’s wallet changes.
  * When liquidity is added
  * When liquidity is removed
  * When LP tokens are sent
  * When LP tokens are received

#### addETHLiquidityWithLimit

```
function addETHLiquidityWithLimit(uint amount, uint minAmount0, uint minAmount1, address user) public payable returns (uint real0, uint real1, uint amountLP) 
```

* Method used to provide liquidity when token0 is MATIC
* Liquidity of the MATIC `msg.value` and token1 `amount` is provided.
* After liquidity is provided, the LP token corresponding to the pool share is minted in the `msg.sender` wallet.
* If the pair ratio is wrong, as much liquidity as possible is supplied at the current pair ratio, and the remaining tokens are returned to `msg.sender`.
* If there is claimable MESH when called, the claim proceeds to `msg.sender`.

**addTokenLiquidityWithLimit**

```
function addTokenLiquidityWithLimit(uint amount0, uint amount1, uint minAmount0, uint minAmount1, address user) public nonReentrant returns (uint real0, uint real1, uint amountLP)
```

* Method used to provide liquidity when token0 is MATIC
* Liquidity of the MATIC `msg.value` and token1 `amount` is provided.
* Set the minimum amount of token0 and token1 to be used for liquidity supply using minAmount0, minAmount1.
* After liquidity is provided, the LP token corresponding to the pool share is minted in the `msg.sender` wallet.
* If the pair ratio is wrong, as much liquidity as possible is supplied at the current pair ratio, and the remaining tokens are returned to `msg.sender`.
* If there is claimable MESH when called, the claim proceeds to `msg.sender`.

#### removeLiquidityWithLimitETH

```
function removeLiquidityWithLimitETH(uint amount, uint minAmount0, uint minAmount1, address user) public nonReentrant returns (uint, uint)
```

* Returns the amount of LP tokens, and distributes the corresponding token0 and token1 to the msg.sender wallet
* The returned LP token amount is burned
* If there is claimable MESH when called, the claim proceeds to msg.sender.

**removeLiquidityWithLimit**

```
function removeLiquidityWithLimit(uint amount, uint minAmount0, uint minAmount1, address user) public nonReentrant returns (uint, uint)
```

* Returns the amount of LP tokens, and distributes the corresponding token0 and token1 to the msg.sender wallet
* Set the minimum amount for the returned token0 and token1 amounts by using minAmount0, minAmount1.
* The returned LP token amount is burned
* If there is claimable MESH when called, the claim proceeds to `msg.sender`.
  {% endtab %}
  {% endtabs %}
