Skip to content

Commit e253e0d

Browse files
committed
MItigate possible precision loss
1 parent d547ab3 commit e253e0d

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/common/lib/numeric.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ export const parseBig = (input: string | number): Big => {
6161
* A locale-aware alternative to {@link parseFloat}.
6262
* Uses {@link parseBig} to handle locale-specific formatting
6363
* and mitigate floating point precision loss.
64+
*
65+
* Also handles cases where the input type may fluctuate in runtime between `string` and `number`.
6466
*/
65-
export const parseNumber = (input: string): number => parseBig(input).toNumber();
67+
export const parseNumber = (input: string | number): number => parseBig(input).toNumber();
6668

6769
export const isBigSource = (value: unknown | BigSource) => {
6870
try {

src/entities/campaign/hooks/forms.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SubmitHandler, useForm, useWatch } from "react-hook-form";
66
import { infer as FromSchema } from "zod";
77

88
import { campaignsContractClient } from "@/common/contracts/core/campaigns";
9-
import { floatToYoctoNear } from "@/common/lib";
9+
import { floatToYoctoNear, parseNumber } from "@/common/lib";
1010
import { CampaignId } from "@/common/types";
1111
import { toast } from "@/common/ui/layout/hooks";
1212
import { useWalletUserSession } from "@/common/wallet";
@@ -41,9 +41,9 @@ export const useCampaignForm = ({ campaignId }: { campaignId?: CampaignId }) =>
4141
const errors: Record<string, { message: string }> = {};
4242

4343
// Convert string values to numbers for comparison
44-
const minAmount = min_amount ? Number(min_amount) : null;
45-
const maxAmount = max_amount ? Number(max_amount) : null;
46-
const targetAmount = target_amount ? Number(target_amount) : null;
44+
const minAmount = min_amount ? parseNumber(min_amount) : null;
45+
const maxAmount = max_amount ? parseNumber(max_amount) : null;
46+
const targetAmount = target_amount ? parseNumber(target_amount) : null;
4747

4848
// Validate min_amount vs max_amount
4949
if (minAmount && maxAmount && minAmount > maxAmount) {

0 commit comments

Comments
 (0)