Initial commit with 🏗️ Scaffold-ETH 2 @ 1.0.2
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { useCallback, useEffect } from "react";
|
||||
import { useTargetNetwork } from "./useTargetNetwork";
|
||||
import { useInterval } from "usehooks-ts";
|
||||
import scaffoldConfig from "~~/scaffold.config";
|
||||
import { useGlobalState } from "~~/services/store/store";
|
||||
import { fetchPriceFromUniswap } from "~~/utils/scaffold-eth";
|
||||
|
||||
const enablePolling = false;
|
||||
|
||||
/**
|
||||
* Get the price of Native Currency based on Native Token/DAI trading pair from Uniswap SDK
|
||||
*/
|
||||
export const useInitializeNativeCurrencyPrice = () => {
|
||||
const setNativeCurrencyPrice = useGlobalState(state => state.setNativeCurrencyPrice);
|
||||
const setIsNativeCurrencyFetching = useGlobalState(state => state.setIsNativeCurrencyFetching);
|
||||
const { targetNetwork } = useTargetNetwork();
|
||||
|
||||
const fetchPrice = useCallback(async () => {
|
||||
setIsNativeCurrencyFetching(true);
|
||||
const price = await fetchPriceFromUniswap(targetNetwork);
|
||||
setNativeCurrencyPrice(price);
|
||||
setIsNativeCurrencyFetching(false);
|
||||
}, [setIsNativeCurrencyFetching, setNativeCurrencyPrice, targetNetwork]);
|
||||
|
||||
// Get the price of ETH from Uniswap on mount
|
||||
useEffect(() => {
|
||||
fetchPrice();
|
||||
}, [fetchPrice]);
|
||||
|
||||
// Get the price of ETH from Uniswap at a given interval
|
||||
useInterval(fetchPrice, enablePolling ? scaffoldConfig.pollingInterval : null);
|
||||
};
|
||||
Reference in New Issue
Block a user