From e2ae962c6938dd5d7e58fcf30b92b63228cb8091 Mon Sep 17 00:00:00 2001 From: Matt Foyle Date: Fri, 20 Sep 2024 11:30:28 +0200 Subject: [PATCH] fix: fvm process --- packages/nextjs/app/fvm/page.tsx | 144 ++++++++++++++++++++++--------- packages/nextjs/lib/networks.ts | 13 +++ 2 files changed, 116 insertions(+), 41 deletions(-) diff --git a/packages/nextjs/app/fvm/page.tsx b/packages/nextjs/app/fvm/page.tsx index 14d59e5..9795f47 100644 --- a/packages/nextjs/app/fvm/page.tsx +++ b/packages/nextjs/app/fvm/page.tsx @@ -2,16 +2,23 @@ import { useEffect, useState } from "react"; import { CONTRACT_ABI, CONTRACT_ADDRESS } from "./smartcontract"; -import { ExternalLinkIcon, getNetwork, useDynamicContext, useSwitchNetwork } from "@dynamic-labs/sdk-react-core"; +import { + ExternalLinkIcon, + getNetwork, + useDynamicContext, + useIsLoggedIn, + useSwitchNetwork, + useWalletConnectorEvent, +} from "@dynamic-labs/sdk-react-core"; import lighthouse from "@lighthouse-web3/sdk"; import { ethers } from "ethers"; import { filecoinCalibration } from "viem/chains"; -import { useAccount, useWaitForTransactionReceipt, useWriteContract } from "wagmi"; +import { useWaitForTransactionReceipt, useWriteContract } from "wagmi"; import { notification } from "~~/utils/scaffold-eth"; const FVMPage = () => { - const { isConnected } = useAccount(); - const { primaryWallet, isAuthenticated } = useDynamicContext(); + const isLoggedIn = useIsLoggedIn(); + const { primaryWallet } = useDynamicContext(); const switchNetwork = useSwitchNetwork(); const { data: hash, isPending, writeContract } = useWriteContract(); @@ -24,6 +31,11 @@ const FVMPage = () => { const [loading, setLoading] = useState(false); const [step, setStep] = useState(0); const [error, setError] = useState(null); + const [isSubmitting, setIsSubmitting] = useState(false); + + useWalletConnectorEvent(primaryWallet?.connector, "chainChange", async ({ chain }) => { + setNetwork(parseInt(chain)); + }); const fetchNetwork = async () => { if (!primaryWallet) return; @@ -68,6 +80,7 @@ const FVMPage = () => { }; const handleSubmitRaas = async () => { + setIsSubmitting(true); try { const fileLinkBytes = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(hostedLink)); await writeContract({ @@ -76,18 +89,19 @@ const FVMPage = () => { functionName: "submitRaaS", args: [fileLinkBytes, 2, 4, 40], }); + setStep(2); // Move to step 3 after successful submission } catch (err) { setError("Failed to submit file."); console.error(err); } finally { - setStep(2); + setIsSubmitting(false); } }; return ( <>
- {isConnected && isAuthenticated && network !== filecoinCalibration.id ? ( + {isLoggedIn && primaryWallet && network !== filecoinCalibration.id ? (
- ) : isConnected && isAuthenticated ? ( + ) : isLoggedIn && primaryWallet ? (
- {/* STEP 1 : Upload File to Lighthouse Storage */} + {/* STEP 0 : Upload File to Lighthouse Storage */} {step == 0 && (

Data Storage on FVM

@@ -163,49 +177,97 @@ const FVMPage = () => {
)} - {/* STEP 2 : Attaching a RaaS worker on-demand to trigger storage deals for files uploaded through the Lighthouse Smart Contract on FVM */} + {/* STEP 1 : Attaching a RaaS worker on-demand */} {step == 1 && ( -
-
- Card Image -
-

RaaS - Renew, Repair, Replication

-

- In the below section we interact with the smart contract on FVM Calibration network by - submitting a CID of the file we uploaded in previous section to the submitRaaS function. This - will create a new deal request that the Lighthouse RaaS Worker will pick up and initiate - deals. -

-
- + {isPending && ( +

+ Transaction submitted. Waiting for confirmation... +

+ )} + + - - - -
+
)} - {/* STEP 3 : View Transaction On Explorer */} - {hash && ( - <> - - - - + {/* STEP 2: Transaction Submitted, Waiting for Hash */} + {step == 2 && ( +
+

Transaction Submitted Successfully

+

+ Your RaaS submission has been processed. {!hash && "Waiting for transaction hash..."} +

+ {hash ? ( + + + + ) : ( +
+
+ Waiting for transaction hash... +
+ )} +
)}
) : ( - )} diff --git a/packages/nextjs/lib/networks.ts b/packages/nextjs/lib/networks.ts index 61759fa..397d8b5 100644 --- a/packages/nextjs/lib/networks.ts +++ b/packages/nextjs/lib/networks.ts @@ -1,4 +1,17 @@ export const customEvmNetworks = [ + { + blockExplorerUrls: ["https://gnosisscan.io"], + chainId: 100, + name: "Gnosis Chain", + rpcUrls: ["https://rpc.gnosischain.com"], + iconUrls: ["https://avatars.githubusercontent.com/u/92709226?s=280&v=4"], + nativeCurrency: { + name: "XDAI", + symbol: "XDAI", + decimals: 18, + }, + networkId: 100, + }, { blockExplorerUrls: ["https://filfox.info/en"], chainId: 314,