# Pool Factory

The SyncSwap Classic Pool Factory contract is responsible for creating and managing Classic Pools within the SyncSwap ecosystem. It interacts with the Pool Master to ensure that newly created pools are properly registered and conform to the expected standards.

#### Methods <a href="#methods" id="methods"></a>

**`constructor`**

Copy

```
constructor(address _master) BasePoolFactory(_master) {
}
```

Initializes the Classic Pool Factory with the address of the Pool Master contract.

**`_createPool`**

Copy

```
function _createPool(address token0, address token1) internal override returns (address pool) {
    // Perform sanity checks.
    IERC20(token0).balanceOf(address(this));
    IERC20(token1).balanceOf(address(this));

    bytes memory deployData = abi.encode(token0, token1);
    cachedDeployData = deployData;

    // The salt is same with deployment data.
    bytes32 salt = keccak256(deployData);
    pool = address(new SyncSwapClassicPool{salt: salt}()); // this will prevent duplicated pools.

    // Register the pool. The config is same with deployment data.
    IPoolMaster(master).registerPool(pool, 1, deployData, token0, token1);
}
```

Internal function that creates a new Classic Pool with the given token pair. It performs necessary sanity checks and ensures that the pool is registered with the Pool Master.

**`getDeployData`**

Copy

```
function getDeployData() external view override returns (bytes memory deployData) {
    deployData = cachedDeployData;
}
```

Returns the deployment data for the pool being created.

**`getSwapFee`**

Copy

```
function getSwapFee(
    address pool,
    address sender,
    address tokenIn,
    address tokenOut,
    bytes calldata data
) external view override returns (uint24 swapFee) {
    swapFee = IPoolMaster(master).getSwapFee(pool, sender, tokenIn, tokenOut, data);
}
```

Returns the swap fee for a given pool and token pair.

**`createPool`**

Copy

```
function createPool(bytes calldata data) external override returns (address pool) {
    (address tokenA, address tokenB) = abi.decode(data, (address, address));

    // Perform safety checks.
    if (tokenA == tokenB) {
        revert InvalidTokens();
    }

    // Sort tokens.
    if (tokenB < tokenA) {
        (tokenA, tokenB) = (tokenB, tokenA);
    }
    if (tokenA == address(0)) {
        revert InvalidTokens();
    }

    // Underlying implementation to deploy the pools and register them.
    pool = _createPool(tokenA, tokenB);

    // Populate mapping in both directions.
    getPool[tokenA][tokenB] = pool;
    getPool[tokenB][tokenA] = pool;

    emit PoolCreated(tokenA, tokenB, pool);
}
```

Creates a new pool with the provided data and registers it with the Pool Master.

#### Events <a href="#events" id="events"></a>

**`PoolCreated`**

Copy

```
event PoolCreated(address indexed token0, address indexed token1, address pool);
```

Emitted when a new pool is created.

**`SetFactoryWhitelisted`**

Copy

```
event SetFactoryWhitelisted(address indexed factory, bool whitelisted);
```

Emitted when a factory's whitelist status is updated.

**`RegisterPool`**

Copy

```
event RegisterPool(address indexed factory, address indexed pool, uint16 indexed poolType, bytes data);
```

Emitted when a new pool is registered.

**`UpdateForwarderRegistry`**

Copy

```
event UpdateForwarderRegistry(address indexed newForwarderRegistry);
```

Emitted when the forwarder registry is updated.

**`UpdateFeeManager`**

Copy

```
event UpdateFeeManager(address indexed newFeeManager);
```

Emitted when the fee manager is updated.

#### Example Usage <a href="#example-usage" id="example-usage"></a>

**Creating a New Classic Pool**

To create a new Classic Pool, the following steps are typically followed:

1. Initialize the factory with the Pool Master address.
2. Call the `createPool` function with the desired token pair.
3. The factory will handle the rest, ensuring the new pool is registered and compliant with the Pool Master.

Example:

Copy

```
address poolMaster = 0x0000000000000000000000000000000000000000;
SyncSwapClassicPoolFactory factory = new SyncSwapClassicPoolFactory(poolMaster);

address tokenA = 0x0000000000000000000000000000000000000000;
address tokenB = 0x0000000000000000000000000000000000000000;
bytes memory data = abi.encode(tokenA, tokenB);
address newPool = factory.createPool(data);

// The new pool is now created and registered with the Pool Master.
```


---

# 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.syncswap.xyz/syncswap-technical-docs/classic-pool/pool-factory.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.
