-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathuseTokenContract.ts
56 lines (49 loc) · 1.56 KB
/
useTokenContract.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
44
45
46
47
48
49
50
51
52
53
54
55
56
"use client";
import { ERC20 as L1_ERC20_ABI } from "@/abi/L1/ERC20";
import { SUPPORTED_L1_CHAIN_ID } from "@/constants/env";
import { isEth } from "@/utils/utils";
import {
useAccount as useL1Account,
useWriteContract as useL1ContractWrite,
useReadContract,
} from "wagmi";
import { LORDS, LORDS_BRIDGE_ADDRESS } from "@realms-world/constants";
export const useTokenContractAPI = (
symbol: "LORDS" | "ETH",
spender?: boolean | string,
) => {
/*const l1BridgeAddress = tokens.L1.LORDS.bridgeAddress?.[ChainType.L1[network]] as `0x${string}`
const l1LordsAddress = tokens.L1.LORDS.tokenAddress?.[ChainType.L1[network]]
const l2BridgeAddress = tokens.L2.LORDS.bridgeAddress?.[ChainType.L2[network]]
const l2LordsAddress = tokens.L2.LORDS.tokenAddress?.[ChainType.L2[network]]*/
const { address: l1Account } = useL1Account();
const l1ERC20Contract = {
address: isEth(symbol)
? "0x0000000000000000"
: (LORDS[SUPPORTED_L1_CHAIN_ID]?.address as `0x${string}`),
abi: L1_ERC20_ABI,
};
const {
writeContractAsync: approve,
data: approveHash,
isPending: approveWriteLoading,
} = useL1ContractWrite();
const { data: allowance /*, isError, isLoading */ } = useReadContract({
...l1ERC20Contract,
functionName: "allowance",
args: [
l1Account!,
(spender == true
? LORDS_BRIDGE_ADDRESS[SUPPORTED_L1_CHAIN_ID]
: spender) as `0x${string}`,
],
//enabled: !!(l1Account && spender),
});
return {
l1ERC20Contract,
approve,
approveWriteLoading,
approveHash,
allowance,
};
};