Skip to content

Commit

Permalink
Correctly set last emitted block for WebSocketProvider (#856).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed May 30, 2020
1 parent 8efd8d2 commit 1b0ad5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/providers/src.ts/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ export class BaseProvider extends Provider {
}

return this.formatter.block(block);
}, { onceBlock: this });
}, { oncePoll: this });
}

getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<Block> {
Expand Down Expand Up @@ -865,7 +865,7 @@ export class BaseProvider extends Provider {
}

return this._wrapTransaction(tx);
}, { onceBlock: this });
}, { oncePoll: this });
}

async getTransactionReceipt(transactionHash: string | Promise<string>): Promise<TransactionReceipt> {
Expand Down Expand Up @@ -903,7 +903,7 @@ export class BaseProvider extends Provider {
}

return receipt;
}, { onceBlock: this });
}, { oncePoll: this });
}

async getLogs(filter: Filter | FilterByBlockHash | Promise<Filter | FilterByBlockHash>): Promise<Array<Log>> {
Expand Down
15 changes: 14 additions & 1 deletion packages/providers/src.ts/websocket-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export class WebSocketProvider extends JsonRpcProvider {
console.warn("this should not happen");
}
};

// This Provider does not actually poll, but we want to trigger
// poll events for things that depend on them (like stalling for
// block and transaction lookups)
const fauxPoll = setInterval(() => {
this.emit("poll");
}, 1000);
if (fauxPoll.unref) { fauxPoll.unref(); }
}

get pollingInterval(): number {
Expand Down Expand Up @@ -180,7 +188,9 @@ export class WebSocketProvider extends JsonRpcProvider {
switch (event.type) {
case "block":
this._subscribe("block", [ "newHeads" ], (result: any) => {
this.emit("block", BigNumber.from(result.number).toNumber());
const blockNumber = BigNumber.from(result.number).toNumber();
this._emitted.block = blockNumber;
this.emit("block", blockNumber);
});
break;

Expand Down Expand Up @@ -221,6 +231,9 @@ export class WebSocketProvider extends JsonRpcProvider {

// Nothing is needed
case "debug":
case "poll":
case "willPoll":
case "didPoll":
case "error":
break;

Expand Down

0 comments on commit 1b0ad5a

Please sign in to comment.