-
Notifications
You must be signed in to change notification settings - Fork 917
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
fix: useScaffoldWatchContractEvent logs args types #837
Conversation
Tysm @rin-st, yeah it was bug indeed! Maybe a good solution would be : Solution :export type UseScaffoldEventConfig<
TContractName extends ContractName,
TEventName extends ExtractAbiEventNames<ContractAbi<TContractName>>,
TEvent extends ExtractAbiEvent<ContractAbi<TContractName>, TEventName> = ExtractAbiEvent<
ContractAbi<TContractName>,
TEventName
>,
> = {
contractName: TContractName;
+ eventName: TEventName;
} & IsContractDeclarationMissing<
+ Omit<UseWatchContractEventParameters, "onLogs" | "address" | "abi" | "eventName"> & {
onLogs: (
logs: Simplify<
Omit<Log<bigint, number, any>, "args" | "eventName"> & {
args: Record<string, unknown>;
eventName: string;
}
>[],
) => void;
},
+ Omit<UseWatchContractEventParameters<ContractAbi<TContractName>>, "onLogs" | "address" | "abi" | "eventName"> & {
onLogs: (
logs: Simplify<
Omit<Log<bigint, number, false, TEvent, false, [TEvent], TEventName>, "args"> & {
args: AbiParametersToPrimitiveTypes<TEvent["inputs"]> &
GetEventArgs<
ContractAbi<TContractName>,
TEventName,
{
IndexedOnly: false;
}
>;
}
>[],
) => void;
}
>;
PS: When the By doing this we make sure the type of
As I understand from the error, due to multiple events there were multiple possibilities of event names ("GreetingChange | "SomeOtherEvent") and because of that multiple possibilities of args and since The reason for TS getting multiple possibilities was because while passing useScaffoldWatchEvent :Even though we have passed PS: I am hovering over useScaffoldReadContract :Notice how "premium" (functionName) was locked in the second slot instead of it being the union of all read functions Also not at all related to this PR (please feel free to ignore), but it seems wagmi's |
Thanks Shiv! Great solution!
Yes, I assigned TEventName to eventName too. And for me autocomplete also works. But your solution looks better, so changed to your variant. One question, how did you understand that
Doesn't work for me too |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how did you understand that eventName is somewhere inside UseWatchContractEventParameters? Because of eventName in generic parameters?
Yup lol because of generic param and also on JS land wagmi's useWatchContractEvent
was accepting it as a param
Yes, I assigned TEventName to eventName too. And for me autocomplete also works.
Ohh yeah yeah it was working perfectly fine for me too 🙌, because you passed TEventName
as the second generic param to Wagmi's UseWatchContractEventParameters
(which in turn inside did eventName : TEventName
) and since TEventName
was assigned to eventName
it was getting nicely locked to one literal event name in UseScaffoldEventConfig
. But it took me some time to figure out how it worked by looking at it at first glance, felt a bit of magic.
Preferred my solution slightly because since the actual bug was we were not assigning eventName: TEventName
, we explicitly assigned it at top level instead of relying on wagmi type to assign it (We are kind of following this pattern over the types of our custom hooks, where we take control over wagmi's type (by omitting / making them partial ) and handling it ourself for eg. functionName
, args
in useSReadC etc, and I think it makes sense and easy to grasp)
But TYSM @rin-st, really loved the discussion and learned a lot :) !!! Merging this !
🤦 sure, thanks! |
Description
Currently, types of arguments doesn't work as expected when contract has more that one event. Example:
if I just add
event SomeOtherEvent(string someString, uint256 someValue);
toYourContract.sol
, I'm receiving this error when trying to work with logs fromuseScaffoldWatchContractEvent
.Additional Information