update planned wallets function
This commit is contained in:
@@ -22,58 +22,80 @@ class WalletService {
|
||||
static async getActiveWalletsBalance(userId) {
|
||||
try {
|
||||
const wallets = await db.allAsync(
|
||||
`SELECT wallet_type, address
|
||||
`SELECT wallet_type, balance
|
||||
FROM crypto_wallets
|
||||
WHERE user_id = ? AND wallet_type NOT LIKE '%#_%' ESCAPE '#'`,
|
||||
[userId]
|
||||
);
|
||||
|
||||
|
||||
const prices = await WalletUtils.getCryptoPrices();
|
||||
|
||||
let totalBalance = 0;
|
||||
for (const wallet of wallets) {
|
||||
const walletUtils = new WalletUtils(
|
||||
wallet.wallet_type === 'BTC' ? wallet.address : null,
|
||||
wallet.wallet_type === 'LTC' ? wallet.address : null,
|
||||
wallet.wallet_type === 'ETH' ? wallet.address : null,
|
||||
wallet.wallet_type === 'USDT' ? wallet.address : null,
|
||||
wallet.wallet_type === 'USDC' ? wallet.address : null,
|
||||
userId
|
||||
);
|
||||
|
||||
const balances = await walletUtils.getAllBalances();
|
||||
totalBalance += balances[wallet.wallet_type]?.usdValue || 0;
|
||||
const baseType = WalletUtils.getBaseWalletType(wallet.wallet_type);
|
||||
const balance = wallet.balance || 0;
|
||||
|
||||
switch (baseType) {
|
||||
case 'BTC':
|
||||
totalBalance += balance * prices.btc;
|
||||
break;
|
||||
case 'LTC':
|
||||
totalBalance += balance * prices.ltc;
|
||||
break;
|
||||
case 'ETH':
|
||||
totalBalance += balance * prices.eth;
|
||||
break;
|
||||
case 'USDT':
|
||||
totalBalance += balance; // USDT is 1:1 with USD
|
||||
break;
|
||||
case 'USDC':
|
||||
totalBalance += balance; // USDC is 1:1 with USD
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return totalBalance;
|
||||
} catch (error) {
|
||||
console.error('Error fetching active wallets balance:', error);
|
||||
throw new Error('Failed to fetch active wallets balance');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static async getArchivedWalletsBalance(userId) {
|
||||
try {
|
||||
const wallets = await db.allAsync(
|
||||
`SELECT wallet_type, address
|
||||
`SELECT wallet_type, balance
|
||||
FROM crypto_wallets
|
||||
WHERE user_id = ? AND wallet_type LIKE '%#_%' ESCAPE '#'`,
|
||||
[userId]
|
||||
);
|
||||
|
||||
|
||||
const prices = await WalletUtils.getCryptoPrices();
|
||||
|
||||
let totalBalance = 0;
|
||||
for (const wallet of wallets) {
|
||||
const walletUtils = new WalletUtils(
|
||||
wallet.wallet_type === 'BTC' ? wallet.address : null,
|
||||
wallet.wallet_type === 'LTC' ? wallet.address : null,
|
||||
wallet.wallet_type === 'ETH' ? wallet.address : null,
|
||||
wallet.wallet_type === 'USDT' ? wallet.address : null,
|
||||
wallet.wallet_type === 'USDC' ? wallet.address : null,
|
||||
userId
|
||||
);
|
||||
|
||||
const balances = await walletUtils.getAllBalances();
|
||||
totalBalance += balances[wallet.wallet_type]?.usdValue || 0;
|
||||
const baseType = WalletUtils.getBaseWalletType(wallet.wallet_type);
|
||||
const balance = wallet.balance || 0;
|
||||
|
||||
switch (baseType) {
|
||||
case 'BTC':
|
||||
totalBalance += balance * prices.btc;
|
||||
break;
|
||||
case 'LTC':
|
||||
totalBalance += balance * prices.ltc;
|
||||
break;
|
||||
case 'ETH':
|
||||
totalBalance += balance * prices.eth;
|
||||
break;
|
||||
case 'USDT':
|
||||
totalBalance += balance; // USDT is 1:1 with USD
|
||||
break;
|
||||
case 'USDC':
|
||||
totalBalance += balance; // USDC is 1:1 with USD
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return totalBalance;
|
||||
} catch (error) {
|
||||
console.error('Error fetching archived wallets balance:', error);
|
||||
@@ -87,10 +109,10 @@ class WalletService {
|
||||
const wallets = await db.allAsync(
|
||||
`SELECT *
|
||||
FROM crypto_wallets
|
||||
WHERE wallet_type = ?`,
|
||||
[walletType]
|
||||
WHERE wallet_type = ? OR wallet_type LIKE ?`,
|
||||
[walletType, `${walletType}_%`]
|
||||
);
|
||||
|
||||
|
||||
return wallets;
|
||||
} catch (error) {
|
||||
console.error('Error fetching wallets by type:', error);
|
||||
|
||||
Reference in New Issue
Block a user