Skip to content

Commit

Permalink
feat: more robust eligible amount calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Oct 11, 2022
1 parent 490dc43 commit 4b431dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,23 @@ export const useSaleTiers = ({
})
: ({} as any);

const eligibleAmount =
finalAddress && (merkleProof !== undefined || !hasAllowlist)
? await getEligibleAmount({
tierId,
minterAddress: finalAddress,
maxAllowance: merkleMetadata?.maxAllowance,
merkleProof,
})
: undefined;
let eligibleAmount;

try {
eligibleAmount =
finalAddress && (merkleProof !== undefined || !hasAllowlist)
? await getEligibleAmount({
args: [
tierId,
finalAddress,
merkleMetadata?.maxAllowance,
merkleProof,
],
})
: undefined;
} catch (e) {
console.warn(`Error when fetching eligible amount: `, e);
}

const remainingSupply = await getTierRemainingSupply(tierId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ZERO_ADDRESS } from '@flair-sdk/common';
import { BigNumberish, BytesLike } from 'ethers';
import { useCallback, useMemo, useState } from 'react';

import { PredefinedReadContractConfig } from '../../../../common';
import { useContractRead } from '../../../../common/hooks/useContractRead';
Expand All @@ -20,23 +19,14 @@ type Config = {
} & PredefinedReadContractConfig<ArgsType>;

export const useTieredSalesEligibleAmount = (config: Config) => {
const [data, setData] = useState<BigNumberish | undefined>();
const [error, setError] = useState();

useMemo(() => {
setData(undefined);
setError(undefined);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [config.tierId]);

const result = useContractRead<BigNumberish, ArgsType>({
return useContractRead<BigNumberish, ArgsType>({
contractInterface: [
'function eligibleForTier(uint256 tierId,address minter,uint256 maxAllowance,bytes32[] calldata proof) external view returns (uint256)',
],
functionName: 'eligibleForTier(uint256,address,uint256,bytes32[])',
cacheOnBlock: false,
cacheTime: 10,
staleTime: 2,
cacheTime: 0,
staleTime: 0,
args: [
config.tierId || 0,
config.minterAddress || ZERO_ADDRESS,
Expand All @@ -47,64 +37,6 @@ export const useTieredSalesEligibleAmount = (config: Config) => {
config.enabled &&
config.tierId !== undefined &&
config.minterAddress !== undefined,
onSettled(data: any, error: any) {
if (error) {
if (!['NOT_STARTED', 'NOT_ALLOWLISTED'].includes(error?.reason)) {
setError(error);
}
} else {
setData(data);
setError(undefined);
}
},
...config,
});

const call = useCallback(
async (overrides?: {
tierId?: BigNumberish;
minterAddress?: BytesLike;
maxAllowance?: BigNumberish;
merkleProof?: BytesLike[];
}) => {
setData(undefined);
setError(undefined);

try {
return await result.call({
args: [
overrides?.tierId !== undefined
? overrides?.tierId
: config.tierId || 0,
overrides?.minterAddress || config.minterAddress || ZERO_ADDRESS,
overrides?.maxAllowance || config.maxAllowance || 1,
overrides?.merkleProof || config.merkleProof || [],
] as ArgsType,
});
} catch (error: any) {
if (
[
'NOT_STARTED',
'MAXED_ALLOWANCE',
'ALREADY_ENDED',
'NOT_ALLOWLISTED',
].includes(error?.reason)
) {
return 0;
}

setData(undefined);
setError(undefined);
}
},
[
config.maxAllowance,
config.merkleProof,
config.minterAddress,
config.tierId,
result,
],
);

return { ...result, error, data, call } as const;
};

0 comments on commit 4b431dd

Please sign in to comment.