From 2fd2334fcfdbc91dd0e17f290ec87bacddb0ab87 Mon Sep 17 00:00:00 2001 From: guoyunfeng Date: Sun, 25 Oct 2026 03:27:06 +0800 Subject: [PATCH] fix(fee-utils): support xlmPrice in calculateBatchFees for USD estimation (closes #1034) --- __tests__/lib/fee-utils.test.ts | 15 +++++++++++++++ lib/fee-utils.ts | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/__tests__/lib/fee-utils.test.ts b/__tests__/lib/fee-utils.test.ts index 20596ee..2195ae7 100644 --- a/__tests__/lib/fee-utils.test.ts +++ b/__tests__/lib/fee-utils.test.ts @@ -76,6 +76,21 @@ describe("calculateBatchFees", () => { expect(result.totalEstimated).toBeGreaterThan(0); expect(result.minFee + result.bufferFee).toBe(result.totalEstimated); }); + + it("omits estimatedUsd when xlmPrice is not provided", () => { + const result = calculateBatchFees(3, 40_000); + expect(result.estimatedUsd).toBeUndefined(); + }); + + it("calculates estimatedUsd when xlmPrice is provided", () => { + const operationCount = 5; + const feePerOperation = 50_000; + const xlmPrice = 0.5; + const result = calculateBatchFees(operationCount, feePerOperation, xlmPrice); + const expectedTotal = Math.ceil(operationCount * feePerOperation * 1.15); + const expectedUsd = (expectedTotal / 1e7) * xlmPrice; + expect(result.estimatedUsd).toBeCloseTo(expectedUsd, 5); + }); }); describe("TYPICAL_FEES", () => { diff --git a/lib/fee-utils.ts b/lib/fee-utils.ts index f778815..5209fec 100644 --- a/lib/fee-utils.ts +++ b/lib/fee-utils.ts @@ -66,7 +66,8 @@ export function formatFee(stroops: number, decimals: number = 7): string { export function calculateBatchFees( operationCount: number, feePerOperation: number, + xlmPrice?: number, ): FeeBreakdown { const totalFee = feePerOperation * operationCount; - return calculateFeeBreakdown(totalFee); + return calculateFeeBreakdown(totalFee, xlmPrice); }