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

Feat/added switch network option one nft deployment and return to polyzkevm #78

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/components/MintErrorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const MintErrorModal: FC<MintErrorModalProps> = (props) => {
return (
<Modal {...props} title="We encountered an issue" className="max-w-2xl">
<div className="flex flex-col gap-3 text-xl max-w-2xl">
<p>Sorry, we couldn’t deploy your NFT smart countract.</p>
<p>Sorry, we couldn’t deploy your NFT smart contract.</p>
<div className="bg-[#F7F7F9;] mt-1 px-6 py-4 rounded-2xl">
<p>
Try again, and if there are still issues, please contact{" "}
Expand Down
15 changes: 13 additions & 2 deletions src/components/TokenForm/ConfirmStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { IconArrowRight } from "../Icons/IconArrowRight";
import { useCollateralCheck, useDeploy } from "@/hooks/useDeploy";
import config from "@/config/configuration";
import { addProject } from "@/app/actions/add-project";
import { useAccount } from "wagmi";
import { useAccount, useSwitchChain } from "wagmi";
import { checkWhiteList } from "@/services/check-white-list";
import { toast } from "sonner";
import { Address } from "viem";
import { formatCurrencyAmount } from "@/helpers/currency";
import { usePolTokenPrice } from "@/hooks/usePolTokenPrice";
import { useSwitchChainIfNeeded } from "@/hooks/useSwitchChainIfNeeded";

const ConfirmStep: React.FC<{ onNext: () => void; onBack: () => void }> = ({
onNext,
Expand All @@ -22,7 +23,8 @@ const ConfirmStep: React.FC<{ onNext: () => void; onBack: () => void }> = ({
const [loading, setLoading] = useState(false);
const { formData, setFormData } = useTokenFormContext();
const methods = useForm<FormData>();
const { address } = useAccount();
const { address, chainId } = useAccount();
const { switchChainAsync } = useSwitchChain();
const { handleSubmit, formState } = methods;
const { deploy, prep, requestedModules, inverter } = useDeploy();
const collateralCheck = useCollateralCheck();
Expand All @@ -31,10 +33,19 @@ const ConfirmStep: React.FC<{ onNext: () => void; onBack: () => void }> = ({
const collateralUsdValueString: string = polTokenPrice.isSuccess
? formatCurrencyAmount(collateralAmount * polTokenPrice.data)
: "-";
const { switchChainIfNeeded } = useSwitchChainIfNeeded();
const targetChain = config.SUPPORTED_CHAINS[0].id; // PolygonZkEvm chain ID

const onSubmit = async (data: FormData) => {
setLoading(true);

try {
// Return user to polygonZkEvm after deployed NFT contract on polygon
const chainSwitched = await switchChainIfNeeded(targetChain);

// If chain switching failed, stop further execution
if (!chainSwitched) return;

const prepData = await prep.mutateAsync();

if (!address) throw new Error("Address not found");
Expand Down
13 changes: 13 additions & 0 deletions src/components/TokenForm/NFTDeploymentStep.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from "react";
import Image from "next/image";
import { Address } from "viem";
import { useAccount, useSwitchChain } from "wagmi";

import StepNavigation from "./StepNavigation";
import { useTokenFormContext } from "./TokenFormContext";
Expand All @@ -9,6 +10,8 @@ import { MintSuccessModal } from "@/components/MintSuccessModal";
import { MintErrorModal } from "@/components/MintErrorModal";
import { useNFT } from "@/hooks/useNFT";
import { toast } from "sonner";
import config from "@/config/configuration";
import { useSwitchChainIfNeeded } from "@/hooks/useSwitchChainIfNeeded";

interface FormData {
nftContractAddress?: Address;
Expand All @@ -26,11 +29,21 @@ const NFTDeploymentStep: React.FC<{
const methods = useForm<FormData>();
const { handleSubmit, formState } = methods;
const { deploy } = useNFT();
const { chainId } = useAccount();
const { switchChainAsync } = useSwitchChain();
const { switchChainIfNeeded } = useSwitchChainIfNeeded();
const targetNFTChain = config.SUPPORTED_CHAINS[1].id; // Polygon chain ID

const onSubmit = async (data: FormData) => {
try {
setLoading(true);

// Deploy NFT contract on the target chain: Polygon
const chainSwitched = await switchChainIfNeeded(targetNFTChain);

// If chain switching failed, stop further execution
if (!chainSwitched) return;

const nftName = formData.tokenName.trim() + " NFT";
const nftSymbol = formData.tokenTicker.trim() + "NFT";

Expand Down
2 changes: 1 addition & 1 deletion src/config/development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Address } from "viem";
import { Chain, optimismSepolia, baseSepolia } from "wagmi/chains";

const config: EnvConfig = {
SUPPORTED_CHAINS: [baseSepolia] as [Chain],
SUPPORTED_CHAINS: [baseSepolia, baseSepolia] as [Chain, Chain],
COLATERAL_TOKEN: "0x065775C7aB4E60ad1776A30DCfB15325d231Ce4F",
// COLATERAL_SUPPLIER: "0x0000000000000000000000000000000000000000",
GRAPHQL_ENDPOINT:
Expand Down
4 changes: 2 additions & 2 deletions src/config/production.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { EnvConfig } from "@/types/config";
import { Address } from "viem";
import { Chain, polygonZkEvm } from "wagmi/chains";
import { Chain, polygonZkEvm, polygon } from "wagmi/chains";

const config: EnvConfig = {
SUPPORTED_CHAINS: [polygonZkEvm] as [Chain],
SUPPORTED_CHAINS: [polygonZkEvm, polygon] as [Chain, Chain],
COLATERAL_TOKEN: "0x0000000000000000000000000000000000000000" as Address,
// COLATERAL_SUPPLIER: "0x0000000000000000000000000000000000000000",
GRAPHQL_ENDPOINT:
Expand Down
36 changes: 36 additions & 0 deletions src/hooks/useSwitchChainIfNeeded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useAccount, useSwitchChain } from "wagmi";
import { toast } from "sonner";

/**
* Custom React hook to switch the blockchain network if needed.
* This hook fetches the `chainId` from the connected account using wagmi and
* performs a chain switch using `switchChainAsync`.
*
* @param {number} targetChain - The chain ID you want to switch to.
* @returns {Promise<boolean>} - Returns `true` if the switch is successful, otherwise `false`.
*/
export const useSwitchChainIfNeeded = () => {
const { chainId } = useAccount();
const { switchChainAsync } = useSwitchChain();

const switchChainIfNeeded = async (targetChain: number): Promise<boolean> => {
if (chainId !== targetChain) {
try {
const switchChainResponse = await switchChainAsync({
chainId: targetChain,
});

if (switchChainResponse.id !== targetChain) {
toast.error("Failed to switch chain. Please switch manually.");
return false;
}
} catch (error) {
toast.error("Error while switching chain. Please try manually.");
return false;
}
}
return true;
};

return { switchChainIfNeeded };
};
2 changes: 1 addition & 1 deletion src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UserArgs } from "./inverter";
import type { inverterFactoryType } from "@/config/configuration";

export type EnvConfig = {
SUPPORTED_CHAINS: [Chain];
SUPPORTED_CHAINS: [Chain, Chain];
COLATERAL_TOKEN: Address;
COLATERAL_SUPPLIER?: Address;
GRAPHQL_ENDPOINT: string;
Expand Down