# UnifiFactory.sol

**Primary Uses** - The uTrade V2 Factory contract creates an LP token for any pairs listed on uTrade V2, and indexes them for easy retrieval. In addition, it can return the address of the LP token based on a call of the addresses of the two tokens that make up the liquidity pool.

## uTrade V2 Factory Code / Interfaces

<table data-header-hidden><thead><tr><th width="277"></th><th></th></tr></thead><tbody><tr><td></td><td></td></tr><tr><td>uTrade V2 Factory (Solidity)</td><td><a href="https://github.com/unifiprotocol/utrade-contracts/blob/main/Avalanche/Factory/UnifiFactory.sol">GitHub</a></td></tr><tr><td>uTrade V2 Factory Interface as JSON</td><td><a href="https://github.com/unifiprotocol/uTradeV2-core/blob/52d66be2a472b9697a577757a0446076b6c32001/ABI/JSON/IUnifiFactory.json">GitHub</a></td></tr><tr><td>uTrade V2 Factory Interface as Typescript</td><td><a href="https://github.com/unifiprotocol/uTradeV2-core/blob/52d66be2a472b9697a577757a0446076b6c32001/ABI/TS/IUnifiFactory.ts">GitHub</a></td></tr><tr><td>Import statement codeblock (when available)</td><td></td></tr></tbody></table>

## uTrade V2 Factory Contract Addresses

| Network           | Address                                                                                                                        |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| Avalanche C-Chain | `0x839547067bc885db205F5fA42dcFeEcDFf5A8530` ([Link](https://snowtrace.io/address/0x839547067bc885db205f5fa42dcfeecdff5a8530)) |

## Events

### PairCreated

```
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
```

Anytime a pair is created on uTrade V2 using the `createPair` function, a `PairCreated` event is emitted. Contracts can be deployed to listen for new pairs on the uTrade V2 AVAX Factory address.&#x20;

* *token0* is the token address of the first asset in the token pair.
* *token1* is the token address of the second asset in the token pair.
* *pair* is the address of the newly created uTrade V2 liquidity pool.
* *uint* refers to the index of this uTrade V2 Factor&#x79;*.* For example, the first liquidity pool created on uTrade V2 is 1, the second liquidity pool is 2, and so on. This number can used with the `allPairs(uint)` to return the address. The current number, and therefore the total number of LP pools on uTrade V2 AVAX, can be accessed using the `allPairsLength` function.

## Read-Only Functions <a href="#read-only-functions" id="read-only-functions"></a>

### getPair

```
function getPair(address tokenA, address tokenB) external view returns (address pair);
```

A call to the `getPair` function returns the address of the pair for *tokenA* and *tokenB*.

* If the pair does not exist, the call will return *address(0)*.&#x20;
* The order of the tokens is irrelevant in this call. For example, a call for USDT.e, WAVAX will return the same pair address as USDT.e, WAVAX.&#x20;

### allPairs

```
function allPairs(uint) external view returns (address pair);
```

A call to the `allPairs` function returns the address of a pair based on the indexed *uint* value assigned upon creation of the LP.

* For example, `allPairs(0)` will return the first pair created on uTrade V2 AVAX.
* &#x20;If the index number is too high, as in, there aren't enough pairs created yet, the function will return *address(0)*.

### allPairsLength

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

A call to the `allPairsLength` function returns the current number of pairs.&#x20;

* For example, if there are 201 total liquidity pool pairs on uTrade V2 AVAX, this call will return *200* as an uint value.

### feeTo

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

A call to the `feeTo` function returns the percentage of trading fees that Unifi Protocol receives.&#x20;

* Due to the nature of UP Token economics, this is set to zero, but is preserved for flexibility in the future.

### feeToSetter

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

A call to the `feeToSetter` function returns the address to which the `feeTo` would send trading fees, if trading fees were collected.

## State-Changing Functions <a href="#state-changing-functions" id="state-changing-functions"></a>

### createPair

```
function createPair(address tokenA, address tokenB) external returns (address pair);
```

Creates a liquidity pool pair for *tokenA* and *tokenB* if one does not currently exist. After the function is confirmed on chain, a `PairCreated` event is emitted.

## Interface Code

```
interface UnifiFactory {  
  event PairCreated(address indexed token0, address indexed token1, address pair, uint);
  function getPair(address tokenA, address tokenB) external view returns (address pair);  
  function allPairs(uint) external view returns (address pair);  
  function allPairsLength() external view returns (uint);
  function feeTo() external view returns (address);  function feeToSetter() external view returns (address);
  function createPair(address tokenA, address tokenB) external returns (address pair);
  }
```

###

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.unifiprotocol.com/utrade-v2/avalanche/unififactory.sol.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
