Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add types to even data in useScaffoldEventHistory #553

Merged
merged 6 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +25,9 @@ import { ContractAbi, ContractName, UseScaffoldEventHistoryConfig } from "~~/uti
export const useScaffoldEventHistory = <
TContractName extends ContractName,
TEventName extends ExtractAbiEventNames<ContractAbi<TContractName>>,
TBlockData extends boolean = false,
TTransactionData extends boolean = false,
TReceiptData extends boolean = false,
>({
contractName,
eventName,
Expand All @@ -28,7 +36,7 @@ export const useScaffoldEventHistory = <
blockData,
transactionData,
receiptData,
}: UseScaffoldEventHistoryConfig<TContractName, TEventName>) => {
}: UseScaffoldEventHistoryConfig<TContractName, TEventName, TBlockData, TTransactionData, TReceiptData>) => {
const [events, setEvents] = useState<any[]>();
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string>();
Expand Down Expand Up @@ -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,
};
Expand Down
68 changes: 51 additions & 17 deletions packages/nextjs/utils/scaffold-eth/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -153,6 +161,10 @@ export type UseScaffoldWriteConfig<
export type UseScaffoldEventConfig<
TContractName extends ContractName,
TEventName extends ExtractAbiEventNames<ContractAbi<TContractName>>,
TEvent extends ExtractAbiEvent<ContractAbi<TContractName>, TEventName> = ExtractAbiEvent<
ContractAbi<TContractName>,
TEventName
>,
> = {
contractName: TContractName;
} & IsContractDeclarationMissing<
Expand All @@ -169,19 +181,8 @@ export type UseScaffoldEventConfig<
Omit<UseContractEventConfig<ContractAbi<TContractName>, TEventName>, "listener"> & {
listener: (
logs: Prettify<
Omit<
Log<
bigint,
number,
false,
ExtractAbiEvent<ContractAbi<TContractName>, TEventName>,
false,
[ExtractAbiEvent<ContractAbi<TContractName>, TEventName>],
TEventName
>,
"args"
> & {
args: AbiParametersToPrimitiveTypes<ExtractAbiEvent<ContractAbi<TContractName>, TEventName>["inputs"]> &
Omit<Log<bigint, number, false, TEvent, false, [TEvent], TEventName>, "args"> & {
args: AbiParametersToPrimitiveTypes<TEvent["inputs"]> &
GetEventArgs<
ContractAbi<TContractName>,
TEventName,
Expand Down Expand Up @@ -218,12 +219,45 @@ export type EventFilters<
export type UseScaffoldEventHistoryConfig<
TContractName extends ContractName,
TEventName extends ExtractAbiEventNames<ContractAbi<TContractName>>,
TBlockData extends boolean = false,
TTransactionData extends boolean = false,
TReceiptData extends boolean = false,
> = {
contractName: TContractName;
eventName: IsContractDeclarationMissing<string, TEventName>;
fromBlock: bigint;
filters?: EventFilters<TContractName, TEventName>;
blockData?: boolean;
transactionData?: boolean;
receiptData?: boolean;
blockData?: TBlockData;
transactionData?: TTransactionData;
receiptData?: TReceiptData;
};

export type UseScaffoldEventHistoryData<
TContractName extends ContractName,
TEventName extends ExtractAbiEventNames<ContractAbi<TContractName>>,
TBlockData extends boolean = false,
TTransactionData extends boolean = false,
TReceiptData extends boolean = false,
TEvent extends ExtractAbiEvent<ContractAbi<TContractName>, TEventName> = ExtractAbiEvent<
ContractAbi<TContractName>,
TEventName
>,
> =
| IsContractDeclarationMissing<
any[],
{
log: Log<bigint, number, false, TEvent, false, [TEvent], TEventName>;
args: AbiParametersToPrimitiveTypes<TEvent["inputs"]> &
GetEventArgs<
ContractAbi<TContractName>,
TEventName,
{
IndexedOnly: false;
}
>;
block: TBlockData extends true ? Block<bigint, true> : null;
receipt: TReceiptData extends true ? GetTransactionReturnType : null;
transaction: TTransactionData extends true ? GetTransactionReceiptReturnType : null;
}[]
>
| undefined;