Skip to content

Commit

Permalink
[Hotfix 🔥 ] - 1.15.1 Fix USD estimation issue (#671)
Browse files Browse the repository at this point in the history
* check native and use wrapped address

* fix ETH price getting
  • Loading branch information
W3stside authored Jun 10, 2022
1 parent 30ac9c0 commit 031f8fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/custom/api/coingecko/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function _get(chainId: ChainId, url: string): Promise<Response> {
export interface CoinGeckoUsdPriceParams {
chainId: ChainId
tokenAddress: string
isNative: boolean
}

interface CoinGeckoUsdQuote {
Expand All @@ -70,7 +71,9 @@ interface CoinGeckoUsdQuote {
}
}

export async function getUSDPriceQuote(params: CoinGeckoUsdPriceParams): Promise<CoinGeckoUsdQuote | null> {
export async function getUSDPriceQuote(
params: Omit<CoinGeckoUsdPriceParams, 'isNative'>
): Promise<CoinGeckoUsdQuote | null> {
const { chainId, tokenAddress } = params
// ethereum/xdai (chains)
const assetPlatform = _getCoinGeckoAssetPlatform(chainId)
Expand All @@ -93,10 +96,10 @@ export async function getUSDPriceQuote(params: CoinGeckoUsdPriceParams): Promise
}

export function useGetCoingeckoUsdPrice(params: Partial<CoinGeckoUsdPriceParams>, options = SWR_OPTIONS) {
const { chainId, tokenAddress } = params
const { chainId, tokenAddress, isNative } = params

return useSWR<PriceInformation | null>(
['getUSDPriceQuote', chainId, tokenAddress],
['getUSDPriceQuote', chainId, tokenAddress, isNative],
() => {
if (chainId && tokenAddress) {
return getUSDPriceQuote({ chainId, tokenAddress }).then(toPriceInformation)
Expand Down
9 changes: 8 additions & 1 deletion src/custom/hooks/useUSDCPrice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
5 changes: 4 additions & 1 deletion src/custom/pages/Swap/SwapMod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 031f8fa

Please sign in to comment.