Wallets Package
The @sws/wallets package is the core of Switchain's wallet management system. It handles all operations related to custodial wallets, deposit addresses, transactions, and balances.
Overview
This package implements a hexagonal architecture (Clean Architecture) with Domain-Driven Design (DDD) to manage:
- Custodial wallets for multiple blockchains
- Dynamic deposit addresses for users
- Inbound and outbound transactions
- Real-time balances
- Automated sweep operations
Architecture
Core Entities
Wallet
Represents a custodial wallet that can hold multiple tokens across different blockchains.
WalletAddress
Specific addresses of a wallet to receive funds. Can be:
- WalletNativeAddress: For native tokens (ETH, BTC, SOL, etc.)
- WalletTokenAddress: For specific tokens (USDT, USDC, etc.)
Transaction
Represents a transaction in the system, with information about source, destination, amount, and status.
WalletToken
Represents the holding of a specific token in a wallet.
Main Controllers
The package exposes multiple controllers organized by functionality:
API Controllers (For external applications)
getWalletDepositAddress: Gets deposit addressesgetWallet: Retrieves wallet informationgetWalletToken: Gets specific token informationretrieveWalletBalance: Queries balancesinitiateWithdrawalTransaction: Initiates withdrawals
Worker Controllers (For background processing)
sendTransaction: Sends transactionsupdateTransaction: Updates transaction statusupdateWalletBalance: Updates balancessweepWalletAddress: Executes sweep operationsdetectWalletAddressDeposit: Detects deposits
Admin Controllers (For administration panel)
updateWalletSettings: Configures walletscancelTransaction: Cancels transactionsscanWallet: Scans complete walletsutxoCPFPTransaction: Accelerates UTXO transactions
Supported Blockchains
The system supports multiple blockchains:
- Bitcoin (UTXO)
- Ethereum (EVM)
- Solana
- Tron
- Cosmos/Atom
Each blockchain has its own adapters and specific logic in the infrastructure layer.
Main Flows
Fund Deposits
- User requests deposit address
- System generates specific address for the token
- User sends funds to the address
- Worker detects the deposit
- Funds are credited to the wallet
Fund Withdrawals
- User requests withdrawal
- System validates available funds
- Outbound transaction is created
- Worker processes and sends the transaction
- Balance is updated
Sweep Operations
- Worker detects funds in deposit addresses
- Consolidates funds to main wallets
- Optimizes fees and manages gas
Directory Structure
packages/wallets/
├── domain/ # Pure business logic
│ ├── entities/ # Domain entities
│ ├── valueObjects/ # Value objects
│ ├── repositories/ # Repository interfaces
│ ├── services/ # Domain services
│ └── events/ # Domain events
├── application/ # Application services
│ ├── wallet/ # Wallet application services
│ ├── walletAddress/ # Address application services
│ ├── transaction/ # Transaction application services
│ ├── services/ # Complex application services
│ ├── utxo/ # UTXO-specific services
│ ├── tron/ # Tron-specific services
│ └── solana/ # Solana-specific services
└── infrastructure/ # Technical implementations
├── controllers/ # HTTP/API controllers
├── repositories/ # Repository implementations
├── adapters/ # Blockchain adapters
└── factories/ # Dependency injection factoriesNext Steps
- Entities - Detailed entity documentation
- Application Services - Application layer services
- Admin Operations - Admin panel operations
- Solana Implementation
- CEX Wallets
