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

fix: useScaffoldWatchContractEvent logs args types #837

Merged
merged 2 commits into from May 7, 2024

Conversation

rin-st
Copy link
Collaborator

@rin-st rin-st commented May 3, 2024

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); to YourContract.sol, I'm receiving this error when trying to work with logs from useScaffoldWatchContractEvent.

Снимок экрана 2024-05-03 в 20 31 39

Additional Information

@technophile-04
Copy link
Collaborator

technophile-04 commented May 6, 2024

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 deployedContracts.ts is empty the type of eventName will be string and as soon as we have contracts in deployed/externalContracts.ts it will give autocompletion / use literal value like "GreetingChange" | "SomeOtherEvent"

By doing this we make sure the type of eventName is locked to one eventName in generic (checkout next section for more details) and will get the right autocompletion and won't get errors.


Diagnostics: 1. Property 'newGreeting' does not exist on type '(readonly [string, string, boolean, bigint] | readonly [string, bigint]) & ({ greetingSetter?: string | undefined; newGreeting?: string | undefined; premium?: boolean | undefined; value?: bigint | undefined; } | { ...; })'.
Property 'newGreeting' does not exist on type 'readonly [string, string, boolean, bigint] & { someString?: string | undefined; someValue?: bigint | undefined; }'. [2339]

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 newGreeting is not present in SomeOtherEvent TS is throwing error (since it's not present in all possible args, TS gets it resultant type as common things checkout this minimal example)

The reason for TS getting multiple possibilities was because while passing eventName as arg to useScaffoldWatchEvent, the generic TEventName was not assigned to eventName in type, because of which the second generic slot of useScaffoldWatchEvent was not getting locked to literal string (eventName passed) instead it was remaining union of "GreetingChange" | "SomeOtherEvent" always. Checkout below examples :

useScaffoldWatchEvent :

Even though we have passed eventName: "GreetingChange", it is not getting locked in second slot of useScaffoldWatchEvent

Screenshot 2024-05-06 at 5 55 15 PM

PS: I am hovering over useScaffoldWatchEvent in my text editor, and its result is on above line 17, lol it looks a bit confusing now I see

useScaffoldReadContract :

Notice how "premium" (functionName) was locked in the second slot instead of it being the union of all read functions

Screenshot 2024-05-06 at 5 56 49 PM


Also not at all related to this PR (please feel free to ignore), but it seems wagmi's useWatchContractEvent does not give autocompletion for eventName checkout this example, am I doing something wrong?

@rin-st
Copy link
Collaborator Author

rin-st commented May 6, 2024

Thanks Shiv! Great solution!

The reason for TS getting multiple possibilities was because while passing eventName as arg to useScaffoldWatchEvent, the generic TEventName was not assigned to eventName in type, because of which the second generic slot of useScaffoldWatchEvent was not getting locked to literal string (eventName passed) instead it was remaining union of "GreetingChange" | "SomeOtherEvent" always.

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 eventName is somewhere inside UseWatchContractEventParameters? Because of eventName in generic parameters? I didn't even search it 🤷‍♂️

Also not at all related to this PR (please feel free to ignore), but it seems wagmi's useWatchContractEvent does not give autocompletion for eventName checkout this example, am I doing something wrong?

Doesn't work for me too

Copy link
Collaborator

@technophile-04 technophile-04 left a 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 !

@technophile-04 technophile-04 merged commit 0f9d9db into main May 7, 2024
1 check passed
@technophile-04 technophile-04 deleted the fix-event-types branch May 7, 2024 07:21
@rin-st
Copy link
Collaborator Author

rin-st commented May 7, 2024

and also on JS land wagmi's useWatchContractEvent was accepting it as a param

🤦 sure, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants