Asset and Balance Features
This document provides a comprehensive overview of the Asset and Balance features in the RCoinX backend system. These features work together to manage cryptocurrency assets, user balances, and wallet functionality.Table of Contents
- Database Structure
- Database Relations
- Asset Feature
- Balance Feature
- API Documentation
- Frontend Integration Guide
Database Structure
Asset Module Tables
1. Assets Table (assets)
The main table for storing cryptocurrency and token information.
| Field | Type | Description | Example |
|---|---|---|---|
id | uint | Primary key, auto-increment | 1 |
symbol | varchar(20) | Unique asset symbol | ”BTC”, “ETH”, “USDT” |
name | varchar(100) | Full asset name | ”Bitcoin”, “Ethereum” |
type | varchar(20) | Asset type (coin/token) | “coin”, “token” |
decimals | int | Number of decimal places | 8, 18 |
kyc_required | bool | Whether KYC is required | false |
website | varchar(255) | Asset website URL | ”https://bitcoin.org” |
metadata | json | Additional asset metadata | {"description": "..."} |
icon_file_id | uint | Reference to icon file | 1 |
is_active | bool | Whether asset is active | true |
is_evm | bool | Whether asset is EVM compatible | false |
is_fungible | bool | Whether asset is fungible | true |
created_at | timestamp | Creation timestamp | ”2023-01-01T12:00:00Z” |
updated_at | timestamp | Last update timestamp | ”2023-01-01T12:00:00Z” |
deleted_at | timestamp | Soft delete timestamp | null |
2. Chains Table (chains)
Stores blockchain network information.
| Field | Type | Description | Example |
|---|---|---|---|
id | uint | Primary key | 1 |
name | varchar(100) | Chain name | ”Ethereum”, “Bitcoin” |
symbol | varchar(20) | Chain symbol | ”ETH”, “BTC” |
type | varchar(20) | Chain type | ”evm”, “bitcoin”, “solana” |
description | text | Chain description | ”A decentralized platform…” |
website_url | varchar(255) | Chain website | ”https://ethereum.org” |
is_active | bool | Whether chain is active | true |
supports_smart_contracts | bool | Smart contract support | true |
consensus_mechanism | varchar(50) | Consensus mechanism | ”Proof of Stake” |
block_time | int | Block time in seconds | 12 |
created_at | timestamp | Creation timestamp | ”2023-01-01T12:00:00Z” |
updated_at | timestamp | Last update timestamp | ”2023-01-01T12:00:00Z” |
deleted_at | timestamp | Soft delete timestamp | null |
3. Networks Table (networks)
Stores specific network configurations for chains.
| Field | Type | Description | Example |
|---|---|---|---|
id | uint | Primary key | 1 |
name | varchar(100) | Network name | ”Ethereum Mainnet” |
chain_id | uint | Reference to chain | 1 |
network_id | varchar(50) | Network identifier | ”1” |
is_active | bool | Whether network is active | true |
is_testnet | bool | Whether it’s a testnet | false |
created_at | timestamp | Creation timestamp | ”2023-01-01T12:00:00Z” |
updated_at | timestamp | Last update timestamp | ”2023-01-01T12:00:00Z” |
deleted_at | timestamp | Soft delete timestamp | null |
4. Token Standards Table (token_standards)
Stores token standard information (ERC-20, BEP-20, etc.).
| Field | Type | Description | Example |
|---|---|---|---|
id | uint | Primary key | 1 |
code | varchar(100) | Standard code | ”ERC-20”, “BEP-20” |
description | text | Standard description | ”Fungible token standard” |
created_at | timestamp | Creation timestamp | ”2023-01-01T12:00:00Z” |
updated_at | timestamp | Last update timestamp | ”2023-01-01T12:00:00Z” |
deleted_at | timestamp | Soft delete timestamp | null |
5. Asset Networks Table (asset_networks)
Junction table linking assets to networks with token standards.
| Field | Type | Description | Example |
|---|---|---|---|
id | uint | Primary key | 1 |
asset_id | uint | Reference to asset | 1 |
token_standard_id | uint | Reference to token standard | 1 |
network_id | uint | Reference to network | 1 |
is_active | bool | Whether configuration is active | true |
created_at | timestamp | Creation timestamp | ”2023-01-01T12:00:00Z” |
updated_at | timestamp | Last update timestamp | ”2023-01-01T12:00:00Z” |
deleted_at | timestamp | Soft delete timestamp | null |
Balance Module Tables
1. Balances Table (balances)
Stores user balance information for each asset.
| Field | Type | Description | Example |
|---|---|---|---|
id | uint | Primary key | 1 |
user_id | uint | Reference to user | 1 |
asset_id | uint | Reference to asset | 1 |
balance | decimal(20,8) | Total balance | 1000.00000000 |
available | decimal(20,8) | Available balance | 950.00000000 |
locked | decimal(20,8) | Locked balance | 50.00000000 |
is_active | bool | Whether balance is active | true |
created_at | timestamp | Creation timestamp | ”2023-01-01T12:00:00Z” |
updated_at | timestamp | Last update timestamp | ”2023-01-01T12:00:00Z” |
deleted_at | timestamp | Soft delete timestamp | null |
2. Balance Wallets Table (balance_wallets)
Stores wallet information for user balances.
| Field | Type | Description | Example |
|---|---|---|---|
id | uint | Primary key | 1 |
balance_id | uint | Reference to balance | 1 |
wallet_address | varchar(255) | Wallet address | ”0x1234…” |
private_key | text | Encrypted private key | ”encrypted_key” |
public_key | text | Public key | ”public_key_data” |
asset_network_id | uint | Reference to asset network | 1 |
mnemonic | text | Wallet mnemonic | ”word1 word2…” |
derivation_path | varchar(255) | Derivation path | ”m/44’/60’/0’/0/0” |
is_active | bool | Whether wallet is active | true |
created_at | timestamp | Creation timestamp | ”2023-01-01T12:00:00Z” |
updated_at | timestamp | Last update timestamp | ”2023-01-01T12:00:00Z” |
deleted_at | timestamp | Soft delete timestamp | null |
3. Transactions Table (transactions)
Stores transaction history for wallets.
| Field | Type | Description | Example |
|---|---|---|---|
id | uint | Primary key | 1 |
wallet_id | uint | Reference to balance wallet | 1 |
hash | varchar(255) | Transaction hash | ”0xabc123…” |
from | varchar(255) | From address | ”0x1234…” |
to | varchar(255) | To address | ”0x5678…” |
value | varchar(255) | Transaction value | ”1000000000000000000” |
asset | varchar(255) | Asset symbol | ”ETH” |
block_num | uint64 | Block number | 12345678 |
timestamp | timestamp | Transaction timestamp | ”2023-01-01T12:00:00Z” |
status | varchar(50) | Transaction status | ”confirmed” |
confirmations | int | Number of confirmations | 12 |
network_fee | varchar(255) | Network fee | ”21000000000000000” |
error_message | text | Error message if failed | null |
created_at | timestamp | Creation timestamp | ”2023-01-01T12:00:00Z” |
updated_at | timestamp | Last update timestamp | ”2023-01-01T12:00:00Z” |
deleted_at | timestamp | Soft delete timestamp | null |
Database Relations
Asset Module Relations
Balance Module Relations
Cross-Module Relations
The Asset and Balance modules are connected through:- Assets → Balances: Each balance is associated with a specific asset
- Asset Networks → Balance Wallets: Each wallet is created for a specific asset-network combination
- Users → Balances: Each balance belongs to a specific user
Asset Feature
Overview
The Asset feature manages cryptocurrency and token information, including:- Asset Management: CRUD operations for cryptocurrencies and tokens
- Network Configuration: Support for multiple blockchain networks
- Token Standards: Support for various token standards (ERC-20, BEP-20, etc.)
- Chain Management: Support for different blockchain types (EVM, Bitcoin, Solana, etc.)
Key Features
1. Asset Types
- Coins: Native cryptocurrencies (BTC, ETH, etc.)
- Tokens: Smart contract-based tokens (USDT, USDC, etc.)
2. Multi-Chain Support
- EVM Chains: Ethereum, BSC, Polygon, etc.
- Bitcoin-like: Bitcoin, Litecoin, etc.
- Solana: Solana ecosystem
- Cosmos: Cosmos SDK chains
3. Token Standards
- ERC-20: Ethereum fungible tokens
- BEP-20: BSC fungible tokens
- SPL: Solana program library tokens
- Custom: Platform-specific standards
Business Logic
Asset Creation Flow
- Create asset with basic information (symbol, name, type)
- Configure asset networks for supported chains
- Set token standards for each network
- Activate asset for trading
Asset Network Configuration
- Select chain and network
- Choose appropriate token standard
- Configure network-specific parameters
- Enable/disable network support
Balance Feature
Overview
The Balance feature manages user cryptocurrency balances and wallets, including:- Balance Management: Track user balances for each asset
- Wallet Generation: Create wallets for different networks
- Transaction Tracking: Monitor incoming and outgoing transactions
- Balance Operations: Lock/unlock funds, transfers
Key Features
1. Balance Types
- Total Balance: Complete balance amount
- Available Balance: Amount available for transactions
- Locked Balance: Amount locked in pending operations
2. Wallet Management
- Multi-Network Wallets: Generate wallets for different networks
- HD Wallet Support: Hierarchical deterministic wallet generation
- Private Key Management: Secure storage of private keys
- Address Generation: Generate unique addresses for each network
3. Transaction Monitoring
- Real-time Updates: Monitor blockchain for new transactions
- Status Tracking: Track transaction confirmation status
- Fee Calculation: Calculate and track network fees
- Error Handling: Handle failed transactions
Business Logic
Balance Creation Flow
- User requests balance for specific asset
- System creates balance record with zero amounts
- Generate wallet for primary network
- Set up transaction monitoring
Wallet Generation Flow
- User requests wallet for specific asset-network
- Check if balance exists, create if needed
- Generate HD wallet with unique derivation path
- Store wallet information securely
- Set up blockchain monitoring
Transaction Processing Flow
- Monitor blockchain for incoming transactions
- Update balance when transaction confirms
- Track transaction status and confirmations
- Handle failed or stuck transactions
API Documentation
Asset API Endpoints
Public Endpoints (No Authentication Required)
Get Assets
page(int, optional): Page number (default: 1)page_size(int, optional): Page size (default: 10, max: 100)search(string, optional): Search term for symbol/nameasset_type(string, optional): Filter by type (“coin” or “token”)
Get Asset by ID
id(int, required): Asset ID
Get Asset by Symbol
symbol(string, required): Asset symbol (e.g., “BTC”, “ETH”)
Get Networks
page(int, optional): Page number (default: 1)page_size(int, optional): Page size (default: 10, max: 100)search(string, optional): Search term for network name
Get All Networks
Get Network by ID
id(int, required): Network ID
Balance API Endpoints
Authenticated Endpoints (Bearer Token Required)
Get User Balances
Get Balance by ID
id(int, required): Balance ID
Get Balance by Asset
asset_id(int, required): Asset ID
Generate Wallet for Balance
asset_network_id(int, required): Asset Network ID
Get User Wallets
Get Wallet by ID
id(int, required): Wallet ID
Get Wallets by Balance
balance_id(int, required): Balance ID
Error Responses
All endpoints return standardized error responses:200: Success400: Bad Request401: Unauthorized (missing or invalid token)403: Forbidden (insufficient permissions)404: Not Found422: Unprocessable Entity (validation errors)500: Internal Server Error
Frontend Integration Guide
Authentication
All balance and wallet endpoints require authentication. Include the Bearer token in the Authorization header:Asset Management
Fetching Assets
Asset Selection Component
Balance Management
Fetching User Balances
Balance Display Component
Wallet Management
Generating Wallets
Wallet Generation Component
Wallet Display Component
Network Management
Fetching Networks
Network Selection Component
Error Handling
Global Error Handler
API Wrapper with Error Handling
Best Practices
1. State Management
- Use React Context or Redux for global state management
- Cache API responses to reduce unnecessary requests
- Implement optimistic updates for better UX
2. Security
- Never store sensitive data (private keys, tokens) in localStorage
- Use secure HTTP-only cookies for authentication when possible
- Implement proper token refresh logic
3. Performance
- Implement pagination for large data sets
- Use debouncing for search inputs
- Lazy load components and data when possible
4. User Experience
- Show loading states for all async operations
- Provide clear error messages
- Implement retry mechanisms for failed requests
- Use skeleton loaders for better perceived performance
5. Data Validation
- Validate data on both client and server side
- Implement proper form validation
- Handle edge cases gracefully