From 49e043e594ecf1697f1c0b2461785f4fc290e9a5 Mon Sep 17 00:00:00 2001 From: bluecco Date: Tue, 18 Jun 2024 09:34:31 +0200 Subject: [PATCH 1/2] chore: udate starknetjs and small fixes --- package.json | 2 +- pnpm-lock.yaml | 2 +- src/app/starknetkitNext/page.tsx | 3 +++ src/components/Actions/Mint.tsx | 1 - src/components/Actions/Transfer.tsx | 3 ++- src/hooks/useWaitForTx.ts | 28 ++++++++++++++-------------- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 6f9d763..dd97612 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "popmotion": "^11.0.5", "react": "^18.3.1", "react-dom": "^18.3.1", - "starknet": "^6.7.0", + "starknet": "^6.9.0", "starknetkit-latest": "npm:starknetkit@^1.1.9", "starknetkit-next": "npm:starknetkit@^2.2.9" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f940a51..2cbff59 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -54,7 +54,7 @@ importers: specifier: ^18.3.1 version: 18.3.1(react@18.3.1) starknet: - specifier: ^6.7.0 + specifier: ^6.9.0 version: 6.9.0 starknetkit-latest: specifier: npm:starknetkit@^1.1.9 diff --git a/src/app/starknetkitNext/page.tsx b/src/app/starknetkitNext/page.tsx index 0eaa373..dad7b2c 100644 --- a/src/app/starknetkitNext/page.tsx +++ b/src/app/starknetkitNext/page.tsx @@ -16,6 +16,7 @@ import { WalletRpcMsgContainer } from "@/components/Actions/WalletRpcMsgContaine import { DisconnectButton } from "@/components/DisconnectButton" import { Section } from "@/components/Section" import { ARGENT_WEBWALLET_URL } from "@/constants" +import { useWaitForTx } from "@/hooks/useWaitForTx" import { connectorAtom, connectorDataAtom, @@ -35,6 +36,8 @@ export default function StarknetkitLatest() { const setConnector = useSetAtom(connectorAtom) const navigate = useRouter() + useWaitForTx() + useEffect(() => { const autoConnect = async () => { const { diff --git a/src/components/Actions/Mint.tsx b/src/components/Actions/Mint.tsx index 5ebb750..afc0a62 100644 --- a/src/components/Actions/Mint.tsx +++ b/src/components/Actions/Mint.tsx @@ -1,4 +1,3 @@ -import { useWaitForTx } from "@/hooks/useWaitForTx" import { mintToken, mintTokenRcpMethod } from "@/services/mint" import { walletStarknetkitLatestAtom } from "@/state/connectedWalletStarknetkitLatest" import { walletStarknetkitNextAtom } from "@/state/connectedWalletStarknetkitNext" diff --git a/src/components/Actions/Transfer.tsx b/src/components/Actions/Transfer.tsx index 497be55..2d40218 100644 --- a/src/components/Actions/Transfer.tsx +++ b/src/components/Actions/Transfer.tsx @@ -1,4 +1,3 @@ -import { useWaitForTx } from "@/hooks/useWaitForTx" import { transfer, transferJSONRpcMethod } from "@/services/transfer" import { walletStarknetkitLatestAtom } from "@/state/connectedWalletStarknetkitLatest" import { walletStarknetkitNextAtom } from "@/state/connectedWalletStarknetkitNext" @@ -43,9 +42,11 @@ const Transfer: FC = ({ account, wallet }) => { e.preventDefault() setTransactionStatus("approve") + const { transaction_hash } = account ? await transfer(account as Account, transferTo, transferAmount) : await transferJSONRpcMethod(wallet, transferTo, transferAmount) + setLastTransactionHash(transaction_hash) setTransactionStatus("pending") setTransferAmount("") diff --git a/src/hooks/useWaitForTx.ts b/src/hooks/useWaitForTx.ts index 9fa93dc..b1325c6 100644 --- a/src/hooks/useWaitForTx.ts +++ b/src/hooks/useWaitForTx.ts @@ -1,11 +1,17 @@ import { provider } from "@/constants" -import { useEffect, useState } from "react" +import { + lastTxErrorAtom, + lastTxHashAtom, + lastTxStatusAtom, +} from "@/state/transactionState" +import { useAtom, useAtomValue, useSetAtom } from "jotai" +import { useEffect } from "react" import { GatewayError } from "starknet" const useWaitForTx = () => { - const [lastTxHash, setLastTxHash] = useState("") - const [lastTxStatus, setLastTxStatus] = useState("idle") - const [lastTxError, setLastTxError] = useState("") + const lastTxHash = useAtomValue(lastTxHashAtom) + const [lastTxStatus, setLastTxStatus] = useAtom(lastTxStatusAtom) + const setLastTxError = useSetAtom(lastTxErrorAtom) useEffect(() => { const waitTx = async () => { @@ -24,17 +30,11 @@ const useWaitForTx = () => { } } } - waitTx() - }, [lastTxStatus, lastTxHash]) - return { - lastTxHash, - setLastTxHash, - lastTxError, - setLastTxError, - lastTxStatus, - setLastTxStatus, - } + if (lastTxHash && lastTxStatus === "pending") { + waitTx() + } + }, [lastTxStatus, lastTxHash]) } export { useWaitForTx } From e22666df120e9ec71ae39a27cf89582e40a6d790 Mon Sep 17 00:00:00 2001 From: bluecco Date: Tue, 18 Jun 2024 09:36:25 +0200 Subject: [PATCH 2/2] fix: state tx --- src/app/withStarknetReactLatest/page.tsx | 3 +++ src/app/withStarknetReactNext/page.tsx | 1 + 2 files changed, 4 insertions(+) diff --git a/src/app/withStarknetReactLatest/page.tsx b/src/app/withStarknetReactLatest/page.tsx index a74eebc..4079564 100644 --- a/src/app/withStarknetReactLatest/page.tsx +++ b/src/app/withStarknetReactLatest/page.tsx @@ -9,6 +9,7 @@ import { Section } from "@/components/Section" import { ConnectStarknetReact } from "@/components/connect/ConnectStarknetReact" import { CHAIN_ID } from "@/constants" import { availableConnectors } from "@/helpers/connectorsLatest" +import { useWaitForTx } from "@/hooks/useWaitForTx" import { Flex } from "@chakra-ui/react" import { mainnet, sepolia } from "@starknet-react/chains" import { @@ -28,6 +29,8 @@ const StarknetReactDappContent = () => { undefined, ) + useWaitForTx() + useEffect(() => { const getChainId = async () => { setChainId(await account?.getChainId()) diff --git a/src/app/withStarknetReactNext/page.tsx b/src/app/withStarknetReactNext/page.tsx index f4eb1ae..ba9b259 100644 --- a/src/app/withStarknetReactNext/page.tsx +++ b/src/app/withStarknetReactNext/page.tsx @@ -1,3 +1,4 @@ export default function StarknetReactNext() { + //useWaitForTx() return <>TODO: wait for starknet-react v3 }