mirror of
https://github.com/luxfhe/example-poker.git
synced 2026-10-30 18:13:22 +00:00
35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { useTargetNetwork } from "./useTargetNetwork";
|
|
import { useInterval } from "usehooks-ts";
|
|
import scaffoldConfig from "~~/scaffold.config";
|
|
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 useNativeCurrencyPrice = () => {
|
|
const { targetNetwork } = useTargetNetwork();
|
|
const [nativeCurrencyPrice, setNativeCurrencyPrice] = useState(0);
|
|
|
|
// Get the price of ETH from Uniswap on mount
|
|
useEffect(() => {
|
|
(async () => {
|
|
const price = await fetchPriceFromUniswap(targetNetwork);
|
|
setNativeCurrencyPrice(price);
|
|
})();
|
|
}, [targetNetwork]);
|
|
|
|
// Get the price of ETH from Uniswap at a given interval
|
|
useInterval(
|
|
async () => {
|
|
const price = await fetchPriceFromUniswap(targetNetwork);
|
|
setNativeCurrencyPrice(price);
|
|
},
|
|
enablePolling ? scaffoldConfig.pollingInterval : null,
|
|
);
|
|
|
|
return nativeCurrencyPrice;
|
|
};
|