Implement zeroEx.awaitTransactionMined

This commit is contained in:
Leonid Logvinov
2017-09-04 19:08:14 +02:00
parent 6325a03818
commit c9e490bdae
4 changed files with 22 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
import * as _ from 'lodash';
import * as BigNumber from 'bignumber.js';
import * as Web3 from 'web3';
import {SchemaValidator, schemas} from '0x-json-schemas';
import {bigNumberConfigs} from './bignumber_config';
import * as ethUtil from 'ethereumjs-util';
@@ -248,4 +249,17 @@ export class ZeroEx {
throw new Error(ZeroExError.InvalidSignature);
}
public async awaitTransactionMined(txHash: string,
pollingIntervalMs: number = 500): Promise<Web3.TransactionReceipt> {
const txReceiptPromise = new Promise((resolve: (receipt: Web3.TransactionReceipt) => void, reject) => {
const intervalId = setInterval(async () => {
const transactionReceipt = await this._web3Wrapper.getTransactionReceiptAsync(txHash);
if (!_.isNull(transactionReceipt)) {
clearInterval(intervalId);
resolve(transactionReceipt);
}
}, pollingIntervalMs);
});
return txReceiptPromise;
}
}

View File

@@ -27,6 +27,10 @@ export class Web3Wrapper {
const nodeVersion = await promisify(this.web3.version.getNode)();
return nodeVersion;
}
public async getTransactionReceiptAsync(txHash: string): Promise<Web3.TransactionReceipt> {
const transactionReceipt = await promisify(this.web3.eth.getTransactionReceipt)(txHash);
return transactionReceipt;
}
public getCurrentProvider(): Web3.Provider {
return this.web3.currentProvider;
}

View File

@@ -205,7 +205,7 @@ describe('ExchangeWrapper', () => {
);
const txHash = await zeroEx.exchange.fillOrderAsync(
signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress);
await blockchainLifecycle.waitUntilMinedAsync(txHash);
await zeroEx.awaitTransactionMined(txHash);
expect(await zeroEx.token.getBalanceAsync(zrxTokenAddress, feeRecipient))
.to.be.bignumber.equal(makerFee.plus(takerFee));
});
@@ -246,7 +246,7 @@ describe('ExchangeWrapper', () => {
it('should successfully fill multiple orders', async () => {
const txHash = await zeroEx.exchange.batchFillOrdersAsync(
orderFillBatch, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress);
await blockchainLifecycle.waitUntilMinedAsync(txHash);
await zeroEx.awaitTransactionMined(txHash);
const filledAmount = await zeroEx.exchange.getFilledTakerAmountAsync(signedOrderHashHex);
const anotherFilledAmount = await zeroEx.exchange.getFilledTakerAmountAsync(anotherOrderHashHex);
expect(filledAmount).to.be.bignumber.equal(fillTakerAmount);
@@ -282,7 +282,7 @@ describe('ExchangeWrapper', () => {
const txHash = await zeroEx.exchange.fillOrdersUpToAsync(
signedOrders, fillUpToAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress,
);
await blockchainLifecycle.waitUntilMinedAsync(txHash);
await zeroEx.awaitTransactionMined(txHash);
const filledAmount = await zeroEx.exchange.getFilledTakerAmountAsync(signedOrderHashHex);
const anotherFilledAmount = await zeroEx.exchange.getFilledTakerAmountAsync(anotherOrderHashHex);
expect(filledAmount).to.be.bignumber.equal(fillableAmount);
@@ -316,7 +316,7 @@ describe('ExchangeWrapper', () => {
describe('successful cancels', () => {
it('should cancel an order', async () => {
const txHash = await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmount);
await blockchainLifecycle.waitUntilMinedAsync(txHash);
await zeroEx.awaitTransactionMined(txHash);
const cancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHashHex);
expect(cancelledAmount).to.be.bignumber.equal(cancelAmount);
});

View File

@@ -17,7 +17,4 @@ export class BlockchainLifecycle {
throw new Error(`Snapshot with id #${this.snapshotId} failed to revert`);
}
}
public async waitUntilMinedAsync(txHash: string): Promise<void> {
return undefined;
}
}