diff options
author | F. Eugene Aumson <feuGeneA@users.noreply.github.com> | 2018-09-25 01:30:37 +0800 |
---|---|---|
committer | F. Eugene Aumson <feuGeneA@users.noreply.github.com> | 2018-09-25 01:30:37 +0800 |
commit | 2116548eed666c3c61277f17e996fee720301f31 (patch) | |
tree | 6f4b601b59659ac9134ffe4efed79c8a8a2750e0 /packages/web3-wrapper/src/web3_wrapper.ts | |
parent | 57fca16d7b1dc61c060c90fa440b1dc947aefb93 (diff) | |
parent | b830c28d83a33c0170d4f150ee287ea97ccf7865 (diff) | |
download | dexon-0x-contracts-2116548eed666c3c61277f17e996fee720301f31.tar.gz dexon-0x-contracts-2116548eed666c3c61277f17e996fee720301f31.tar.zst dexon-0x-contracts-2116548eed666c3c61277f17e996fee720301f31.zip |
Merge remote-tracking branch 'upstream/development' into sol-doc
Diffstat (limited to 'packages/web3-wrapper/src/web3_wrapper.ts')
-rw-r--r-- | packages/web3-wrapper/src/web3_wrapper.ts | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index ea78f8801..dc634a57f 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -329,23 +329,29 @@ 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 + * (e.g the node isn't fully synced, there was a block re-org and the requested block was uncles, etc...) */ - public async getBlockAsync(blockParam: string | BlockParam): Promise<BlockWithoutTransactionData> { + public async getBlockIfExistsAsync( + blockParam: string | BlockParam, + ): Promise<BlockWithoutTransactionData | undefined> { 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 +382,11 @@ export class Web3Wrapper { */ public async getBlockTimestampAsync(blockParam: string | BlockParam): Promise<number> { 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 @@ -539,7 +548,7 @@ export class Web3Wrapper { } // Immediately check if the transaction has already been mined. let transactionReceipt = await this.getTransactionReceiptAsync(txHash); - if (!_.isNull(transactionReceipt)) { + if (!_.isNull(transactionReceipt) && !_.isNull(transactionReceipt.blockNumber)) { const logsWithDecodedArgs = _.map( transactionReceipt.logs, this.abiDecoder.tryToDecodeLogOrNoop.bind(this.abiDecoder), |