Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 66 additions & 65 deletions src/components/wallet/LoanSimulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,73 @@ export default function LoanSimulator({ mode = "borrow", collateralType = "WBTC"
{/* Input Field */}
<div className="mt-6">
{mode === "simulate" ? (
/* Sandbox Mode - Collateral Type Selector + Collateral Input + Borrow Slider */
/* Sandbox Mode - Borrow Slider + Collateral Type Selector + Collateral Input */
<div className="flex flex-col gap-6">
{/* Borrow Amount Slider */}
<div>
<div className="flex justify-between items-center mb-2">
<div className="flex items-center gap-2">
<TokenIcon symbol="USDT" width={24} height={24} />
<span className="text-white/70 text-sm">Borrow Amount (USDT)</span>
</div>
<button
type="button"
onClick={() => setInputValue(String(maxBorrow))}
className="text-amber-500 text-xs hover:text-amber-400 transition-colors"
>
Max: {formatCurrency(maxBorrow, 0, "$", false)}
</button>
</div>
<div className="bg-white/5 border border-white/10 rounded-lg px-4 py-3 text-center mb-3">
<span className="text-white font-semibold text-2xl">
{formatCurrency(parsedInput, 0, "$", false)}
</span>
</div>
<div className="px-1">
<input
type="range"
min="0"
max={maxBorrow || 1}
step={maxBorrow / 100 || 1}
value={parsedInput}
onChange={(e) => setInputValue(e.target.value)}
disabled={maxBorrow <= 0}
className={cn(
"w-full h-2 rounded-full appearance-none cursor-pointer bg-white/10",
"[&::-webkit-slider-thumb]:appearance-none",
"[&::-webkit-slider-thumb]:w-5",
"[&::-webkit-slider-thumb]:h-5",
"[&::-webkit-slider-thumb]:rounded-full",
"[&::-webkit-slider-thumb]:bg-amber-500",
"[&::-webkit-slider-thumb]:shadow-lg",
"[&::-webkit-slider-thumb]:border-2",
"[&::-webkit-slider-thumb]:border-white/20",
"[&::-moz-range-thumb]:w-5",
"[&::-moz-range-thumb]:h-5",
"[&::-moz-range-thumb]:rounded-full",
"[&::-moz-range-thumb]:bg-amber-500",
"[&::-moz-range-thumb]:border-2",
"[&::-moz-range-thumb]:border-white/20",
"disabled:opacity-50"
)}
style={{
background: maxBorrow > 0
? `linear-gradient(to right, rgb(245 158 11) 0%, rgb(245 158 11) ${(parsedInput / maxBorrow) * 100}%, rgba(255,255,255,0.1) ${(parsedInput / maxBorrow) * 100}%, rgba(255,255,255,0.1) 100%)`
: "rgba(255,255,255,0.1)"
}}
/>
<div className="flex justify-between text-white/40 text-xs mt-2">
<span>$0</span>
<span>{formatCurrency(maxBorrow, 0, "$", false)}</span>
</div>
</div>
{maxBorrow <= 0 && parsedSandboxCollateral === 0 && (
<p className="text-white/40 text-xs mt-2 text-center">
Enter collateral amount to enable borrowing
</p>
)}
</div>

{/* Collateral Type Selector */}
<div className="flex gap-2">
<button
Expand Down Expand Up @@ -732,70 +797,6 @@ export default function LoanSimulator({ mode = "borrow", collateralType = "WBTC"
</p>
</div>

{/* Borrow Amount Slider */}
<div>
<div className="flex justify-between items-center mb-2">
<div className="flex items-center gap-2">
<TokenIcon symbol="USDT" width={24} height={24} />
<span className="text-white/70 text-sm">Borrow Amount (USDT)</span>
</div>
<button
type="button"
onClick={() => setInputValue(String(maxBorrow))}
className="text-amber-500 text-xs hover:text-amber-400 transition-colors"
>
Max: {formatCurrency(maxBorrow, 0, "$", false)}
</button>
</div>
<div className="bg-white/5 border border-white/10 rounded-lg px-4 py-3 text-center mb-3">
<span className="text-white font-semibold text-2xl">
{formatCurrency(parsedInput, 0, "$", false)}
</span>
</div>
<div className="px-1">
<input
type="range"
min="0"
max={maxBorrow || 1}
step={maxBorrow / 100 || 1}
value={parsedInput}
onChange={(e) => setInputValue(e.target.value)}
disabled={maxBorrow <= 0}
className={cn(
"w-full h-2 rounded-full appearance-none cursor-pointer bg-white/10",
"[&::-webkit-slider-thumb]:appearance-none",
"[&::-webkit-slider-thumb]:w-5",
"[&::-webkit-slider-thumb]:h-5",
"[&::-webkit-slider-thumb]:rounded-full",
"[&::-webkit-slider-thumb]:bg-amber-500",
"[&::-webkit-slider-thumb]:shadow-lg",
"[&::-webkit-slider-thumb]:border-2",
"[&::-webkit-slider-thumb]:border-white/20",
"[&::-moz-range-thumb]:w-5",
"[&::-moz-range-thumb]:h-5",
"[&::-moz-range-thumb]:rounded-full",
"[&::-moz-range-thumb]:bg-amber-500",
"[&::-moz-range-thumb]:border-2",
"[&::-moz-range-thumb]:border-white/20",
"disabled:opacity-50"
)}
style={{
background: maxBorrow > 0
? `linear-gradient(to right, rgb(245 158 11) 0%, rgb(245 158 11) ${(parsedInput / maxBorrow) * 100}%, rgba(255,255,255,0.1) ${(parsedInput / maxBorrow) * 100}%, rgba(255,255,255,0.1) 100%)`
: "rgba(255,255,255,0.1)"
}}
/>
<div className="flex justify-between text-white/40 text-xs mt-2">
<span>$0</span>
<span>{formatCurrency(maxBorrow, 0, "$", false)}</span>
</div>
</div>
{maxBorrow <= 0 && parsedSandboxCollateral === 0 && (
<p className="text-white/40 text-xs mt-2 text-center">
Enter collateral amount to enable borrowing
</p>
)}
</div>
</div>
) : (
<>
Expand Down
14 changes: 8 additions & 6 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export function formatAmount(
const abs = Math.abs(num);
// Tiny positive numbers: show '< 0.0001' for values > 0 and < 0.0001
if (abs > 0 && abs < 0.0001) return `< 0.0001`;
// Cap maximum decimals to 4 to avoid very long fractional displays
// Cap decimals to valid range (0-4) and ensure min <= max
const maxDecimals = Math.min(4, Math.max(0, decimals));
return num.toLocaleString("en-US", {
minimumFractionDigits: 2,
minimumFractionDigits: Math.min(2, maxDecimals),
maximumFractionDigits: maxDecimals,
});
}
Expand All @@ -51,9 +51,10 @@ export function formatCurrency(
return `${currencySymbol}${abbreviateNumber(num, 1)}`;
}
// For smaller numbers, show with requested decimals (no grouping abbreviation)
const safeDecimals = Math.max(0, decimals);
return `${currencySymbol}${num.toLocaleString("en-US", {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals,
minimumFractionDigits: safeDecimals,
maximumFractionDigits: safeDecimals,
})}`;
}

Expand All @@ -64,9 +65,10 @@ export function formatPercent(
if (value === null || value === undefined) return `0%`;
const num = Number(value);
if (!Number.isFinite(num)) return `0%`;
const safeDecimals = Math.max(0, decimals);
return `${Number(value).toLocaleString("en-US", {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals,
minimumFractionDigits: safeDecimals,
maximumFractionDigits: safeDecimals,
})}%`;
}

Expand Down
30 changes: 30 additions & 0 deletions test/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ describe("formatAmount", () => {
it("should respect custom decimals", () => {
expect(formatAmount(1.123456, 2)).toBe("1.12");
});

it("should not throw RangeError when decimals is 0", () => {
expect(() => formatAmount(100000, 0)).not.toThrow();
expect(formatAmount(100000, 0)).toBe("100,000");
});

it("should not throw RangeError when decimals is 1", () => {
expect(() => formatAmount(1234.5, 1)).not.toThrow();
expect(formatAmount(1234.5, 1)).toBe("1,234.5");
});

it("should not throw RangeError when decimals is negative", () => {
expect(() => formatAmount(1234, -1)).not.toThrow();
expect(formatAmount(1234, -1)).toBe("1,234");
});
});

describe("formatCurrency", () => {
Expand Down Expand Up @@ -110,6 +125,16 @@ describe("formatCurrency", () => {
it("should not abbreviate when disabled", () => {
expect(formatCurrency(5000, 2, "$", false)).toBe("$5,000.00");
});

it("should not throw RangeError when decimals is 0", () => {
expect(() => formatCurrency(100, 0)).not.toThrow();
expect(formatCurrency(100, 0, "$", false)).toBe("$100");
});

it("should not throw RangeError when decimals is negative", () => {
expect(() => formatCurrency(100, -1)).not.toThrow();
expect(formatCurrency(100, -1, "$", false)).toBe("$100");
});
});

describe("formatPercent", () => {
Expand All @@ -132,6 +157,11 @@ describe("formatPercent", () => {
it("should respect custom decimals", () => {
expect(formatPercent(33.3333, 1)).toBe("33.3%");
});

it("should not throw RangeError when decimals is negative", () => {
expect(() => formatPercent(50, -1)).not.toThrow();
expect(formatPercent(50, -1)).toBe("50%");
});
});

describe("maskString", () => {
Expand Down
Loading