Skip to content

Commit

Permalink
Merge pull request #5731 from NomicFoundation/fix-event-forwarding-ex…
Browse files Browse the repository at this point in the history
…plicit-init

Fix provider events when init is called
  • Loading branch information
alcuadrado authored Sep 13, 2024
2 parents ee8d62b + dfb232e commit 8c564b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-sheep-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Fixed a problem with provider events when `provider.init` was explicitly called before the first request.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ export class LazyInitializationProviderAdapter implements EthereumProvider {
this._initializingPromise = this._providerFactory();
}
this.provider = await this._initializingPromise;

// Copy any event emitter events before initialization over to the provider
const recordedEvents = this._emitter.eventNames();

this.provider.setMaxListeners(this._emitter.getMaxListeners());

for (const event of recordedEvents) {
const listeners = this._emitter.rawListeners(event) as Listener[];
for (const listener of listeners) {
this.provider.on(event, listener);
this._emitter.removeListener(event, listener);
}
}
}
return this.provider;
}
Expand Down Expand Up @@ -158,19 +171,6 @@ export class LazyInitializationProviderAdapter implements EthereumProvider {

if (this.provider === undefined) {
this.provider = await this.init();

// Copy any event emitter events before initialization over to the provider
const recordedEvents = this._emitter.eventNames();

for (const event of recordedEvents) {
const listeners = this._emitter.rawListeners(event) as Listener[];
for (const listener of listeners) {
this.provider.on(event, listener);
this._emitter.removeListener(event, listener);
}
}

this.provider.setMaxListeners(this._emitter.getMaxListeners());
}

return this.provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe("LazyInitializationProviderAdapter", () => {
assert.equal(callTimes, 3);
});

it("should move the registered events to the provider after initialization", async () => {
it("should move the registered events to the provider after implicit initialization", async () => {
provider.on("event", eventHandler);
provider.on("otherevent", eventHandler);
provider.once("onceevent", eventHandler);
Expand All @@ -110,6 +110,20 @@ describe("LazyInitializationProviderAdapter", () => {

assert.deepEqual(callTimes, 3);
});

it("should move the registered events to the provider after explicit initialization", async () => {
provider.on("event", eventHandler);
provider.on("otherevent", eventHandler);
provider.once("onceevent", eventHandler);

await provider.init();
provider.emit("event"); // 1
provider.emit("otherevent"); // 2
provider.emit("onceevent"); // 3
provider.emit("onceevent"); // 3

assert.deepEqual(callTimes, 3);
});
});

describe("request", () => {
Expand Down

0 comments on commit 8c564b4

Please sign in to comment.