-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make swapper great again (#7097) feat: make swapper great again
- Loading branch information
1 parent
2c6c77e
commit 6aeabc1
Showing
3 changed files
with
75 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |