From 373c433ecb47d21d7fd91aea40ecc0298d6d166a Mon Sep 17 00:00:00 2001 From: Sashimi <93623541+sashimi36@users.noreply.github.com> Date: Tue, 23 Aug 2022 20:02:50 -0700 Subject: [PATCH] Protect `useGetTokenDollarValueQuery` in case if token to token price is not defined (#252) --- queries/useGetTokenDollarValueQuery.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/queries/useGetTokenDollarValueQuery.ts b/queries/useGetTokenDollarValueQuery.ts index 929d7409..01c14171 100644 --- a/queries/useGetTokenDollarValueQuery.ts +++ b/queries/useGetTokenDollarValueQuery.ts @@ -1,13 +1,15 @@ -/* - * takes base token price, fetches the ratio of the token provided vs the base token - * and calculates the dollar value of the provided token - * */ +import { protectAgainstNaN } from 'junoblocks' + import { useCosmWasmClient } from '../hooks/useCosmWasmClient' import { useTokenDollarValue } from '../hooks/useTokenDollarValue' import { useBaseTokenInfo } from '../hooks/useTokenInfo' import { tokenToTokenPriceQueryWithPools } from './tokenToTokenPriceQuery' import { useGetQueryMatchingPoolForSwap } from './useQueryMatchingPoolForSwap' +/* + * takes base token price, fetches the ratio of the token provided vs the base token + * and calculates the dollar value of the provided token + * */ export const useGetTokenDollarValueQuery = () => { const tokenA = useBaseTokenInfo() const client = useCosmWasmClient() @@ -22,17 +24,19 @@ export const useGetTokenDollarValueQuery = () => { async function getTokenDollarValue({ tokenInfo, tokenAmountInDenom }) { if (!tokenAmountInDenom) return 0 - const { price: priceForOneToken } = await tokenToTokenPriceQueryWithPools( - { + const priceForOneToken = ( + await tokenToTokenPriceQueryWithPools({ matchingPools: getMatchingPoolForSwap({ tokenA, tokenB: tokenInfo }), tokenA, tokenB: tokenInfo, client, amount: 1, - } - ) + }) + )?.price - return (tokenAmountInDenom / priceForOneToken) * tokenADollarPrice + return protectAgainstNaN( + (tokenAmountInDenom / priceForOneToken) * tokenADollarPrice + ) }, Boolean( tokenA && client && !fetchingDollarPrice && !isLoadingPoolForSwapMatcher