import { useReadContract } from "wagmi" import { useMemo } from "react" import { mainnetBorrowContracts, testnetBorrowContracts, } from "../lib/contracts" import { usePassportContext } from "./usePassportContext" import { CHAIN_ID } from "../constants" import { useWalletAccount } from "./useWalletAccount" // Wagmi handles typesafety with ABI const assertions. TypeScript doesn't // support importing JSON as const yet so types cannot be inferred from the // imported contract. As a workaround there is minimal ABI definition that can // be asserted types from. // Ref: https://wagmi.sh/core/typescript#const-assert-abis-typed-data const PRICE_FEED_ABI = [ { inputs: [], name: "fetchPrice", outputs: [ { internalType: "uint256", name: "", type: "uint256", }, ], stateMutability: "view", type: "function", }, ] as const /** * Query hook for getting borrow data. Returns collateral and trove debt for the * connected account, based on it's evm address. * @param queryOptions Query options passed to the underlying `useQuery` hook. */ export function useCollateralPrice(queryOptions = {}) { const { environment = "mainnet", borrowDataRefetchInterval } = usePassportContext() const walletAccount = useWalletAccount() const priceFeedContractAddress = useMemo(() => { if (environment === "mainnet") { return mainnetBorrowContracts.PriceFeed.address } return testnetBorrowContracts.PriceFeed.address }, [environment]) const chainId = CHAIN_ID[environment] return useReadContract({ abi: PRICE_FEED_ABI, address: priceFeedContractAddress, functionName: "fetchPrice", chainId, query: { enabled: !!walletAccount.accountAddress, staleTime: borrowDataRefetchInterval, refetchInterval: borrowDataRefetchInterval, retry: 1, ...queryOptions, }, }) }