Skip to content

Commit

Permalink
feat: rfox claim cleanup (#7101)
Browse files Browse the repository at this point in the history
fix: make swapper great again (#7097)

feat: make swapper great again
  • Loading branch information
gomesalexandre authored Jun 12, 2024
1 parent 2c6c77e commit 6aeabc1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 42 deletions.
31 changes: 1 addition & 30 deletions src/pages/RFOX/components/Claim/ClaimConfirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
import {
selectAccountNumberByAccountId,
selectAssetById,
selectFeeAssetByChainId,
selectMarketDataByAssetIdUserCurrency,
selectTxById,
} from 'state/slices/selectors'
Expand Down Expand Up @@ -65,14 +64,6 @@ export const ClaimConfirm: FC<Pick<ClaimRouteProps, 'headerComponent'> & ClaimCo
const translate = useTranslate()
const wallet = useWallet().state.wallet

const feeAsset = useAppSelector(state =>
selectFeeAssetByChainId(state, fromAssetId(claimQuote.stakingAssetId).chainId),
)

const feeAssetMarketDataUserCurrency = useAppSelector(state =>
selectMarketDataByAssetIdUserCurrency(state, feeAsset?.assetId ?? ''),
)

const handleGoBack = useCallback(() => {
history.push(ClaimRoutePaths.Select)
}, [history])
Expand Down Expand Up @@ -157,27 +148,7 @@ export const ClaimConfirm: FC<Pick<ClaimRouteProps, 'headerComponent'> & ClaimCo
},
})

const isGetClaimFeesEnabled = useMemo(
() =>
Boolean(
isClaimMutationIdle &&
stakingAssetAccountNumber !== undefined &&
wallet &&
stakingAsset &&
callData &&
feeAsset &&
feeAssetMarketDataUserCurrency,
),
[
isClaimMutationIdle,
stakingAssetAccountNumber,
wallet,
stakingAsset,
callData,
feeAsset,
feeAssetMarketDataUserCurrency,
],
)
const isGetClaimFeesEnabled = useMemo(() => Boolean(isClaimMutationIdle), [isClaimMutationIdle])

const claimFeesQueryInput = useMemo(
() => ({
Expand Down
15 changes: 3 additions & 12 deletions src/pages/RFOX/components/Claim/ClaimSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { RFOX_PROXY_CONTRACT_ADDRESS } from 'contracts/constants'
import dayjs from 'dayjs'
import { type FC, useCallback, useEffect, useMemo } from 'react'
import { useTranslate } from 'react-polyglot'
import type { Address } from 'viem'
import { getAddress } from 'viem'
import { arbitrum } from 'viem/chains'
import { useReadContract, useReadContracts } from 'wagmi'
import { useReadContracts } from 'wagmi'
import { AssetIcon } from 'components/AssetIcon'
import { SlideTransition } from 'components/SlideTransition'
import { RawText, Text } from 'components/Text'
import { fromBaseUnit } from 'lib/math'
import { chainIdToChainDisplayName } from 'lib/utils'
import { useGetUnstakingRequestCountQuery } from 'pages/RFOX/hooks/useGetUnstakingRequestCountQuery'
import { RfoxTabIndex } from 'pages/RFOX/RFOX'
import { selectAssetById, selectFirstAccountIdByChainId } from 'state/slices/selectors'
import { useAppSelector } from 'state/store'
Expand Down Expand Up @@ -99,16 +99,7 @@ export const ClaimSelect: FC<ClaimSelectProps & ClaimRouteProps> = ({
isSuccess: isUnstakingRequestCountSuccess,
isLoading: isUnstakingRequestCountLoading,
refetch: refetchUnstakingRequestCount,
} = useReadContract({
abi: foxStakingV1Abi,
address: RFOX_PROXY_CONTRACT_ADDRESS,
functionName: 'getUnstakingRequestCount',
args: [stakingAssetAccountAddress ? getAddress(stakingAssetAccountAddress) : ('' as Address)], // actually defined, see enabled below
chainId: arbitrum.id,
query: {
enabled: Boolean(stakingAssetAccountAddress),
},
})
} = useGetUnstakingRequestCountQuery({ stakingAssetAccountAddress })

const hasClaims = useMemo(
() =>
Expand Down
71 changes: 71 additions & 0 deletions src/pages/RFOX/hooks/useGetUnstakingRequestCountQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { skipToken } from '@tanstack/react-query'
import { foxStakingV1Abi } from 'contracts/abis/FoxStakingV1'
import { RFOX_PROXY_CONTRACT_ADDRESS } from 'contracts/constants'
import { useMemo } from 'react'
import type { Address, ReadContractReturnType } from 'viem'
import { getAddress } from 'viem'
import { readContract } from 'viem/actions'
import { arbitrum } from 'viem/chains'
import type { Config } from 'wagmi'
import { type ReadContractQueryKey, useQuery } from 'wagmi/query'
import { viemClientByNetworkId } from 'lib/viem-client'

type GetUnstakingRequestCountQueryKey = ReadContractQueryKey<
typeof foxStakingV1Abi,
'getUnstakingRequestCount',
readonly [Address],
Config
>
type UnstakingRequestCount = ReadContractReturnType<
typeof foxStakingV1Abi,
'getUnstakingRequestCount',
readonly [Address]
>
type UseGetUnstakingRequestCountQueryProps<SelectData = UnstakingRequestCount> = {
stakingAssetAccountAddress: string | undefined
select?: (unstakingRequestCount: UnstakingRequestCount) => SelectData
}
const client = viemClientByNetworkId[arbitrum.id]

export const useGetUnstakingRequestCountQuery = <SelectData = UnstakingRequestCount>({
stakingAssetAccountAddress,
select,
}: UseGetUnstakingRequestCountQueryProps<SelectData>) => {
// wagmi doesn't expose queryFn, so we reconstruct the queryKey and queryFn ourselves to leverage skipToken type safety
const queryKey: GetUnstakingRequestCountQueryKey = useMemo(
() => [
'readContract',
{
address: RFOX_PROXY_CONTRACT_ADDRESS,
functionName: 'getUnstakingRequestCount',
args: [
stakingAssetAccountAddress ? getAddress(stakingAssetAccountAddress) : ('' as Address),
],
chainId: arbitrum.id,
},
],
[stakingAssetAccountAddress],
)

const getUnstakingRequestCountQueryFn = useMemo(
() =>
stakingAssetAccountAddress
? () =>
readContract(client, {
abi: foxStakingV1Abi,
address: RFOX_PROXY_CONTRACT_ADDRESS,
functionName: 'getUnstakingRequestCount',
args: [getAddress(stakingAssetAccountAddress)],
})
: skipToken,
[stakingAssetAccountAddress],
)

const unstakingRequestCountQuery = useQuery({
queryKey,
queryFn: getUnstakingRequestCountQueryFn,
select,
})

return unstakingRequestCountQuery
}

0 comments on commit 6aeabc1

Please sign in to comment.