From 03f4cb987fb869560983e6915b32f870f0b7a0a9 Mon Sep 17 00:00:00 2001 From: Dhaiwat Pandya Date: Wed, 29 Dec 2021 22:20:02 +0530 Subject: [PATCH 1/4] Add check for `context.connected` in `useContract` for readiness --- packages/hooks/src/hooks/useContract.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/hooks/useContract.ts b/packages/hooks/src/hooks/useContract.ts index ff92e2a8..cbcfe996 100644 --- a/packages/hooks/src/hooks/useContract.ts +++ b/packages/hooks/src/hooks/useContract.ts @@ -7,7 +7,7 @@ export function useContract(address: string, abi) { const [contract, setContract] = React.useState({}); const [isReady, setIsReady] = React.useState(false); React.useEffect(() => { - if (context) { + if (context && context.connected) { const newContract = new Contract( address, abi, From 9ebfb407081dbd7e689457035a02f1a0169ff158 Mon Sep 17 00:00:00 2001 From: Dhaiwat Pandya Date: Wed, 29 Dec 2021 22:24:04 +0530 Subject: [PATCH 2/4] Add changeset --- .changeset/rare-days-punch.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/rare-days-punch.md diff --git a/.changeset/rare-days-punch.md b/.changeset/rare-days-punch.md new file mode 100644 index 00000000..f268b97b --- /dev/null +++ b/.changeset/rare-days-punch.md @@ -0,0 +1,5 @@ +--- +'@web3-ui/hooks': patch +--- + +Add check for user connection during contract init in useContract From f92e07804b51d02605269ebd085b97696d0307f2 Mon Sep 17 00:00:00 2001 From: Dhaiwat Pandya Date: Thu, 30 Dec 2021 16:21:19 +0530 Subject: [PATCH 3/4] Update changeset message Co-authored-by: with-heart --- .changeset/rare-days-punch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/rare-days-punch.md b/.changeset/rare-days-punch.md index f268b97b..52d12f27 100644 --- a/.changeset/rare-days-punch.md +++ b/.changeset/rare-days-punch.md @@ -2,4 +2,4 @@ '@web3-ui/hooks': patch --- -Add check for user connection during contract init in useContract +`useContract` now initializes the contract only after the user has connected their wallet. This helps prevent errors from calling contracts without a connection. From 429aeae6000b217eca58fa6f48f4a580c85d25b7 Mon Sep 17 00:00:00 2001 From: Dhaiwat Pandya Date: Thu, 30 Dec 2021 16:21:33 +0530 Subject: [PATCH 4/4] Refactor! Co-authored-by: with-heart --- packages/hooks/src/hooks/useContract.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hooks/src/hooks/useContract.ts b/packages/hooks/src/hooks/useContract.ts index cbcfe996..1c228d70 100644 --- a/packages/hooks/src/hooks/useContract.ts +++ b/packages/hooks/src/hooks/useContract.ts @@ -7,7 +7,7 @@ export function useContract(address: string, abi) { const [contract, setContract] = React.useState({}); const [isReady, setIsReady] = React.useState(false); React.useEffect(() => { - if (context && context.connected) { + if (context?.connected) { const newContract = new Contract( address, abi,