Skip to content

Commit

Permalink
fix: Pass isEnabled to balance/allowance queries (#3639)
Browse files Browse the repository at this point in the history
* fix: Pass isEnabled to balance/allowance queries

* chore: Fix tests
  • Loading branch information
garethfuller authored Jul 6, 2023
1 parent 3eeb059 commit dd409e1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/composables/queries/useAllowancesQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('Returns token allowances from balancer SDK', async () => {
]);

const { result } = mountComposable(() =>
useAllowancesQuery(tokens, spenders)
useAllowancesQuery({ tokens, contractAddresses: spenders })
);

const data = await waitForQueryData(result);
Expand Down
18 changes: 11 additions & 7 deletions src/composables/queries/useAllowancesQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ import useNetwork from '../useNetwork';
*/
type QueryResponse = ContractAllowancesMap;
type QueryOptions = UseQueryOptions<QueryResponse>;
interface QueryInputs {
tokens: Ref<TokenInfoMap>;
contractAddresses: Ref<string[]>;
isEnabled?: Ref<boolean>;
}

/**
* Fetches all allowances for given tokens for each provided contract address.
*/
export default function useAllowancesQuery(
tokens: Ref<TokenInfoMap> = ref({}),
contractAddresses: Ref<string[]> = ref([]),
options: QueryOptions = {}
) {
export default function useAllowancesQuery({
tokens,
contractAddresses,
isEnabled = ref(true),
}: QueryInputs) {
/**
* COMPOSABLES
*/
Expand All @@ -32,7 +37,7 @@ export default function useAllowancesQuery(
/**
* COMPUTED
*/
const enabled = computed(() => isWalletReady.value);
const enabled = computed(() => isWalletReady.value && isEnabled.value);
const tokenAddresses = computed(() => Object.keys(tokens.value));

/**
Expand Down Expand Up @@ -62,7 +67,6 @@ export default function useAllowancesQuery(
enabled,
keepPreviousData: true,
refetchOnWindowFocus: false,
...options,
});

return useQuery<QueryResponse>(
Expand Down
4 changes: 1 addition & 3 deletions src/composables/queries/useBalancesQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ test('Returns token balances', async () => {
[daiAddress]: aTokenInfo({ address: daiAddress }),
});

const { result } = mountComposable(() =>
useBalancesQuery(tokens, { keepPreviousData: true })
);
const { result } = mountComposable(() => useBalancesQuery({ tokens }));

const data = await waitForQueryData(result);

Expand Down
15 changes: 9 additions & 6 deletions src/composables/queries/useBalancesQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ import useNetwork from '../useNetwork';
*/
type QueryResponse = BalanceMap;
type QueryOptions = UseQueryOptions<QueryResponse>;
interface QueryInputs {
tokens: Ref<TokenInfoMap>;
isEnabled?: Ref<boolean>;
}

/**
* Fetches all balances for provided tokens.
*/
export default function useBalancesQuery(
tokens: Ref<TokenInfoMap> = ref({}),
options: QueryOptions = {}
) {
export default function useBalancesQuery({
tokens,
isEnabled = ref(true),
}: QueryInputs) {
/**
* COMPOSABLES
*/
Expand All @@ -32,7 +36,7 @@ export default function useBalancesQuery(
/**
* COMPUTED
*/
const enabled = computed(() => isWalletReady.value);
const enabled = computed(() => isWalletReady.value && isEnabled.value);
const tokenAddresses = computed(() => Object.keys(tokens.value));

/**
Expand All @@ -51,7 +55,6 @@ export default function useBalancesQuery(
enabled,
keepPreviousData: true,
refetchOnWindowFocus: false,
...options,
});

return useQuery<QueryResponse>(
Expand Down
8 changes: 5 additions & 3 deletions src/providers/tokens.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const tokensProvider = (
isRefetching: balanceQueryRefetching,
isError: balancesQueryError,
refetch: refetchBalances,
} = useBalancesQuery(tokens, { enabled: queriesEnabled });
} = useBalancesQuery({ tokens, isEnabled: queriesEnabled });

const {
data: allowanceData,
Expand All @@ -173,8 +173,10 @@ export const tokensProvider = (
isRefetching: allowanceQueryRefetching,
isError: allowancesQueryError,
refetch: refetchAllowances,
} = useAllowancesQuery(tokens, toRef(state, 'spenders'), {
enabled: queriesEnabled,
} = useAllowancesQuery({
tokens,
contractAddresses: toRef(state, 'spenders'),
isEnabled: queriesEnabled,
});

const prices = computed(
Expand Down

1 comment on commit dd409e1

@vercel
Copy link

@vercel vercel bot commented on dd409e1 Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.