feat: hooks first architecture#563
Conversation
- Add comprehensive architecture options documentation - Compare Clean Layers, Feature Modules, and Hooks-First approaches - Document decision framework and migration strategies - Choose Hooks-First architecture for implementation
- Create calculations service for deposit fees and UTXO selection - Add validation functions with consistent result format - Implement data transformers for deposit operations - Extract business logic from components into pure functions
- Add main deposit flow orchestration hook - Create validation hook with UTXO and balance checks - Implement transaction creation and submission hook - Separate business logic from UI components using hooks pattern
- Provide step-by-step migration instructions - Include code examples for common patterns - Add testing strategy for each layer - Document benefits and next steps for team adoption
- Fix undefined type check in useDepositFlow - Remove unused imports and variables - Correct LocalStorageStatus enum values in transformers - Update PeginDisplayLabel types to match state machine
- Handle empty strings and edge cases properly - Validate decimal point count (prevent multiple decimals) - Handle strings with only decimals (e.g., '.5') - Add regex validation for final satoshi string - Return 0n for invalid inputs instead of throwing
- Use bigint arithmetic throughout instead of Number conversion - Avoid precision loss for values >= 2^53 - Handle fractional part with string padding and trimming - Maintain correct decimal place handling
- Analyze code merged from main against new architecture - Verify all changes are compliant with hooks-first pattern - Document positive patterns (error utilities, useBorrowUI) - Identify areas for future migration - Provide integration recommendations
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a hooks-first architecture to improve code organization in the vault service. The refactoring separates business logic (in hooks) from pure utility functions (in services) and presentational code (in components), following a React-centric approach for better maintainability.
Key Changes:
- Created a three-layer service architecture with calculations, validations, and transformers
- Implemented deposit flow orchestration using custom React hooks
- Added comprehensive architecture documentation with migration guides
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| services/vault/src/services/deposit/validations.ts | Pure validation functions for deposit amounts, balances, UTXOs, providers, and BTC addresses |
| services/vault/src/services/deposit/transformers.ts | Data transformation functions for converting between formats and user-friendly displays |
| services/vault/src/services/deposit/calculations.ts | Pure calculation functions for fees, UTXO selection, and transaction sizing |
| services/vault/src/services/deposit/index.ts | Barrel export aggregating all deposit service functions |
| services/vault/src/hooks/deposit/useDepositValidation.ts | Hook for deposit validation logic integrating service functions |
| services/vault/src/hooks/deposit/useDepositTransaction.ts | Hook for transaction creation and submission |
| services/vault/src/hooks/deposit/useDepositFlow.ts | Main orchestration hook managing the complete deposit flow |
| services/vault/src/hooks/deposit/index.ts | Barrel export for deposit hooks and types |
| services/vault/MIGRATION_GUIDE.md | Comprehensive guide for migrating features to the new architecture |
| services/vault/ARCHITECTURE_OPTION_3.md | Detailed documentation of the hooks-first architecture pattern |
| services/vault/ARCHITECTURE_OPTION_2.md | Alternative feature-based modular architecture documentation |
| services/vault/ARCHITECTURE_OPTION_1.md | Alternative clean layered architecture documentation |
| services/vault/ARCHITECTURE_COMPARISON.md | Comparison of architecture options with recommendations |
| services/vault/ARCHITECTURE.md | Main architecture documentation for the chosen hooks-first pattern |
Comments suppressed due to low confidence (1)
services/vault/MIGRATION_GUIDE.md:1
- Contains an unusual memory reference marker
[[memory:10537065]]that appears to be an artifact or debug reference. This should be removed from the documentation.
# Migration Guide - Hooks-First Architecture
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot |
… feat/hooks-first-architecture
- Run prettier to fix formatting issues - Fix import ordering - Remove unused variables - Standardize quote styles to double quotes
… feat/hooks-first-architecture
… feat/hooks-first-architecture
… feat/hooks-first-architecture
gbarkhatov
left a comment
There was a problem hiding this comment.
@jonybur approving
My suggestion for future is that we split such issues into multiple sub issues
I would review 3-5 PRs instead of such a big one, step by step - with tests being a part of another PR for sure
Code wise looks good, moving into a proper direction. As long as functionality is there, I think we are okay to merge it
| providers, | ||
| isLoadingProviders, | ||
| amountSats, | ||
| // estimatedFees, // TODO: Display estimated fees in UI |
There was a problem hiding this comment.
Do we have a ticket for that? Let's resolve once ticket is there, point to this line of code (or PR comment)
Vault Architecture - Hooks-First Pattern
Core Principles
Architecture Layers
Directory Structure
Benefits of This Architecture
Migrated to New Architecture
Deposit Flow (Partial)
Status: Core deposit entry and signing migrated, but review and success modals still use old architecture
Not Yet Migrated (Old Architecture)
Deposit Flow (Remaining)
Redeem Flow (Complete)
Position Management
Market Operations
Summary
Migrated: 4 components (deposit form, sign, overview, coordination)
Not Migrated: 16+ components (review modals, redeem flow, position, market)
Migration Progress: Approximately 20%