Amba-Agent-App/lib/utils/feeUtils.ts
2026-01-16 00:22:35 +03:00

76 lines
2.3 KiB
TypeScript

/**
* Fee calculation utilities for money transactions
*/
export const PROCESSING_FEE_RATE = 0.0125; // 1.25%
/**
* Calculate the processing fee for a given amount
* @param amount - The transaction amount in cents
* @returns The processing fee in cents
*/
export const calculateProcessingFee = (amount: number): number => {
return Math.ceil(amount * PROCESSING_FEE_RATE);
};
/**
* Calculate the total amount required (including processing fee) for sending money
* @param amount - The amount to send in cents
* @returns The total amount required in cents (amount + processing fee)
*/
export const calculateTotalAmountForSending = (amount: number): number => {
const fee = calculateProcessingFee(amount);
return amount + fee;
};
/**
* Calculate the total amount required (including processing fee) for accepting a money request
* @param amount - The amount to send in cents
* @returns The total amount required in cents (amount + processing fee)
*/
export const calculateTotalAmountForRequesting = (amount: number): number => {
const fee = calculateProcessingFee(amount);
return amount + fee;
};
/**
* Format fee information for display
* @param amount - The base amount in cents
* @returns Object with formatted fee information
*/
export const getFeeInformation = (amount: number) => {
const fee = calculateProcessingFee(amount);
const total = amount + fee;
return {
baseAmount: amount,
fee,
total,
feePercentage: PROCESSING_FEE_RATE * 100,
formatted: {
baseAmount: (amount / 100).toFixed(2),
fee: (fee / 100).toFixed(2),
total: (total / 100).toFixed(2),
}
};
};
/**
* Validate if user has sufficient balance for a transaction including processing fee
* @param userBalance - User's current balance in cents
* @param transactionAmount - The transaction amount in cents
* @returns Object with validation result and fee information
*/
export const validateBalanceWithFee = (userBalance: number, transactionAmount: number) => {
const totalRequired = calculateTotalAmountForSending(transactionAmount);
const feeInfo = getFeeInformation(transactionAmount);
return {
hasSufficientBalance: userBalance >= totalRequired,
requiredBalance: totalRequired,
currentBalance: userBalance,
shortfall: Math.max(0, totalRequired - userBalance),
feeInfo
};
};