Skip to content

wittiden/WalletManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

75 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿช™ WalletManager

WalletManager is a backend service for managing users, wallets, balances, and transactions. The project is built using Clean Architecture / DDD-lite principles with a clear separation of domain logic, infrastructure, and application use cases.


๐Ÿš€ Key Features

  • ๐Ÿ‘ค User management (registration, login, blocking)

  • ๐Ÿ‘› Wallet management (creation, blocking, closing)

  • ๐Ÿ’ฐ Balance management:

    • regular (single currency)
    • multi-currency (multiple assets per wallet)
  • ๐Ÿ’ณ Transaction management:

    • deposit
    • withdrawal
    • exchange
  • ๐Ÿญ Factory + Registry pattern for all domain entities

  • ๐Ÿ”„ Mapper layer (domain โ†” database)

  • ๐Ÿง  Domain invariants (DomainInvariant)

  • ๐Ÿช Event signaling via blinker

  • ๐Ÿ—„ SQLAlchemy + PostgreSQL integration

  • ๐Ÿ“ฆ Facade layer for use cases


๐Ÿง  Project Architecture

The project is structured according to Clean Architecture principles.


๐Ÿ“Œ Domain Layer

Pure business logic. Does not depend on database, frameworks, or infrastructure.

๐Ÿ‘ค Users

  • UserBase, Client, Admin
  • domain invariants (email, name, password)
  • user status handling

๐Ÿ‘› Wallets

  • DebitWallet, CreditWallet
  • ownership relation
  • blocking / closing logic

๐Ÿ’ฐ Balances

  • RegularBalance โ€” single currency
  • ForeignBalance โ€” multiple currencies
  • stored internally as dict[currency -> amount]

๐Ÿ’ณ Transactions

  • DepositTransaction
  • WithdrawTransaction
  • ExchangeTransaction

๐Ÿ“Œ Domain layer contains:

  • no ORM
  • no SQL
  • only business rules and invariants

๐Ÿ“Œ Infrastructure Layer

Handles persistence and external dependencies.

๐Ÿ—„ Database (SQLAlchemy)

  • UserTable
  • WalletTable
  • BalanceTable
  • TransactionTable

๐Ÿ”„ Mappers

Responsible for transforming:

Domain โ†” ORM (SQLAlchemy models)
  • UserMapper
  • WalletMapper
  • BalanceMapper
  • TransactionMapper

๐Ÿ“Œ Important detail:

  • ForeignBalance is stored as multiple rows in DB
  • and reconstructed via grouping

๐Ÿ“ฆ Repositories

Split into:

  • commands โ€” write operations (insert/update/delete)
  • queries โ€” read operations

Examples:

  • UserCommandsRepository
  • WalletQueriesRepository
  • BalanceCommandsRepository

โš™๏ธ Application / Use-cases Layer

Encapsulates business workflows.

๐Ÿ‘ค Users

  • CreateUserService
  • LoginUserService
  • BlockUserService
  • CloseUserService

๐Ÿ‘› Wallets

  • CreateWalletService
  • BlockWalletService
  • CloseWalletService

๐Ÿ’ฐ Balances

  • CreateBalanceService
  • FreezeBalanceService
  • ShowBalanceService

๐Ÿ’ณ Transactions

  • CreateTransactionService
  • ShowTransactionService
  • SortTransactionService

๐ŸŽญ Facade Layer

Provides a simplified interface:

  • UserServiceFacade
  • WalletServiceFacade
  • BalanceServiceFacade
  • TransactionServiceFacade

๐Ÿ“Œ Avoids direct interaction with multiple services


๐Ÿญ Factory + Registry

Each domain has:

  • Factory โ€” creates instances
  • Registry โ€” maps type โ†’ class

Example:

BalanceTypesEnum.FOREIGN โ†’ ForeignBalance

๐Ÿ“Œ Benefits:

  • extensibility
  • no hardcoded conditionals
  • easy addition of new types

๐Ÿงฉ Core Layer

Shared components:

  • ๐Ÿ”ข Enums (User, Wallet, Balance, Transaction)
  • โš ๏ธ DomainInvariant (validation rules)
  • ๐Ÿช Signals (blinker)
  • ๐Ÿงฐ utilities / decorators
  • ๐Ÿ“Š logging

๐Ÿ”ฅ Implementation Highlights

1. Multi-currency balance

Stored in DB as:

wallet_id | currency | amount

In domain:

{ "BTC": 2, "USDT": 3, "TON": 5 }

๐Ÿ“Œ Reconstructed using:

grouped[obj.balance_id]

2. Domain invariants

Validation happens inside domain models:

DomainInvariant.no_empty(...)
DomainInvariant.is_instance(...)

๐Ÿ“Œ Ensures consistent object state


3. Signals (blinker)

On state change:

user_upgrade_signal.send(...)

๐Ÿ“Œ Enables:

  • logging
  • auditing
  • event-driven extensions

4. Mapper layer

Clear separation:

Database model โ‰  Domain model

๐Ÿ“Œ Prevents ORM leakage into business logic


๐Ÿงฉ Design Patterns Used

  • ๐Ÿญ Factory Pattern
  • ๐Ÿ—‚ Registry Pattern
  • ๐ŸŽญ Facade Pattern
  • ๐Ÿงฑ Mapper Pattern
  • ๐Ÿช Observer Pattern (via signals)
  • ๐Ÿง  DDD-lite principles

๐Ÿ”ฎ Possible Improvements

๐Ÿ”น Architecture

  • Unit of Work pattern
  • CQRS (separate read/write models)
  • Domain Events (instead of direct signals)

๐Ÿ”น Infrastructure

  • Proper Alembic migration setup
  • Async SQLAlchemy
  • Redis (caching / locks)

๐Ÿ”น Dependency Injection

  • Integrate DI container (e.g. Dishka)

๐Ÿ”น API Layer

  • FastAPI integration
  • Pydantic DTOs for request/response

๐ŸŽฏ Project Goal

This project demonstrates:

  • strong backend architecture design skills
  • separation of concerns
  • clean domain modeling
  • practical usage of design patterns
  • building scalable, production-ready systems

About

Backend service for managing users, wallets, balances, and transactions built with Clean Architecture / DDD-lite principles. The project features a strict separation of domain, application, and infrastructure layers, with extensible factories, mappers, and use-case driven design.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages