diff options
author | Fabio Berger <me@fabioberger.com> | 2018-09-24 22:49:58 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-24 22:49:58 +0800 |
commit | fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f (patch) | |
tree | 9a3e827452b8c980148fac7f53eb9fd84d282e33 /packages/contract-wrappers | |
parent | 8bce407aec414fbaf80a7132bdf43c5b9f66247b (diff) | |
parent | 7516959c9f733c0ee73c2551db185a7751d9f94c (diff) | |
download | dexon-0x-contracts-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar dexon-0x-contracts-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.gz dexon-0x-contracts-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.bz2 dexon-0x-contracts-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.lz dexon-0x-contracts-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.xz dexon-0x-contracts-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.tar.zst dexon-0x-contracts-fc33eacd2cbcc088d238f5e1f34b50b06ea8d58f.zip |
Merge pull request #1082 from 0xProject/fixGetBlocks
Fix block fetch error if block not found
Diffstat (limited to 'packages/contract-wrappers')
-rw-r--r-- | packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts | 18 | ||||
-rw-r--r-- | packages/contract-wrappers/test/subscription_test.ts | 2 |
2 files changed, 16 insertions, 4 deletions
diff --git a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts index 19de17c0a..19a882712 100644 --- a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts @@ -2,6 +2,7 @@ import { AbiDecoder, intervalUtils, logUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { BlockParamLiteral, + BlockWithoutTransactionData, ContractAbi, ContractArtifact, FilterObject, @@ -174,7 +175,7 @@ export abstract class ContractWrapper { throw new Error(ContractWrappersError.SubscriptionAlreadyPresent); } this._blockAndLogStreamerIfExists = new BlockAndLogStreamer( - this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper), + this._getBlockOrNullAsync.bind(this), this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper), ContractWrapper._onBlockAndLogStreamerError.bind(this, isVerbose), ); @@ -194,6 +195,14 @@ export abstract class ContractWrapper { this._onLogStateChanged.bind(this, isRemoved), ); } + // This method only exists in order to comply with the expected interface of Blockstream's constructor + private async _getBlockOrNullAsync(): Promise<BlockWithoutTransactionData | null> { + const blockIfExists = await this._web3Wrapper.getBlockIfExistsAsync.bind(this._web3Wrapper); + if (_.isUndefined(blockIfExists)) { + return null; + } + return blockIfExists; + } // HACK: This should be a package-scoped method (which doesn't exist in TS) // We don't want this method available in the public interface for all classes // who inherit from ContractWrapper, and it is only used by the internal implementation @@ -212,11 +221,14 @@ export abstract class ContractWrapper { delete this._blockAndLogStreamerIfExists; } private async _reconcileBlockAsync(): Promise<void> { - const latestBlock = await this._web3Wrapper.getBlockAsync(BlockParamLiteral.Latest); + const latestBlockIfExists = await this._web3Wrapper.getBlockIfExistsAsync(BlockParamLiteral.Latest); + if (_.isUndefined(latestBlockIfExists)) { + return; // noop + } // We need to coerce to Block type cause Web3.Block includes types for mempool blocks if (!_.isUndefined(this._blockAndLogStreamerIfExists)) { // If we clear the interval while fetching the block - this._blockAndLogStreamer will be undefined - await this._blockAndLogStreamerIfExists.reconcileNewBlock((latestBlock as any) as Block); + await this._blockAndLogStreamerIfExists.reconcileNewBlock((latestBlockIfExists as any) as Block); } } } diff --git a/packages/contract-wrappers/test/subscription_test.ts b/packages/contract-wrappers/test/subscription_test.ts index 81b9012bd..68ef7225e 100644 --- a/packages/contract-wrappers/test/subscription_test.ts +++ b/packages/contract-wrappers/test/subscription_test.ts @@ -61,7 +61,7 @@ describe('SubscriptionTest', () => { callback, ); stubs = [ - Sinon.stub((contractWrappers as any)._web3Wrapper, 'getBlockAsync').throws( + Sinon.stub((contractWrappers as any)._web3Wrapper, 'getBlockIfExistsAsync').throws( new Error('JSON RPC error'), ), ]; |