Skip to content

refactor(common-ts): restructure utils into domain-based modules#329

Merged
ChesterSim merged 18 commits intomasterfrom
chester/the-great-refactor
Mar 26, 2026
Merged

refactor(common-ts): restructure utils into domain-based modules#329
ChesterSim merged 18 commits intomasterfrom
chester/the-great-refactor

Conversation

@ChesterSim
Copy link
Copy Markdown
Contributor

Summary

  • Reorganize 865-line utils/index.ts god file and common-ui-utils/ into 11 domain-based modules under src/utils/ (math, strings, enum, validation, token, trading, markets, orders, positions, accounts, core)
  • Add 11 subpath exports to package.json so consumers can import from @drift-labs/common/utils/math, @drift-labs/common/utils/trading, etc.
  • Deduplicate chunks, getTokenAddress, getTokenAccount (had two copies each)
  • Preserve backwards compatibility via deprecation facades in src/_deprecated/COMMON_UI_UTILS, COMMON_UTILS, TRADING_UTILS, MARKET_UTILS, ORDER_COMMON_UTILS, USER_UTILS, COMMON_MATH, EQUALITY_CHECKS all still work
  • Resolve 9 circular dependencies (down to 0)
  • Add typesVersions fallback for consumers using moduleResolution: "node"

New import paths

// Targeted imports (new)
import { calculateMean, bnMax } from '@drift-labs/common/utils/math';
import { abbreviateAddress } from '@drift-labs/common/utils/strings';
import { deriveMarketOrderParams } from '@drift-labs/common/utils/trading';

// Old namespace pattern still works (deprecated)
import { COMMON_UI_UTILS } from '@drift-labs/common';
COMMON_UI_UTILS.abbreviateAddress(...)

Test plan

  • All 114 existing tests pass
  • TypeScript compiles with 0 errors in src/
  • madge --circular reports 0 circular dependencies
  • ESLint + Prettier pass
  • tsc build produces correct lib/ output with all subpath targets
  • Deprecated namespace exports (COMMON_UI_UTILS, etc.) match original shapes
  • Verify NextJS app builds against this version
  • Verify React Native app builds against this version

🤖 Generated with Claude Code

ChesterSim and others added 18 commits March 26, 2026 00:03
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add missing functions: getLpSharesAmountForQuote, getQuoteValueForLpShares,
  roundToStepSizeIfLargeEnough, POTENTIAL_PROFIT_DEFAULT_STATE
- Fix function placements: roundToDecimal, getPctCompletion, aprFromApy to numbers.ts
- Fix featureFlags.ts filename (was incorrectly hyphenated)
- Add HighLeverageOptions type to orders/flags.ts
- Add all deprecation facade files (COMMON_MATH, EQUALITY_CHECKS, etc.)
- Add typesVersions fallback for moduleResolution: "node" consumers
- Add types conditions to all subpath exports
- Remove duplicate exports in barrel (MultiplexWebSocket, WalletConnectionState)
- Document shared cache infrastructure (uiStringCache)
- Add TypeScript moduleResolution risk to Risks section

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Already removed by user prior to spec finalization.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
16-task plan covering: domain module creation (core, math, strings,
enum, validation, token, trading, markets, orders, positions, accounts),
deprecation facades, barrel rewrites, package.json exports, and
verification.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…tions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…tils/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…bility

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…d files

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
….json

Enables targeted imports like '@drift-labs/common/utils/math'.
Includes typesVersions fallback for moduleResolution: "node" consumers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Point MarketId.ts and UIMarket.ts directly at utils/enum instead of
the utils barrel, breaking the cycle: types/ → utils/ → core/equality
→ types/.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Merge duplicate imports from trading/size in _deprecated/trading-utils.ts
- Use top-level BN import instead of inline import() in trading/pnl.ts
- Remove unnecessary export of private updateUserAccount in accounts/init.ts
- Consistent export style for timedPromise in core/async.ts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ChesterSim ChesterSim merged commit fe67e94 into master Mar 26, 2026
8 checks passed
@ChesterSim ChesterSim deleted the chester/the-great-refactor branch March 26, 2026 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant