-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-token.ts
115 lines (102 loc) · 3.79 KB
/
use-token.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { PAYMASTER_ADDRESS, TOKEN_ABI, TOKEN_ADDRESS } from "@/constants/contract";
import { useWeb3ModalAccount, useWeb3ModalProvider } from "@web3modal/ethers/react";
import { Contract, BrowserProvider, Wallet, Provider } from "zksync-ethers"
import { ethers } from "ethers";
import { useMutation, useQuery } from "react-query";
import { toast } from "sonner";
import usePaymaster from "./use-paymaster";
const useToken = () => {
const { isConnected, address } = useWeb3ModalAccount()
const { walletProvider } = useWeb3ModalProvider()
const { paymasterBalance, customData } = usePaymaster();
const getContract = async () => {
const ethersProvider = new BrowserProvider((window as any)?.ethereum)
const signer = await ethersProvider.getSigner();
const erc20Contract = new Contract(TOKEN_ADDRESS, TOKEN_ABI, signer);
return erc20Contract;
}
const getTokenMintEstimate = async () => {
const ethersProvider = new BrowserProvider(walletProvider!)
const erc20Contract = await getContract();
const gasEstimate = await erc20Contract.mint.estimateGas(address, ethers.parseEther("1000"));
const gasPrice = await ethersProvider.getGasPrice();
const cost = gasPrice * gasEstimate;
return {
gas: ethers.formatEther(gasEstimate).toString(),
gasPrice: ethers.formatEther(gasPrice).toString(),
cost: ethers.formatEther(cost).toString()
}
}
const { data: tokenBalance, refetch: refetchToken } = useQuery(
["token", address],
async () => {
const erc20Contract = await getContract();
const balance = await erc20Contract.balanceOf(address);
return ethers.formatEther(balance).toString();
},
{
enabled: isConnected,
refetchInterval: 0
}
)
const {data: allowance, refetch: refetchAllowance} = useQuery(["tokenAllowance", address], async () => {
const erc20Contract = await getContract();
const allowance = await erc20Contract.allowance(address, customData.paymasterParams.paymaster);
return allowance;
}, {
enabled: isConnected,
refetchInterval: 0
})
const { data: mintTx, isLoading: isMintLoading, mutateAsync: mint } = useMutation(
["mint", address],
async () => {
const erc20Contract = await getContract();
const tx = await erc20Contract.mint(address, ethers.parseEther("1000"), {
customData: ethers.parseEther(tokenBalance!) > ethers.parseEther("1") ? customData : undefined
});
await tx.wait();
return tx;
},
{
onSuccess: () => {
refetchToken();
toast.success("mint 1000 token success");
},
onError: (error: any) => {
console.log(error);
toast.error(error.message);
}
}
)
const {
data: approvePaymasterTx,
isLoading: isApprovePaymasterLoading,
mutateAsync: approvePaymaster
} = useMutation("approve", async () => {
const erc20Contract = await getContract();
const tx = await erc20Contract.approve(
PAYMASTER_ADDRESS,
ethers.MaxUint256
);
await tx.wait();
return tx;
}, {
onSuccess: () => {
toast.success("approve paymaster success");
refetchAllowance();
}
})
return {
tokenBalance,
refetchToken,
mint,
mintTx,
isMintLoading,
getTokenMintEstimate,
isAllowancePaymaster: allowance! > ethers.parseEther("1"),
approvePaymaster,
isApprovePaymasterLoading,
approvePaymasterTx
}
}
export default useToken;