Skip to main content

QsnPair

The QsnPair contract is the core AMM pool of QsnDEX. Each pair holds reserves of two ERC20 tokens, mints LP tokens to liquidity providers, and executes swaps according to one of two supported pricing models.

Pricing Models

Constant Product (x * y = k)

The default pricing model for volatile asset pairs. The product of the two reserve balances must remain constant (or increase) after every swap, ensuring liquidity is available at all price levels.

StableSwap (Amplification Factor A = 85)

An optimized curve for correlated assets (e.g., stablecoin pairs) that concentrates liquidity around the 1:1 price ratio. The amplification coefficient A = 85 controls the degree of concentration. Higher values produce tighter spreads for equal-value assets while still falling back to constant product behavior at extreme price deviations.

LP Token

Each pair is itself an ERC20 token ("QsnDEX LP" / "QSN-LP") that represents a proportional share of the pool's reserves. LP tokens implement EIP-2612 permit, enabling gasless approvals for remove-liquidity operations via off-chain signatures.

Minimum Liquidity

The first 1,000 wei of LP tokens minted are permanently locked (sent to the zero address) to prevent the pool from being fully drained and to mitigate share inflation attacks.

TWAP Oracle

The pair maintains cumulative price accumulators (price0CumulativeLast and price1CumulativeLast) that are updated on the first transaction of each block. External contracts can sample these values at two different points in time and compute a time-weighted average price (TWAP), which is resistant to single-block manipulation.

Flash Swaps

QsnPair supports flash swaps, allowing a caller to receive output tokens before paying for them, provided the debt is settled within the same transaction. The contract verifies repayment via the constant product invariant check at the end of the swap function. A callback mechanism allows the borrower to execute arbitrary logic with the borrowed tokens before repaying.

Reentrancy Protection

The contract uses a custom lock modifier based on a single storage bit, which is more gas-efficient than OpenZeppelin's ReentrancyGuard. All state-modifying external functions (mint, burn, swap) are protected against reentrancy.

Protocol Fee

When the factory's feeTo address is set, the pair mints additional LP tokens to the protocol fee recipient equal to 1/6 of the accumulated LP trading fees. This calculation occurs during mint and burn operations, not on every swap, to minimize gas costs.

Key Functions

mint(address to) returns (uint256 liquidity)

Mints LP tokens to the specified address proportional to the deposited token amounts. Called by the Router after transferring tokens into the pair.

burn(address to) returns (uint256 amount0, uint256 amount1)

Burns LP tokens and returns the proportional share of both underlying tokens to the specified address.

swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data)

Executes a swap, sending the specified output amounts to the recipient. If data is non-empty, a flash swap callback is triggered. The function verifies the constant product invariant after the swap, accounting for fees.

skim(address to)

Transfers any excess token balances (beyond the tracked reserves) to the specified address. Used by the FeeCollector and for recovery of accidentally sent tokens.

sync()

Updates the tracked reserves to match the actual token balances held by the contract. Useful for correcting reserve state after direct token transfers.

Events

EventDescription
SwapEmitted on every swap with input and output amounts.
MintEmitted when liquidity is added and LP tokens are minted.
BurnEmitted when LP tokens are burned and underlying tokens are returned.
SyncEmitted whenever reserves are updated, after every mint, burn, swap, or sync call.