Skip to content

Commit

Permalink
Protect useGetTokenDollarValueQuery in case if token to token price…
Browse files Browse the repository at this point in the history
… is not defined (#252)
  • Loading branch information
sashimi36 authored Aug 24, 2022
1 parent ae91d69 commit 373c433
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions queries/useGetTokenDollarValueQuery.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 373c433

Please sign in to comment.