# UnifiPair.sol

**Primary Uses -** UnifiPair.sol is responsible for many of the functionalities of liquidity pool tokens and UP tokens. First, it is responsible for the issuing and burning of Liquidity Pool Tokens (uTokens). In addition, it allows for direct reads of the reserves and ratio of the liquidity pool, as well as swaps. Lastly, it is where UP claims are processed.&#x20;

## uTrade V2 Pair Code / Interfaces

|                                             |                                                                                                       |
| ------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| uTrade V2 Pair (Solidity)                   | [Github](https://github.com/unifiprotocol/utrade-contracts/blob/main/Avalanche/Factory/UnifiPair.sol) |
| uTrade V2 Pair Interface as JSON            | Link Here                                                                                             |
| uTrade V2 Pair as Typescript                | Link Here                                                                                             |
| Import statement codeblock (when available) |                                                                                                       |

## uTrade V2 Pair Contract Addresses

Each uTrade V2 Liquidity Pool uses the uTrade V2 ERC20 Interface in the contract.&#x20;

## Events

### Mint

```
event Mint(address indexed sender, uint amount0, uint amount1);
```

The `Mint` event is emitted any time liquidity tokens are created via the `mint` function. In other words, when a user adds liquidity to a pair, then they will receive LP tokens, therefore the `Mint` event will be emitted.

### Burn

```
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
```

The `Burn` event is emitted any time liquidity tokens are burned via the `burn` function. In other words, when a user removes liquidity from a pair,  their LP tokens will be burned, therefore the `Burn` event will be emitted.

### Swap

```
event Swap(
        address indexed sender,
        uint amount0In,
        uint amount1In,
        uint amount0Out,
        uint amount1Out,
        address indexed to
 );
```

The `Swap` event is emitted any time the `swap` function is used. Under the hood, all trades on uTrade V2 are swaps. Therefore, any time somebody trades on the pair, the uTrade contract for that pair will emit a `Swap` event.

### Sync

```
event Sync(uint112 reserve0, uint112 reserve1);
```

The `Sync` event is emitted anytime a function occurs that may change the reserves of a token pair. In other words, anytime the amount of the two tokens within a liquidity pool may change. Therefore, whenever a`mint`, `burn`, `swap`, or `sync` function is called, the `Sync` event will be emitted.

## Read-Only Functions

### MINIMUM\_LIQUIDITY <a href="#minimum_liquidity" id="minimum_liquidity"></a>

```
function MINIMUM_LIQUIDITY() external pure returns (uint);
```

The `MINIMUM_LIQUIDITY`function will always return 1000. The function itself refers to the burning of initial LP tokens that occurs once when a pool is created. This burn of a tiny amount allows for cleaner LP token numbers therefore avoiding LP tokens being represented as very small decimals value. This allows the tick size to be more precise and prevents rounding errors.

### factory

```
function factory() external view returns (address);
```

The `factory` function will return the current factory address for uTrade V2.

### WBNB

```
function WBNB() external view returns (address);
```

The WBNB function will return the address of WBTT on BTTC. As this does not change, it will always return `0x8D193c6efa90BCFf940A98785d1Ce9D093d3DC8A`.

### token0

```
function token0() external view returns (address);
```

The `token0` function will return the contract address of the first token that makes up the liquidity pair. In other words, if the liquidity pool is made up of USDT.t / WBTT, it will return the contract address of USDT.t.

### token1

```
function token1() external view returns (address);
```

The `token1` function will return the contract address of the first token that makes up the liquidity pair. In other words, if the liquidity pool is made up of USDT.t / WBTT, it will return the contract address of WBTT.

### getReserves

```
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
```

The `getReserves` function returns the reserves of the two tokens that make up the liquidity pool as *reserve0* and *reserve1*. These two values can be helpful in determining the current price of each asset. The function also returns a timestamp with the block number.

### price0CumulativeLast

```
function price0CumulativeLast() external view returns (uint);
```

The `price0CumulativeLast` function is for Oracle usage on uTrade V2. The value of *token0* is captured at the end of each block, and can be called using this function to feed into an Oracle to determine a more time-weighted 'average' price.&#x20;

### price1CumulativeLast

```
function price1CumulativeLast() external view returns (uint);
```

The `price1CumulativeLast` function is for Oracle usage on uTrade V2. The value of *token1* is captured at the end of each block, and can be called using this function to feed into an Oracle to determine a more time-weighted 'average' price.&#x20;

### kLast

```
function kLast() external view returns (uint);
```

The `kLast` function returns the value of *reserve0* \* *reserve1*, after any event that may have triggered a change in the liquidity. For example, the execution of a *swap* function or a *mint* function.

## State-Changing Functions

### mint

```
function mint(address to) external returns (uint liquidity);
```

The `mint` function creates the LP tokens that represent a user's tokens in a liquidity pool. For example, if a user provides 1,000,000 BTT and 10 USDT.t liquidity to a pool, the Unifi Pair Smart Contract will mint an amount of uBTTUSDT.t tokens. Will emit the `Mint`, `Sync`, and `Transfer` events.

### burn

```
function burn(address to) external returns (uint amount0, uint amount1);
```

The `burn` function destroys the LP tokens that represent a user's token in a liquidity pool. For example, if a user removes 1,000,000 BTT and 10 USDT.t liquidity to a pool, the Unifi Pair Smart Contract will burn an amount of uBTTUSDT.t tokens. Will emit the `Burn`, `Sync`, and `Transfer` events.

### claimUP

```
function claimUP(address to) external lock returns(uint) {
```

The `claimUP` function claims any UP earned from providing liquidity if any exists, and sends the UP to the address provided.

### swap

```
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
```

The `swap` function exchanges one token for another. Under the hood, all trades on uTrade V2 use this function. The *calldata* must be 0 during a normal swap, but must contain data if executing a flash loan. Emits the `Swap` and `Sync` events.

### skim

```
function skim(address to) external;
```

The `skim` function operates as a safeguard if the amount of tokens causes a data error due to too large of a number in the reserves pools. In this unusual circumstance, this will trigger failures in trades. The `skim` function can be called to return the overflowed tokens to the caller.

### sync

```
function sync() external;
```

The `sync` function operates as a safeguard in certain events where the token balance changes outside of normal trading. An example would be an algorithmic stablecoin re-balancing, therefore lowering or raising the amount of the algorithmic stablecoin in the pool. The `sync` function may be called to reset the price ratio to the new reserves. Emits the `Sync`event.

## Interface Code

```
interface IUnifiPair {
    event Mint(address indexed sender, uint amount0, uint amount1);
    event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
    event Swap(
        address indexed sender,
        uint amount0In,
        uint amount1In,
        uint amount0Out,
        uint amount1Out,
        address indexed to
    );
    event Sync(uint112 reserve0, uint112 reserve1);

    function MINIMUM_LIQUIDITY() external pure returns (uint);
    function factory() external view returns (address);
    function token0() external view returns (address);
    function token1() external view returns (address);
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
    function price0CumulativeLast() external view returns (uint);
    function price1CumulativeLast() external view returns (uint);
    function kLast() external view returns (uint);

    function mint(address to) external returns (uint liquidity);
    function burn(address to) external returns (uint amount0, uint amount1);
    function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
    function skim(address to) external;
    function sync() external;

    function initialize(address, address) external;
}
```
