Core Concepts
Currency models every financial operation around immutable value objects, exact arbitrary-precision decimal math, and locked historical rate records.
Rate Sync & Conversion Architecture
Section titled “Rate Sync & Conversion Architecture”flowchart LR Feed["ECB / TCMB / Custom Provider"] --> Transport["WireHttpTransport (SSRF & DNS Pinning)"] Transport --> Sync["CurrencyRateSyncService (CurrencyWriteLock)"] Sync --> Spot["currency-currencies (8dp Spot Rates + Spread)"] Sync --> Hist["currency-rate-history (Locked Daily Snapshots)"] Spot --> Conv["CurrencyConverter + DecimalMath (ext-bcmath)"] Hist --> Conv Conv --> Money["Money (Integer Minor Units)"] Money --> Format["MoneyFormatter (ext-intl) + VisitorCurrencyResolver"]
Immutable Value Objects
Section titled “Immutable Value Objects”Money(Totoglu\Currency\Value\Money): Stores integer$amountMinor, 3-letter uppercase ISO 4217$currency, and$minorUnits(0..8).Money::fromMajor()acceptsstring|intand intentionally rejectsfloatto prevent IEEE-754 drift.RoundingMode(Totoglu\Currency\Domain\RoundingMode): Enum supportingHalfUp(half_up),HalfEven(half_even),HalfDown(half_down),Ceil(ceil), andFloor(floor).DecimalMath(Totoglu\Currency\Support\DecimalMath): Arbitrary-precisionext-bcmathcalculator bounded to 128 mantissa digits,[-100, +100]scientific exponents, and<= 256scale shifts to prevent memory DoS.CurrencyDefinition,ConversionResult,ExchangeRateBatch,SyncResult: TypedreadonlyDTOs returned by converter and sync operations.
Three Entries Blueprints (CurrencyBlueprints)
Section titled “Three Entries Blueprints (CurrencyBlueprints)”Currency stores all configuration and FX history inside three Entries blueprints with zero custom SQL tables:
| Blueprint | Type | Purpose |
|---|---|---|
currency-settings |
Singleton | Active provider (ecb, tcmb, manual, or custom), tcmbRateType, autoSyncEnabled, syncIntervalHours (1..168), defaultRoundingMode, allowVisitorOverride, visitorCookieName (pw_currency), visitorCookieDays (1..365), historyRetentionDays (0..3650), and lastSync* metadata. |
currency-currencies |
Collection | Managed ISO 4217 currencies with symbol, minor_units, 8-decimal exchange_rate, spread_percent, rounding_increment, rate_source (auto, manual, or provider key), is_base, and sort_order. |
currency-rate-history |
Collection (locked: true) |
Immutable historical FX snapshots keyed by provider, base currency, quote currency, and YYYY-MM-DD effective date. |
Concurrency & CurrencyWriteLock
Section titled “Concurrency & CurrencyWriteLock”Base-currency transitions (setBaseCurrency()) and rate sync writes (syncRates(), recordBatch()) execute inside CurrencyWriteLock::run(), which acquires a re-entrant database-scoped lock (GET_LOCK on MySQL, pg_advisory_xact_lock on PostgreSQL, or transaction serialization on SQLite) while performing network fetches outside the lock.
Admin Data Providers
Section titled “Admin Data Providers”ProcessCurrency registers two dedicated React data providers under prefix currency:
CurrenciesDataProvider(currency:currencies): Powers the Currencies table with★ Basedecoration, inline/quick-edit guards (protectingcodeand the active base row from deletion or unpublishing), andcurrency-admintrash isolation.RateHistoryDataProvider(currency:history): Always-locked read-only viewport exposing historical snapshots with CSV/JSON export gated bycurrency-view.