From 031f8faedd485e6f2ed99455485b5e08dd00310a Mon Sep 17 00:00:00 2001 From: dave Date: Fri, 10 Jun 2022 10:24:09 +0100 Subject: [PATCH] [Hotfix :fire: ] - 1.15.1 Fix USD estimation issue (#671) * check native and use wrapped address * fix ETH price getting --- src/custom/api/coingecko/index.ts | 9 ++++++--- src/custom/hooks/useUSDCPrice/index.ts | 9 ++++++++- src/custom/pages/Swap/SwapMod.tsx | 5 ++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/custom/api/coingecko/index.ts b/src/custom/api/coingecko/index.ts index 6f8881d0d0..4131fe9e4a 100644 --- a/src/custom/api/coingecko/index.ts +++ b/src/custom/api/coingecko/index.ts @@ -62,6 +62,7 @@ function _get(chainId: ChainId, url: string): Promise { export interface CoinGeckoUsdPriceParams { chainId: ChainId tokenAddress: string + isNative: boolean } interface CoinGeckoUsdQuote { @@ -70,7 +71,9 @@ interface CoinGeckoUsdQuote { } } -export async function getUSDPriceQuote(params: CoinGeckoUsdPriceParams): Promise { +export async function getUSDPriceQuote( + params: Omit +): Promise { const { chainId, tokenAddress } = params // ethereum/xdai (chains) const assetPlatform = _getCoinGeckoAssetPlatform(chainId) @@ -93,10 +96,10 @@ export async function getUSDPriceQuote(params: CoinGeckoUsdPriceParams): Promise } export function useGetCoingeckoUsdPrice(params: Partial, options = SWR_OPTIONS) { - const { chainId, tokenAddress } = params + const { chainId, tokenAddress, isNative } = params return useSWR( - ['getUSDPriceQuote', chainId, tokenAddress], + ['getUSDPriceQuote', chainId, tokenAddress, isNative], () => { if (chainId && tokenAddress) { return getUSDPriceQuote({ chainId, tokenAddress }).then(toPriceInformation) diff --git a/src/custom/hooks/useUSDCPrice/index.ts b/src/custom/hooks/useUSDCPrice/index.ts index d68247ce34..6e111b91bb 100644 --- a/src/custom/hooks/useUSDCPrice/index.ts +++ b/src/custom/hooks/useUSDCPrice/index.ts @@ -207,13 +207,20 @@ export function useCoingeckoUsdPrice(currency?: Currency) { const currencyRef = useRef(currency) currencyRef.current = currency - const tokenAddress = currencyRef.current ? currencyId(currencyRef.current) : undefined + const isNative = !!currency?.isNative + // use wrapped address equivalent if native (DONT USE "ETH" or "XDAI") + const tokenAddress = currencyRef.current + ? isNative + ? currency.wrapped.address + : currencyId(currencyRef.current) + : undefined const chainIdSupported = supportedChainId(chainId) // get SWR cached coingecko usd price const { data: priceResponse, error: errorResponse } = useGetCoingeckoUsdPrice({ chainId: chainIdSupported, tokenAddress, + isNative, }) useEffect(() => { diff --git a/src/custom/pages/Swap/SwapMod.tsx b/src/custom/pages/Swap/SwapMod.tsx index c5b23f913a..0d0eab89a3 100644 --- a/src/custom/pages/Swap/SwapMod.tsx +++ b/src/custom/pages/Swap/SwapMod.tsx @@ -204,7 +204,10 @@ export default function Swap({ } = useDerivedSwapInfo() // detects trade load - const { quote, isGettingNewQuote } = useGetQuoteAndStatus({ token: INPUT.currencyId, chainId }) + const { quote, isGettingNewQuote } = useGetQuoteAndStatus({ + token: currencies.INPUT?.isNative ? currencies.INPUT.wrapped.address : INPUT.currencyId, + chainId, + }) // Log all trade information // logTradeDetails(v2Trade, allowedSlippage)