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 indexed args to events #540

Merged
merged 4 commits into from
Sep 29, 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
10 changes: 9 additions & 1 deletion packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,16 @@ export const useScaffoldEventHistory = <
]);

return {
data: events,
data: events?.map(addIndexedArgsToEvent),
isLoading: isLoading,
error: error,
};
};

export const addIndexedArgsToEvent = (event: any) => {
if (event.args && !Array.isArray(event.args)) {
return { ...event, args: { ...event.args, ...Object.values(event.args) } };
}

return event;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Abi, ExtractAbiEventNames } from "abitype";
import { Log } from "viem";
import { useContractEvent } from "wagmi";
import { useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import { addIndexedArgsToEvent, useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import { getTargetNetwork } from "~~/utils/scaffold-eth";
import { ContractAbi, ContractName, UseScaffoldEventConfig } from "~~/utils/scaffold-eth/contract";

Expand All @@ -22,11 +22,15 @@ export const useScaffoldEventSubscriber = <
}: UseScaffoldEventConfig<TContractName, TEventName>) => {
const { data: deployedContractData } = useDeployedContractInfo(contractName);

const addInexedArgsToLogs = (logs: Log[]) => logs.map(addIndexedArgsToEvent);
const listenerWithIndexedArgs = (logs: Log[]) =>
listener(addInexedArgsToLogs(logs) as Parameters<typeof listener>[0]);

return useContractEvent({
address: deployedContractData?.address,
abi: deployedContractData?.abi as Abi,
chainId: getTargetNetwork().id,
listener: listener as (logs: Log[]) => void,
listener: listenerWithIndexedArgs,
eventName,
});
};
39 changes: 36 additions & 3 deletions packages/nextjs/utils/scaffold-eth/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ExtractAbiFunction,
} from "abitype";
import type { ExtractAbiFunctionNames } from "abitype";
import { Address, Log, TransactionReceipt } from "viem";
import { Address, GetEventArgs, Log, TransactionReceipt } from "viem";
import { UseContractEventConfig, UseContractReadConfig, UseContractWriteConfig } from "wagmi";
import contractsData from "~~/generated/deployedContracts";
import scaffoldConfig from "~~/scaffold.config";
Expand Down Expand Up @@ -170,9 +170,42 @@ export type UseScaffoldEventConfig<
contractName: TContractName;
} & IsContractDeclarationMissing<
Omit<UseContractEventConfig, "listener"> & {
listener: (logs: Prettify<Omit<Log<bigint, number, any>, "args"> & { args: Record<string, unknown> }>[]) => void;
listener: (
logs: Prettify<
Omit<Log<bigint, number, any>, "args" | "eventName"> & {
args: Record<string, unknown>;
eventName: string;
}
>[],
) => void;
},
UseContractEventConfig<ContractAbi<TContractName>, TEventName>
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"]> &
GetEventArgs<
ContractAbi<TContractName>,
TEventName,
{
IndexedOnly: false;
}
>;
}
>[],
) => void;
}
>;

type IndexedEventInputs<
Expand Down