# Factory

{% hint style="warning" %}
Lovely Swap is a highly customized version of Uniswap v2. It contains additional logic and constraints around validating and listing token pairs.

Read the [Uniswap v2 documentation](https://uniswap.org/docs/v2/).\
For more in-depth information on the core contract logic, read the [Uniswap v2 Core whitepaper](https://uniswap.org/whitepaper.pdf).
{% endhint %}

## Contract info

**Contract name:** LovelyFactory

View [Lovely Swap Factory.sol on GitHub](https://github.com/Lovely-Swap/lovelyswap-v1/blob/main/contracts/LovelyFactory.sol).

| Chain   | Address                                    |
| ------- | ------------------------------------------ |
| BSC     | 0x7db16925214B2F5D65dB741D59208A1187B9961c |
| Base    | 0x7db16925214B2F5D65dB741D59208A1187B9961c |
| Polygon | 0x177aeb3727c91c4796766336923c4da431c59637 |
| X Layer | 0x364ba5dfd5920bd4a82d2ae3fa392da187f7331e |

## Read functions

**SetDefaultValidationAmount**

```solidity
event SetDefaultValidationAmount(uint amount)
```

Emitted when the `defaultValidationAmount` is updated.

**SetMainToken**

```solidity
event SetMainToken(address token)
```

Emitted when the `mainToken` is set.

**PairCreated**

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

Emitted each time a pair is created via createPair.

* `token0` is guaranteed to be strictly less than `token1` by sort order.
* The final `uint` log value will be `1` for the first pair created, `2` for the second, etc. (see allPairs/getPair).

#### Read-Only Functions

**getTokenList**

```solidity
function getTokenList() external view returns (LOVELYTokenList)
```

Returns the on-chain token list reference.

**getPair**

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

Returns the address of the pair for `tokenA` and `tokenB`, if it has been created, else `address(0)` (`0x0000000000000000000000000000000000000000`).

* `tokenA` and `tokenB` are interchangeable.
* Pair addresses can also be calculated deterministically via the SDK.

**allPairs**

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

Returns the address of the `n`th pair (`0`-indexed) created through the factory, `or address(0)` (`0x0000000000000000000000000000000000000000`) if not enough pairs have been created yet.

* Pass `0` for the address of the first pair created, `1` for the second, etc.

**allPairsLength**

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

Returns the total number of pairs created through the factory so far.

**feeTo**

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

See Protocol Charge Calculation.

**feeToSetter**

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

The address allowed to change `feeTo`.

#### State-Changing Functions

**createValidatedPair**

```solidity
function createValidatedPair(
  address tokenA,
  address tokenB,  
  address tokenC, 
  uint validationAmount,
  uint fee    
) external returns (address pair)
```

Creates a validated token pair. Calls `LOVELYTokenList` to ensure tokens are valid and activated.

Enforces constraints around the main token if set and minimum validation amounts.

**setDefaultValidationAmount**

```solidity
function setDefaultValidationAmount(uint amount) external
```

Sets the default validation amount required to list new pairs.

**setMainToken**

```solidity
function setMainToken(address token) external
```

Sets the main DEX token. New pairs must contain this token.
