refactor(arch): Phase 2 — deduplicate isAdmin, convertToUsd, getBaseWalletType
- #54: Extract isAdmin() to src/middleware/auth.js, remove duplicates from 7 admin handlers - #55: Add WalletUtils.convertToUsd(), replace 8 switch-case blocks across 4 files - #56: Unify getBaseWalletType() — keep only WalletUtils version (most complete), remove duplicates from Wallet.js and userWalletsHandler.js New file: src/middleware/auth.js Net: -215 lines, +80 lines Closes: #54, #55, #56
This commit is contained in:
@@ -76,20 +76,21 @@ export default class WalletUtils {
|
||||
}
|
||||
|
||||
static getBaseWalletType(walletType) {
|
||||
// Убираем суффикс ERC-20, если он есть
|
||||
if (walletType.includes('ERC-20')) {
|
||||
return 'ETH';
|
||||
}
|
||||
|
||||
// Убираем суффикс с таймштампом (например, USDT_1735846098129 -> USDT)
|
||||
if (walletType.includes('_')) {
|
||||
return walletType.split('_')[0];
|
||||
}
|
||||
|
||||
// Возвращаем исходный тип, если это не ERC-20 и не архивный кошелек
|
||||
return walletType;
|
||||
}
|
||||
|
||||
static convertToUsd(walletType, balance, prices) {
|
||||
const baseType = WalletUtils.getBaseWalletType(walletType);
|
||||
const rate = prices[baseType.toLowerCase()] || 0;
|
||||
return balance * rate;
|
||||
}
|
||||
|
||||
static async getCryptoPrices() {
|
||||
// Если кеш актуален, возвращаем его
|
||||
if (cryptoPricesCache && Date.now() - cacheTimestamp < CACHE_TTL) {
|
||||
@@ -291,30 +292,13 @@ export default class WalletUtils {
|
||||
`, [this.userId]);
|
||||
|
||||
for (const wallet of wallets) {
|
||||
const [baseType] = wallet.wallet_type.split('_'); // Учитываем только базовый тип
|
||||
const baseType = WalletUtils.getBaseWalletType(wallet.wallet_type);
|
||||
const balance = wallet.balance || 0;
|
||||
|
||||
switch (baseType) {
|
||||
case 'BTC':
|
||||
balances.BTC.amount += balance;
|
||||
balances.BTC.usdValue += balance * prices.btc;
|
||||
break;
|
||||
case 'LTC':
|
||||
balances.LTC.amount += balance;
|
||||
balances.LTC.usdValue += balance * prices.ltc;
|
||||
break;
|
||||
case 'ETH':
|
||||
balances.ETH.amount += balance;
|
||||
balances.ETH.usdValue += balance * prices.eth;
|
||||
break;
|
||||
case 'USDT':
|
||||
balances.USDT.amount += balance;
|
||||
balances.USDT.usdValue += balance;
|
||||
break;
|
||||
case 'USDC':
|
||||
balances.USDC.amount += balance;
|
||||
balances.USDC.usdValue += balance;
|
||||
break;
|
||||
const usdValue = WalletUtils.convertToUsd(wallet.wallet_type, balance, prices);
|
||||
|
||||
if (balances[baseType]) {
|
||||
balances[baseType].amount += balance;
|
||||
balances[baseType].usdValue += usdValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user