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

chore: update sui token decimals #475

Merged
merged 2 commits into from
Dec 17, 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 @@ -7,7 +7,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { uniq, without } from "rambda";
import { z } from "zod";

import { useAccount } from "~/lib/hooks";
import { SUI_CHAIN_ID, useAccount, useChainId } from "~/lib/hooks";
import { logger } from "~/lib/logger";
import {
hex64Literal,
Expand All @@ -26,6 +26,7 @@ const TOKEN_DETAILS_FORM_SCHEMA = z.object({
});

export type TokenDetailsFormState = z.infer<typeof TOKEN_DETAILS_FORM_SCHEMA>;
const MAX_UINT64 = BigInt(2) ** BigInt(64) - BigInt(1);

export type DeployAndRegisterTransactionState =
| {
Expand Down Expand Up @@ -86,6 +87,7 @@ function useInterchainTokenDeploymentState(
});

const { address } = useAccount();
const chainId = useChainId();

/**
* Generate a random salt on first render
Expand All @@ -97,6 +99,10 @@ function useInterchainTokenDeploymentState(
const salt = generateRandomHash();

tokenDetailsForm.setValue("salt", salt);

if (chainId === SUI_CHAIN_ID) {
tokenDetailsForm.setValue("tokenDecimals", 9);
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[address]
Expand Down Expand Up @@ -147,6 +153,10 @@ function useInterchainTokenDeploymentState(
// reset form
tokenDetailsForm.reset(initialState.tokenDetails);

if (chainId === SUI_CHAIN_ID) {
tokenDetailsForm.setValue("tokenDecimals", 9);
}

tokenDetailsForm.setValue("salt", generateRandomHash());
// tokenDetailsForm.setValue("minter", address);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useInterchainTokenDeploymentStateContainer,
type TokenDetailsFormState,
} from "~/features/InterchainTokenDeployment";
import { SUI_CHAIN_ID, useChainId } from "~/lib/hooks";
import {
isValidEVMAddress,
preventNonHexInput,
Expand All @@ -29,8 +30,11 @@ import {
ValidationError,
} from "~/ui/compounds/MultiStepForm";

const MAX_UINT64 = BigInt(2) ** BigInt(64) - BigInt(1);

const TokenDetails: FC = () => {
const { state, actions } = useInterchainTokenDeploymentStateContainer();
const chainId = useChainId();

const { register, handleSubmit, formState, watch } = state.tokenDetailsForm;

Expand All @@ -40,6 +44,7 @@ const TokenDetails: FC = () => {
const isMintable = watch("isMintable");
const minter = watch("minter");
const supply = watch("initialSupply");
const tokenDecimals = watch("tokenDecimals");

const minterErrorMessage = useMemo<FieldError | undefined>(() => {
if (!isMintable) {
Expand Down Expand Up @@ -72,7 +77,18 @@ const TokenDetails: FC = () => {
message: "Fixed supply token requires an initial balance",
};
}
}, [isMintable, minter, supply]);

if (
chainId === SUI_CHAIN_ID &&
supply &&
BigInt(supply) * BigInt(10) ** BigInt(tokenDecimals) > MAX_UINT64
) {
return {
type: "validate",
message: "Supply must be less than 2^64 - 1 for Sui",
};
}
}, [isMintable, minter, supply, chainId, tokenDecimals]);

const isFormValid = useMemo(() => {
if (minterErrorMessage || initialSupplyErrorMessage) {
Expand Down
2 changes: 1 addition & 1 deletion apps/maestro/src/lib/hooks/useChainId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useChainId as useWagmiChainId } from "wagmi";
// Sui's chain ID
import { NEXT_PUBLIC_NETWORK_ENV } from "~/config/env";

const SUI_CHAIN_ID = NEXT_PUBLIC_NETWORK_ENV === "mainnet" ? 101 : 103;
export const SUI_CHAIN_ID = NEXT_PUBLIC_NETWORK_ENV === "mainnet" ? 101 : 103;

// TODO: check if this is the best way to use chain ids, maybe we should combine it with chain type
export function useChainId(): number {
Expand Down
Loading