aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/transaction_utils.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-01-23 04:53:32 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-01-30 23:01:36 +0800
commit387363283ca03ac1d6c9be5b7be2107790bbf79d (patch)
tree7f9ce518e2f4931321901dfeb2675d70854e996d /packages/utils/src/transaction_utils.ts
parent709026bf1a49d468850b4ebed845c8598fa4fd75 (diff)
downloaddexon-0x-contracts-387363283ca03ac1d6c9be5b7be2107790bbf79d.tar
dexon-0x-contracts-387363283ca03ac1d6c9be5b7be2107790bbf79d.tar.gz
dexon-0x-contracts-387363283ca03ac1d6c9be5b7be2107790bbf79d.tar.bz2
dexon-0x-contracts-387363283ca03ac1d6c9be5b7be2107790bbf79d.tar.lz
dexon-0x-contracts-387363283ca03ac1d6c9be5b7be2107790bbf79d.tar.xz
dexon-0x-contracts-387363283ca03ac1d6c9be5b7be2107790bbf79d.tar.zst
dexon-0x-contracts-387363283ca03ac1d6c9be5b7be2107790bbf79d.zip
Remove truffle from tests
Diffstat (limited to 'packages/utils/src/transaction_utils.ts')
-rw-r--r--packages/utils/src/transaction_utils.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/packages/utils/src/transaction_utils.ts b/packages/utils/src/transaction_utils.ts
new file mode 100644
index 000000000..a1db90817
--- /dev/null
+++ b/packages/utils/src/transaction_utils.ts
@@ -0,0 +1,52 @@
+import { AbiDecoder } from '@0xproject/abi-decoder';
+import { TransactionReceiptWithDecodedLogs } from '@0xproject/types';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
+import * as _ from 'lodash';
+
+import { intervalUtils } from './interval_utils';
+import { TransactionError } from './types';
+
+export const awaitTransactionMinedAsync = async (
+ web3Wrapper: Web3Wrapper,
+ abiDecoder: AbiDecoder,
+ txHash: string,
+ pollingIntervalMs = 1000,
+ timeoutMs?: number,
+) => {
+ 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(TransactionError.TransactionMiningTimeout);
+ }
+
+ const transactionReceipt = await web3Wrapper.getTransactionReceiptAsync(txHash);
+ if (!_.isNull(transactionReceipt)) {
+ intervalUtils.clearAsyncExcludingInterval(intervalId);
+ const logsWithDecodedArgs = _.map(
+ transactionReceipt.logs,
+ abiDecoder.tryToDecodeLogOrNoop.bind(abiDecoder),
+ );
+ const transactionReceiptWithDecodedLogArgs: TransactionReceiptWithDecodedLogs = {
+ ...transactionReceipt,
+ logs: logsWithDecodedArgs,
+ };
+ resolve(transactionReceiptWithDecodedLogArgs);
+ }
+ },
+ pollingIntervalMs,
+ (err: Error) => {
+ intervalUtils.clearAsyncExcludingInterval(intervalId);
+ reject(err);
+ },
+ );
+ });
+
+ return txReceiptPromise;
+};