diff options
author | Fabio Berger <me@fabioberger.com> | 2018-09-25 18:20:09 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-09-25 18:20:09 +0800 |
commit | 0afe55f2ff028d03e3a9f110819806f68446dee9 (patch) | |
tree | 54b009844ed69b0f4679e876b0cc0f83e332dc1a /packages/contract-wrappers | |
parent | 21910a712965e5f992f540d94620be2b45384d8f (diff) | |
download | dexon-0x-contracts-0afe55f2ff028d03e3a9f110819806f68446dee9.tar.gz dexon-0x-contracts-0afe55f2ff028d03e3a9f110819806f68446dee9.tar.zst dexon-0x-contracts-0afe55f2ff028d03e3a9f110819806f68446dee9.zip |
Refactor blockstream integration to use the proper callback method interface
Diffstat (limited to 'packages/contract-wrappers')
-rw-r--r-- | packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts index 19a882712..660f2d122 100644 --- a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts @@ -175,8 +175,8 @@ export abstract class ContractWrapper { throw new Error(ContractWrappersError.SubscriptionAlreadyPresent); } this._blockAndLogStreamerIfExists = new BlockAndLogStreamer( - this._getBlockOrNullAsync.bind(this), - this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper), + this._blockstreamGetBlockOrNullAsync.bind(this), + this._blockstreamGetLogsAsync.bind(this), ContractWrapper._onBlockAndLogStreamerError.bind(this, isVerbose), ); const catchAllLogFilter = {}; @@ -196,12 +196,30 @@ export abstract class ContractWrapper { ); } // 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; + private async _blockstreamGetBlockOrNullAsync(hash: string): Promise<Block | null> { + const shouldIncludeTransactionData = false; + const blockOrNull = await this._web3Wrapper.sendRawPayloadAsync({ + method: 'eth_getBlockByHash', + params: [hash, shouldIncludeTransactionData], + }); + return blockOrNull as Block; + } + // This method only exists in order to comply with the expected interface of Blockstream's constructor + private async _blockstreamGetLatestBlockOrNullAsync(): Promise<Block | null> { + const shouldIncludeTransactionData = false; + const blockOrNull = await this._web3Wrapper.sendRawPayloadAsync({ + method: 'eth_getBlockByNumber', + params: ['latest', shouldIncludeTransactionData], + }); + return blockOrNull as Block; + } + // This method only exists in order to comply with the expected interface of Blockstream's constructor + private async _blockstreamGetLogsAsync(filterOptions: FilterObject): Promise<Log[]> { + const logs = await this._web3Wrapper.sendRawPayloadAsync({ + method: 'eth_getLogs', + params: [filterOptions], + }); + return logs as Log[]; } // 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 @@ -221,14 +239,14 @@ export abstract class ContractWrapper { delete this._blockAndLogStreamerIfExists; } private async _reconcileBlockAsync(): Promise<void> { - const latestBlockIfExists = await this._web3Wrapper.getBlockIfExistsAsync(BlockParamLiteral.Latest); - if (_.isUndefined(latestBlockIfExists)) { + const latestBlockOrNull = await this._blockstreamGetLatestBlockOrNullAsync(); + if (_.isNull(latestBlockOrNull)) { 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((latestBlockIfExists as any) as Block); + await this._blockAndLogStreamerIfExists.reconcileNewBlock((latestBlockOrNull as any) as Block); } } } |