Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: query-key-factory doesn't pass options through #6168

Merged
merged 44 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
271aae0
wip: wip
gomesalexandre Feb 7, 2024
66beabb
feat: throw on pool just halted at sign step so we don't infinitely spin
gomesalexandre Feb 7, 2024
93d09b4
Merge branch 'develop' into feat_thor_lp_improve_halted_checks_more
gomesalexandre Feb 8, 2024
67b2af8
feat: improve thorchain savers JIT halted checks
gomesalexandre Feb 8, 2024
0e0e9e5
feat: handle @lukemorales/query-key-factory shenanigans
gomesalexandre Feb 8, 2024
10997b2
feat: remove swapperApi's getIsTradingActive
gomesalexandre Feb 8, 2024
2fad5f8
feat: safety
gomesalexandre Feb 8, 2024
b1380a8
[skip ci] wip: query-key-factory shenanigans
gomesalexandre Feb 8, 2024
a1019fd
[skip ci] feat: swapData query
gomesalexandre Feb 9, 2024
7fd1613
[skip ci] feat: midgard.poolData query
gomesalexandre Feb 9, 2024
2a0d339
feat: midgard.poolsData and use 5mn staleTime
gomesalexandre Feb 9, 2024
041aca3
feat: thornode.poolData
gomesalexandre Feb 9, 2024
93334a1
feat: thornode.poolsData
gomesalexandre Feb 9, 2024
9b4f215
feat: thornode.mimir
gomesalexandre Feb 9, 2024
0325f7c
feat: thornode.block
gomesalexandre Feb 9, 2024
60a2ee0
feat: thornode.inboundAddress
gomesalexandre Feb 9, 2024
2439281
feat: thorchainLp.earnings
gomesalexandre Feb 9, 2024
cbbfbbb
feat: thorchainLp.tvl24hChange
gomesalexandre Feb 9, 2024
dbf0818
feat: thorchainLp.allTimeVolume
gomesalexandre Feb 9, 2024
92b73b1
feat: thorchainLp.liquidityProviderPosition
gomesalexandre Feb 9, 2024
6c2be01
feat: commentary
gomesalexandre Feb 9, 2024
ad8137d
wip: closer to network queries
gomesalexandre Feb 9, 2024
7fa3b3a
feat: refetch
gomesalexandre Feb 9, 2024
66f8d6a
feat: almost there
gomesalexandre Feb 9, 2024
53381b0
fix: tests
gomesalexandre Feb 9, 2024
4416d88
fix: types
gomesalexandre Feb 9, 2024
b300cdf
feat: disable sign and broadcast button on trading halted
gomesalexandre Feb 12, 2024
5c9c913
Merge remote-tracking branch 'origin/develop' into feat_thor_lp_impro…
gomesalexandre Feb 12, 2024
29d5975
Merge branch 'feat_thor_lp_improve_halted_checks_more' into feat_save…
gomesalexandre Feb 12, 2024
68eecd4
Merge branch 'feat_savers_improve_halted_checks_jit' into feat_remove…
gomesalexandre Feb 12, 2024
8dd0100
Merge branch 'feat_remove_swapperApi_getIsTradingActive' into feat_qu…
gomesalexandre Feb 12, 2024
e5e3c82
feat: gm
gomesalexandre Feb 12, 2024
6ce85fd
feat: improve thorchain savers JIT halted checks
gomesalexandre Feb 8, 2024
94f38a2
feat: handle @lukemorales/query-key-factory shenanigans
gomesalexandre Feb 8, 2024
fece98a
feat: disable sign and broadcast button on trading halted
gomesalexandre Feb 12, 2024
ce48c00
Merge branch 'feat_savers_improve_halted_checks_jit' into feat_remove…
gomesalexandre Feb 12, 2024
54d55b0
feat: remove swapperApiBase
gomesalexandre Feb 12, 2024
56046d1
Merge branch 'feat_remove_swapperApi_getIsTradingActive' into feat_qu…
gomesalexandre Feb 13, 2024
1ce9785
feat: what is even safety
gomesalexandre Feb 13, 2024
9e777f9
Merge remote-tracking branch 'origin/develop' into feat_query_key_fac…
gomesalexandre Feb 13, 2024
a4b0000
Merge remote-tracking branch 'origin/develop' into feat_query_key_fac…
gomesalexandre Feb 14, 2024
7114fbf
Merge branch 'develop' into feat_query_key_factory_shenanigans
gomesalexandre Feb 15, 2024
3e7844f
Merge branch 'develop' into feat_query_key_factory_shenanigans
gomesalexandre Feb 16, 2024
c54ca54
feat: cleanup following-up on review comments
gomesalexandre Feb 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { TradeQuoteError as SwapperTradeQuoteError } from '@shapeshiftoss/swappe
import type { FC } from 'react'
import { useCallback, useMemo } from 'react'
import { useTranslate } from 'react-polyglot'
import { useIsTradingActive } from 'react-queries/hooks/useIsTradingActive'
import { Amount } from 'components/Amount/Amount'
import { SlippageIcon } from 'components/Icons/Slippage'
import { getQuoteErrorTranslation } from 'components/MultiHopTrade/components/TradeInput/getQuoteErrorTranslation'
import { useIsTradingActive } from 'components/MultiHopTrade/hooks/useIsTradingActive'
import { RawText } from 'components/Text'
import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
Expand Down Expand Up @@ -62,10 +62,17 @@ export const TradeQuoteLoaded: FC<TradeQuoteProps> = ({
number: { toPercent },
} = useLocaleFormatter()

const { isTradingActive } = useIsTradingActive()

const buyAsset = useAppSelector(selectInputBuyAsset)
const sellAsset = useAppSelector(selectInputSellAsset)
const { isTradingActive: isTradingActiveOnBuyPool } = useIsTradingActive({
assetId: buyAsset.assetId,
})
const { isTradingActive: isTradingActiveOnSellPool } = useIsTradingActive({
assetId: sellAsset.assetId,
})

const isTradingActive = Boolean(isTradingActiveOnBuyPool && isTradingActiveOnSellPool)

const userSlippagePercentageDecimal = useAppSelector(selectUserSlippagePercentageDecimal)

const buyAssetMarketData = useAppSelector(state =>
Expand Down
59 changes: 0 additions & 59 deletions src/components/MultiHopTrade/hooks/useIsTradingActive.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { fromAccountId, fromAssetId, toAssetId } from '@shapeshiftoss/caip'
import { CONTRACT_INTERACTION, FeeDataKey } from '@shapeshiftoss/chain-adapters'
import type { BuildCustomTxInput } from '@shapeshiftoss/chain-adapters/src/evm/types'
import { supportsETH } from '@shapeshiftoss/hdwallet-core'
import { SwapperName } from '@shapeshiftoss/swapper'
import type { Asset, KnownChainIds } from '@shapeshiftoss/types'
import { useQuery } from '@tanstack/react-query'
import { getConfig } from 'config'
import { getOrCreateContractByType } from 'contracts/contractManager'
import { ContractType } from 'contracts/types'
Expand All @@ -29,7 +27,7 @@ import type {
import { DefiStep } from 'features/defi/contexts/DefiManagerProvider/DefiCommon'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { useTranslate } from 'react-polyglot'
import { reactQueries } from 'react-queries'
import { useIsTradingActive } from 'react-queries/hooks/useIsTradingActive'
import { encodeFunctionData, getAddress } from 'viem'
import { Amount } from 'components/Amount/Amount'
import { AssetIcon } from 'components/AssetIcon'
Expand Down Expand Up @@ -540,20 +538,8 @@ export const Confirm: React.FC<ConfirmProps> = ({ accountId, onNext }) => {
})()
}, [accountId, accountMetadata, accountType, assetId, bip44Params, chainAdapter, wallet])

const { data: isTradingActive, refetch: refetchIsTradingActive } = useQuery({
...reactQueries.common.isTradingActive({
assetId,
swapperName: SwapperName.Thorchain,
}),
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
enabled: Boolean(assetId),
// Go stale instantly
staleTime: 0,
// Never store queries in cache since we always want fresh data
gcTime: 0,
refetchOnWindowFocus: true,
refetchOnMount: true,
refetchInterval: 60_000,
const { isTradingActive, refetch: refetchIsTradingActive } = useIsTradingActive({
assetId,
})
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved

const handleDeposit = useCallback(async () => {
Expand All @@ -578,14 +564,14 @@ export const Confirm: React.FC<ConfirmProps> = ({ accountId, onNext }) => {
contextDispatch({ type: ThorchainSaversDepositActionType.SET_LOADING, payload: true })

// Was the pool active when it was fetched at the time of the component mount
// If it wasn't, it's definitely not going to become active again in the few seconds it takes to go from mount to sign click
if (isTradingActive === false) {
throw new Error(`THORChain pool halted for assetId: ${assetId}`)
}

// Refetch the trading active state JIT to ensure the pool didn't just become halted
const { data: isTradingActiveData } = await refetchIsTradingActive()

if (!isTradingActiveData) {
const _isTradingActive = await refetchIsTradingActive()
if (_isTradingActive === false) {
throw new Error(`THORChain pool halted for assetId: ${assetId}`)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import {
import { QueryStatus } from '@reduxjs/toolkit/dist/query'
import type { AccountId } from '@shapeshiftoss/caip'
import { toAssetId } from '@shapeshiftoss/caip'
import { SwapperName } from '@shapeshiftoss/swapper'
import type { Asset } from '@shapeshiftoss/types'
import { TxStatus } from '@shapeshiftoss/unchained-client'
import { useQuery } from '@tanstack/react-query'
import BigNumber from 'bignumber.js'
import type { DefiButtonProps } from 'features/defi/components/DefiActionButtons'
import { Overview } from 'features/defi/components/Overview/Overview'
Expand All @@ -29,7 +27,7 @@ import { DefiAction } from 'features/defi/contexts/DefiManagerProvider/DefiCommo
import { useCallback, useEffect, useMemo, useState } from 'react'
import { FaTwitter } from 'react-icons/fa'
import { useTranslate } from 'react-polyglot'
import { reactQueries } from 'react-queries'
import { useIsTradingActive } from 'react-queries/hooks/useIsTradingActive'
import type { AccountDropdownProps } from 'components/AccountDropdown/AccountDropdown'
import { Amount } from 'components/Amount/Amount'
import { CircularProgress } from 'components/CircularProgress/CircularProgress'
Expand Down Expand Up @@ -130,20 +128,8 @@ export const ThorchainSaversOverview: React.FC<OverviewProps> = ({
[accountId, defaultAccountId, highestBalanceAccountId],
)

const { data: isTradingActive, isLoading: isTradingActiveLoading } = useQuery({
...reactQueries.common.isTradingActive({
assetId,
swapperName: SwapperName.Thorchain,
}),
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
enabled: Boolean(assetId),
// Go stale instantly
staleTime: 0,
// Never store queries in cache since we always want fresh data
gcTime: 0,
refetchOnWindowFocus: true,
refetchOnMount: true,
refetchInterval: 60_000,
const { isTradingActive, isLoading: isTradingActiveLoading } = useIsTradingActive({
assetId,
})

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { bchChainId, fromAccountId, fromAssetId, toAssetId } from '@shapeshiftos
import { FeeDataKey } from '@shapeshiftoss/chain-adapters'
import type { BuildCustomTxInput } from '@shapeshiftoss/chain-adapters/src/evm/types'
import { supportsETH } from '@shapeshiftoss/hdwallet-core'
import { SwapperName } from '@shapeshiftoss/swapper'
import { useQuery } from '@tanstack/react-query'
import { getConfig } from 'config'
import { getOrCreateContractByType } from 'contracts/contractManager'
import { ContractType } from 'contracts/types'
Expand All @@ -30,7 +28,7 @@ import type {
import { DefiStep } from 'features/defi/contexts/DefiManagerProvider/DefiCommon'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { useTranslate } from 'react-polyglot'
import { reactQueries } from 'react-queries'
import { useIsTradingActive } from 'react-queries/hooks/useIsTradingActive'
import { encodeFunctionData, getAddress } from 'viem'
import { Amount } from 'components/Amount/Amount'
import { AssetIcon } from 'components/AssetIcon'
Expand Down Expand Up @@ -594,20 +592,8 @@ export const Confirm: React.FC<ConfirmProps> = ({ accountId, onNext }) => {
return txId
}, [getWithdrawInput, wallet])

const { data: isTradingActive, refetch: refetchIsTradingActive } = useQuery({
...reactQueries.common.isTradingActive({
assetId,
swapperName: SwapperName.Thorchain,
}),
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
enabled: Boolean(assetId),
// Go stale instantly
staleTime: 0,
// Never store queries in cache since we always want fresh data
gcTime: 0,
refetchOnWindowFocus: true,
refetchOnMount: true,
refetchInterval: 60_000,
const { isTradingActive, refetch: refetchIsTradingActive } = useIsTradingActive({
assetId,
})

const handleConfirm = useCallback(async () => {
Expand Down Expand Up @@ -642,14 +628,14 @@ export const Confirm: React.FC<ConfirmProps> = ({ accountId, onNext }) => {
}

// Was the pool active when it was fetched at the time of the component mount
// If it wasn't, it's definitely not going to become active again in the few seconds it takes to go from mount to sign click
if (isTradingActive === false) {
throw new Error(`THORChain pool halted for assetId: ${assetId}`)
}

// Refetch the trading active state JIT to ensure the pool didn't just become halted
const { data: isTradingActiveData } = await refetchIsTradingActive()

if (!isTradingActiveData) {
const _isTradingActive = await refetchIsTradingActive()
if (_isTradingActive === false) {
throw new Error(`THORChain pool halted for assetId: ${assetId}`)
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/swapper/swappers/ThorchainSwapper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export type InboundAddressResponse = {
gas_rate: string
gas_rate_units: string
router?: string
dust_threshold?: string
global_trading_paused: boolean
chain_trading_paused: boolean
chain_lp_actions_paused: boolean
Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils/thorchain/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { bn } from 'lib/bignumber/bignumber'

export const THOR_PRECISION = 8
export const BASE_BPS_POINTS = '10000'
export const THORCHAIN_AFFILIATE_NAME = 'ss'
export const THORCHAIN_POOL_MODULE_ADDRESS = 'thor1g98cy3n9mmjrpn0sxmn63lztelera37n8n67c0'

// Current blocktime as per https://thorchain.network/stats
export const THORCHAIN_BLOCK_TIME_SECONDS = '6.1'
export const thorchainBlockTimeMs = bn(THORCHAIN_BLOCK_TIME_SECONDS).times(1000).toNumber()
3 changes: 3 additions & 0 deletions src/pages/Lending/hooks/useLendingSupportedAssets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const useLendingSupportedAssets = ({ type }: { type: 'collateral' | 'borr

const { data: availablePools } = useQuery({
...reactQueries.thornode.poolsData(),
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
// Infinity staleTime as we handle halted state JIT
staleTime: Infinity,
select: pools => pools.filter(pool => pool.status === 'Available'),
})

Expand Down
13 changes: 10 additions & 3 deletions src/pages/Lending/hooks/useRepaymentLockData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { AccountId, AssetId } from '@shapeshiftoss/caip'
import type { QueryObserverOptions } from '@tanstack/react-query'
import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
import { reactQueries, thorchainBlockTimeSeconds } from 'react-queries'
import { reactQueries } from 'react-queries'
import { bnOrZero } from 'lib/bignumber/bignumber'
import { THORCHAIN_BLOCK_TIME_SECONDS, thorchainBlockTimeMs } from 'lib/utils/thorchain/constants'

import { thorchainLendingPositionQueryFn } from './useLendingPositionData'

Expand All @@ -20,6 +21,9 @@ export const useRepaymentLockData = ({
enabled = true,
}: UseLendingPositionDataProps & QueryObserverOptions) => {
const { data: blockHeight } = useQuery({
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
// We use the block query to get the current height, so we obviously need to mark it stale at the end of each THOR block
staleTime: thorchainBlockTimeMs,
...reactQueries.thornode.block(),
select: block => block.header.height,
enabled,
Expand All @@ -44,6 +48,9 @@ export const useRepaymentLockData = ({

const repaymentLockData = useQuery({
...reactQueries.thornode.mimir(),
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
// We use the mimir query to get the repayment maturity block, so need to mark it stale at the end of each THOR block
staleTime: thorchainBlockTimeMs,
select: mimirData => {
if (!mimirData || !blockHeight) return null

Expand All @@ -52,7 +59,7 @@ export const useRepaymentLockData = ({
// If position is not available, return the repayment maturity as specified by the network, currently about 30 days
if (!position) {
return bnOrZero(repaymentMaturity)
.times(thorchainBlockTimeSeconds)
.times(THORCHAIN_BLOCK_TIME_SECONDS)
.div(60 * 60 * 24)
.toString()
}
Expand All @@ -61,7 +68,7 @@ export const useRepaymentLockData = ({
const repaymentBlock = bnOrZero(position.last_open_height).plus(repaymentMaturity)
return bnOrZero(repaymentBlock)
.minus(blockHeight)
.times(thorchainBlockTimeSeconds)
.times(THORCHAIN_BLOCK_TIME_SECONDS)
.div(60 * 60 * 24)
.toFixed(1)
},
Expand Down
19 changes: 5 additions & 14 deletions src/pages/ThorChainLP/AvailablePools.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { GridProps } from '@chakra-ui/react'
import { Box, Button, Flex, SimpleGrid, Skeleton, Stack, Tag } from '@chakra-ui/react'
import { thorchainAssetId } from '@shapeshiftoss/caip'
import { SwapperName } from '@shapeshiftoss/swapper'
import { useQuery } from '@tanstack/react-query'
import { useCallback, useMemo } from 'react'
import { reactQueries } from 'react-queries'
import { useIsTradingActive } from 'react-queries/hooks/useIsTradingActive'
import { generatePath, useHistory } from 'react-router'
import { Amount } from 'components/Amount/Amount'
import { Main } from 'components/Layout/Main'
Expand Down Expand Up @@ -51,19 +51,8 @@ type PoolButtonProps = {
const PoolButton = ({ pool }: PoolButtonProps) => {
const history = useHistory()

const { data: isTradingActive, isLoading: isTradingActiveLoading } = useQuery({
...reactQueries.common.isTradingActive({
assetId: pool.assetId,
swapperName: SwapperName.Thorchain,
}),
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
// Go stale instantly
staleTime: 0,
// Never store queries in cache since we always want fresh data
gcTime: 0,
refetchOnWindowFocus: true,
refetchOnMount: true,
refetchInterval: 60_000,
const { isTradingActive, isLoading: isTradingActiveLoading } = useIsTradingActive({
assetId: pool?.assetId,
})

const handlePoolClick = useCallback(() => {
Expand All @@ -89,6 +78,8 @@ const PoolButton = ({ pool }: PoolButtonProps) => {

const { data: volume7D, isLoading: isVolume7DLoading } = useQuery({
...reactQueries.midgard.swapsData(pool.assetId, '7d'),
// @lukemorales/query-key-factory only returns queryFn and queryKey - all others will be ignored in the returned object
staleTime: Infinity,
select: data => getVolume(runeMarketData.price, data),
})

Expand Down
Loading
Loading