diff --git a/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts b/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts index df9123575..d71ee1e75 100644 --- a/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts +++ b/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts @@ -4,7 +4,12 @@ import { Hash } from "viem"; import { usePublicClient } from "wagmi"; import { useDeployedContractInfo } from "~~/hooks/scaffold-eth"; import { replacer } from "~~/utils/scaffold-eth/common"; -import { ContractAbi, ContractName, UseScaffoldEventHistoryConfig } from "~~/utils/scaffold-eth/contract"; +import { + ContractAbi, + ContractName, + UseScaffoldEventHistoryConfig, + UseScaffoldEventHistoryData, +} from "~~/utils/scaffold-eth/contract"; /** * @dev reads events from a deployed contract @@ -20,6 +25,9 @@ import { ContractAbi, ContractName, UseScaffoldEventHistoryConfig } from "~~/uti export const useScaffoldEventHistory = < TContractName extends ContractName, TEventName extends ExtractAbiEventNames>, + TBlockData extends boolean = false, + TTransactionData extends boolean = false, + TReceiptData extends boolean = false, >({ contractName, eventName, @@ -28,7 +36,7 @@ export const useScaffoldEventHistory = < blockData, transactionData, receiptData, -}: UseScaffoldEventHistoryConfig) => { +}: UseScaffoldEventHistoryConfig) => { const [events, setEvents] = useState(); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(); @@ -101,7 +109,13 @@ export const useScaffoldEventHistory = < ]); return { - data: events?.map(addIndexedArgsToEvent), + data: events?.map(addIndexedArgsToEvent) as UseScaffoldEventHistoryData< + TContractName, + TEventName, + TBlockData, + TTransactionData, + TReceiptData + >, isLoading: isLoading, error: error, }; diff --git a/packages/nextjs/utils/scaffold-eth/contract.ts b/packages/nextjs/utils/scaffold-eth/contract.ts index 7f99d6a53..046bd559b 100644 --- a/packages/nextjs/utils/scaffold-eth/contract.ts +++ b/packages/nextjs/utils/scaffold-eth/contract.ts @@ -7,7 +7,15 @@ import { ExtractAbiFunction, } from "abitype"; import type { ExtractAbiFunctionNames } from "abitype"; -import { Address, GetEventArgs, Log, TransactionReceipt } from "viem"; +import { + Address, + Block, + GetEventArgs, + GetTransactionReceiptReturnType, + GetTransactionReturnType, + Log, + TransactionReceipt, +} from "viem"; import { UseContractEventConfig, UseContractReadConfig, UseContractWriteConfig } from "wagmi"; import contractsData from "~~/generated/deployedContracts"; import scaffoldConfig from "~~/scaffold.config"; @@ -153,6 +161,10 @@ export type UseScaffoldWriteConfig< export type UseScaffoldEventConfig< TContractName extends ContractName, TEventName extends ExtractAbiEventNames>, + TEvent extends ExtractAbiEvent, TEventName> = ExtractAbiEvent< + ContractAbi, + TEventName + >, > = { contractName: TContractName; } & IsContractDeclarationMissing< @@ -169,19 +181,8 @@ export type UseScaffoldEventConfig< Omit, TEventName>, "listener"> & { listener: ( logs: Prettify< - Omit< - Log< - bigint, - number, - false, - ExtractAbiEvent, TEventName>, - false, - [ExtractAbiEvent, TEventName>], - TEventName - >, - "args" - > & { - args: AbiParametersToPrimitiveTypes, TEventName>["inputs"]> & + Omit, "args"> & { + args: AbiParametersToPrimitiveTypes & GetEventArgs< ContractAbi, TEventName, @@ -218,12 +219,45 @@ export type EventFilters< export type UseScaffoldEventHistoryConfig< TContractName extends ContractName, TEventName extends ExtractAbiEventNames>, + TBlockData extends boolean = false, + TTransactionData extends boolean = false, + TReceiptData extends boolean = false, > = { contractName: TContractName; eventName: IsContractDeclarationMissing; fromBlock: bigint; filters?: EventFilters; - blockData?: boolean; - transactionData?: boolean; - receiptData?: boolean; + blockData?: TBlockData; + transactionData?: TTransactionData; + receiptData?: TReceiptData; }; + +export type UseScaffoldEventHistoryData< + TContractName extends ContractName, + TEventName extends ExtractAbiEventNames>, + TBlockData extends boolean = false, + TTransactionData extends boolean = false, + TReceiptData extends boolean = false, + TEvent extends ExtractAbiEvent, TEventName> = ExtractAbiEvent< + ContractAbi, + TEventName + >, +> = + | IsContractDeclarationMissing< + any[], + { + log: Log; + args: AbiParametersToPrimitiveTypes & + GetEventArgs< + ContractAbi, + TEventName, + { + IndexedOnly: false; + } + >; + block: TBlockData extends true ? Block : null; + receipt: TReceiptData extends true ? GetTransactionReturnType : null; + transaction: TTransactionData extends true ? GetTransactionReceiptReturnType : null; + }[] + > + | undefined;