From f824abe8de02a092ca48b6ffa8ca1243d563ed6f Mon Sep 17 00:00:00 2001 From: Rinat Date: Mon, 16 Dec 2024 09:17:14 +0100 Subject: [PATCH] Fix useScaffoldEventHistory duplicated events (#1014) --- .../hooks/scaffold-eth/useScaffoldEventHistory.ts | 15 +++++++++++++-- packages/nextjs/utils/scaffold-eth/contract.ts | 3 +-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts b/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts index 2477a9b20..73c8e98e5 100644 --- a/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts +++ b/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts @@ -121,8 +121,18 @@ export const useScaffoldEventHistory = < }, enabled: enabled && isContractAddressAndClientReady, initialPageParam: fromBlock, - getNextPageParam: () => { - return blockNumber; + getNextPageParam: (lastPage, allPages, lastPageParam) => { + if (!blockNumber || fromBlock >= blockNumber) return undefined; + + const lastPageHighestBlock = Math.max( + Number(fromBlock), + ...(lastPage || []).map(event => Number(event.blockNumber || 0)), + ); + const nextBlock = BigInt(Math.max(Number(lastPageParam), lastPageHighestBlock) + 1); + + if (nextBlock > blockNumber) return undefined; + + return nextBlock; }, select: data => { const events = data.pages.flat(); @@ -133,6 +143,7 @@ export const useScaffoldEventHistory = < TTransactionData, TReceiptData >; + return { pages: eventHistoryData?.reverse(), pageParams: data.pageParams, diff --git a/packages/nextjs/utils/scaffold-eth/contract.ts b/packages/nextjs/utils/scaffold-eth/contract.ts index b66f42a9b..e405cf480 100644 --- a/packages/nextjs/utils/scaffold-eth/contract.ts +++ b/packages/nextjs/utils/scaffold-eth/contract.ts @@ -297,7 +297,6 @@ export type UseScaffoldEventHistoryData< | IsContractDeclarationMissing< any[], { - log: Log; args: AbiParametersToPrimitiveTypes & GetEventArgs< ContractAbi, @@ -309,7 +308,7 @@ export type UseScaffoldEventHistoryData< blockData: TBlockData extends true ? Block : null; receiptData: TReceiptData extends true ? GetTransactionReturnType : null; transactionData: TTransactionData extends true ? GetTransactionReceiptReturnType : null; - }[] + } & Log[] > | undefined;