# MaverickV2Factory

**Inherits:** [IMaverickV2Factory](https://docs.mav.xyz/technical-reference/maverick-v2/v2-contracts/maverick-v2-common-contracts/interfaces/imaverickv2factory), [IMaverickV2FactoryAdmin](https://docs.mav.xyz/technical-reference/maverick-v2/v2-contracts/maverick-v2-common-contracts/interfaces/imaverickv2factoryadmin), Ownable

Pool Factory contract. Deploys both permissionless and permissioned Maverick V2 pools using deterministic create2 addresses. Deployed pools can be looked up by their parameters or by the token pair. This contract is ownable with the owner having permission to set protocol fee for all pools and collect protocol fee proceeds from pools.

### State Variables <a href="#state-variables" id="state-variables"></a>

#### poolLists <a href="#poollists" id="poollists"></a>

Mapping elements are \[tokenA]\[tokenB] -> list of pools.

```solidity
mapping(IERC20 => mapping(IERC20 => IMaverickV2Pool[])) private poolLists;
```

#### poolListsPermissioned <a href="#poollistspermissioned" id="poollistspermissioned"></a>

Mapping elements are \[accessor]\[tokenA]\[tokenB] -> list of pools.

```solidity
mapping(address => mapping(IERC20 => mapping(IERC20 => IMaverickV2Pool[]))) private poolListsPermissioned;
```

#### isFactoryPool <a href="#isfactorypool" id="isfactorypool"></a>

Bool indicating whether the pool was deployed from this factory.

```solidity
mapping(IMaverickV2Pool => bool) public isFactoryPool;
```

#### isFactoryPoolPermissioned <a href="#isfactorypoolpermissioned" id="isfactorypoolpermissioned"></a>

Bool indicating whether the pool was deployed from this factory.

```solidity
mapping(IMaverickV2Pool => bool) public isFactoryPoolPermissioned;
```

#### protocolFeeRatioD3 <a href="#protocolfeeratiod3" id="protocolfeeratiod3"></a>

Proportion of protocol fee to collect on each swap. Value is in 3-decimal format with a maximum value of 0.25e3.

```solidity
uint8 public protocolFeeRatioD3;
```

#### protocolLendingFeeRateD18 <a href="#protocollendingfeerated18" id="protocollendingfeerated18"></a>

Fee rate charged by the protocol for flashloans. Value is in 18-decimal format with a maximum value of 0.02e18.

```solidity
uint256 public protocolLendingFeeRateD18;
```

#### deployParameters <a href="#deployparameters" id="deployparameters"></a>

Called by deployer library to initialize a pool.

```solidity
DeployParameters public deployParameters;
```

#### protocolFeeReceiver <a href="#protocolfeereceiver" id="protocolfeereceiver"></a>

Address that receives the protocol fee when users call `claimProtocolFeeForPool`.

```solidity
address public protocolFeeReceiver;
```

#### allPools <a href="#allpools" id="allpools"></a>

Array of all permissionless pools.

```solidity
IMaverickV2Pool[] private allPools;
```

#### allPoolsPermissioned <a href="#allpoolspermissioned" id="allpoolspermissioned"></a>

Array of all permissioned pools.

```solidity
IMaverickV2Pool[] private allPoolsPermissioned;
```

### Functions <a href="#functions" id="functions"></a>

#### constructor <a href="#constructor" id="constructor"></a>

```solidity
constructor(address initialOwner) Ownable(initialOwner);
```

#### setProtocolFeeRatio <a href="#setprotocolfeeratio" id="setprotocolfeeratio"></a>

Set the protocol fee ratio.

```solidity
function setProtocolFeeRatio(uint8 _protocolFeeRatioD3) external onlyOwner;
```

**Parameters**

| Name                  | Type    | Description                                           |
| --------------------- | ------- | ----------------------------------------------------- |
| `_protocolFeeRatioD3` | `uint8` | The new protocol fee ratio to set in 3-decimal units. |

#### setProtocolLendingFeeRate <a href="#setprotocollendingfeerate" id="setprotocollendingfeerate"></a>

Set the protocol lending fee rate.

```solidity
function setProtocolLendingFeeRate(uint256 _protocolLendingFeeRateD18) external onlyOwner;
```

**Parameters**

| Name                         | Type      | Description                                                   |
| ---------------------------- | --------- | ------------------------------------------------------------- |
| `_protocolLendingFeeRateD18` | `uint256` | The new protocol lending fee rate to set in 18-decimal units. |

#### setProtocolFeeReceiver <a href="#setprotocolfeereceiver" id="setprotocolfeereceiver"></a>

Set the protocol fee receiver address. If protocol fee is non-zero, user will be able to permissionlessly push protocol fee from a given pool to this address.

```solidity
function setProtocolFeeReceiver(address receiver) external onlyOwner;
```

#### transferOwnership <a href="#transferownership" id="transferownership"></a>

Transfer ownership of the contract to a new owner.

```solidity
function transferOwnership(address newOwner) public override(IMaverickV2FactoryAdmin, Ownable) onlyOwner;
```

**Parameters**

| Name       | Type      | Description                   |
| ---------- | --------- | ----------------------------- |
| `newOwner` | `address` | The address of the new owner. |

#### renounceOwnership <a href="#renounceownership" id="renounceownership"></a>

Renounce ownership of the contract.

```solidity
function renounceOwnership() public override(IMaverickV2FactoryAdmin, Ownable) onlyOwner;
```

#### claimProtocolFeeForPool <a href="#claimprotocolfeeforpool" id="claimprotocolfeeforpool"></a>

Claim protocol fee for a pool and transfer it to the protocolFeeReceiver.

```solidity
function claimProtocolFeeForPool(IMaverickV2Pool pool, bool isTokenA) public;
```

**Parameters**

| Name       | Type              | Description                                                                      |
| ---------- | ----------------- | -------------------------------------------------------------------------------- |
| `pool`     | `IMaverickV2Pool` | The pool from which to claim the protocol fee.                                   |
| `isTokenA` | `bool`            | A boolean indicating whether tokenA (true) or tokenB (false) is being collected. |

#### claimProtocolFeeForPool <a href="#claimprotocolfeeforpool-1" id="claimprotocolfeeforpool-1"></a>

Claim protocol fee for a pool and transfer it to the protocolFeeReceiver.

```solidity
function claimProtocolFeeForPool(IMaverickV2Pool pool) public;
```

**Parameters**

| Name   | Type              | Description                                    |
| ------ | ----------------- | ---------------------------------------------- |
| `pool` | `IMaverickV2Pool` | The pool from which to claim the protocol fee. |

#### updateProtocolFeeRatioForPool <a href="#updateprotocolfeeratioforpool" id="updateprotocolfeeratioforpool"></a>

Update the protocol fee ratio for a pool. Can be called permissionlessly allowing any user to sync the pool protocol fee value with the factory protocol fee value.

```solidity
function updateProtocolFeeRatioForPool(IMaverickV2Pool pool) public;
```

**Parameters**

| Name   | Type              | Description                   |
| ------ | ----------------- | ----------------------------- |
| `pool` | `IMaverickV2Pool` | The pool for which to update. |

#### updateProtocolLendingFeeRateForPool <a href="#updateprotocollendingfeerateforpool" id="updateprotocollendingfeerateforpool"></a>

Update the protocol lending fee rate for a pool. Can be called permissionlessly allowing any user to sync the pool protocol lending fee rate value with the factory value.

```solidity
function updateProtocolLendingFeeRateForPool(IMaverickV2Pool pool) public;
```

**Parameters**

| Name   | Type              | Description                   |
| ------ | ----------------- | ----------------------------- |
| `pool` | `IMaverickV2Pool` | The pool for which to update. |

#### create <a href="#create-1" id="create-1"></a>

Create a new MaverickV2Pool with symmetric swap fees.

```solidity
function create(
    uint64 feeAIn,
    uint64 feeBIn,
    uint16 tickSpacing,
    uint32 lookback,
    IERC20 tokenA,
    IERC20 tokenB,
    int32 activeTick,
    uint8 kinds
) public returns (IMaverickV2Pool pool);
```

**Parameters**

| Name          | Type     | Description                                                                                                                                                        |
| ------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `feeAIn`      | `uint64` |                                                                                                                                                                    |
| `feeBIn`      | `uint64` |                                                                                                                                                                    |
| `tickSpacing` | `uint16` | Tick spacing of pool where 1.0001^tickSpacing is the bin width.                                                                                                    |
| `lookback`    | `uint32` | Pool lookback in second in D2 scale.                                                                                                                               |
| `tokenA`      | `IERC20` | Address of tokenA.                                                                                                                                                 |
| `tokenB`      | `IERC20` | Address of tokenB.                                                                                                                                                 |
| `activeTick`  | `int32`  | Tick position that contains the active bins.                                                                                                                       |
| `kinds`       | `uint8`  | 1-15 number to represent the active kinds 0b0001 = static; 0b0010 = right; 0b0100 = left; 0b1000 = both. E.g. a pool with all 4 modes will have kinds = b1111 = 15 |

#### createPermissioned <a href="#createpermissioned" id="createpermissioned"></a>

Create a new MaverickV2PoolPermissioned with symmetric swap fees.

```solidity
function createPermissioned(
    uint64 fee,
    uint16 tickSpacing,
    uint32 lookback,
    IERC20 tokenA,
    IERC20 tokenB,
    int32 activeTick,
    uint8 kinds,
    address accessor
) public returns (IMaverickV2Pool pool);
```

**Parameters**

| Name          | Type      | Description                                                                                                                                                        |
| ------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `fee`         | `uint64`  | Fraction of the pool swap amount that is retained as an LP in D18 scale.                                                                                           |
| `tickSpacing` | `uint16`  | Tick spacing of pool where 1.0001^tickSpacing is the bin width.                                                                                                    |
| `lookback`    | `uint32`  | Pool lookback in second in D2 scale.                                                                                                                               |
| `tokenA`      | `IERC20`  | Address of tokenA.                                                                                                                                                 |
| `tokenB`      | `IERC20`  | Address of tokenB.                                                                                                                                                 |
| `activeTick`  | `int32`   | Tick position that contains the active bins.                                                                                                                       |
| `kinds`       | `uint8`   | 1-15 number to represent the active kinds 0b0001 = static; 0b0010 = right; 0b0100 = left; 0b1000 = both. E.g. a pool with all 4 modes will have kinds = b1111 = 15 |
| `accessor`    | `address` | Only address that can access the pool's public write functions.                                                                                                    |

#### owner <a href="#owner" id="owner"></a>

Get the current factory owner.

```solidity
function owner() public view override(IMaverickV2Factory, Ownable) returns (address);
```

#### lookup <a href="#lookup" id="lookup"></a>

Lookup a pool for given parameters.

```solidity
function lookup(
    uint256 _feeAIn,
    uint256 _feeBIn,
    uint256 _tickSpacing,
    uint256 _lookback,
    IERC20 _tokenA,
    IERC20 _tokenB,
    uint8 kinds
) public view returns (IMaverickV2Pool pool);
```

#### lookup <a href="#lookup-1" id="lookup-1"></a>

Lookup a pool for given parameters.

```solidity
function lookup(IERC20 _tokenA, IERC20 _tokenB, uint256 startIndex, uint256 endIndex)
    external
    view
    returns (IMaverickV2Pool[] memory);
```

Lookup a pool for given parameters.

```solidity
function lookup(uint256 startIndex, uint256 endIndex) external view returns (IMaverickV2Pool[] memory);
```

Lookup a pool for given parameters.

```solidity
function lookupPermissioned(
    uint256 _feeAIn,
    uint256 _feeBIn,
    uint256 _tickSpacing,
    uint256 _lookback,
    IERC20 _tokenA,
    IERC20 _tokenB,
    uint8 kinds,
    address accessor
) public view returns (IMaverickV2Pool pool);
```

Lookup a pool for given parameters.

```solidity
function lookupPermissioned(IERC20 _tokenA, IERC20 _tokenB, address accessor, uint256 startIndex, uint256 endIndex)
    external
    view
    returns (IMaverickV2Pool[] memory);
```

Lookup a pool for given parameters.

```solidity
function lookupPermissioned(uint256 startIndex, uint256 endIndex) external view returns (IMaverickV2Pool[] memory);
```

Address of a permissionless pool.

```solidity
function poolAddress(
    uint256 feeAIn,
    uint256 feeBIn,
    uint256 tickSpacing,
    uint256 lookback,
    IERC20 tokenA,
    IERC20 tokenB,
    uint8 kinds
) public view returns (IMaverickV2Pool pool);
```

Address of a permissionless pool.

```solidity
function poolAddress(
    uint256 feeAIn,
    uint256 feeBIn,
    uint256 tickSpacing,
    uint256 lookback,
    IERC20 tokenA,
    IERC20 tokenB,
    uint8 kinds,
    address accessor
) public view returns (IMaverickV2Pool pool);
```

#### \_slice <a href="#slice" id="slice"></a>

Prune array from storage to subset of array with range \[startIndex, endIndex).

```solidity
function _slice(IMaverickV2Pool[] storage _pools, uint256 startIndex, uint256 endIndex)
    internal
    view
    returns (IMaverickV2Pool[] memory returnPools);
```

**Parameters**

| Name         | Type                | Description                                                  |
| ------------ | ------------------- | ------------------------------------------------------------ |
| `_pools`     | `IMaverickV2Pool[]` | Storage array of full pool list to be sliced.                |
| `startIndex` | `uint256`           | The first index of the pool list in storage to return.       |
| `endIndex`   | `uint256`           | Upper bound index that is not included in the returned list. |

**Returns**

| Name          | Type                | Description                           |
| ------------- | ------------------- | ------------------------------------- |
| `returnPools` | `IMaverickV2Pool[]` | An array of MaverickV2Pool addresses. |

#### \_createChecks <a href="#createchecks" id="createchecks"></a>

Check create pool parameters to ensure they are valid. Reverts on an invalid paramter sets.

```solidity
function _createChecks(
    uint256 feeAIn,
    uint256 feeBIn,
    uint256 tickSpacing,
    uint256 lookback,
    IERC20 tokenA,
    IERC20 tokenB,
    uint8 kinds
) internal view returns (uint64 tokenAScale, uint64 tokenBScale);
```

#### \_checkProtocolFeeRatio <a href="#checkprotocolfeeratio" id="checkprotocolfeeratio"></a>

Checks the validity of the protocol fee ratio.

```solidity
function _checkProtocolFeeRatio(uint8 _protocolFeeRatioD3) internal pure;
```

#### \_checkProtocolLendingFeeRate <a href="#checkprotocollendingfeerate" id="checkprotocollendingfeerate"></a>

Checks the validity of the protocol lending fee rate.

```solidity
function _checkProtocolLendingFeeRate(uint256 _protocolLendingFeeRateD18) internal pure;
```
