import { useWallet } from 'contexts/wallet' import { useCallback, useEffect, useState } from 'react' import type { InstantiateResponse, WhiteListContract, WhiteListInstance, WhitelistMessages } from './contract' import { WhiteList as initContract } from './contract' export interface UseWhiteListContractProps { instantiate: ( codeId: number, initMsg: Record, label: string, admin?: string, ) => Promise use: (customAddress?: string) => WhiteListInstance | undefined updateContractAddress: (contractAddress: string) => void messages: (contractAddress: string) => WhitelistMessages | undefined } export function useWhiteListContract(): UseWhiteListContractProps { const wallet = useWallet() const [address, setAddress] = useState('') const [whiteList, setWhiteList] = useState() useEffect(() => { setAddress(localStorage.getItem('contract_address') || '') }, []) useEffect(() => { const whiteListContract = initContract(wallet.getClient(), wallet.address) setWhiteList(whiteListContract) }, [wallet]) const updateContractAddress = (contractAddress: string) => { setAddress(contractAddress) } const instantiate = useCallback( (codeId: number, initMsg: Record, label: string, admin?: string): Promise => { return new Promise((resolve, reject) => { if (!whiteList) { reject(new Error('Contract is not initialized.')) return } whiteList.instantiate(codeId, initMsg, label, admin).then(resolve).catch(reject) }) }, [whiteList], ) const use = useCallback( (customAddress = ''): WhiteListInstance | undefined => { return whiteList?.use(address || customAddress) }, [whiteList, address], ) const messages = useCallback( (customAddress = ''): WhitelistMessages | undefined => { return whiteList?.messages(address || customAddress) }, [whiteList, address], ) return { instantiate, use, updateContractAddress, messages, } }