This document outlines the changes made to support multi-currency wallets in the SwapTrade backend.
The wallet system has been refactored to allow users to hold balances in multiple currencies (Virtual Assets). The system now supports dynamic currency conversion based on asset prices and tracks balances per asset.
- UserBalance Entity: Replaced the deprecated
Balanceentity.- Links
UserandVirtualAsset. - Stores
balanceas a decimal string. - Includes
totalInvested,totalPnL,averageBuyPricefor portfolio tracking.
- Links
- VirtualAsset Entity:
- Added
pricecolumn (decimal) to support currency conversion. - Removed unused
balancescolumn.
- Added
- CurrencyService:
- Handles currency conversion logic using
VirtualAsset.price. - Provides helper methods to list supported currencies.
- Handles currency conversion logic using
- UserBalanceService:
- Manages balance operations (deposit, withdraw, get balance).
- Ensures atomic updates via transactions where applicable.
- SwapService:
- Updated to use
CurrencyServicefor exchange rate calculation. - Supports swapping between any two supported assets.
- Updates
UserBalanceandTradehistory transactionally.
- Updated to use
-
Balance Controller (
/balances):GET /balances/currencies: List supported currencies.GET /balances/:userId: Returns all asset balances for a user.POST /balances/withdraw: Allows withdrawing a specific amount of an asset.- Body:
{ userId: number, assetId: number, amount: number }
- Body:
POST /balances/deposit: Allows depositing a specific amount of an asset.- Body:
{ userId: number, assetId: number, amount: number }
- Body:
POST /balances/convert: Helper to calculate conversion rates.- Body:
{ amount: number, fromAssetId: number, toAssetId: number }
- Body:
-
Swap Controller (
/swap):POST /swap: Executes a swap between assets.- Body:
{ userId: number, fromAssetId: number, toAssetId: number, amount: number }
- Body:
- The old
Balanceentity andBalanceServicehave been removed to avoid conflicts. - All balance-related operations now go through
UserBalanceService.
POST /balances/deposit
Content-Type: application/json
{
"userId": 1,
"assetId": 2,
"amount": 500.00
}POST /balances/withdraw
Content-Type: application/json
{
"userId": 1,
"assetId": 2,
"amount": 100.50
}POST /swap
Content-Type: application/json
{
"userId": 1,
"fromAssetId": 1,
"toAssetId": 2,
"amount": 50.00
}