Skip to content

Commit

Permalink
fix: read mutability for multicall and propagate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Dec 10, 2022
1 parent b92e301 commit e9f688b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
13 changes: 6 additions & 7 deletions packages/react/src/common/hooks/useMultiCallRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ContractCall } from '@flair-sdk/registry';
import { ReadContractConfig } from '@wagmi/core';
import { utils } from 'ethers';
import { useEffect, useMemo, useState } from 'react';
import { useContractRead, useNetwork } from 'wagmi';
import { useContractRead } from 'wagmi';

type Config = Omit<ReadContractConfig, 'args' | 'functionName' | 'address'> & {
address?: string;
Expand Down Expand Up @@ -76,7 +76,7 @@ export const useMultiCallRead = <TData extends any[]>({
type: 'bytes[]',
},
],
stateMutability: 'nonpayable',
stateMutability: 'view',
type: 'function',
},
],
Expand All @@ -94,7 +94,6 @@ export const useMultiCallRead = <TData extends any[]>({
!calls ||
!enabled ||
result.isLoading ||
result.fetchStatus !== 'idle' ||
result.isFetching ||
result.isRefetching
) {
Expand All @@ -112,9 +111,9 @@ export const useMultiCallRead = <TData extends any[]>({
throw new Error(`Call not found for result ${index}`);
}

const iface = new utils.Interface([
(abi as any) || `function ${call.function}`,
]);
const iface = new utils.Interface(
abi ? (abi as any) : [`function ${call.function}`],
);

if (!call.function || !iface.functions[call.function]) {
throw new Error(
Expand Down Expand Up @@ -148,7 +147,7 @@ export const useMultiCallRead = <TData extends any[]>({

return {
...result,
error,
error: error || (result.status === 'error' && result.error),
data: resultData,
};
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { BigNumberish } from 'ethers';

import {
PredefinedReadContractConfig,
useContractRead,
} from '../../../../common';
import { PredefinedReadContractConfig } from '../../../../common';
import { useContractRead } from '../../../../common/hooks/useContractRead';
import { Tier } from '../types';

type ArgsType = [tierId: BigNumberish];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import '@wagmi/core';

import { Environment, ZERO_BYTES32 } from '@flair-sdk/common';
import { QueryFunctionContext, useQuery } from '@tanstack/react-query';
import { BigNumber, BigNumberish, BytesLike, ethers } from 'ethers';
import { BigNumber, BigNumberish, BytesLike } from 'ethers';
import { useCallback } from 'react';

import { Environment, ZERO_BYTES32 } from '@flair-sdk/common';

import { PredefinedReadContractConfig } from '../../../../common';
import { useMergeQueryStates } from '../../../../core';
import { Tier, TiersDictionary } from '../types';
Expand All @@ -30,6 +29,7 @@ export const useSaleTiers = ({
contractAddress,
enabled,
minterAddress,
...restOfConfig
}: Config) => {
const { call: checkAllowlist } = useTieredSalesAllowlistChecker({
env,
Expand All @@ -56,6 +56,7 @@ export const useSaleTiers = ({
chainId,
contractAddress,
enabled,
...restOfConfig,
});

const queryKey = [
Expand Down Expand Up @@ -183,6 +184,7 @@ export const useSaleTiers = ({
),
cacheTime: 60,
staleTime: 30,
...restOfConfig,
});

const mergedStates = useMergeQueryStates([saleTiersQuery, tiersConfigsQuery]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import '@wagmi/core';
import { BigNumber, BigNumberish, ethers } from 'ethers';
import _ from 'lodash';
import { useMemo } from 'react';

import { PredefinedReadContractConfig } from '../../../../common';
import { useMultiCallRead } from '../../../../common/hooks/useMultiCallRead';
import { PredefinedReadContractConfig, useMultiCallRead } from '../../../../common';
import { Tier } from '../types';
import { normalizeTiers } from '../util';

type ArgsType = [tierId: BigNumberish];

type Config = {
chainId?: number;
address?: `0x${string}`;
contractAddress?: `0x${string}`;
enabled?: boolean;
} & PredefinedReadContractConfig<ArgsType>;

Expand Down Expand Up @@ -81,7 +79,11 @@ const abi = [
},
] as const;

export const useSaleTiersConfigs = ({ chainId, address, enabled }: Config) => {
export const useSaleTiersConfigs = ({
chainId,
contractAddress,
enabled,
}: Config) => {
// Create an array of calls to get tiers by index from 0 to 20
const calls = useMemo(() => {
const calls = [];
Expand All @@ -97,9 +99,9 @@ export const useSaleTiersConfigs = ({ chainId, address, enabled }: Config) => {

const result = useMultiCallRead<Tier[]>({
chainId,
address: address || ethers.constants.AddressZero,
address: contractAddress || ethers.constants.AddressZero,
abi,
enabled: Boolean(enabled && address),
enabled: Boolean(enabled && contractAddress),
calls,
});

Expand Down

0 comments on commit e9f688b

Please sign in to comment.