From 810a50b265fc735ff1b00267e8580740ae33bb17 Mon Sep 17 00:00:00 2001 From: Shiv Bhonde Date: Wed, 1 May 2024 15:01:34 +0530 Subject: [PATCH 1/3] use caret for burner-connector versioning --- templates/base/packages/nextjs/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/base/packages/nextjs/package.json b/templates/base/packages/nextjs/package.json index 3ec055b17..442d02eb9 100644 --- a/templates/base/packages/nextjs/package.json +++ b/templates/base/packages/nextjs/package.json @@ -20,7 +20,7 @@ "@uniswap/sdk-core": "~4.0.1", "@uniswap/v2-sdk": "~3.0.1", "blo": "~1.0.1", - "burner-connector": "~0.0.3", + "burner-connector": "^0.0.3", "daisyui": "4.5.0", "next": "~14.0.4", "next-themes": "~0.2.1", From e08fca0942d864df943f688898b4182020852ee7 Mon Sep 17 00:00:00 2001 From: Rinat Date: Wed, 1 May 2024 14:13:49 +0200 Subject: [PATCH 2/3] add useWatchBalance hook (#829) --- .../components/scaffold-eth/Balance.tsx | 16 +++----------- .../components/scaffold-eth/FaucetButton.tsx | 20 ++++-------------- .../hooks/scaffold-eth/useWatchBalance.ts | 21 +++++++++++++++++++ 3 files changed, 28 insertions(+), 29 deletions(-) create mode 100644 packages/nextjs/hooks/scaffold-eth/useWatchBalance.ts diff --git a/packages/nextjs/components/scaffold-eth/Balance.tsx b/packages/nextjs/components/scaffold-eth/Balance.tsx index 0108e953a..c10c24f7e 100644 --- a/packages/nextjs/components/scaffold-eth/Balance.tsx +++ b/packages/nextjs/components/scaffold-eth/Balance.tsx @@ -1,10 +1,9 @@ "use client"; -import { useEffect, useState } from "react"; -import { useQueryClient } from "@tanstack/react-query"; +import { useState } from "react"; import { Address, formatEther } from "viem"; -import { useBalance, useBlockNumber } from "wagmi"; import { useTargetNetwork } from "~~/hooks/scaffold-eth/useTargetNetwork"; +import { useWatchBalance } from "~~/hooks/scaffold-eth/useWatchBalance"; import { useGlobalState } from "~~/services/store/store"; type BalanceProps = { @@ -18,16 +17,12 @@ type BalanceProps = { */ export const Balance = ({ address, className = "", usdMode }: BalanceProps) => { const { targetNetwork } = useTargetNetwork(); - - const queryClient = useQueryClient(); - const { data: blockNumber } = useBlockNumber({ watch: true, chainId: targetNetwork.id }); const price = useGlobalState(state => state.nativeCurrencyPrice); const { data: balance, isError, isLoading, - queryKey, - } = useBalance({ + } = useWatchBalance({ address, }); @@ -39,11 +34,6 @@ export const Balance = ({ address, className = "", usdMode }: BalanceProps) => { } }; - useEffect(() => { - queryClient.invalidateQueries({ queryKey }); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [blockNumber]); - if (!address || isLoading || balance === null) { return (
diff --git a/packages/nextjs/components/scaffold-eth/FaucetButton.tsx b/packages/nextjs/components/scaffold-eth/FaucetButton.tsx index 5b91bbcdd..2246f8275 100644 --- a/packages/nextjs/components/scaffold-eth/FaucetButton.tsx +++ b/packages/nextjs/components/scaffold-eth/FaucetButton.tsx @@ -1,14 +1,12 @@ "use client"; -import { useEffect, useState } from "react"; -import { useQueryClient } from "@tanstack/react-query"; +import { useState } from "react"; import { createWalletClient, http, parseEther } from "viem"; import { hardhat } from "viem/chains"; -import { useAccount, useBlockNumber } from "wagmi"; -import { useBalance } from "wagmi"; +import { useAccount } from "wagmi"; import { BanknotesIcon } from "@heroicons/react/24/outline"; import { useTransactor } from "~~/hooks/scaffold-eth"; -import { useTargetNetwork } from "~~/hooks/scaffold-eth/useTargetNetwork"; +import { useWatchBalance } from "~~/hooks/scaffold-eth/useWatchBalance"; // Number of ETH faucet sends to an address const NUM_OF_ETH = "1"; @@ -24,23 +22,13 @@ const localWalletClient = createWalletClient({ */ export const FaucetButton = () => { const { address, chain: ConnectedChain } = useAccount(); - const { targetNetwork } = useTargetNetwork(); - const queryClient = useQueryClient(); - const { data: blockNumber } = useBlockNumber({ watch: true, chainId: targetNetwork.id }); - const { data: balance, queryKey } = useBalance({ - address, - }); + const { data: balance } = useWatchBalance({ address }); const [loading, setLoading] = useState(false); const faucetTxn = useTransactor(localWalletClient); - useEffect(() => { - queryClient.invalidateQueries({ queryKey }); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [blockNumber]); - const sendETH = async () => { try { setLoading(true); diff --git a/packages/nextjs/hooks/scaffold-eth/useWatchBalance.ts b/packages/nextjs/hooks/scaffold-eth/useWatchBalance.ts new file mode 100644 index 000000000..26acda9bf --- /dev/null +++ b/packages/nextjs/hooks/scaffold-eth/useWatchBalance.ts @@ -0,0 +1,21 @@ +import { useEffect } from "react"; +import { useTargetNetwork } from "./useTargetNetwork"; +import { useQueryClient } from "@tanstack/react-query"; +import { UseBalanceParameters, useBalance, useBlockNumber } from "wagmi"; + +/** + * Wrapper around wagmi's useBalance hook. Updates data on every block change. + */ +export const useWatchBalance = (useBalanceParameters: UseBalanceParameters) => { + const { targetNetwork } = useTargetNetwork(); + const queryClient = useQueryClient(); + const { data: blockNumber } = useBlockNumber({ watch: true, chainId: targetNetwork.id }); + const { queryKey, ...restUseBalanceReturn } = useBalance(useBalanceParameters); + + useEffect(() => { + queryClient.invalidateQueries({ queryKey }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [blockNumber]); + + return restUseBalanceReturn; +}; From b3159d07ad73063da580cd7b8af11ed325695ea7 Mon Sep 17 00:00:00 2001 From: Shiv Bhonde Date: Wed, 1 May 2024 18:19:41 +0530 Subject: [PATCH 3/3] add changeset --- .changeset/curvy-drinks-wave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/curvy-drinks-wave.md diff --git a/.changeset/curvy-drinks-wave.md b/.changeset/curvy-drinks-wave.md new file mode 100644 index 000000000..69afbeae0 --- /dev/null +++ b/.changeset/curvy-drinks-wave.md @@ -0,0 +1,5 @@ +--- +"create-eth": patch +--- + +add useWatchBalance hook