-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseAccountBalance.ts
More file actions
34 lines (29 loc) · 990 Bytes
/
useAccountBalance.ts
File metadata and controls
34 lines (29 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { useCallback, useEffect, useState } from "react";
import { useBalance } from "wagmi";
import { useGlobalState } from "~~/services/store/store";
import { getTargetNetwork } from "~~/utils/scaffold-eth";
export function useAccountBalance(address?: string) {
const [isEthBalance, setIsEthBalance] = useState(true);
const [balance, setBalance] = useState<number | null>(null);
const price = useGlobalState(state => state.nativeCurrencyPrice);
const {
data: fetchedBalanceData,
isError,
isLoading,
} = useBalance({
address,
watch: true,
chainId: getTargetNetwork().id,
});
const onToggleBalance = useCallback(() => {
if (price > 0) {
setIsEthBalance(!isEthBalance);
}
}, [isEthBalance, price]);
useEffect(() => {
if (fetchedBalanceData?.formatted) {
setBalance(Number(fetchedBalanceData.formatted));
}
}, [fetchedBalanceData]);
return { balance, price, isError, isLoading, onToggleBalance, isEthBalance };
}