CLMSR (Continuous Logarithmic Market Scoring Rule) implementation with comprehensive security hardening and 686 passing tests.
# Install dependencies
npm install
# Run tests (686 tests)
npm test
# Enforce coverage thresholds
npm run coverage && npm run coverage:check
# Compile contracts
npm run compile
# Generate complete codebase documentation
./combine_all_files.sh| Metric | Status | Details |
|---|---|---|
| Tests | β 686 passing | Full hardhat suite + subgraph matchstick |
| Security | β Hardened | Critical vulnerabilities fixed |
| Documentation | β Complete | Auto-generated comprehensive docs |
| Gas Optimization | β Optimized | Efficient chunk-split algorithms |
| Coverage | β β₯80% gated | Enforced via npm run coverage:check |
| Development Status | β Production Ready | Deployed on Base Mainnet |
CLMSR is an automated market maker algorithm for prediction markets:
- Price Formula:
P_i = exp(q_i/Ξ±) / Ξ£_j exp(q_j/Ξ±) - Cost Formula:
C = Ξ± * ln(Ξ£_after / Ξ£_before) - Liquidity Parameter:
Ξ±(configurable per market)
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β CLMSRRouter β β CLMSRMarketCore β β CLMSRPosition β
β (UX Layer) βββββΆβ (Core Logic) βββββΆβ (NFT Mgmt) β
β π
PLANNED β β β
ACTIVE β β β
ACTIVE β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β CLMSRManager β β LazyMulSegTree β β FixedPointMath β
β (Governance) β β (Efficient DS) β β (Math Library) β
β π
PLANNED β β β
ACTIVE β β β
ACTIVE β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
π Implementation Status: Core contracts (CLMSRMarketCore, CLMSRPosition) and libraries are fully implemented and tested. Manager and Router contracts are planned for future implementation.
signals-v0/
βββ π contracts/
β βββ π― core/CLMSRMarketCore.sol # Core trading logic (1,031 lines)
β βββ π interfaces/ # Contract interfaces (4 files)
β βββ π libraries/ # Math libraries (2 files)
β βββ π§ͺ test/ # Solidity test helpers (2 files)
β βββ π mocks/ # Testing mocks (2 files)
βββ π§ͺ test/
β βββ π core/ # Core functionality tests (7 files)
β βββ π’ FixedPointMath.test.ts # Math library tests (52 tests)
β βββ π³ LazyMulSegmentTree.test.ts # Segment tree tests (79 tests)
βββ βοΈ hardhat.config.ts # Build configuration
βββ π¦ package.json # Dependencies
βββ π combine_all_files.sh # Auto documentation generator
| Issue | Severity | Description | Status |
|---|---|---|---|
| Zero-Cost Attack | π΄ Critical | fromWad() truncation allowing free positions |
β FIXED |
| Gas DoS Attack | π΄ Critical | Unlimited chunk splitting causing gas exhaustion | β FIXED |
| Time Validation | π‘ Medium | Trading in expired markets | β FIXED |
| Overflow Protection | π‘ Medium | Mathematical overflow in large trades | β FIXED |
-
Round-Up Cost Calculation
// Before: fromWad() - truncation allows 0 cost uint256 cost6 = costWad.fromWad(); // After: fromWadRoundUp() - guarantees minimum 1 micro USDC uint256 cost6 = costWad.fromWadRoundUp();
-
Gas DoS Protection
uint256 private constant MAX_CHUNKS_PER_TX = 100; uint256 requiredChunks = (quantity + maxSafeQuantityPerChunk - 1) / maxSafeQuantityPerChunk; if (requiredChunks > MAX_CHUNKS_PER_TX) { revert InvalidQuantity(uint128(quantity)); }
-
Time Boundary Validation
if (block.timestamp < market.startTimestamp) { revert InvalidMarketParameters("Market not started"); } if (block.timestamp > market.endTimestamp) { market.isActive = false; revert InvalidMarketParameters("Market expired"); }
| Category | Tests | Coverage | Description |
|---|---|---|---|
| FixedPointMath | 52 | 100% | Mathematical operations & precision |
| LazyMulSegmentTree | 79 | 100% | Segment tree operations |
| Core Boundaries | 42 | 100% | Edge cases & boundary conditions |
| Core Deployment | 15 | 100% | Deployment & configuration |
| Core Events | 25 | 100% | Event emission & authorization |
| Core Execution | 67 | 100% | Trade execution & position management |
| Core Invariants | 12 | 100% | Mathematical invariants |
| Core Markets | 32 | 100% | Market creation & management |
| Total | 324 | 100% | Complete test coverage |
- Security Attack Prevention: Zero-cost positions, gas DoS attacks
- Boundary Testing: Min/max quantities, time boundaries, tick boundaries
- Mathematical Accuracy: CLMSR formulas, chunk splitting, precision
- Gas Optimization: Large trades, complex operation scenarios
- Error Handling: All revert conditions and edge cases
-
Complete CLMSR Implementation
- Continuous logarithmic market scoring rule
- Chunk-split support for large trades
- Per-market liquidity parameter configuration
-
NFT-Based Position Management
- ERC721 compatible position tokens
- Range-based positions (lowerTick ~ upperTick)
- Complete position lifecycle management
-
High-Performance Data Structures
- Lazy Multiplication Segment Tree
- O(log N) updates and queries
- Memory-efficient sparse arrays
-
Attack Prevention Mechanisms
- Zero-cost attack prevention
- Gas DoS attack prevention
- Time-based validation
-
Mathematical Stability
- Overflow protection
- Precision maintenance
- Safe exponential operations
-
Access Control
- Role-based permission management
- Emergency pause mechanism
- Authorized callers only
# Testing
npm test # Run all tests (324 tests)
npm run test:core # Core functionality tests only
npm run test:math # Math library tests only
# Build & Compilation
npm run compile # Compile smart contracts
npm run clean # Clean build artifacts
# Documentation
./combine_all_files.sh # Generate complete codebase documentation
npm run docs # Generate API documentation
# Code Quality
npm run lint # Code style checks
npm run format # Code formattingThe new combine_all_files.sh provides:
- β Automatic File Detection: Auto-recognizes new files
- β Live Test Results: Runs tests during script execution
- β Project Statistics: Auto-calculates file counts, sizes, lines
- β Git Integration: Extracts commit counts and contributors
- β Security Tracking: Auto-counts security fixes from README
- β Beautiful Output: Colorized output with emojis
| Operation | Gas Cost | Optimization |
|---|---|---|
| Position Open | ~150K gas | Optimized segment tree |
| Position Increase | ~80K gas | Cached calculations |
| Position Decrease | ~90K gas | Efficient state updates |
| Large Trade (10x chunk) | ~800K gas | Chunk-split algorithm |
- Test Suite: 324 tests in ~4 seconds
- Compilation: Full build in ~10 seconds
- Documentation: Complete docs in ~5 seconds
- Core CLMSR implementation
- Security hardening
- Comprehensive testing
- Documentation automation
- Gas optimization
- Frontend integration
- Gas optimization improvements
- Enhanced error handling
- Manager contract implementation
- Router contract with permit support
- Oracle integration (price feeds for automatic settlement)
# Clone repository
git clone https://github.com/your-org/signals-v0.git
cd signals-v0
# Install dependencies
npm install
# Run tests to verify setup
npm test
# Start developing!- Solidity: 0.8.24, via-IR optimization
- TypeScript: Strict mode, comprehensive typing
- Testing: 100% coverage requirement
- Documentation: Auto-generated, always up-to-date
When reporting bugs:
- Write reproducible test case
- Describe expected vs actual behavior
- Include environment info (Node.js, npm versions)
MIT License - see LICENSE for details.
- π― 324 Tests Passing - Complete test coverage
- π‘οΈ Security Hardened - Critical vulnerabilities fixed
- β‘ Gas Optimized - Efficient chunk-split algorithms
- π Well Documented - Auto-generated comprehensive docs
- π Production Deployed - Live on Base Mainnet
β PRODUCTION READY - Successfully deployed on multiple networks!
Successfully deployed and operational on Base Mainnet:
| Contract | Address | Verified |
|---|---|---|
| CLMSRMarketCore | 0x4424687a25302db5d1D3A9f7504e4710b0ab17E9 |
β |
| CLMSRPosition | 0x93E61D408456D27d5AB3fC4281B56Eb69A8296e7 |
β |
| SUSD Token | 0x19593B3AF55ad58Ce74514924a2F253FDF89CA34 |
β |
| FixedPointMathU | 0xA8Af982597D7A17c651c801B801EC86C92171A5d |
β |
| LazyMulSegmentTree | 0x1Ba59a311Fb42D475dBC55C9bc780e3883E25A53 |
β |
- Studio: https://thegraph.com/studio/subgraph/signals-v-0
- Query Endpoint:
https://api.studio.thegraph.com/query/116469/signals-v-0/1.0.2
- Market ID: 1
- Network: Base Mainnet (Chain ID: 8453)
- Tick Range: 100,000 - 140,000 (400 bins)
- Liquidity Parameter: 1000.0
- Payment Token: SUSD (Signals USD)
π READY FOR DEPLOYMENT - Configured for Citrea Testnet:
Network: Citrea Testnet Tangerine
Chain ID: 5115
RPC: https://rpc.testnet.citrea.xyz
Explorer: https://explorer.testnet.citrea.xyz
Deployment Commands:
# Deploy to Citrea Development
yarn deploy:citrea:dev
# Deploy to Citrea Production
yarn deploy:citrea:prodIndexer: GoldSky (TheGraph not supported on Citrea)
cd clmsr-subgraph
npm run deploy:goldsky:citrea:dev
npm run deploy:goldsky:citrea:prodπ Full Guide: CITREA_DEPLOYMENT.md
The core CLMSR functionality is production ready and successfully deployed on Base Mainnet. All contracts are verified and operational.
This project is continuously improving. Run ./combine_all_files.sh for the latest documentation.