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

ID-913 ZkEvm provider events [NO-CHANGELOG] #580

Merged
merged 7 commits into from
Jul 21, 2023

Conversation

haydenfowler
Copy link
Contributor

Summary

This PR adds support for events to the ZkEvm Provider. Only accountsChanged is currently being emitted as per this decision log.

Additionally, there are a few misc. changes:

  • Improves ZkEvm error codes
  • Moves zkEvm/rpcMethods/eth_sendTransaction to zkEvm/sendTransaction, as no other ethereum methods have been implemented, nor do we have any plans to override any other methods
    • removed EthMethodWithAuthParams, as sendTransaction can specify its own params
    • removed authWrapper in ZkEvmProvider.ts
  • Renamed registerZkEvmUser to loginZkEvmUser, as it's responsible for logging in the user, not registering them
  • Renamed createCounterfactualAddress to registerZkEvmUser
  • Adds default values to sample app for ZkEvm eth methods

@haydenfowler haydenfowler requested a review from a team as a code owner July 20, 2023 02:17
@github-actions
Copy link

Skipping changelog updated check. Detected NO-CHANGELOG in PR title.

};

export enum ProviderEventNames {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export enum ProviderEventNames {
export enum ProviderEvent {

@@ -31,7 +33,7 @@ type LoggedInZkEvmProvider = {
user: UserZkEvm;
};

export class ZkEvmProvider implements Provider {
export class ZkEvmProvider extends TypedEventEmitter<ProviderEvents> implements Provider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reasons why we're picking inheritance over composition here?

Copy link
Contributor Author

@haydenfowler haydenfowler Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EIP-1193 spec states that events must implement on & removeListener, and:

To satisfy these requirements, Provider implementers should consider simply extending the Node.js EventEmitter class and bundling it for the target environment.

Alternatively we could manually create these functions on the provider & delegate the handling to the TypedEventEmitter

eventName: TEventName,
handler: (...eventArg: TEvents[TEventName]) => void,
) {
this.emitter.on(eventName, handler as any);
Copy link
Contributor

@Jon-Alonso Jon-Alonso Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Types look off in this class, can we avoid any casting here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class comes from here, and the reason is:

In strict mode, an error is raised because TEvents[TEventName] != any[]. This is because (...args: TEvents[TEventName]) => void is narrower than the default typings on the EventEmitter type of (...args: any[]) => void and since function parameters are contravariant, this results in a type error. Because we constrain types when events are raised via the emit function, we can safely suppress this error with an as any type

testEvent2: [{ id: number }],
};

describe('TypedEventEmitter', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@haydenfowler haydenfowler merged commit 9c387de into main Jul 21, 2023
4 checks passed
@haydenfowler haydenfowler deleted the feature/ID-913-zkevm-provider-events branch July 21, 2023 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants