Domain: Portfolio
Bounded Context: Orchestration and Aggregation
Type: Service Library (NPM Package)
Package: @cygnus-wealth/portfolio-aggregation
The Portfolio Aggregation service library is a reusable npm package that provides portfolio orchestration and aggregation capabilities for client applications. This library coordinates data collection from multiple integration packages and combines them into a unified portfolio view.
Key Characteristics:
- Service layer library, not a standalone application
- No UI components (consumed by @cygnus-wealth/cygnus-wealth-core)
- Orchestrates but doesn't implement integrations
- Provides domain services and aggregates as reusable components
- Emits domain events for consuming applications
npm install @cygnus-wealth/portfolio-aggregationThis service library follows Domain-Driven Design (DDD) principles with the following structure:
src/
├── domain/ # Core business logic
│ ├── entities/ # Domain entities
│ ├── value-objects/ # Value objects
│ ├── aggregates/ # Domain aggregates
│ └── services/ # Domain services
├── application/ # Application layer
│ ├── services/ # Application services (main exports)
│ ├── use-cases/ # Use case implementations
│ └── commands/ # Command handlers
├── infrastructure/ # Infrastructure layer
│ ├── repositories/ # Repository implementations
│ ├── adapters/ # Integration adapters
│ └── cache/ # Caching strategies
├── contracts/ # Public interfaces and contracts
├── shared/ # Shared utilities and types
└── index.ts # Public API exports
Note: This library has no presentation layer. UI components are implemented in consuming applications like @cygnus-wealth/cygnus-wealth-core.
- Data Orchestration: Coordinates parallel data fetching from multiple integration domains
- Portfolio Composition: Combines assets from different sources into a unified portfolio
- Deduplication: Identifies and merges duplicate assets across sources
- Reconciliation: Handles discrepancies between different data sources
- Caching Strategy: Implements intelligent caching to minimize external API calls
- Error Handling: Provides graceful degradation when some sources are unavailable
@cygnus-wealth/data-models- Shared data contracts and unified structures@cygnus-wealth/evm-integration- EVM blockchain integration@cygnus-wealth/sol-integration- Solana blockchain integration@cygnus-wealth/robinhood-integration- Traditional finance integration@cygnus-wealth/asset-valuator- Asset pricing and valuation services
- TypeScript - Type safety and library definitions
- Vite - Build tooling for library bundling
- Vitest - Testing framework
# Install dependencies
npm install
# Run development server
npm run dev
# Run tests
npm run test
# Run tests with coverage
npm run test:coverage
# Type checking
npm run typecheck
# Build for production
npm run buildThe project uses Vitest for testing with the following structure:
src/tests/unit/- Unit tests for domain logicsrc/tests/integration/- Integration testssrc/tests/e2e/- End-to-end tests
Run tests with:
npm run test # Run tests in watch mode
npm run test:once # Run tests once
npm run test:coverage # Generate coverage reportnpm install @cygnus-wealth/portfolio-aggregationimport {
PortfolioAggregationService,
IIntegrationRepository,
IPortfolioRepository,
IAssetValuatorRepository
} from '@cygnus-wealth/portfolio-aggregation';
// Client application provides implementations
const integrations: IIntegrationRepository[] = [
evmIntegration,
solanaIntegration,
robinhoodIntegration
];
const portfolioRepository: IPortfolioRepository = // client's storage implementation
const assetValuator: IAssetValuatorRepository = // valuator service instance
// Initialize the service
const service = new PortfolioAggregationService(
integrations,
portfolioRepository,
assetValuator
);
// Use the service to aggregate portfolio
const portfolio = await service.aggregatePortfolio({
sources: [IntegrationSource.EVM, IntegrationSource.SOLANA],
addresses: new Map([
['ethereum', ['0x...']],
['solana', ['...']],
]),
userId: 'user123',
forceRefresh: false
});// Subscribe to domain events
service.on('PortfolioAggregated', (event) => {
console.log('Portfolio aggregation completed', event);
});
service.on('IntegrationSourceFailed', (event) => {
console.error('Integration failed', event.source, event.error);
});The library exports the following for use by consuming applications:
PortfolioAggregationService- Main orchestration serviceReconciliationService- Asset deduplication and reconciliation
IIntegrationRepository- Interface for integration adaptersIPortfolioRepository- Portfolio persistence interfaceIAssetValuatorRepository- Valuation service interface
PortfolioAggregate- Core portfolio aggregateAssetEntity- Asset domain entityMoney- Value object for monetary valuesAddress- Value object for blockchain addresses
- Domain event interfaces and types
- Event emitter integration
This library is primarily consumed by @cygnus-wealth/cygnus-wealth-core, which:
- Provides the UI components and user interactions
- Manages client-side state and browser storage
- Handles user configuration and preferences
- Instantiates and coordinates service library usage
When contributing to this service library:
- Maintain clear domain boundaries - this is orchestration only
- Don't add UI components - those belong in consuming applications
- Follow DDD principles for domain modeling
- Ensure all public API changes are backward compatible or properly versioned
- Add integration interfaces through the IIntegrationRepository pattern
MIT