From bdfdb42bb17b96a7d45d0430fc292263bf905e4d Mon Sep 17 00:00:00 2001 From: Luka Jeran Date: Mon, 23 Sep 2024 09:22:26 +0200 Subject: [PATCH] Fix rounding issue when converting to wei --- .../scaffold-eth/Contract/WriteOnlyFunctionForm.tsx | 2 +- .../components/scaffold-eth/Input/IntegerInput.tsx | 10 ++++------ packages/nextjs/components/scaffold-eth/Input/utils.ts | 5 +---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/nextjs/components/scaffold-eth/Contract/WriteOnlyFunctionForm.tsx b/packages/nextjs/components/scaffold-eth/Contract/WriteOnlyFunctionForm.tsx index b5360377..1e45bc25 100644 --- a/packages/nextjs/components/scaffold-eth/Contract/WriteOnlyFunctionForm.tsx +++ b/packages/nextjs/components/scaffold-eth/Contract/WriteOnlyFunctionForm.tsx @@ -33,7 +33,7 @@ export const WriteOnlyFunctionForm = ({ }: WriteOnlyFunctionFormProps) => { const mainChainId = useAbiNinjaState(state => state.mainChainId); const [form, setForm] = useState>(() => getInitialFormState(abiFunction)); - const [txValue, setTxValue] = useState(""); + const [txValue, setTxValue] = useState(""); const { chain } = useAccount(); const writeTxn = useTransactor(); const { address: connectedAddress } = useAccount(); diff --git a/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx b/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx index 7e71e761..8a10ec08 100644 --- a/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx +++ b/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx @@ -1,7 +1,8 @@ import { useCallback, useEffect, useState } from "react"; +import { parseEther } from "viem"; import { CommonInputProps, InputBase, IntegerVariant, isValidInteger } from "~~/components/scaffold-eth"; -type IntegerInputProps = CommonInputProps & { +type IntegerInputProps = CommonInputProps & { variant?: IntegerVariant; disableMultiplyBy1e18?: boolean; }; @@ -20,14 +21,11 @@ export const IntegerInput = ({ if (!value) { return; } - if (typeof value === "bigint") { - return onChange(value * 10n ** 18n); - } - return onChange(BigInt(Math.round(Number(value) * 10 ** 18))); + return onChange(parseEther(value).toString()); }, [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 481b5d16..cc49f9a9 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; }