Skip to content

Commit

Permalink
Add requiredFilters param to useScaffoldEventHistory hook
Browse files Browse the repository at this point in the history
  • Loading branch information
damianmarti committed Nov 22, 2023
1 parent a586fc2 commit defe6ef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
* @param config.transactionData - if set to true it will return the transaction data for each event (default: false)
* @param config.receiptData - if set to true it will return the receipt data for each event (default: false)
* @param config.watch - if set to true, the events will be updated every pollingInterval milliseconds set at scaffoldConfig (default: false)
* @param config.requiredFilters - if a required filter is not set, no events are returned and the error is set (default: empty)
*/
export const useScaffoldEventHistory = <
TContractName extends ContractName,
Expand All @@ -42,6 +43,7 @@ export const useScaffoldEventHistory = <
transactionData,
receiptData,
watch,
requiredFilters,
}: UseScaffoldEventHistoryConfig<TContractName, TEventName, TBlockData, TTransactionData, TReceiptData>) => {
const [events, setEvents] = useState<any[]>();
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -63,6 +65,13 @@ export const useScaffoldEventHistory = <
part => part.type === "event" && part.name === eventName,
) as AbiEvent;

if (requiredFilters && requiredFilters.length > 0) {
const missingFields = requiredFilters.filter(field => !filters || !filters[field]);
if (missingFields.length > 0) {
throw new Error(`Missing required fields: ${missingFields.join(", ")}`);
}
}

const blockNumber = await publicClient.getBlockNumber({ cacheTime: 0 });

if ((fromBlock && blockNumber >= fromBlock) || blockNumber >= fromBlockUpdated) {
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/utils/scaffold-eth/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export type UseScaffoldEventHistoryConfig<
transactionData?: TTransactionData;
receiptData?: TReceiptData;
watch?: boolean;
requiredFilters?: IndexedEventInputs<TContractName, TEventName>["name"][];
};

export type UseScaffoldEventHistoryData<
Expand Down

0 comments on commit defe6ef

Please sign in to comment.