{"size": 778, "ext": "js", "max_stars_repo_path": "hackfs-frontend/packages/react-app/src/hooks/ExchangePrice.js", "max_stars_repo_name": "kaustavha/crypto-steganography", "max_stars_repo_stars_event_min_datetime": "2020-09-26T07:06:08.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-23T07:13:11.000Z", "max_issues_repo_path": "hackfs-frontend/packages/react-app/src/hooks/ExchangePrice.js", "max_issues_repo_name": "kaustavha/crypto-steganography", "max_issues_repo_issues_event_min_datetime": "2020-07-23T00:27:15.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-10T02:02:43.000Z", "max_forks_repo_path": "hackfs-frontend/packages/react-app/src/hooks/ExchangePrice.js", "max_forks_repo_name": "kaustavha/crypto-steganography", "max_forks_repo_forks_event_min_datetime": "2020-07-22T22:55:18.000Z", "max_forks_repo_forks_event_max_datetime": "2020-07-25T05:38:05.000Z"}
{"max_stars_count": 2, "max_issues_count": 7, "max_forks_count": 2, "avg_line_length": 29.9230769231, "max_line_length": 104, "alphanum_fraction": 0.6979434447}
import { useState } from 'react';
import { usePoller } from ".";
import { ethers } from "ethers";
import { MAINNET_ID, addresses, abis } from "@uniswap-v1-app/contracts";

export default function useExchangePrice(mainnetProvider,pollTime) {

  const [price, setPrice] = useState(0);

  const pollPrice = () => {
    async function getPrice() {
      const ethDaiExchangeContract = new ethers.Contract(
        addresses[MAINNET_ID].exchanges["ETH-DAI"],
        abis.exchange,
        mainnetProvider,
      );
      const exchangeRate = await ethDaiExchangeContract.getEthToTokenInputPrice("10000000000000000000");
      setPrice(parseFloat(exchangeRate.div("100000000000000000"))/100)
    }
    getPrice();
  }
  usePoller(pollPrice,pollTime?pollTime:9777)

  return price;
}
