Skip to main content

QsnFactory

The QsnFactory contract is responsible for creating and managing all trading pair contracts on QsnDEX. It serves as the central registry for pools and enforces the fee tier system.

CREATE2 Deterministic Deployment

All pairs are deployed using the CREATE2 opcode, which produces deterministic contract addresses based on the factory address, token pair salt, and the pair bytecode hash. This allows any party to compute a pair's address off-chain without querying the blockchain.

The address of a pair is determined by:

address = keccak256(0xff, factory, keccak256(token0, token1), PAIR_CODE_HASH)

Multi-Tier Fee System

The factory supports multiple fee tiers, allowing different pools to charge different swap fees. The following tiers are enabled by default:

Fee TierBasis PointsTypical Use Case
0.01%100Stablecoin-stablecoin pairs
0.05%500Correlated assets
0.30%3,000Standard volatile pairs
1.00%10,000Exotic or low-liquidity pairs

The owner can enable additional custom fee tiers as needed via the enableFeeTier function.

Protocol Fee Configuration

The factory manages the feeTo address, which receives a share of LP fees across all pools when set. When feeTo is the zero address, protocol fee collection is disabled and all fees accrue entirely to liquidity providers.

The feeToSetter is the only address authorized to change the feeTo recipient. Ownership of the setter role can be transferred via setFeeToSetter.

Key Functions

createPair(address tokenA, address tokenB, uint256 fee)

Creates a new trading pair for the given tokens at the specified fee tier. Reverts if the pair already exists or the fee tier is not enabled. Emits a PairCreated event on success.

setFeeTo(address _feeTo)

Sets the protocol fee recipient address. Callable only by the current feeToSetter.

setFeeToSetter(address _feeToSetter)

Transfers the fee setter role to a new address. Callable only by the current feeToSetter.

enableFeeTier(uint256 fee)

Enables a new fee tier for pair creation. Callable only by the owner. The fee value is specified in basis points (e.g., 3000 = 0.30%).

Events

PairCreated(address indexed token0, address indexed token1, address pair, uint256 fee, uint256 pairCount)

Emitted when a new trading pair is successfully created. Includes the sorted token addresses, the deployed pair contract address, the fee tier, and the total number of pairs created to date.