Why Data Composition Matters
Raw blockchain data is optimized for storage and consensus, not application consumption. DLMM pools store data across multiple accounts, use different numeric formats, and require complex calculations to derive meaningful metrics. Data composition bridges this gap.The Raw Data Challenge
Blockchain Storage Reality:- Pool state scattered across multiple accounts
- Numbers stored as big integers for precision
- Bin data stored in compressed arrays
- No human-readable token symbols or metadata
- Time-sensitive data mixed with static configuration
- Single unified data structures
- Human-readable numbers and formats
- Real-time calculated metrics and analytics
- Type-safe interfaces with validation
- Cached and optimized data access patterns
Composition Architecture Patterns
1. Layered Composition Pattern
The SDK uses a layered approach where each layer adds value and abstraction:2. Aggregation Pattern
Multiple data sources are efficiently combined: Parallel Data Fetching:- Parallel Execution: Multiple RPC calls execute simultaneously
- Failure Isolation: One failed call doesn’t block others
- Dependency Management: Clear separation of independent vs dependent data
- Performance Optimization: Reduces total wait time by 60-80%
3. Transformation Pipeline Pattern
Raw data flows through transformation stages:- Modularity: Each stage has single responsibility
- Testability: Individual stages can be unit tested
- Flexibility: Easy to modify or extend pipeline stages
- Debugging: Clear visibility into transformation process
4. Caching Strategy Pattern
Different data types require different caching approaches:- Efficiency: Frequently accessed static data cached longer
- Accuracy: Volatile data refreshed frequently
- Resource Optimization: Balances performance vs freshness
- Cost Reduction: Fewer expensive RPC calls
Advanced Composition Patterns
5. Dependency Resolution Pattern
Complex data often depends on other data being fetched first:- Optimal Parallelization: Maximum concurrent operations at each level
- Clear Dependencies: Explicit dependency relationships
- Error Handling: Failures at each level handled appropriately
- Performance: Minimizes sequential bottlenecks
6. Partial Composition Pattern
Allow applications to request only needed data components:- Performance: Only fetch/calculate required data
- Flexibility: Different views need different data granularity
- Resource Efficiency: Reduce bandwidth and compute costs
- User Experience: Faster loading for lightweight operations
7. Event-Driven Composition Pattern
Respond to data changes with smart recomposition:- Efficiency: Only recompute affected calculations
- Consistency: Ensures all dependent data stays synchronized
- Real-time Updates: Applications stay current with minimal overhead
- Scalability: Handles complex dependency graphs gracefully
Why These Patterns Matter
For Application Performance
Without Composition Patterns:For Code Maintainability
Pattern Benefits:- Single Responsibility: Each pattern handles one concern
- Testability: Individual components easily unit tested
- Extensibility: New data sources integrate cleanly
- Error Handling: Centralized error recovery strategies
For Developer Experience
Consistent APIs:Design Trade-offs and Decisions
Memory vs Speed
Trade-off: Caching improves speed but increases memory usage Decision: Tiered caching with size limits- High-frequency data: Small cache with short TTL
- Low-frequency data: Larger cache with longer TTL
- Automatic eviction prevents memory overflow
Accuracy vs Performance
Trade-off: Real-time data is more accurate but slower to fetch Decision: Configurable staleness tolerance- Critical operations: Always fetch fresh data
- Analytics: Accept 30-60 second staleness
- Overview displays: Accept 5-minute staleness
Complexity vs Flexibility
Trade-off: Simple APIs are easier but less flexible Decision: Layered API design- Simple methods for common use cases
- Advanced methods for custom requirements
- Escape hatches for direct blockchain access
Implementation Guidelines
When to Use Each Pattern
Layered Composition: Always - fundamental architecture Aggregation: Multi-source data requirements Transformation Pipeline: Complex data processing needs Strategic Caching: Performance-critical applications Dependency Resolution: Complex interdependent data Partial Composition: Variable performance requirements Event-Driven: Real-time applications with subscriptionsAnti-Patterns to Avoid
Sequential Data Fetching:Future Evolution
Emerging Patterns
Stream-Based Composition: Real-time data streams with reactive updates ML-Enhanced Caching: Machine learning to predict data access patternsCross-Chain Composition: Unified data from multiple blockchain sources Decentralized Caching: Shared cache layers across applications
Scalability Considerations
As DeFi ecosystems grow, composition patterns must evolve:- Horizontal Scaling: Distribute composition across multiple services
- Edge Computing: Push composition closer to users geographically
- Protocol Aggregation: Compose data from multiple DeFi protocols
- Interoperability: Standard composition interfaces across ecosystems