diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-11-11 00:38:02 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-11 00:38:02 +0800 |
commit | c5b347bb15ee4a34b2e847c26d02bbcf903b2bf5 (patch) | |
tree | c77a6ecd9fccbb463b62f7756e76a09e826abf39 | |
parent | a7bedad9f020cf0bbd91d3823a14a0711ea78e0b (diff) | |
parent | d11087c28fef86e3cdbaadd6299b33dca5b5b50b (diff) | |
download | dexon-sol-tools-c5b347bb15ee4a34b2e847c26d02bbcf903b2bf5.tar dexon-sol-tools-c5b347bb15ee4a34b2e847c26d02bbcf903b2bf5.tar.gz dexon-sol-tools-c5b347bb15ee4a34b2e847c26d02bbcf903b2bf5.tar.bz2 dexon-sol-tools-c5b347bb15ee4a34b2e847c26d02bbcf903b2bf5.tar.lz dexon-sol-tools-c5b347bb15ee4a34b2e847c26d02bbcf903b2bf5.tar.xz dexon-sol-tools-c5b347bb15ee4a34b2e847c26d02bbcf903b2bf5.tar.zst dexon-sol-tools-c5b347bb15ee4a34b2e847c26d02bbcf903b2bf5.zip |
Merge pull request #206 from lukeautry/await_transaction_timeout
Allow timeout for await transaction mined
-rw-r--r-- | src/0x.ts | 14 | ||||
-rw-r--r-- | src/types.ts | 1 |
2 files changed, 14 insertions, 1 deletions
@@ -289,13 +289,24 @@ export class ZeroEx { * Waits for a transaction to be mined and returns the transaction receipt. * @param txHash Transaction hash * @param pollingIntervalMs How often (in ms) should we check if the transaction is mined. + * @param timeoutMs How long (in ms) to poll for transaction mined until aborting. * @return Transaction receipt with decoded log args. */ public async awaitTransactionMinedAsync( - txHash: string, pollingIntervalMs: number = 1000): Promise<TransactionReceiptWithDecodedLogs> { + txHash: string, pollingIntervalMs = 1000, timeoutMs?: number): Promise<TransactionReceiptWithDecodedLogs> { + let timeoutExceeded = false; + if (timeoutMs) { + setTimeout(() => timeoutExceeded = true, timeoutMs); + } + const txReceiptPromise = new Promise( (resolve: (receipt: TransactionReceiptWithDecodedLogs) => void, reject) => { const intervalId = intervalUtils.setAsyncExcludingInterval(async () => { + if (timeoutExceeded) { + intervalUtils.clearAsyncExcludingInterval(intervalId); + return reject(ZeroExError.TransactionMiningTimeout); + } + const transactionReceipt = await this._web3Wrapper.getTransactionReceiptAsync(txHash); if (!_.isNull(transactionReceipt)) { intervalUtils.clearAsyncExcludingInterval(intervalId); @@ -311,6 +322,7 @@ export class ZeroEx { } }, pollingIntervalMs); }); + return txReceiptPromise; } /* diff --git a/src/types.ts b/src/types.ts index 9ac726ef8..a9eac56d8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,6 +16,7 @@ export enum ZeroExError { OutOfGas = 'OUT_OF_GAS', NoNetworkId = 'NO_NETWORK_ID', SubscriptionNotFound = 'SUBSCRIPTION_NOT_FOUND', + TransactionMiningTimeout = 'TRANSACTION_MINING_TIMEOUT', } export enum InternalZeroExError { |