import { useWeb3Provider } from "../useWeb3Provider/useWeb3Provider"; import { useEffect, useMemo } from "react"; import { config } from "../../config"; import { ReliquaryABI__factory, DepositHelper2ABI__factory, } from "digit-shared/src/types/generatedContractTypes"; /** * A hook to access our contracts * Warning!: use only where you know the user will have connected their wallet (conditionally rendered components) */ export const useContracts = () => { const { signer } = useWeb3Provider(); useEffect(() => { if ( !config?.reliquaryContractAddress || !config.reliquaryHelperContractAddress ) { throw new Error( "Missing config. Require contract addresses as reliquaryContractAddress & reliquaryHelperContractAddress in chain config!" ); } }, []); const contracts = useMemo(() => { if ( config == undefined || config.reliquaryContractAddress == undefined || config.reliquaryHelperContractAddress == undefined ) { return null; } const contracts = signer ? { reliquary: ReliquaryABI__factory.connect( config!.reliquaryContractAddress!, signer ), depositHelper: DepositHelper2ABI__factory.connect( config!.reliquaryHelperContractAddress!, signer ), } : null; return contracts; }, [signer, config]); if (!contracts) { throw new Error( "Contracts have not been initialized. Use the useConnect hook to initialize the contracts" ); } return contracts!; };