Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding issue when converting to wei #159

Merged
merged 1 commit into from
Sep 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const WriteOnlyFunctionForm = ({
}: WriteOnlyFunctionFormProps) => {
const mainChainId = useAbiNinjaState(state => state.mainChainId);
const [form, setForm] = useState<Record<string, any>>(() => getInitialFormState(abiFunction));
const [txValue, setTxValue] = useState<string | bigint>("");
const [txValue, setTxValue] = useState<string>("");
const { chain } = useAccount();
const writeTxn = useTransactor();
const { address: connectedAddress } = useAccount();
Expand Down
10 changes: 4 additions & 6 deletions packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx
Original file line number Diff line number Diff line change
@@ -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<string | bigint> & {
type IntegerInputProps = CommonInputProps<string> & {
variant?: IntegerVariant;
disableMultiplyBy1e18?: boolean;
};
Expand All @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions packages/nextjs/components/scaffold-eth/Input/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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;
}
Expand Down
Loading