-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuseContract.ts
43 lines (38 loc) · 1.34 KB
/
useContract.ts
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
35
36
37
38
39
40
41
42
43
import { useMemo } from 'react';
import { Abi, WalletClient } from 'viem';
import { getContract } from '../utils/getContract';
import { babtABI, collabABI } from '../abis';
import { Address, useContractRead, useNetwork, usePublicClient, useWalletClient } from 'wagmi';
import { BABT_ADDRESSES, COLLAB_ADDRESS } from '../constants/addresses';
export function useContract<TAbi extends Abi>(address?: Address, abi?: TAbi, chainId?: number) {
const publicClient = usePublicClient({ chainId });
const { data: walletClient } = useWalletClient();
return useMemo(() => {
if (!address || !abi) return null;
try {
return getContract({
abi,
address,
publicClient: publicClient,
walletClient: walletClient as WalletClient,
});
} catch (error) {
console.error('Failed to get contract', error);
return null;
}
}, [abi, address, publicClient, walletClient]);
}
export function useBABTBalanceOf({ address }: { address?: Address }) {
const { chain } = useNetwork();
const babtAddress = chain ? BABT_ADDRESSES[chain.id] : undefined;
return useContractRead({
address: babtAddress,
abi: babtABI,
functionName: 'balanceOf',
args: address ? [address] : undefined,
enabled: !!address,
});
}
export function useCollabContract() {
return useContract(COLLAB_ADDRESS, collabABI);
}