Correctly set last emitted block for WebSocketProvider (#856).

This commit is contained in:
Richard Moore 2020-05-29 20:41:20 -04:00
parent 8efd8d2031
commit 1b0ad5aa69
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
2 changed files with 17 additions and 4 deletions

View File

@ -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> {
@ -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> {
@ -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>> {

View File

@ -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 {
@ -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;
@ -221,6 +231,9 @@ export class WebSocketProvider extends JsonRpcProvider {
// Nothing is needed
case "debug":
case "poll":
case "willPoll":
case "didPoll":
case "error":
break;