Skip to content

Commit

Permalink
chore: update sui token decimals (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
npty authored Dec 17, 2024
1 parent df15fbe commit 9c6db01
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
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

0 comments on commit 9c6db01

Please sign in to comment.