Skip to content

Commit

Permalink
feat: less overfetchy
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre committed Feb 9, 2024
1 parent 6c2be01 commit da7216f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import {
type FeeDataEstimate,
FeeDataKey,
} from '@shapeshiftoss/chain-adapters'
import type { SwapErrorRight } from '@shapeshiftoss/swapper'
import { SwapperName } from '@shapeshiftoss/swapper'
import type { KnownChainIds } from '@shapeshiftoss/types'
import { TxStatus } from '@shapeshiftoss/unchained-client'
import { Err, Ok } from '@sniptt/monads'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import dayjs from 'dayjs'
import { useCallback, useEffect, useMemo, useState } from 'react'
Expand All @@ -42,6 +44,7 @@ import { Row } from 'components/Row/Row'
import { useWallet } from 'hooks/useWallet/useWallet'
import { fromBaseUnit, toBaseUnit } from 'lib/math'
import { sleep } from 'lib/poll/poll'
import type { InboundAddressResponse } from 'lib/swapper/swappers/ThorchainSwapper/types'
import { assetIdToPoolAssetId } from 'lib/swapper/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers'
import { assertUnreachable, isToken } from 'lib/utils'
import { assertGetThorchainChainAdapter } from 'lib/utils/cosmosSdk'
Expand Down Expand Up @@ -258,14 +261,23 @@ export const TransactionRow: React.FC<TransactionRowProps> = ({
}, [mutateAsync, onComplete, status, tx, txId])

const { data: inboundAddressData, isLoading: isInboundAddressLoading } = useQuery({
...reactQueries.thornode.inboundAddress(assetId),
enabled: !!assetId,
...reactQueries.thornode.inboundAddresses(),
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
// We technically don't care about going stale immediately here - halted checks are done JIT at signing time in case the pool went
// halted by the time the user clicked the confirm button
// But we still have some sane 60s stale time rather than 0 for paranoia's sake, as a balance of safety and not overfetching
staleTime: 60_000,
select: data => data?.unwrap(),
select: data =>
data
?.andThen<InboundAddressResponse | undefined>(data => {
if (!assetId) return Err('AssetId is required' as unknown as SwapErrorRight)

const assetPoolId = assetIdToPoolAssetId({ assetId })
const assetChainSymbol = assetPoolId?.slice(0, assetPoolId.indexOf('.'))
return Ok(data.find(inbound => inbound.chain === assetChainSymbol))
})
.unwrap(),
enabled: !!assetId,
})

const estimateFeesArgs = useMemo(() => {
Expand Down
24 changes: 18 additions & 6 deletions src/react-queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import type { HDWallet } from '@shapeshiftoss/hdwallet-core'
import { supportsETH } from '@shapeshiftoss/hdwallet-core'
import type { SwapperName } from '@shapeshiftoss/swapper'
import type { KnownChainIds } from '@shapeshiftoss/types'
import { Ok } from '@sniptt/monads'
import axios from 'axios'
import { getConfig } from 'config'
import type {
InboundAddressResponse,
MidgardPoolResponse,
ThornodePoolResponse,
} from 'lib/swapper/swappers/ThorchainSwapper/types'
import { assetIdToPoolAssetId } from 'lib/swapper/swappers/ThorchainSwapper/utils/poolAssetHelpers/poolAssetHelpers'
import { thorService } from 'lib/swapper/swappers/ThorchainSwapper/utils/thorService'
import { thorService, thorService } from 'lib/swapper/swappers/ThorchainSwapper/utils/thorService'
import { isToken } from 'lib/utils'
import {
assertGetEvmChainAdapter,
Expand Down Expand Up @@ -255,15 +257,25 @@ const thornode = createQueryKeys('thornode', {
enabled: true,
}
},
inboundAddress: (assetId: AssetId | undefined) => {
inboundAddresses: () => {
return {
queryKey: ['thorchainInboundAddress', assetId],
queryKey: ['thorchainInboundAddress'],
queryFn: async () => {
if (!assetId) throw new Error('assetId is required')
const daemonUrl = getConfig().REACT_APP_THORCHAIN_NODE_URL
const data = await getInboundAddressDataForChain(daemonUrl, assetId)

return data
return (
// Get all inbound addresses
(
await thorService.get<InboundAddressResponse[]>(
`${daemonUrl}/lcd/thorchain/inbound_addresses`,
)
).andThen(({ data: inboundAddresses }) => {
// Exclude halted
const activeInboundAddresses = inboundAddresses.filter(a => !a.halted)

return Ok(activeInboundAddresses)
})
)
},
}
},
Expand Down

0 comments on commit da7216f

Please sign in to comment.