Skip to content

Commit

Permalink
Fixed waitForTransaction and removeListener (#410).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Feb 12, 2019
1 parent e4a2f8a commit 72edcd0
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src.ts/providers/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,15 +776,19 @@ export class BaseProvider extends Provider {

waitForTransaction(transactionHash: string, confirmations?: number): Promise<TransactionReceipt> {
if (confirmations == null) { confirmations = 1; }
return poll(() => {
return this.getTransactionReceipt(transactionHash).then((receipt) => {
if (confirmations === 0) { return receipt; }
if (receipt == null || receipt.confirmations < confirmations) {
return undefined;
}
return receipt;
});
}, { onceBlock: this });

if (confirmations === 0) {
return this.getTransactionReceipt(transactionHash);
}

return new Promise((resolve) => {
let handler = (receipt: TransactionReceipt) => {
if (receipt.confirmations < confirmations) { return; }
this.removeListener(transactionHash, handler);
resolve(receipt);
}
this.on(transactionHash, handler);
});
}

getBlockNumber(): Promise<number> {
Expand Down Expand Up @@ -1306,13 +1310,18 @@ export class BaseProvider extends Provider {
});
}

removeAllListeners(eventName: EventType): Provider {
let eventTag = getEventTag(eventName);
this._events = this._events.filter((event) => {
return (event.tag !== eventTag);
});
removeAllListeners(eventName?: EventType): Provider {
if (eventName == null) {
this._events = [ ];
this._stopPending();
} else {
let eventTag = getEventTag(eventName);
this._events = this._events.filter((event) => {
return (event.tag !== eventTag);
});
if (eventName === 'pending') { this._stopPending(); }
}

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

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

let eventTag = getEventTag(eventName);
this._events = this._events.filter((event) => {
if (event.tag !== eventTag) { return true; }
if (event.tag !== eventTag || event.listener != listener) { return true; }
if (found) { return true; }
found = false;
found = true;
return false;
});

Expand Down

0 comments on commit 72edcd0

Please sign in to comment.