forked from Scaffold-Stark/scaffold-stark-2
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Eduardo
committed
Apr 15, 2024
1 parent
02b8c73
commit eb2de3a
Showing
8 changed files
with
92 additions
and
9 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
39 changes: 39 additions & 0 deletions
39
packages/nextjs/hooks/scaffold-stark/useNativeCurrencyPrice.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,39 @@ | ||
import { useEffect } from "react"; | ||
import { useTargetNetwork } from "./useTargetNetwork"; | ||
import { useInterval } from "usehooks-ts"; | ||
import scaffoldConfig from "~~/scaffold.config"; | ||
import { fetchPriceFromCoingecko } from "~~/utils/scaffold-stark"; | ||
import { useGlobalState } from "~~/services/store/store"; | ||
|
||
/** | ||
* Get the price of Native Currency based on Native Token/DAI trading pair from Uniswap SDK | ||
*/ | ||
export const useNativeCurrencyPrice = () => { | ||
const { targetNetwork } = useTargetNetwork(); | ||
const nativeCurrencyPrice = useGlobalState( | ||
(state) => state.nativeCurrencyPrice, | ||
); | ||
const setNativeCurrencyPrice = useGlobalState( | ||
(state) => state.setNativeCurrencyPrice, | ||
); | ||
// Get the price of ETH from Coingecko on mount | ||
useEffect(() => { | ||
(async () => { | ||
if (nativeCurrencyPrice == 0) { | ||
const price = await fetchPriceFromCoingecko(targetNetwork); | ||
setNativeCurrencyPrice(price); | ||
} | ||
})(); | ||
}, [targetNetwork]); | ||
|
||
// Get the price of ETH from Coingecko at a given interval | ||
useInterval( | ||
async () => { | ||
const price = await fetchPriceFromCoingecko(targetNetwork); | ||
setNativeCurrencyPrice(price); | ||
}, | ||
scaffoldConfig.pollingInterval ? 4000 : scaffoldConfig.pollingInterval, | ||
); | ||
|
||
//return nativeCurrencyPrice; | ||
}; |
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
27 changes: 27 additions & 0 deletions
27
packages/nextjs/utils/scaffold-stark/fetchPriceFromCoingecko.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,27 @@ | ||
import { ChainWithAttributes } from "~~/utils/scaffold-stark"; | ||
|
||
export const fetchPriceFromCoingecko = async ( | ||
targetNetwork: ChainWithAttributes, | ||
): Promise<number> => { | ||
if ( | ||
targetNetwork.nativeCurrency.symbol !== "ETH" && | ||
targetNetwork.nativeCurrency.symbol !== "SEP" && | ||
!targetNetwork.nativeCurrencyTokenAddress | ||
) { | ||
return 0; | ||
} | ||
|
||
try { | ||
const response = await fetch( | ||
"https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd", | ||
); | ||
const data = await response.json(); | ||
return data.ethereum.usd; | ||
} catch (error) { | ||
console.error( | ||
`useNativeCurrencyPrice - Error fetching ${targetNetwork.nativeCurrency.symbol} price from Coingecko: `, | ||
error, | ||
); | ||
return 0; | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./networks"; | ||
export * from "./notification"; | ||
export * from "./fetchPriceFromCoingecko"; |
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