/
useRegisterToken.ts
68 lines (60 loc) 路 1.96 KB
/
useRegisterToken.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
import { ContractTransaction, ethers } from 'ethers'
import React from 'react'
import { Values } from '../components/RegisterToken'
import { ErrorsContext } from '../contexts/errors'
import { erc20MessagingContract } from '../contracts'
import useEthers from './useEthers'
export const zeroAddress = '0x0000000000000000000000000000000000000000'
export default function useRegisterToken() {
const { provider } = useEthers({
viaMetaMask: true,
})
const { setErrors } = React.useContext(ErrorsContext)
const [loading, setLoading] = React.useState(false)
const contract = React.useMemo(
() => erc20MessagingContract.connect(provider.getSigner()),
[provider]
)
const registerToken = React.useCallback(
async ({ cap, dailyMintLimit, name, symbol, supply }: Values) => {
setLoading(true)
const params = ethers.utils.defaultAbiCoder.encode(
['string', 'string', 'uint256', 'address', 'uint256', 'uint256'],
[
name,
symbol,
ethers.utils.parseUnits(cap.toString()),
zeroAddress,
ethers.utils.parseUnits(dailyMintLimit.toString()),
ethers.utils.parseUnits(supply.toString()),
]
)
return new Promise((resolve, reject) => {
contract
.deployToken(params)
.then((tx: ContractTransaction) =>
tx
.wait()
.then((receipt) => {
resolve(receipt)
})
.catch((error: Error) => {
console.error(error)
setErrors((e) => [...e, `Error when registering the token`])
reject(error)
})
)
.catch((error: Error) => {
console.error(error)
setErrors((e) => [...e, `Error when registering the token`])
reject(error)
})
.finally(() => {
setLoading(false)
})
})
},
[contract]
)
return { loading, registerToken }
}