Shared Library Migration Plan
Overview
This document outlines a plan for migrating common functionality from application code into shared libraries. The goal is to reduce duplication, improve maintainability, and eliminate circular dependencies.
Identified Common Functionality
Based on analysis of the current codebase, the following common functionalities have been identified as candidates for migration to shared libraries:
1. API and Validation Utilities (@premia/api-utils
)
Functionality |
Current Location |
Target Library |
Priority |
Request validation (Ajv schemas) |
Multiple applications |
@premia/api-utils |
High |
Error handling middleware |
Multiple applications |
@premia/api-utils |
High |
Response formatting |
Multiple applications |
@premia/api-utils |
Medium |
Rate limiting |
orderbook, websockets |
@premia/api-utils |
Medium |
Authentication middleware |
Multiple applications |
@premia/api-utils |
High |
2. Market Data Processing (@premia/market-data
)
Functionality |
Current Location |
Target Library |
Priority |
Options pricing models |
datastream, orderbook |
@premia/market-data |
High |
Index price logic |
datastream |
@premia/market-data |
High |
Market data caching |
Multiple applications |
@premia/market-data |
Medium |
Option greeks calculations |
Multiple applications |
@premia/market-data |
High |
3. Blockchain Integration (@premia/blockchain
)
Functionality |
Current Location |
Target Library |
Priority |
Wallet signature validation |
sequencer, orderbook |
@premia/blockchain |
High |
Contract interaction utilities |
Multiple applications |
@premia/blockchain |
Medium |
Transaction construction |
relayer, sequencer |
@premia/blockchain |
High |
Chain-specific configurations |
Multiple applications |
@premia/blockchain |
Medium |
4. Database Access (@premia/db-access
)
Functionality |
Current Location |
Target Library |
Priority |
Redis access patterns |
Multiple applications |
@premia/db-access |
High |
SQL query builders |
orderbook, sequencer |
@premia/db-access |
High |
Cache management |
Multiple applications |
@premia/db-access |
Medium |
Database migration utilities |
Multiple applications |
@premia/db-access |
Low |
5. Monitoring and Logging (@premia/monitoring
)
Functionality |
Current Location |
Target Library |
Priority |
Structured logging |
All applications |
@premia/monitoring |
High |
Error reporting |
All applications |
@premia/monitoring |
High |
Performance metrics |
All applications |
@premia/monitoring |
Medium |
Health checking |
All applications |
@premia/monitoring |
Medium |
Migration Process
The migration should follow these steps for each component:
- Identify & Extract: Identify all implementations of the functionality across the codebase
- Create Interface: Design a clean interface for the new shared library component
- Implement Library: Create the shared library implementation
- Replace Usage: Replace application-specific implementations with the shared library
- Test & Validate: Ensure functionality is maintained through comprehensive testing
- Document: Create clear documentation for the new shared library
Prioritization
Migration should be prioritized based on:
- Components involved in circular dependencies
- Functionality duplicated across the most applications
- Components with the most inconsistencies across implementations
- Components that are most likely to change in the future
Timelines
Phase |
Components |
Target Completion |
1 |
Auth Middleware, Logging, Error Handling |
2 weeks |
2 |
Market Data Processing, API Validation |
2 weeks |
3 |
Database Access Patterns |
2 weeks |
4 |
Blockchain Integration |
2 weeks |
5 |
Monitoring, Metrics |
2 weeks |
Benefits
- Reduced Duplication: Eliminate duplicate code across applications
- Consistency: Ensure consistent implementation of common functionality
- Maintainability: Centralize changes to common components
- Testability: Improve test coverage for core functionality
- Onboarding: Make it easier for new developers to understand the codebase
- Scalability: Facilitate the development of new applications
Potential Challenges
- Breaking Changes: Some APIs may require changes during migration
- Testing Overhead: Ensuring all edge cases are covered
- Deployment Coordination: Coordinating releases of libraries and applications
- Integration Complexity: Some deeply integrated functionality may be challenging to extract
Conclusion
This migration plan provides a roadmap for improving the architecture of the Premia monorepo by extracting common functionality into well-defined shared libraries. The result will be a more maintainable, testable, and scalable codebase.