From d74e7973de287487f0c493f135d64bef5e38f47e Mon Sep 17 00:00:00 2001 From: lukaw3d Date: Mon, 16 Sep 2024 20:19:03 +0200 Subject: [PATCH 1/3] Fix rounding issue when converting to wei --- packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx b/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx index 4fde183e0..496c51126 100644 --- a/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx +++ b/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx @@ -1,4 +1,5 @@ import { useCallback, useEffect, useState } from "react"; +import { parseUnits } from "viem"; import { CommonInputProps, InputBase, IntegerVariant, isValidInteger } from "~~/components/scaffold-eth"; type IntegerInputProps = CommonInputProps & { @@ -23,7 +24,7 @@ export const IntegerInput = ({ if (typeof value === "bigint") { return onChange(value * 10n ** 18n); } - return onChange(BigInt(Math.round(Number(value) * 10 ** 18))); + return onChange(parseUnits(value, 18)); }, [onChange, value]); useEffect(() => { From 950ba9aeedfdcd4dcd865eaa36c9ab7df3495556 Mon Sep 17 00:00:00 2001 From: lukaw3d Date: Thu, 19 Sep 2024 00:18:51 +0200 Subject: [PATCH 2/3] Simplify IntegerInput: remove bigint type --- .../debug/_components/contract/WriteOnlyFunctionForm.tsx | 2 +- .../components/scaffold-eth/Input/IntegerInput.tsx | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/app/debug/_components/contract/WriteOnlyFunctionForm.tsx b/packages/nextjs/app/debug/_components/contract/WriteOnlyFunctionForm.tsx index b8e8f8490..c7b0e6740 100644 --- a/packages/nextjs/app/debug/_components/contract/WriteOnlyFunctionForm.tsx +++ b/packages/nextjs/app/debug/_components/contract/WriteOnlyFunctionForm.tsx @@ -33,7 +33,7 @@ export const WriteOnlyFunctionForm = ({ inheritedFrom, }: WriteOnlyFunctionFormProps) => { const [form, setForm] = useState>(() => getInitialFormState(abiFunction)); - const [txValue, setTxValue] = useState(""); + const [txValue, setTxValue] = useState(""); const { chain } = useAccount(); const writeTxn = useTransactor(); const { targetNetwork } = useTargetNetwork(); diff --git a/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx b/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx index 496c51126..5ec38be60 100644 --- a/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx +++ b/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx @@ -1,8 +1,8 @@ import { useCallback, useEffect, useState } from "react"; -import { parseUnits } from "viem"; +import { parseEther } from "viem"; import { CommonInputProps, InputBase, IntegerVariant, isValidInteger } from "~~/components/scaffold-eth"; -type IntegerInputProps = CommonInputProps & { +type IntegerInputProps = CommonInputProps & { variant?: IntegerVariant; disableMultiplyBy1e18?: boolean; }; @@ -21,10 +21,7 @@ export const IntegerInput = ({ if (!value) { return; } - if (typeof value === "bigint") { - return onChange(value * 10n ** 18n); - } - return onChange(parseUnits(value, 18)); + return onChange(parseEther(value).toString()); }, [onChange, value]); useEffect(() => { From a15ed62d91c6ff9c17b5bc7628c3e5c359794edf Mon Sep 17 00:00:00 2001 From: Shiv Bhonde Date: Mon, 23 Sep 2024 15:10:29 +0800 Subject: [PATCH 3/3] remove bignInt possiblity in input for isValidInteger and remove strict mode --- .../nextjs/components/scaffold-eth/Input/IntegerInput.tsx | 2 +- packages/nextjs/components/scaffold-eth/Input/utils.ts | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx b/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx index 5ec38be60..bccc94fac 100644 --- a/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx +++ b/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx @@ -25,7 +25,7 @@ export const IntegerInput = ({ }, [onChange, value]); useEffect(() => { - if (isValidInteger(variant, value, false)) { + if (isValidInteger(variant, value)) { setInputError(false); } else { setInputError(true); diff --git a/packages/nextjs/components/scaffold-eth/Input/utils.ts b/packages/nextjs/components/scaffold-eth/Input/utils.ts index 481b5d165..cc49f9a9a 100644 --- a/packages/nextjs/components/scaffold-eth/Input/utils.ts +++ b/packages/nextjs/components/scaffold-eth/Input/utils.ts @@ -76,7 +76,7 @@ export enum IntegerVariant { export const SIGNED_NUMBER_REGEX = /^-?\d+\.?\d*$/; export const UNSIGNED_NUMBER_REGEX = /^\.?\d+\.?\d*$/; -export const isValidInteger = (dataType: IntegerVariant, value: bigint | string, strict = true) => { +export const isValidInteger = (dataType: IntegerVariant, value: string) => { const isSigned = dataType.startsWith("i"); const bitcount = Number(dataType.substring(isSigned ? 3 : 4)); @@ -85,9 +85,6 @@ export const isValidInteger = (dataType: IntegerVariant, value: bigint | string, valueAsBigInt = BigInt(value); } catch (e) {} if (typeof valueAsBigInt !== "bigint") { - if (strict) { - return false; - } if (!value || typeof value !== "string") { return true; }