All checks were successful
CI / lint-and-test (push) Successful in 9m52s
Co-authored-by: Cursor <cursoragent@cursor.com>
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import { useBalance, useReadContract } from "wagmi";
|
|
import { CHAIN138_ID } from "@/lib/constants/chain138";
|
|
import { CHAIN138_TOKENS, ERC20_BALANCE_ABI } from "@/lib/tokens";
|
|
|
|
const ERC20_TOKENS = CHAIN138_TOKENS.filter((t) => t.address !== "native");
|
|
|
|
export function useTokenBalances(address?: `0x${string}`) {
|
|
const enabled = Boolean(address);
|
|
|
|
const { data: nativeBalance, isLoading: nativeLoading, refetch: refetchNative } =
|
|
useBalance({
|
|
address,
|
|
chainId: CHAIN138_ID,
|
|
query: { enabled },
|
|
});
|
|
|
|
const { data: cusdtRaw, isLoading: cusdtLoading, refetch: refetchCusdt } =
|
|
useReadContract({
|
|
address: ERC20_TOKENS[0]?.address as `0x${string}`,
|
|
abi: ERC20_BALANCE_ABI,
|
|
functionName: "balanceOf",
|
|
args: address ? [address] : undefined,
|
|
chainId: CHAIN138_ID,
|
|
query: { enabled },
|
|
});
|
|
|
|
const { data: cusdcRaw, isLoading: cusdcLoading, refetch: refetchCusdc } =
|
|
useReadContract({
|
|
address: ERC20_TOKENS[1]?.address as `0x${string}`,
|
|
abi: ERC20_BALANCE_ABI,
|
|
functionName: "balanceOf",
|
|
args: address ? [address] : undefined,
|
|
chainId: CHAIN138_ID,
|
|
query: { enabled },
|
|
});
|
|
|
|
const isLoading = nativeLoading || cusdtLoading || cusdcLoading;
|
|
|
|
const refetch = () => {
|
|
void refetchNative();
|
|
void refetchCusdt();
|
|
void refetchCusdc();
|
|
};
|
|
|
|
return {
|
|
nativeBalance,
|
|
cusdtRaw: cusdtRaw as bigint | undefined,
|
|
cusdcRaw: cusdcRaw as bigint | undefined,
|
|
erc20Tokens: ERC20_TOKENS,
|
|
isLoading,
|
|
refetch,
|
|
};
|
|
}
|