Controller

The Controller contract manages pool creation, protocol fee collection, and fee distribution to multiple accounts.

Source: contracts/periphery/Controller.sol

Overview

The Controller provides:

  • Pool creation wrapper with ownership control

  • Standard pool creation with default parameters

  • Protocol fee collection from pools

  • Multi-account fee distribution with configurable shares

  • Pool initialization and fee configuration

  • Factory ownership management

State Variables

accounts

address[] public accounts;

Array of addresses receiving fee distributions.

shares

Distribution shares for each account.

totalShares

Sum of all shares (for proportional calculation).

defaultFeeManager

Default fee manager for each factory (used by createStandardPool).

defaultTickSpacing

Default tick spacing for each factory and fee tier (used by createStandardPool).

Constructor

Parameter
Description

_accounts

Initial fee recipient addresses

_shares

Initial distribution shares

Requirements:

  • At least one account required

  • Accounts and shares arrays must have same length

  • No zero addresses

  • All shares must be greater than zero

Example:

Functions

createPool

Creates and initializes a new pool via the specified factory.

Access Control: Owner only

Parameters:

Name
Type
Description

factory

address

Factory contract to use

tokenA

address

First token

tokenB

address

Second token

fee

uint24

Pool fee

tickSpacing

int24

Tick spacing

feeManager

address

Fee manager contract

sqrtPriceX96

uint160

Initial sqrt price

Returns: Address of the created pool

Events: PoolCreated(token0, token1, fee, factory, tickSpacing, feeManager, sqrtPriceX96, pool)


createStandardPool

Creates a pool using pre-configured default fee manager and tick spacing.

Access Control: Any caller (uses pre-set defaults)

Parameters:

Name
Type
Description

factory

address

Factory contract to use

tokenA

address

First token

tokenB

address

Second token

fee

uint24

Pool fee tier

sqrtPriceX96

uint160

Initial sqrt price

Requirements:

  • Default fee manager must be set for the factory

  • Default tick spacing must be set for the factory/fee combination

Returns: Address of the created pool


setDefaultFeeManager

Sets the default fee manager for standard pool creation.

Access Control: Owner only

Parameters:

Name
Type
Description

factory

address

Factory address

feeManager

address

Default fee manager (zero to disable)


setDefaultTickSpacing

Sets the default tick spacing for standard pool creation.

Access Control: Owner only

Parameters:

Name
Type
Description

factory

address

Factory address

fee

uint24

Fee tier

tickSpacing

int24

Default tick spacing (0 to disable, max 16383)


initialize

Initializes a pool with its starting price.

Access Control: Owner only

Parameters:

Name
Type
Description

pool

address

Pool to initialize

sqrtPriceX96

uint160

Initial sqrt price

Events: Initialize(pool, sqrtPriceX96)


setFeeProtocol

Sets the protocol fee for a pool.

Access Control: Owner only

Parameters:

Name
Type
Description

pool

address

Pool address

feeProtocol0

uint8

Token0 protocol fee (0 or 4-10)

feeProtocol1

uint8

Token1 protocol fee (0 or 4-10)

Events: SetFeeProtocol(pool, feeProtocol0, feeProtocol1)


collectProtocolFees

Collects protocol fees from a pool and distributes to accounts.

Access Control: Any account in the accounts array OR the owner

Parameters:

Name
Type
Description

pool

address

Pool to collect from

amount0Requested

uint128

Maximum token0 to collect

amount1Requested

uint128

Maximum token1 to collect

Distribution Logic:

Events: CollectedFees(pool, token0, token1, amount0, amount1)


withdraw

Withdraws accumulated fees for the caller.

Access Control: Any caller with a balance

Parameters:

Name
Type
Description

token

address

Token to withdraw

amount

uint256

Amount to withdraw

Requirements:

  • Amount must be greater than zero

  • Caller must have sufficient balance

Events: Withdrawal(account, token, amount)


balanceOf

Returns the pending balance for an account.

Parameters:

Name
Type
Description

account

address

Account address

token

address

Token address

Returns: Pending balance


transferFactoryOwnership

Transfers ownership of a factory to a new address.

Access Control: Owner only

Parameters:

Name
Type
Description

factory

address

Factory address

newOwner

address

New owner address

Events

Usage Examples

Initial Setup

Pool Creation

Fee Collection Workflow

Checking Balances

Distribution Calculation

Share Proportions

Rounding

Due to integer division, small amounts may be lost to rounding:

Security Considerations

Owner Privileges

The owner can:

  • Create pools with custom parameters

  • Set protocol fees

  • Initialize pools

  • Set default parameters for standard pools

  • Transfer factory ownership

Recommendations:

  • Use multisig for owner address

  • Add timelock for configuration changes

  • Consider governance for major decisions

Fee Collection

  • Any registered account OR the owner can trigger fee collection

  • Fees are automatically distributed to all accounts based on shares

  • Each account can only withdraw their own balance

Account Configuration

  • Accounts and shares are set at construction time

  • There is no public function to add/remove accounts after deployment

  • Consider deploying a new Controller if account changes are needed

Integration with Factory

When Controller owns the factory:

Last updated