AMM & Pool Models
QsnDEX supports two automated market maker (AMM) models within a unified Pair contract, selected at pool creation time and immutable thereafter.
Constant Product (Type 0)
The default model for volatile token pairs. It follows the standard x * y = k invariant, where x and y are the reserves of each token and k is a constant that can only increase (through fees and liquidity additions).
Swap mechanics:
- The trading fee is deducted from the input amount before the invariant check.
- The output amount is calculated such that the post-swap product of reserves is greater than or equal to
k. - The invariant is verified after the swap completes, including any callback execution (flash swaps).
This model is suitable for all general-purpose trading pairs where the two assets are not expected to maintain a fixed price ratio.
StableSwap (Type 1)
A Curve-style invariant optimized for assets that trade near a 1:1 ratio, such as stablecoin pairs (USDC/USDT) and wrapped token pairs (WETH/stETH).
Key parameters:
- Amplification coefficient (A): 85 (hardcoded)
- Convergence method: Newton's method iterative solver
- Maximum iterations: 255
The StableSwap invariant provides significantly lower slippage than constant product for trades within the expected price range. The Newton's method solver converges to the output amount iteratively, with a hard cap of 255 iterations to bound gas consumption.
LP Tokens
Each Pair contract is itself an ERC20 token representing liquidity provider shares.
| Property | Value |
|---|---|
| Name | QsnDEX LP |
| Symbol | QSN-LP |
| Decimals | 18 |
Standards supported:
- ERC20: Full compliance including transfer, approve, and allowance.
- EIP-2612 (Permit): Gasless approvals via off-chain signatures. The
permitfunction allows a spender to be approved without an on-chain transaction from the token holder. - EIP-712: Typed structured data signing with a domain separator that includes the chain ID. The domain separator is recalculated if a chain fork is detected (chain ID changes), preventing cross-chain replay attacks.
- Unlimited allowance: Setting allowance to
type(uint256).maxis treated as infinite and is not decremented on transfers, reducing gas costs for trusted integrations.
TWAP Oracle
Each Pair maintains cumulative price accumulators (price0CumulativeLast and price1CumulativeLast) that are updated at the start of each block in which a trade occurs. External contracts can sample these accumulators at two points in time to compute a time-weighted average price (TWAP) that is resistant to single-block manipulation.
Flash Swaps
QsnDEX Pairs support flash swaps: a caller can withdraw tokens from the pool and return them (plus the fee) within the same transaction. The contract verifies the invariant holds after the callback completes.
The callback interface requires the caller to implement a specific function signature. The Pair validates that the callback was invoked correctly and that the invariant condition is satisfied before the transaction finalizes.
Minimum Liquidity
When a pool is initialized with its first liquidity deposit, 1000 wei of LP tokens are permanently locked by sending them to the zero address. This is the MINIMUM_LIQUIDITY constant.
This mechanism serves two purposes:
- First-depositor attack prevention: Without minimum liquidity, the first depositor could manipulate the initial price ratio to extract value from subsequent depositors.
- Donation attack mitigation: Locking a small amount of liquidity ensures the pool can never be fully drained, preventing certain classes of economic exploits.
Pool Creation
Pairs are deployed by the Factory contract using the CREATE2 opcode. The salt is derived from the sorted token addresses and the pool type, producing a deterministic deployment address. This means any party can compute the address of a Pair contract off-chain without making an RPC call, given the Factory address, token addresses, pool type, and the Pair contract bytecode hash.