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

use caret for burner-connector versioning #17

Merged
merged 4 commits into from
May 1, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/curvy-drinks-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-eth": patch
---

add useWatchBalance hook
16 changes: 3 additions & 13 deletions templates/base/packages/nextjs/components/scaffold-eth/Balance.tsx
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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,
});

Expand All @@ -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 (
<div className="animate-pulse flex space-x-4">
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 1 addition & 1 deletion templates/base/packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
carletex marked this conversation as resolved.
Show resolved Hide resolved
"daisyui": "4.5.0",
"next": "~14.0.4",
"next-themes": "~0.2.1",
Expand Down