-
Notifications
You must be signed in to change notification settings - Fork 918
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
101 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { useGlobalState } from "~~/services/store/store"; | ||
|
||
export const useDisplayUsdMode = ({ defaultUsdMode = false }: { defaultUsdMode?: boolean }) => { | ||
const nativeCurrencyPrice = useGlobalState(state => state.nativeCurrency.price); | ||
const isPriceFetched = nativeCurrencyPrice > 0; | ||
const predefinedUsdMode = isPriceFetched ? Boolean(defaultUsdMode) : false; | ||
const [displayUsdMode, setDisplayUsdMode] = useState(predefinedUsdMode); | ||
|
||
useEffect(() => { | ||
setDisplayUsdMode(predefinedUsdMode); | ||
}, [predefinedUsdMode]); | ||
|
||
const toggleDisplayUsdMode = useCallback(() => { | ||
if (isPriceFetched) { | ||
setDisplayUsdMode(!displayUsdMode); | ||
} | ||
}, [displayUsdMode, isPriceFetched]); | ||
|
||
return { displayUsdMode, toggleDisplayUsdMode }; | ||
}; |
32 changes: 32 additions & 0 deletions
32
packages/nextjs/hooks/scaffold-eth/useInitializeNativeCurrencyPrice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
34 changes: 0 additions & 34 deletions
34
packages/nextjs/hooks/scaffold-eth/useNativeCurrencyPrice.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters