Skip to content

Commit bdfdb42

Browse files
author
Luka Jeran
committed
Fix rounding issue when converting to wei
1 parent 1cf1f8d commit bdfdb42

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

packages/nextjs/components/scaffold-eth/Contract/WriteOnlyFunctionForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const WriteOnlyFunctionForm = ({
3333
}: WriteOnlyFunctionFormProps) => {
3434
const mainChainId = useAbiNinjaState(state => state.mainChainId);
3535
const [form, setForm] = useState<Record<string, any>>(() => getInitialFormState(abiFunction));
36-
const [txValue, setTxValue] = useState<string | bigint>("");
36+
const [txValue, setTxValue] = useState<string>("");
3737
const { chain } = useAccount();
3838
const writeTxn = useTransactor();
3939
const { address: connectedAddress } = useAccount();

packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useCallback, useEffect, useState } from "react";
2+
import { parseEther } from "viem";
23
import { CommonInputProps, InputBase, IntegerVariant, isValidInteger } from "~~/components/scaffold-eth";
34

4-
type IntegerInputProps = CommonInputProps<string | bigint> & {
5+
type IntegerInputProps = CommonInputProps<string> & {
56
variant?: IntegerVariant;
67
disableMultiplyBy1e18?: boolean;
78
};
@@ -20,14 +21,11 @@ export const IntegerInput = ({
2021
if (!value) {
2122
return;
2223
}
23-
if (typeof value === "bigint") {
24-
return onChange(value * 10n ** 18n);
25-
}
26-
return onChange(BigInt(Math.round(Number(value) * 10 ** 18)));
24+
return onChange(parseEther(value).toString());
2725
}, [onChange, value]);
2826

2927
useEffect(() => {
30-
if (isValidInteger(variant, value, false)) {
28+
if (isValidInteger(variant, value)) {
3129
setInputError(false);
3230
} else {
3331
setInputError(true);

packages/nextjs/components/scaffold-eth/Input/utils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export enum IntegerVariant {
7676
export const SIGNED_NUMBER_REGEX = /^-?\d+\.?\d*$/;
7777
export const UNSIGNED_NUMBER_REGEX = /^\.?\d+\.?\d*$/;
7878

79-
export const isValidInteger = (dataType: IntegerVariant, value: bigint | string, strict = true) => {
79+
export const isValidInteger = (dataType: IntegerVariant, value: string) => {
8080
const isSigned = dataType.startsWith("i");
8181
const bitcount = Number(dataType.substring(isSigned ? 3 : 4));
8282

@@ -85,9 +85,6 @@ export const isValidInteger = (dataType: IntegerVariant, value: bigint | string,
8585
valueAsBigInt = BigInt(value);
8686
} catch (e) {}
8787
if (typeof valueAsBigInt !== "bigint") {
88-
if (strict) {
89-
return false;
90-
}
9188
if (!value || typeof value !== "string") {
9289
return true;
9390
}

0 commit comments

Comments
 (0)