scaffold-eth / scaffold-eth-2

Open source forkable Ethereum dev stack
https://scaffoldeth.io
MIT License
1.19k stars 745 forks source link

fix: useScaffoldWatchContractEvent logs args types #837

Closed rin-st closed 2 months ago

rin-st commented 2 months ago

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 commented 2 months ago

Tysm @rin-st, yeah it was bug indeed!

Maybe a good solution would be :

Solution : ```diff export type UseScaffoldEventConfig< TContractName extends ContractName, TEventName extends ExtractAbiEventNames>, TEvent extends ExtractAbiEvent, TEventName> = ExtractAbiEvent< ContractAbi, TEventName >, > = { contractName: TContractName; + eventName: TEventName; } & IsContractDeclarationMissing< + Omit & { onLogs: ( logs: Simplify< Omit, "args" | "eventName"> & { args: Record; eventName: string; } >[], ) => void; }, + Omit>, "onLogs" | "address" | "abi" | "eventName"> & { onLogs: ( logs: Simplify< Omit, "args"> & { args: AbiParametersToPrimitiveTypes & GetEventArgs< ContractAbi, 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](https://github.com/scaffold-eth/scaffold-eth-2/assets/80153681/bd13f0ce-32ad-4e63-bde3-c56e9a35bf5d) 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](https://github.com/scaffold-eth/scaffold-eth-2/assets/80153681/e4135af6-2565-425e-ad52-07f520ab29d5)

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 commented 2 months ago

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

rin-st commented 2 months ago

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

🤦 sure, thanks!