Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 5x 6x 6x 6x 3x 3x 1x 2x 1x 1x 4x 4x 4x 2x 5x 5x 5x 5x 1x 4x 4x 1x 3x 3x 1x 1x 1x 2x 1x 1x 4x 4x 1x 3x 1x 2x 1x 1x | /**
* Utility functions for wallet operations
*/
import { currencyFormatter } from '@/lib/currency-formatter';
import type { TRANSACTION_TYPES } from '@/constants/wallet';
import {
DEFAULT_CURRENCY,
DEFAULT_LOCALE,
WALLET_ERROR_MESSAGES,
WALLET_HEALTH_STATUS,
WALLET_HEALTH_COLORS,
WALLET_BALANCE_THRESHOLDS,
WITHDRAWAL_CONSTRAINTS,
} from '@/constants/wallet';
export const walletUtils = {
/**
* Format currency amount with proper locale and currency symbol
* @deprecated Use currencyFormatter.format() instead for consistency
*/
formatCurrency: (
amount: string | number,
currency = DEFAULT_CURRENCY,
locale = DEFAULT_LOCALE
): string => {
return currencyFormatter.format(amount, currency, locale);
},
/**
* Validate withdrawal amount
*/
validateWithdrawalAmount: (
amount: string,
availableBalance: string
): {
isValid: boolean;
error?: string;
} => {
const withdrawAmount = parseFloat(amount);
const balance = parseFloat(availableBalance);
if (isNaN(withdrawAmount) || withdrawAmount <= 0) {
return {
isValid: false,
error: WALLET_ERROR_MESSAGES.INVALID_AMOUNT,
};
}
if (withdrawAmount > balance) {
return {
isValid: false,
error: WALLET_ERROR_MESSAGES.EXCEEDS_BALANCE,
};
}
// Check minimum withdrawal
if (withdrawAmount < WITHDRAWAL_CONSTRAINTS.MINIMUM_AMOUNT) {
return {
isValid: false,
error: WALLET_ERROR_MESSAGES.MINIMUM_WITHDRAWAL,
};
}
return { isValid: true };
},
/**
* Calculate percentage of total balance
*/
calculatePercentage: (amount: string, total: string): number => {
const amountNum = parseFloat(amount);
const totalNum = parseFloat(total);
if (isNaN(amountNum) || isNaN(totalNum) || totalNum === 0) return 0;
return (amountNum / totalNum) * 100;
},
/**
* Generate transaction reference ID
*/
generateTransactionId: (
type:
| typeof TRANSACTION_TYPES.DEPOSIT
| typeof TRANSACTION_TYPES.WITHDRAWAL
| typeof TRANSACTION_TYPES.TRANSFER
): string => {
const timestamp = Date.now();
const random = Math.random().toString(36).substring(2, 8);
return `${type}_${timestamp}_${random}`;
},
/**
* Check if withdrawal is allowed based on business rules
*/
canWithdraw: (
amount: string,
balance: string,
minimumBalance = WITHDRAWAL_CONSTRAINTS.MINIMUM_BALANCE.toString()
): boolean => {
const withdrawAmount = parseFloat(amount);
const currentBalance = parseFloat(balance);
const minBalance = parseFloat(minimumBalance);
if (isNaN(withdrawAmount) || isNaN(currentBalance)) return false;
// Check if withdrawal would leave enough minimum balance
return currentBalance - withdrawAmount >= minBalance;
},
/**
* Parse and clean amount input
*/
cleanAmountInput: (input: string): string => {
// Remove any non-numeric characters except decimal point
const cleaned = input.replace(/[^\d.]/g, '');
// Ensure only one decimal point
const parts = cleaned.split('.');
if (parts.length > 2) {
return parts[0] + '.' + parts.slice(1).join('');
}
// Limit to 2 decimal places
if (parts[1] && parts[1].length > 2) {
return parts[0] + '.' + parts[1].substring(0, 2);
}
return cleaned;
},
/**
* Sanitize amount input - similar to cleanAmountInput but with different edge case handling
*/
sanitizeAmount: (input: string): string => {
if (!input || input.trim() === '') {
return '';
}
// Remove any non-numeric characters except decimal point
const cleaned = input.replace(/[^\d.]/g, '');
// Handle only decimal point case
if (cleaned === '.') {
return '';
}
// Ensure only one decimal point (keep first decimal, ignore others)
const parts = cleaned.split('.');
if (parts.length > 2) {
// Take only the first part and the first decimal part
const result = parts[0] + '.' + parts[1];
// Limit to 2 decimal places
Iif (parts[1] && parts[1].length > 2) {
return parts[0] + '.' + parts[1].substring(0, 2);
}
return result;
}
// Limit to 2 decimal places
if (parts[1] && parts[1].length > 2) {
return parts[0] + '.' + parts[1].substring(0, 2);
}
return cleaned;
},
/**
* Get wallet health status based on balance
*/
getWalletHealth: (
balance: string
): {
status:
| typeof WALLET_HEALTH_STATUS.EXCELLENT
| typeof WALLET_HEALTH_STATUS.GOOD
| typeof WALLET_HEALTH_STATUS.WARNING
| typeof WALLET_HEALTH_STATUS.CRITICAL;
message: string;
color: string;
} => {
const balanceNum = parseFloat(balance);
if (balanceNum >= WALLET_BALANCE_THRESHOLDS.EXCELLENT) {
return {
status: WALLET_HEALTH_STATUS.EXCELLENT,
message: 'Excellent wallet balance',
color: WALLET_HEALTH_COLORS.EXCELLENT,
};
} else if (balanceNum >= WALLET_BALANCE_THRESHOLDS.GOOD) {
return {
status: WALLET_HEALTH_STATUS.GOOD,
message: 'Good wallet balance',
color: WALLET_HEALTH_COLORS.GOOD,
};
} else if (balanceNum >= WALLET_BALANCE_THRESHOLDS.WARNING) {
return {
status: WALLET_HEALTH_STATUS.WARNING,
message: 'Consider adding funds',
color: WALLET_HEALTH_COLORS.WARNING,
};
} else {
return {
status: WALLET_HEALTH_STATUS.CRITICAL,
message: 'Low wallet balance',
color: WALLET_HEALTH_COLORS.CRITICAL,
};
}
},
};
|