-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* issue #867 2. added usd value using coingecko api * fix lockfile * refactor: convert getPrice to support both hook and direct fetch function using react-query + takes in param to fetch any coins price * fixed implementation of getPrice in BalanceCard.tsx --------- Co-authored-by: Greg Nazario <[email protected]>
- Loading branch information
1 parent
207bed0
commit fc0b65b
Showing
2 changed files
with
98 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {useQuery} from "@tanstack/react-query"; | ||
|
||
const COINGECKO_API_ENDPOINT = "https://api.coingecko.com/api/v3/simple/price"; | ||
|
||
/** | ||
* Fetches the USD price for a cryptocurrency using its CoinGecko ID. | ||
* | ||
* Common CoinGecko IDs: | ||
* - "aptos" : Aptos (APT) | ||
* - "bitcoin": Bitcoin (BTC) | ||
* - "ethereum": Ethereum (ETH) | ||
* - "solana": Solana (SOL) | ||
* - "binancecoin": Binance Coin (BNB) | ||
* - "cardano": Cardano (ADA) | ||
* | ||
* Complete list of supported IDs: | ||
* https://api.coingecko.com/api/v3/coins/list | ||
* | ||
* @param coinId - The CoinGecko ID of the cryptocurrency (defaults to "aptos") | ||
* @returns The USD price of the cryptocurrency or null if the price fetch fails | ||
*/ | ||
export async function getPrice( | ||
coinId: string = "aptos", | ||
): Promise<number | null> { | ||
const query = { | ||
ids: coinId, | ||
vs_currencies: "usd", | ||
}; | ||
|
||
const queryString = new URLSearchParams(query); | ||
const url = `${COINGECKO_API_ENDPOINT}?${queryString}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: "GET", | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! Status: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
return Number(data[coinId].usd); | ||
} catch (error) { | ||
console.error(`Error fetching ${coinId} price from CoinGecko:`, error); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Fetches the USD price for a cryptocurrency using its CoinGecko ID. | ||
* | ||
* @param coinId - The CoinGecko ID of the cryptocurrency (defaults to "aptos") | ||
* @returns React Query result object containing the price data and query state | ||
*/ | ||
export function useGetPrice(coinId: string = "aptos") { | ||
return useQuery({ | ||
queryKey: ["price", coinId], | ||
queryFn: () => getPrice(coinId), | ||
}); | ||
} |
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