Skip to content

Commit 72edcd0

Browse files
committed
Fixed waitForTransaction and removeListener (ethers-io#410).
1 parent e4a2f8a commit 72edcd0

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src.ts/providers/base-provider.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -776,15 +776,19 @@ export class BaseProvider extends Provider {
776776

777777
waitForTransaction(transactionHash: string, confirmations?: number): Promise<TransactionReceipt> {
778778
if (confirmations == null) { confirmations = 1; }
779-
return poll(() => {
780-
return this.getTransactionReceipt(transactionHash).then((receipt) => {
781-
if (confirmations === 0) { return receipt; }
782-
if (receipt == null || receipt.confirmations < confirmations) {
783-
return undefined;
784-
}
785-
return receipt;
786-
});
787-
}, { onceBlock: this });
779+
780+
if (confirmations === 0) {
781+
return this.getTransactionReceipt(transactionHash);
782+
}
783+
784+
return new Promise((resolve) => {
785+
let handler = (receipt: TransactionReceipt) => {
786+
if (receipt.confirmations < confirmations) { return; }
787+
this.removeListener(transactionHash, handler);
788+
resolve(receipt);
789+
}
790+
this.on(transactionHash, handler);
791+
});
788792
}
789793

790794
getBlockNumber(): Promise<number> {
@@ -1306,13 +1310,18 @@ export class BaseProvider extends Provider {
13061310
});
13071311
}
13081312

1309-
removeAllListeners(eventName: EventType): Provider {
1310-
let eventTag = getEventTag(eventName);
1311-
this._events = this._events.filter((event) => {
1312-
return (event.tag !== eventTag);
1313-
});
1313+
removeAllListeners(eventName?: EventType): Provider {
1314+
if (eventName == null) {
1315+
this._events = [ ];
1316+
this._stopPending();
1317+
} else {
1318+
let eventTag = getEventTag(eventName);
1319+
this._events = this._events.filter((event) => {
1320+
return (event.tag !== eventTag);
1321+
});
1322+
if (eventName === 'pending') { this._stopPending(); }
1323+
}
13141324

1315-
if (eventName === 'pending') { this._stopPending(); }
13161325
if (this._events.length === 0) { this.polling = false; }
13171326

13181327
return this;
@@ -1323,9 +1332,9 @@ export class BaseProvider extends Provider {
13231332

13241333
let eventTag = getEventTag(eventName);
13251334
this._events = this._events.filter((event) => {
1326-
if (event.tag !== eventTag) { return true; }
1335+
if (event.tag !== eventTag || event.listener != listener) { return true; }
13271336
if (found) { return true; }
1328-
found = false;
1337+
found = true;
13291338
return false;
13301339
});
13311340

0 commit comments

Comments
 (0)