From d0448c2bbd90c6c103f07b201886670dc4675a43 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 24 Sep 2018 15:02:06 +0100 Subject: Fix bug where if block wasn't found, getBlockAsync would throw. Now it returns `undefined` --- packages/web3-wrapper/src/web3_wrapper.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'packages/web3-wrapper/src') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index af0fe3a72..40e0a73f0 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -329,23 +329,28 @@ export class Web3Wrapper { /** * Fetch a specific Ethereum block without transaction data * @param blockParam The block you wish to fetch (blockHash, blockNumber or blockLiteral) - * @returns The requested block without transaction data + * @returns The requested block without transaction data, or undefined if block was not found */ - public async getBlockAsync(blockParam: string | BlockParam): Promise { + public async getBlockIfExistsAsync( + blockParam: string | BlockParam, + ): Promise { Web3Wrapper._assertBlockParamOrString(blockParam); const encodedBlockParam = marshaller.marshalBlockParam(blockParam); const method = utils.isHexStrict(blockParam) ? 'eth_getBlockByHash' : 'eth_getBlockByNumber'; const shouldIncludeTransactionData = false; - const blockWithoutTransactionDataWithHexValues = await this._sendRawPayloadAsync< + const blockWithoutTransactionDataWithHexValuesOrNull = await this._sendRawPayloadAsync< BlockWithoutTransactionDataRPC >({ method, params: [encodedBlockParam, shouldIncludeTransactionData], }); - const blockWithoutTransactionData = marshaller.unmarshalIntoBlockWithoutTransactionData( - blockWithoutTransactionDataWithHexValues, - ); - return blockWithoutTransactionData; + let blockWithoutTransactionDataIfExists; + if (!_.isNull(blockWithoutTransactionDataWithHexValuesOrNull)) { + blockWithoutTransactionDataIfExists = marshaller.unmarshalIntoBlockWithoutTransactionData( + blockWithoutTransactionDataWithHexValuesOrNull, + ); + } + return blockWithoutTransactionDataIfExists; } /** * Fetch a specific Ethereum block with transaction data @@ -376,8 +381,11 @@ export class Web3Wrapper { */ public async getBlockTimestampAsync(blockParam: string | BlockParam): Promise { Web3Wrapper._assertBlockParamOrString(blockParam); - const { timestamp } = await this.getBlockAsync(blockParam); - return timestamp; + const blockIfExists = await this.getBlockIfExistsAsync(blockParam); + if (_.isUndefined(blockIfExists)) { + throw new Error(`Failed to fetch block with blockParam: ${JSON.stringify(blockParam)}`); + } + return blockIfExists.timestamp; } /** * Retrieve the user addresses available through the backing provider -- cgit v1.2.3 From 311b92591955e9b6eaaaffe6b92b9f33a05d38b4 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 24 Sep 2018 15:14:07 +0100 Subject: Add to doc comment why a block might not be returned to the caller --- packages/web3-wrapper/src/web3_wrapper.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'packages/web3-wrapper/src') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index 40e0a73f0..dc634a57f 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -330,6 +330,7 @@ export class Web3Wrapper { * Fetch a specific Ethereum block without transaction data * @param blockParam The block you wish to fetch (blockHash, blockNumber or blockLiteral) * @returns The requested block without transaction data, or undefined if block was not found + * (e.g the node isn't fully synced, there was a block re-org and the requested block was uncles, etc...) */ public async getBlockIfExistsAsync( blockParam: string | BlockParam, -- cgit v1.2.3