Pool Factory

The SyncSwap Stable Pool Factory contract is responsible for creating and managing Stable 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.

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

constructor(address _master)

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

constructor(address _master) BasePoolFactory(_master) {
}

_createPool(address token0, address token1)

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

function _createPool(address token0, address token1) internal override returns (address pool) {
    // Tokens with decimals more than 18 are not supported and will lead to reverts.
    uint token0PrecisionMultiplier = 10 ** (18 - IERC20(token0).decimals());
    uint token1PrecisionMultiplier = 10 ** (18 - IERC20(token1).decimals());

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

    // Remove precision multipliers from salt and config.
    deployData = abi.encode(token0, token1);

    bytes32 salt = keccak256(deployData);
    pool = address(new SyncSwapStablePool{salt: salt}()); // this will prevent duplicated pools.

    // Register the pool with config.
    IPoolMaster(master).registerPool(pool, 2, deployData, token0, token1);
}

getDeployData()

Returns the deployment data for the pool being created.

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

getSwapFee(address pool, address sender, address tokenIn, address tokenOut, bytes calldata data)

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

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);
}

createPool(bytes calldata data)

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

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);
}

Events

PoolCreated

Emitted when a new pool is created.

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

SetFactoryWhitelisted

Emitted when a factory's whitelist status is updated.

event SetFactoryWhitelisted(address indexed factory, bool whitelisted);

RegisterPool

Emitted when a new pool is registered.

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

UpdateForwarderRegistry

Emitted when the forwarder registry is updated.

event UpdateForwarderRegistry(address indexed newForwarderRegistry);

UpdateFeeManager

Emitted when the fee manager is updated.

event UpdateFeeManager(address indexed newFeeManager);

Example Usage

Creating a New Stable Pool

To create a new Stable 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:

address poolMaster = 0x0000000000000000000000000000000000000000;
SyncSwapStablePoolFactory factory = new SyncSwapStablePoolFactory(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.

Last updated