/* eslint-disable no-param-reassign */ // import { FetchStatus } from 'config/constants/types' import { Contract } from '@ethersproject/contracts' import { map } from 'lodash' import { useMemo } from 'react' import { QueryOptions, UseQueryOptions } from 'react-query' import { useCustomQuery } from '../customQuery/useCustomQuery' import { ContractMethodName, UseQueryContractKey } from './types' import { getContractKey, serializesContractKey } from './utils' /** * @example * const key = [contract, 'methodName', [params]] * const key = { contract, methodName, params } * const { data, error, mutate } = useQueryContract(key) */ export function useQueryContract< Error = any, T extends Contract = Contract, N extends ContractMethodName = ContractMethodName, Data = Awaited>, >(key?: UseQueryContractKey | null, config: UseQueryOptions = {}) { const { contract, methodName, variables } = getContractKey(key) || {} const serializedKeys = useMemo(() => serializesContractKey(key), [key]) return useCustomQuery( [serializedKeys], async () => { if (!contract || !methodName) return null if (!variables) return contract[methodName]() return contract[methodName](...map(variables, (i) => i)) }, config, ) }