Status: Draft
Type: Standards Track
Category: Core
Created: 2026-01-18
Requires: CIP-20
Abstract
CIP-21 defines Cowboy’s standard for decentralized exchanges and liquidity pools. The design is hybrid: pools are actors (maximum flexibility) with platform-level primitives for efficiency (math helpers, routing, LP tokens). Key features:- Two pool types: Constant product (V2-style) and concentrated liquidity (V3-style)
- Platform LP tokens: Fungible LP shares for V2 pools (CIP-20 tokens)
- Actor-managed positions: Non-fungible liquidity positions for V3 pools
- Validation hooks:
can_swap/on_swapfor compliance and MEV protection - Dual routing: Actor-based router for flexibility, platform primitive for efficient multi-hop
Motivation
DEXes are critical infrastructure for any blockchain ecosystem. Cowboy’s unique features—actors, timers, platform tokens—enable DEX designs not possible on Ethereum:- Native TWAP oracles via timers (no external keeper)
- On-chain limit orders via state-triggered timers
- Efficient batch swaps via platform routing
- Compliance pools via validation hooks
Specification
Overview
Part 1: Platform Primitives
The runtime provides efficient helpers for common AMM operations.1.1 Math Primitives
1.2 Concentrated Liquidity Math
1.3 Platform Routing
Part 2: V2 Pool Standard (Constant Product)
V2 pools use the classicx * y = k formula with fungible LP tokens.
2.1 Interface
2.2 Validation Hooks
V2 pools MAY specify a validation hook for compliance or MEV protection:2.3 Reference Implementation
Part 3: V3 Pool Standard (Concentrated Liquidity)
V3 pools allow LPs to concentrate liquidity in price ranges for higher capital efficiency.3.1 Core Concepts
Ticks: Price space is divided into discrete ticks. Each tick represents a 0.01% price change. Positions: LPs provide liquidity between two ticks (a price range). Positions are non-fungible. Liquidity: A position’s liquidity value determines its share of fees when price is in range.3.2 Interface
3.3 Position Data
Positions are stored in actor state (not as NFTs):3.4 Validation Hooks
V3 pools use the same hook interface as V2:3.5 Position Manager (Optional)
For better UX, a position manager actor can wrap positions as transferable:Part 4: Factory
The factory creates and indexes pools:Standard Fee Tiers
| Tier | Fee (bps) | V3 Tick Spacing | Use Case |
|---|---|---|---|
| Stable | 1 | 1 | Stablecoin pairs |
| Low | 5 | 10 | Correlated pairs |
| Medium | 30 | 60 | Most pairs |
| High | 100 | 200 | Exotic pairs |
Part 5: Router
5.1 Actor Router (Flexible)
5.2 Platform Router (Efficient)
The platform router (amm_swap_exact_in / amm_swap_exact_out) provides:
- Lower gas cost (no actor call overhead per hop)
- Atomic multi-hop execution
- Standardized interface
- Standard path-based swap
- No custom logic needed
- Maximum efficiency required
- Complex operations (add liquidity + swap)
- Custom fee handling
- Flash swaps
Part 6: Advanced Features
6.1 Native TWAP Oracle
V2 pools include a built-in TWAP oracle updated via timers:6.2 On-Chain Limit Orders
Using CIP-5 state-triggered timers:6.3 MEV Protection via Hooks
6.4 Compliance Pools
Events
V2 Pool Events
V3 Pool Events
Security Considerations
Reentrancy
The actor model provides natural reentrancy protection—actors process one message at a time. However, cross-actor calls during swaps should follow checks-effects-interactions pattern.Price Manipulation
- TWAP oracles mitigate flash loan attacks
- Concentrated liquidity pools are more sensitive to manipulation at range boundaries
- Hooks can implement additional protections (MEV detection, circuit breakers)
Hook Security
- Hooks are capped at 50,000 Cycles (same as CIP-20 token hooks)
- Malicious hooks can block all swaps—pool deployers must be trusted
- Consider timelock for hook updates on major pools
Integer Precision
- Use Q128 format for prices to maintain precision
- Concentrated liquidity math uses Q64.96 (matching Uniswap V3)
- Platform primitives handle precision; actor implementations should use them
Rationale
Why Hybrid (Actor + Platform)?
Pure actor-based: Maximum flexibility but higher gas costs Pure platform-based: Maximum efficiency but inflexible Hybrid gives:- Flexibility for pool logic (actors)
- Efficiency for common operations (platform primitives)
- Best of both worlds
Why Both V2 and V3?
V2 (constant product):- Simpler for LPs
- Fungible LP tokens (composable with DeFi)
- Lower gas cost
- Good for stable pairs
- Higher capital efficiency
- Better for professional LPs
- Required for competitive pricing on major pairs
Why Validation-Only Hooks?
Full hooks (modifying amounts) add complexity:- Unpredictable outputs
- Gas estimation difficulty
- Hidden fees
- Swap succeeds or fails
- Predictable gas
- Covers compliance use cases
Backwards Compatibility
This is a new standard. No backwards compatibility concerns.Reference Implementation
Seecowboy-core/src/runtime/amm.rs for platform primitive implementations.
See examples/cowswap/ for reference V2 and V3 pool implementations.
