-
Notifications
You must be signed in to change notification settings - Fork 22
/
useCreateCw1Whitelist.ts
117 lines (105 loc) · 2.94 KB
/
useCreateCw1Whitelist.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
116
117
import { useCallback, useState } from 'react'
import toast from 'react-hot-toast'
import { useTranslation } from 'react-i18next'
import { useUpdatingRef } from '@dao-dao/stateless'
import { CreateCw1Whitelist } from '@dao-dao/types'
import { InstantiateMsg as Cw1WhitelistInstantiateMsg } from '@dao-dao/types/contracts/Cw1Whitelist'
import {
getSupportedChainConfig,
instantiateSmartContract,
isValidBech32Address,
processError,
} from '@dao-dao/utils'
import { useWallet } from './useWallet'
export type UseCreateCw1WhitelistOptions = {
/**
* If passed, use this chain. Otherwise, use the current chain context.
*/
chainId?: string
/**
* If passed, will be executed with the parameters passed before
* instantiating.
*/
validation?: (
...params: Parameters<CreateCw1Whitelist>
) => void | Promise<void>
/**
* If passed, override the contract label. Defaults to 'Cw1Whitelist'.
*/
contractLabel?: string
}
export type UseCreateCw1WhitelistReturn = {
creatingCw1Whitelist: boolean
createCw1Whitelist: CreateCw1Whitelist
}
export const useCreateCw1Whitelist = ({
chainId: _chainId,
validation,
contractLabel = 'Cw1Whitelist',
}: UseCreateCw1WhitelistOptions = {}) => {
const { t } = useTranslation()
const {
address: walletAddress,
getSigningClient,
chain: { chainId, bech32Prefix },
} = useWallet({
chainId: _chainId,
})
const cw1WhitelistCodeId =
getSupportedChainConfig(chainId)?.codeIds?.Cw1Whitelist ?? -1
const [creatingCw1Whitelist, setCreatingCw1Whitelist] = useState(false)
const validationRef = useUpdatingRef(validation)
const createCw1Whitelist: CreateCw1Whitelist = useCallback(
async (admins: string[], mutable = false) => {
try {
if (!walletAddress) {
throw new Error(t('error.logInToContinue'))
}
setCreatingCw1Whitelist(true)
// Custom validation.
await validationRef.current?.(admins, mutable)
if (admins.length < 2) {
throw new Error(t('error.enterAtLeastTwoAccounts'))
}
if (
admins.some((admin) => !isValidBech32Address(admin, bech32Prefix))
) {
throw new Error(t('error.invalidAccount'))
}
const contractAddress = await instantiateSmartContract(
getSigningClient,
walletAddress,
cw1WhitelistCodeId,
contractLabel,
{
admins,
mutable,
} as Cw1WhitelistInstantiateMsg
)
return contractAddress
} catch (err) {
console.error(err)
toast.error(
processError(err, {
forceCapture: false,
})
)
} finally {
setCreatingCw1Whitelist(false)
}
},
[
bech32Prefix,
contractLabel,
cw1WhitelistCodeId,
getSigningClient,
t,
validationRef,
walletAddress,
]
)
return {
creatingCw1Whitelist,
createCw1Whitelist,
}
}