Skip to content

Commit

Permalink
feat: add chain ID to query hooks (#1753)
Browse files Browse the repository at this point in the history
  • Loading branch information
therealemjy authored Nov 10, 2023
1 parent 41b0edc commit df0ea43
Show file tree
Hide file tree
Showing 56 changed files with 561 additions and 172 deletions.
17 changes: 10 additions & 7 deletions src/clients/api/queries/getAllowance/useGetAllowance.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { useGetTokenContract } from 'packages/contracts';
import { QueryObserverOptions, useQuery } from 'react-query';
import { Token } from 'types';
import { ChainId, Token } from 'types';
import { callOrThrow } from 'utilities';

import getAllowance, {
GetAllowanceInput,
GetAllowanceOutput,
} from 'clients/api/queries/getAllowance';
import FunctionKey from 'constants/functionKey';
import { useAuth } from 'context/AuthContext';

type TrimmedGetAllowanceInput = Omit<GetAllowanceInput, 'tokenContract'> & { token: Token };

export type UseGetAllowanceQueryKey = [
FunctionKey.GET_TOKEN_ALLOWANCE,
{
Omit<TrimmedGetAllowanceInput, 'token'> & {
chainId: ChainId;
tokenAddress: string;
spenderAddress: string;
accountAddress: string;
},
];

Expand All @@ -26,19 +28,20 @@ type Options = QueryObserverOptions<
UseGetAllowanceQueryKey
>;

type TrimmedGetAllowanceInput = Omit<GetAllowanceInput, 'tokenContract'> & { token: Token };

const useGetAllowance = (
{ token, spenderAddress, accountAddress }: TrimmedGetAllowanceInput,
options?: Options,
) => {
const { chainId } = useAuth();
const tokenContract = useGetTokenContract({ token });

const queryKey: UseGetAllowanceQueryKey = [
FunctionKey.GET_TOKEN_ALLOWANCE,
{
chainId,
accountAddress,
tokenAddress: token.address,
spenderAddress,
accountAddress,
},
];

Expand Down
24 changes: 15 additions & 9 deletions src/clients/api/queries/getBalanceOf/useGetBalanceOf.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import { QueryObserverOptions, useQuery } from 'react-query';
import { ChainId } from 'types';

import { GetBalanceOfInput, GetBalanceOfOutput, getBalanceOf } from 'clients/api';
import FunctionKey from 'constants/functionKey';
import { useAuth } from 'context/AuthContext';

type TrimmedGetBalanceOfInput = Omit<GetBalanceOfInput, 'signer' | 'provider'>;

export type UseGetBalanceOfQueryKey = [
FunctionKey.GET_BALANCE_OF,
Omit<TrimmedGetBalanceOfInput, 'token'> & {
tokenAddress: string;
chainId: ChainId;
},
];

type Options = QueryObserverOptions<
GetBalanceOfOutput,
Error,
GetBalanceOfOutput,
GetBalanceOfOutput,
[
FunctionKey.GET_BALANCE_OF,
{
accountAddress: string;
tokenAddress: string;
},
]
UseGetBalanceOfQueryKey
>;

const useGetBalanceOf = (
{ accountAddress, token }: Omit<GetBalanceOfInput, 'signer' | 'provider'>,
{ accountAddress, token }: TrimmedGetBalanceOfInput,
options?: Options,
) => {
const { provider } = useAuth();
const { provider, chainId } = useAuth();

return useQuery(
[
FunctionKey.GET_BALANCE_OF,
{
chainId,
accountAddress,
tokenAddress: token.address,
},
Expand Down
9 changes: 6 additions & 3 deletions src/clients/api/queries/getBlockNumber/useGetBlockNumber.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { QueryObserverOptions, useQuery } from 'react-query';
import { ChainId } from 'types';

import { getBlockNumber } from 'clients/api/';
import { BLOCK_TIME_MS } from 'constants/bsc';
import FunctionKey from 'constants/functionKey';
import { useAuth } from 'context/AuthContext';

export type UseGetBlockNumberQueryKey = [FunctionKey.GET_BLOCK_NUMBER, { chainId: ChainId }];

interface GetBlockNumberOutput {
blockNumber: number;
}
Expand All @@ -14,13 +17,13 @@ type Options = QueryObserverOptions<
Error,
GetBlockNumberOutput,
GetBlockNumberOutput,
FunctionKey.GET_BLOCK_NUMBER
UseGetBlockNumberQueryKey
>;

const useGetBlockNumber = (options?: Options) => {
const { provider } = useAuth();
const { provider, chainId } = useAuth();

return useQuery(FunctionKey.GET_BLOCK_NUMBER, () => getBlockNumber({ provider }), {
return useQuery([FunctionKey.GET_BLOCK_NUMBER, { chainId }], () => getBlockNumber({ provider }), {
refetchInterval: BLOCK_TIME_MS,
...options,
});
Expand Down
13 changes: 11 additions & 2 deletions src/clients/api/queries/getCurrentVotes/useGetCurrentVotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ import getCurrentVotes, {
GetCurrentVotesInput,
GetCurrentVotesOutput,
} from 'clients/api/queries/getCurrentVotes';
import { governanceChain } from 'clients/web3';
import FunctionKey from 'constants/functionKey';

type TrimmedGetCurrentVotesInput = Omit<GetCurrentVotesInput, 'xvsVaultContract'>;

export type UseGetCurrentVotesQueryKey = [
FunctionKey.GET_CURRENT_VOTES,
TrimmedGetCurrentVotesInput,
];

type Options = QueryObserverOptions<
GetCurrentVotesOutput,
Error,
GetCurrentVotesOutput,
GetCurrentVotesOutput,
[FunctionKey.GET_CURRENT_VOTES, TrimmedGetCurrentVotesInput]
UseGetCurrentVotesQueryKey
>;

const useGetCurrentVotes = (input: TrimmedGetCurrentVotesInput, options?: Options) => {
const xvsVaultContract = useGetXvsVaultContract();
const xvsVaultContract = useGetXvsVaultContract({
chainId: governanceChain.id,
});

return useQuery(
[FunctionKey.GET_CURRENT_VOTES, input],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useGetPrimeContract } from 'packages/contracts';
import { QueryObserverOptions, useQuery } from 'react-query';
import { ChainId } from 'types';
import { callOrThrow } from 'utilities';

import {
Expand All @@ -8,27 +9,36 @@ import {
getHypotheticalPrimeApys,
} from 'clients/api';
import FunctionKey from 'constants/functionKey';
import { useAuth } from 'context/AuthContext';
import { useIsFeatureEnabled } from 'hooks/useIsFeatureEnabled';

interface UseGetPrimeTokenInput
extends Omit<GetHypotheticalPrimeApysInput, 'primeContract' | 'accountAddress'> {
accountAddress?: string;
}

export type UseGetHypotheticalPrimeApysQueryKey = [
FunctionKey.GET_HYPOTHETICAL_PRIME_APYS,
UseGetPrimeTokenInput & {
chainId: ChainId;
},
];

type Options = QueryObserverOptions<
GetHypotheticalPrimeApysOutput,
Error,
GetHypotheticalPrimeApysOutput,
GetHypotheticalPrimeApysOutput,
[FunctionKey.GET_HYPOTHETICAL_PRIME_APYS, UseGetPrimeTokenInput]
UseGetHypotheticalPrimeApysQueryKey
>;

const useGetHypotheticalPrimeApys = (input: UseGetPrimeTokenInput, options?: Options) => {
const { chainId } = useAuth();
const isPrimeEnabled = useIsFeatureEnabled({ name: 'prime' });
const primeContract = useGetPrimeContract();

return useQuery(
[FunctionKey.GET_HYPOTHETICAL_PRIME_APYS, input],
[FunctionKey.GET_HYPOTHETICAL_PRIME_APYS, { ...input, chainId }],
() =>
callOrThrow({ primeContract, accountAddress: input.accountAddress }, params =>
getHypotheticalPrimeApys({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import getIsAddressAuthorized, {
} from 'clients/api/queries/getIsAddressAuthorized';
import FunctionKey from 'constants/functionKey';

export type UseGetIsAddressAuthorizedQueryKey = [
FunctionKey.GET_IS_ADDRESS_AUTHORIZED,
{ accountAddress: string },
];

type Options = QueryObserverOptions<
GetIsAddressAuthorizedOutput,
Error,
GetIsAddressAuthorizedOutput,
GetIsAddressAuthorizedOutput,
[FunctionKey.GET_IS_ADDRESS_AUTHORIZED, { accountAddress: string }]
UseGetIsAddressAuthorizedQueryKey
>;

const ONE_HOUR_MS = 60 * 60 * 1000;
Expand All @@ -23,7 +28,9 @@ const useGetIsAddressAuthorized = (accountAddress: string, options?: Options) =>
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
staleTime: ONE_HOUR_MS,
refetchInterval: ONE_HOUR_MS,
staleTime: Infinity,
cacheTime: Infinity,
...options,
},
);
Expand Down
14 changes: 11 additions & 3 deletions src/clients/api/queries/getIsolatedPools/useGetIsolatedPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from 'packages/contracts';
import { useGetTokens } from 'packages/tokens';
import { QueryObserverOptions, useQuery } from 'react-query';
import { ChainId } from 'types';
import { callOrThrow, generatePseudoRandomRefetchInterval } from 'utilities';

import getIsolatedPools, {
Expand All @@ -23,26 +24,33 @@ type TrimmedInput = Omit<
| 'tokens'
>;

export type UseGetIsolatedPoolsQueryKey = [
FunctionKey.GET_ISOLATED_POOLS,
TrimmedInput & {
chainId: ChainId;
},
];

type Options = QueryObserverOptions<
GetIsolatedPoolsOutput,
Error,
GetIsolatedPoolsOutput,
GetIsolatedPoolsOutput,
[FunctionKey.GET_ISOLATED_POOLS, TrimmedInput]
UseGetIsolatedPoolsQueryKey
>;

const refetchInterval = generatePseudoRandomRefetchInterval();

const useGetIsolatedPools = (input: TrimmedInput, options?: Options) => {
const { provider } = useAuth();
const { provider, chainId } = useAuth();
const tokens = useGetTokens();

const poolLensContract = useGetPoolLensContract();
const resilientOracleContract = useGetResilientOracleContract();
const poolRegistryContractAddress = useGetPoolRegistryContractAddress();

return useQuery(
[FunctionKey.GET_ISOLATED_POOLS, input],
[FunctionKey.GET_ISOLATED_POOLS, { ...input, chainId }],
() =>
callOrThrow(
{ poolLensContract, poolRegistryContractAddress, resilientOracleContract },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import getLatestProposalIdByProposer, {
GetLatestProposalIdByProposerInput,
GetLatestProposalIdByProposerOutput,
} from 'clients/api/queries/getLatestProposalIdByProposer';
import { governanceChain } from 'clients/web3';
import { BLOCK_TIME_MS } from 'constants/bsc';
import FunctionKey from 'constants/functionKey';

Expand All @@ -21,7 +22,9 @@ const useGetLatestProposalIdByProposer = (
{ accountAddress }: Omit<GetLatestProposalIdByProposerInput, 'governorBravoDelegateContract'>,
options?: Options,
) => {
const governorBravoDelegateContract = useGetGovernorBravoDelegateContract();
const governorBravoDelegateContract = useGetGovernorBravoDelegateContract({
chainId: governanceChain.id,
});

return useQuery(
[FunctionKey.GET_LATEST_PROPOSAL_ID_BY_PROPOSER, accountAddress],
Expand Down
12 changes: 10 additions & 2 deletions src/clients/api/queries/getMainPool/useGetMainPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
import { useGetToken, useGetTokens } from 'packages/tokens';
import { QueryObserverOptions, useQuery } from 'react-query';
import { useTranslation } from 'translation';
import { ChainId } from 'types';
import { callOrThrow, generatePseudoRandomRefetchInterval } from 'utilities';

import getMainPool, { GetMainPoolInput, GetMainPoolOutput } from 'clients/api/queries/getMainPool';
import FunctionKey from 'constants/functionKey';
import { useAuth } from 'context/AuthContext';
import { useIsFeatureEnabled } from 'hooks/useIsFeatureEnabled';

type TrimmedInput = Omit<
Expand All @@ -28,17 +30,23 @@ type TrimmedInput = Omit<
| 'tokens'
>;

export type UseGetMainPoolQueryKey = [
FunctionKey.GET_MAIN_POOL,
TrimmedInput & { chainId: ChainId },
];

type Options = QueryObserverOptions<
GetMainPoolOutput,
Error,
GetMainPoolOutput,
GetMainPoolOutput,
[FunctionKey.GET_MAIN_POOL, TrimmedInput]
UseGetMainPoolQueryKey
>;

const refetchInterval = generatePseudoRandomRefetchInterval();

const useGetMainPool = (input: TrimmedInput, options?: Options) => {
const { chainId } = useAuth();
const { t } = useTranslation();

const xvs = useGetToken({ symbol: 'XVS' });
Expand All @@ -55,7 +63,7 @@ const useGetMainPool = (input: TrimmedInput, options?: Options) => {
const primeContract = useGetPrimeContract();

return useQuery(
[FunctionKey.GET_MAIN_POOL, input],
[FunctionKey.GET_MAIN_POOL, { ...input, chainId }],
() =>
callOrThrow(
{
Expand Down
13 changes: 11 additions & 2 deletions src/clients/api/queries/getMintableVai/useGetMintableVai.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import { useGetVaiControllerContract } from 'packages/contracts';
import { QueryObserverOptions, useQuery } from 'react-query';
import { ChainId } from 'types';
import { callOrThrow } from 'utilities';

import getMintableVai, {
GetMintableVaiInput,
GetMintableVaiOutput,
} from 'clients/api/queries/getMintableVai';
import FunctionKey from 'constants/functionKey';
import { useAuth } from 'context/AuthContext';

type TrimmedGetMintableVaiInput = Omit<GetMintableVaiInput, 'vaiControllerContract'>;

export type UseGetMintableVaiQueryKey = [
FunctionKey.GET_MINTABLE_VAI,
TrimmedGetMintableVaiInput & { chainId: ChainId },
];

type Options = QueryObserverOptions<
GetMintableVaiOutput | undefined,
Error,
GetMintableVaiOutput | undefined,
GetMintableVaiOutput | undefined,
[FunctionKey.GET_MINTABLE_VAI, TrimmedGetMintableVaiInput]
UseGetMintableVaiQueryKey
>;

const useGetMintableVai = (input: TrimmedGetMintableVaiInput, options?: Options) => {
const { chainId } = useAuth();
const vaiControllerContract = useGetVaiControllerContract();

return useQuery(
[FunctionKey.GET_MINTABLE_VAI, input],
[FunctionKey.GET_MINTABLE_VAI, { ...input, chainId }],
() =>
callOrThrow({ vaiControllerContract }, params =>
getMintableVai({
Expand Down
Loading

0 comments on commit df0ea43

Please sign in to comment.