aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web3-wrapper/src
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-09-24 22:49:58 +0800
committerGitHub <noreply@github.com>2018-09-24 22:49:58 +0800
commitfc33eacd2cbcc088d238f5e1f34b50b06ea8d58f (patch)
tree9a3e827452b8c980148fac7f53eb9fd84d282e33 /packages/web3-wrapper/src
parent8bce407aec414fbaf80a7132bdf43c5b9f66247b (diff)
parent7516959c9f733c0ee73c2551db185a7751d9f94c (diff)
downloaddexon-sol-tools-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar
dexon-sol-tools-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.gz
dexon-sol-tools-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.bz2
dexon-sol-tools-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.lz
dexon-sol-tools-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.xz
dexon-sol-tools-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.zst
dexon-sol-tools-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.zip
Merge pull request #1082 from 0xProject/fixGetBlocks
Fix block fetch error if block not found
Diffstat (limited to 'packages/web3-wrapper/src')
-rw-r--r--packages/web3-wrapper/src/web3_wrapper.ts27
1 files changed, 18 insertions, 9 deletions
diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts
index af0fe3a72..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