Fix callback types

This commit is contained in:
Leonid Logvinov 2018-01-05 15:33:00 +01:00
parent f917a4a34a
commit c8c86c44f8
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
9 changed files with 203 additions and 157 deletions

View File

@ -87,7 +87,7 @@ export class ContractWrapper {
} }
const filterToken = filterUtils.generateUUID(); const filterToken = filterUtils.generateUUID();
this._filters[filterToken] = filter; this._filters[filterToken] = filter;
this._filterCallbacks[filterToken] = callback; this._filterCallbacks[filterToken] = callback as EventCallback<ContractEventArgs>;
return filterToken; return filterToken;
} }
protected async _getLogsAsync<ArgsType extends ContractEventArgs>( protected async _getLogsAsync<ArgsType extends ContractEventArgs>(

View File

@ -22,6 +22,7 @@ import { DoneCallback } from '../src/types';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { reportNodeCallbackErrors } from './utils/report_callback_errors';
import { TokenUtils } from './utils/token_utils'; import { TokenUtils } from './utils/token_utils';
import { web3Factory } from './utils/web3_factory'; import { web3Factory } from './utils/web3_factory';
@ -153,8 +154,8 @@ describe('EtherTokenWrapper', () => {
// Source: https://github.com/mochajs/mocha/issues/2407 // Source: https://github.com/mochajs/mocha/issues/2407
it('Should receive the Transfer event when tokens are transfered', (done: DoneCallback) => { it('Should receive the Transfer event when tokens are transfered', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<TransferContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
expect(err).to.be.null(); (logEvent: DecodedLogEvent<TransferContractEventArgs>) => {
expect(logEvent).to.not.be.undefined(); expect(logEvent).to.not.be.undefined();
expect(logEvent.isRemoved).to.be.false(); expect(logEvent.isRemoved).to.be.false();
expect(logEvent.log.logIndex).to.be.equal(0); expect(logEvent.log.logIndex).to.be.equal(0);
@ -164,8 +165,8 @@ describe('EtherTokenWrapper', () => {
expect(args._from).to.be.equal(addressWithETH); expect(args._from).to.be.equal(addressWithETH);
expect(args._to).to.be.equal(addressWithoutFunds); expect(args._to).to.be.equal(addressWithoutFunds);
expect(args._value).to.be.bignumber.equal(transferAmount); expect(args._value).to.be.bignumber.equal(transferAmount);
done(); },
}; );
await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH); await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH);
zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Transfer, indexFilterValues, callback); zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Transfer, indexFilterValues, callback);
await zeroEx.token.transferAsync( await zeroEx.token.transferAsync(
@ -178,16 +179,16 @@ describe('EtherTokenWrapper', () => {
}); });
it('Should receive the Approval event when allowance is being set', (done: DoneCallback) => { it('Should receive the Approval event when allowance is being set', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
expect(err).to.be.null(); (logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
expect(logEvent).to.not.be.undefined(); expect(logEvent).to.not.be.undefined();
expect(logEvent.isRemoved).to.be.false(); expect(logEvent.isRemoved).to.be.false();
const args = logEvent.log.args; const args = logEvent.log.args;
expect(args._owner).to.be.equal(addressWithETH); expect(args._owner).to.be.equal(addressWithETH);
expect(args._spender).to.be.equal(addressWithoutFunds); expect(args._spender).to.be.equal(addressWithoutFunds);
expect(args._value).to.be.bignumber.equal(allowanceAmount); expect(args._value).to.be.bignumber.equal(allowanceAmount);
done(); },
}; );
zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Approval, indexFilterValues, callback); zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Approval, indexFilterValues, callback);
await zeroEx.token.setAllowanceAsync( await zeroEx.token.setAllowanceAsync(
etherTokenAddress, etherTokenAddress,
@ -199,30 +200,30 @@ describe('EtherTokenWrapper', () => {
}); });
it('Should receive the Deposit event when ether is being deposited', (done: DoneCallback) => { it('Should receive the Deposit event when ether is being deposited', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<DepositContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
expect(err).to.be.null(); (logEvent: DecodedLogEvent<DepositContractEventArgs>) => {
expect(logEvent).to.not.be.undefined(); expect(logEvent).to.not.be.undefined();
expect(logEvent.isRemoved).to.be.false(); expect(logEvent.isRemoved).to.be.false();
const args = logEvent.log.args; const args = logEvent.log.args;
expect(args._owner).to.be.equal(addressWithETH); expect(args._owner).to.be.equal(addressWithETH);
expect(args._value).to.be.bignumber.equal(depositAmount); expect(args._value).to.be.bignumber.equal(depositAmount);
done(); },
}; );
zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Deposit, indexFilterValues, callback); zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Deposit, indexFilterValues, callback);
await zeroEx.etherToken.depositAsync(etherTokenAddress, depositAmount, addressWithETH); await zeroEx.etherToken.depositAsync(etherTokenAddress, depositAmount, addressWithETH);
})().catch(done); })().catch(done);
}); });
it('Should receive the Withdrawal event when ether is being withdrawn', (done: DoneCallback) => { it('Should receive the Withdrawal event when ether is being withdrawn', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<WithdrawalContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
expect(err).to.be.null(); (logEvent: DecodedLogEvent<WithdrawalContractEventArgs>) => {
expect(logEvent).to.not.be.undefined(); expect(logEvent).to.not.be.undefined();
expect(logEvent.isRemoved).to.be.false(); expect(logEvent.isRemoved).to.be.false();
const args = logEvent.log.args; const args = logEvent.log.args;
expect(args._owner).to.be.equal(addressWithETH); expect(args._owner).to.be.equal(addressWithETH);
expect(args._value).to.be.bignumber.equal(depositAmount); expect(args._value).to.be.bignumber.equal(depositAmount);
done(); },
}; );
await zeroEx.etherToken.depositAsync(etherTokenAddress, depositAmount, addressWithETH); await zeroEx.etherToken.depositAsync(etherTokenAddress, depositAmount, addressWithETH);
zeroEx.etherToken.subscribe( zeroEx.etherToken.subscribe(
etherTokenAddress, etherTokenAddress,
@ -235,18 +236,18 @@ describe('EtherTokenWrapper', () => {
}); });
it('should cancel outstanding subscriptions when ZeroEx.setProvider is called', (done: DoneCallback) => { it('should cancel outstanding subscriptions when ZeroEx.setProvider is called', (done: DoneCallback) => {
(async () => { (async () => {
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { const callbackNeverToBeCalled = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
done(new Error('Expected this subscription to have been cancelled')); done(new Error('Expected this subscription to have been cancelled'));
}; },
);
zeroEx.etherToken.subscribe( zeroEx.etherToken.subscribe(
etherTokenAddress, etherTokenAddress,
EtherTokenEvents.Transfer, EtherTokenEvents.Transfer,
indexFilterValues, indexFilterValues,
callbackNeverToBeCalled, callbackNeverToBeCalled,
); );
const callbackToBeCalled = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { const callbackToBeCalled = reportNodeCallbackErrors(done)();
done();
};
const newProvider = web3Factory.getRpcProvider(); const newProvider = web3Factory.getRpcProvider();
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH); await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH);
@ -266,9 +267,11 @@ describe('EtherTokenWrapper', () => {
}); });
it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => { it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => {
(async () => { (async () => {
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { const callbackNeverToBeCalled = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
done(new Error('Expected this subscription to have been cancelled')); done(new Error('Expected this subscription to have been cancelled'));
}; },
);
await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH); await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH);
const subscriptionToken = zeroEx.etherToken.subscribe( const subscriptionToken = zeroEx.etherToken.subscribe(
etherTokenAddress, etherTokenAddress,

View File

@ -1,6 +1,7 @@
import { BlockchainLifecycle } from '@0xproject/dev-utils'; import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils'; import { BigNumber } from '@0xproject/utils';
import * as chai from 'chai'; import * as chai from 'chai';
import * as _ from 'lodash';
import 'mocha'; import 'mocha';
import * as Web3 from 'web3'; import * as Web3 from 'web3';
@ -22,6 +23,7 @@ import { BlockParamLiteral, DoneCallback } from '../src/types';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { FillScenarios } from './utils/fill_scenarios'; import { FillScenarios } from './utils/fill_scenarios';
import { reportNodeCallbackErrors } from './utils/report_callback_errors';
import { TokenUtils } from './utils/token_utils'; import { TokenUtils } from './utils/token_utils';
import { web3Factory } from './utils/web3_factory'; import { web3Factory } from './utils/web3_factory';
@ -31,6 +33,13 @@ const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const NON_EXISTENT_ORDER_HASH = '0x79370342234e7acd6bbeac335bd3bb1d368383294b64b8160a00f4060e4d3777'; const NON_EXISTENT_ORDER_HASH = '0x79370342234e7acd6bbeac335bd3bb1d368383294b64b8160a00f4060e4d3777';
function noError<T>(err: Error | null, value: T | undefined): value is T {
if (_.isNull(err)) {
throw err;
}
return _.isNull(err);
}
describe('ExchangeWrapper', () => { describe('ExchangeWrapper', () => {
let web3: Web3; let web3: Web3;
let zeroEx: ZeroEx; let zeroEx: ZeroEx;
@ -851,10 +860,11 @@ describe('ExchangeWrapper', () => {
// Source: https://github.com/mochajs/mocha/issues/2407 // Source: https://github.com/mochajs/mocha/issues/2407
it('Should receive the LogFill event when an order is filled', (done: DoneCallback) => { it('Should receive the LogFill event when an order is filled', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<LogFillContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<LogFillContractEventArgs>) => {
expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogFill); expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogFill);
done(); },
}; );
zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callback); zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callback);
await zeroEx.exchange.fillOrderAsync( await zeroEx.exchange.fillOrderAsync(
signedOrder, signedOrder,
@ -866,28 +876,32 @@ describe('ExchangeWrapper', () => {
}); });
it('Should receive the LogCancel event when an order is cancelled', (done: DoneCallback) => { it('Should receive the LogCancel event when an order is cancelled', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<LogCancelContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<LogCancelContractEventArgs>) => {
expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogCancel); expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogCancel);
done(); },
}; );
zeroEx.exchange.subscribe(ExchangeEvents.LogCancel, indexFilterValues, callback); zeroEx.exchange.subscribe(ExchangeEvents.LogCancel, indexFilterValues, callback);
await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelTakerAmountInBaseUnits); await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelTakerAmountInBaseUnits);
})().catch(done); })().catch(done);
}); });
it('Outstanding subscriptions are cancelled when zeroEx.setProvider called', (done: DoneCallback) => { it('Outstanding subscriptions are cancelled when zeroEx.setProvider called', (done: DoneCallback) => {
(async () => { (async () => {
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<LogFillContractEventArgs>) => { const callbackNeverToBeCalled = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<LogFillContractEventArgs>) => {
done(new Error('Expected this subscription to have been cancelled')); done(new Error('Expected this subscription to have been cancelled'));
}; },
);
zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callbackNeverToBeCalled); zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callbackNeverToBeCalled);
const newProvider = web3Factory.getRpcProvider(); const newProvider = web3Factory.getRpcProvider();
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
const callback = (err: Error, logEvent: DecodedLogEvent<LogFillContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<LogFillContractEventArgs>) => {
expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogFill); expect(logEvent.log.event).to.be.equal(ExchangeEvents.LogFill);
done(); },
}; );
zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callback); zeroEx.exchange.subscribe(ExchangeEvents.LogFill, indexFilterValues, callback);
await zeroEx.exchange.fillOrderAsync( await zeroEx.exchange.fillOrderAsync(
signedOrder, signedOrder,
@ -899,9 +913,11 @@ describe('ExchangeWrapper', () => {
}); });
it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => { it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => {
(async () => { (async () => {
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<LogFillContractEventArgs>) => { const callbackNeverToBeCalled = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<LogFillContractEventArgs>) => {
done(new Error('Expected this subscription to have been cancelled')); done(new Error('Expected this subscription to have been cancelled'));
}; },
);
const subscriptionToken = zeroEx.exchange.subscribe( const subscriptionToken = zeroEx.exchange.subscribe(
ExchangeEvents.LogFill, ExchangeEvents.LogFill,
indexFilterValues, indexFilterValues,

View File

@ -15,7 +15,7 @@ import { utils } from '../src/utils/utils';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { constants as testConstants } from './utils/constants'; import { constants as testConstants } from './utils/constants';
import { FillScenarios } from './utils/fill_scenarios'; import { FillScenarios } from './utils/fill_scenarios';
import { reportCallbackErrors } from './utils/report_callback_errors'; import { reportNoErrorCallbackErrors } from './utils/report_callback_errors';
import { TokenUtils } from './utils/token_utils'; import { TokenUtils } from './utils/token_utils';
import { web3Factory } from './utils/web3_factory'; import { web3Factory } from './utils/web3_factory';
@ -87,10 +87,9 @@ describe('ExpirationWatcher', () => {
); );
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec.times(1000)); expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec.times(1000));
const callbackAsync = reportCallbackErrors(done)(async (hash: string) => { const callbackAsync = reportNoErrorCallbackErrors(done)((hash: string) => {
expect(hash).to.be.equal(orderHash); expect(hash).to.be.equal(orderHash);
expect(utils.getCurrentUnixTimestampSec()).to.be.bignumber.gte(expirationUnixTimestampSec); expect(utils.getCurrentUnixTimestampSec()).to.be.bignumber.gte(expirationUnixTimestampSec);
done();
}); });
expirationWatcher.subscribe(callbackAsync); expirationWatcher.subscribe(callbackAsync);
timer.tick(orderLifetimeSec * 1000); timer.tick(orderLifetimeSec * 1000);
@ -110,7 +109,7 @@ describe('ExpirationWatcher', () => {
); );
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec.times(1000)); expirationWatcher.addOrder(orderHash, signedOrder.expirationUnixTimestampSec.times(1000));
const callbackAsync = reportCallbackErrors(done)(async (hash: string) => { const callbackAsync = reportNoErrorCallbackErrors(done)(async (hash: string) => {
done(new Error('Emitted expiration went before the order actually expired')); done(new Error('Emitted expiration went before the order actually expired'));
}); });
expirationWatcher.subscribe(callbackAsync); expirationWatcher.subscribe(callbackAsync);
@ -146,7 +145,8 @@ describe('ExpirationWatcher', () => {
expirationWatcher.addOrder(orderHash2, signedOrder2.expirationUnixTimestampSec.times(1000)); expirationWatcher.addOrder(orderHash2, signedOrder2.expirationUnixTimestampSec.times(1000));
expirationWatcher.addOrder(orderHash1, signedOrder1.expirationUnixTimestampSec.times(1000)); expirationWatcher.addOrder(orderHash1, signedOrder1.expirationUnixTimestampSec.times(1000));
const expirationOrder = [orderHash1, orderHash2]; const expirationOrder = [orderHash1, orderHash2];
const callbackAsync = reportCallbackErrors(done)(async (hash: string) => { const expectToBeCalledOnce = false;
const callbackAsync = reportNoErrorCallbackErrors(done, expectToBeCalledOnce)((hash: string) => {
const orderHash = expirationOrder.shift(); const orderHash = expirationOrder.shift();
expect(hash).to.be.equal(orderHash); expect(hash).to.be.equal(orderHash);
if (_.isEmpty(expirationOrder)) { if (_.isEmpty(expirationOrder)) {

View File

@ -20,7 +20,7 @@ import { DoneCallback } from '../src/types';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { FillScenarios } from './utils/fill_scenarios'; import { FillScenarios } from './utils/fill_scenarios';
import { reportCallbackErrors } from './utils/report_callback_errors'; import { reportNoErrorCallbackErrors } from './utils/report_callback_errors';
import { TokenUtils } from './utils/token_utils'; import { TokenUtils } from './utils/token_utils';
import { web3Factory } from './utils/web3_factory'; import { web3Factory } from './utils/web3_factory';
@ -134,12 +134,11 @@ describe('OrderStateWatcher', () => {
); );
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.false(); expect(orderState.isValid).to.be.false();
const invalidOrderState = orderState as OrderStateInvalid; const invalidOrderState = orderState as OrderStateInvalid;
expect(invalidOrderState.orderHash).to.be.equal(orderHash); expect(invalidOrderState.orderHash).to.be.equal(orderHash);
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerAllowance); expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerAllowance);
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, new BigNumber(0)); await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, new BigNumber(0));
@ -155,7 +154,7 @@ describe('OrderStateWatcher', () => {
fillableAmount, fillableAmount,
); );
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
throw new Error('OrderState callback fired for irrelevant order'); throw new Error('OrderState callback fired for irrelevant order');
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
@ -179,12 +178,11 @@ describe('OrderStateWatcher', () => {
); );
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.false(); expect(orderState.isValid).to.be.false();
const invalidOrderState = orderState as OrderStateInvalid; const invalidOrderState = orderState as OrderStateInvalid;
expect(invalidOrderState.orderHash).to.be.equal(orderHash); expect(invalidOrderState.orderHash).to.be.equal(orderHash);
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerBalance); expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerBalance);
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
const anyRecipient = taker; const anyRecipient = taker;
@ -204,12 +202,11 @@ describe('OrderStateWatcher', () => {
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.false(); expect(orderState.isValid).to.be.false();
const invalidOrderState = orderState as OrderStateInvalid; const invalidOrderState = orderState as OrderStateInvalid;
expect(invalidOrderState.orderHash).to.be.equal(orderHash); expect(invalidOrderState.orderHash).to.be.equal(orderHash);
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderRemainingFillAmountZero); expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderRemainingFillAmountZero);
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
@ -237,7 +234,7 @@ describe('OrderStateWatcher', () => {
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.true(); expect(orderState.isValid).to.be.true();
const validOrderState = orderState as OrderStateValid; const validOrderState = orderState as OrderStateValid;
expect(validOrderState.orderHash).to.be.equal(orderHash); expect(validOrderState.orderHash).to.be.equal(orderHash);
@ -251,7 +248,6 @@ describe('OrderStateWatcher', () => {
remainingFillable, remainingFillable,
); );
expect(orderRelevantState.makerBalance).to.be.bignumber.equal(remainingMakerBalance); expect(orderRelevantState.makerBalance).to.be.bignumber.equal(remainingMakerBalance);
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
const shouldThrowOnInsufficientBalanceOrAllowance = true; const shouldThrowOnInsufficientBalanceOrAllowance = true;
@ -277,9 +273,7 @@ describe('OrderStateWatcher', () => {
fillableAmount, fillableAmount,
taker, taker,
); );
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)();
done();
});
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, maker, new BigNumber(0)); await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, maker, new BigNumber(0));
@ -301,7 +295,7 @@ describe('OrderStateWatcher', () => {
const fillAmountInBaseUnits = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals); const fillAmountInBaseUnits = ZeroEx.toBaseUnitAmount(new BigNumber(2), decimals);
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.true(); expect(orderState.isValid).to.be.true();
const validOrderState = orderState as OrderStateValid; const validOrderState = orderState as OrderStateValid;
expect(validOrderState.orderHash).to.be.equal(orderHash); expect(validOrderState.orderHash).to.be.equal(orderHash);
@ -312,7 +306,6 @@ describe('OrderStateWatcher', () => {
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal( expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
ZeroEx.toBaseUnitAmount(new BigNumber(8), decimals), ZeroEx.toBaseUnitAmount(new BigNumber(8), decimals),
); );
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
const shouldThrowOnInsufficientBalanceOrAllowance = true; const shouldThrowOnInsufficientBalanceOrAllowance = true;
@ -337,7 +330,7 @@ describe('OrderStateWatcher', () => {
const changedMakerApprovalAmount = ZeroEx.toBaseUnitAmount(new BigNumber(3), decimals); const changedMakerApprovalAmount = ZeroEx.toBaseUnitAmount(new BigNumber(3), decimals);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
const validOrderState = orderState as OrderStateValid; const validOrderState = orderState as OrderStateValid;
const orderRelevantState = validOrderState.orderRelevantState; const orderRelevantState = validOrderState.orderRelevantState;
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
@ -346,7 +339,6 @@ describe('OrderStateWatcher', () => {
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal( expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
changedMakerApprovalAmount, changedMakerApprovalAmount,
); );
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, changedMakerApprovalAmount); await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, changedMakerApprovalAmount);
@ -368,7 +360,7 @@ describe('OrderStateWatcher', () => {
const transferAmount = makerBalance.sub(remainingAmount); const transferAmount = makerBalance.sub(remainingAmount);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.true(); expect(orderState.isValid).to.be.true();
const validOrderState = orderState as OrderStateValid; const validOrderState = orderState as OrderStateValid;
const orderRelevantState = validOrderState.orderRelevantState; const orderRelevantState = validOrderState.orderRelevantState;
@ -378,7 +370,6 @@ describe('OrderStateWatcher', () => {
expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal( expect(orderRelevantState.remainingFillableTakerTokenAmount).to.be.bignumber.equal(
remainingAmount, remainingAmount,
); );
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.token.transferAsync(makerToken.address, maker, ZeroEx.NULL_ADDRESS, transferAmount); await zeroEx.token.transferAsync(makerToken.address, maker, ZeroEx.NULL_ADDRESS, transferAmount);
@ -404,14 +395,13 @@ describe('OrderStateWatcher', () => {
const transferTokenAmount = makerFee.sub(remainingTokenAmount); const transferTokenAmount = makerFee.sub(remainingTokenAmount);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.true(); expect(orderState.isValid).to.be.true();
const validOrderState = orderState as OrderStateValid; const validOrderState = orderState as OrderStateValid;
const orderRelevantState = validOrderState.orderRelevantState; const orderRelevantState = validOrderState.orderRelevantState;
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
remainingTokenAmount, remainingTokenAmount,
); );
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.exchange.cancelOrderAsync(signedOrder, transferTokenAmount); await zeroEx.exchange.cancelOrderAsync(signedOrder, transferTokenAmount);
@ -439,13 +429,12 @@ describe('OrderStateWatcher', () => {
const transferTokenAmount = makerFee.sub(remainingTokenAmount); const transferTokenAmount = makerFee.sub(remainingTokenAmount);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
const validOrderState = orderState as OrderStateValid; const validOrderState = orderState as OrderStateValid;
const orderRelevantState = validOrderState.orderRelevantState; const orderRelevantState = validOrderState.orderRelevantState;
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
remainingFeeAmount, remainingFeeAmount,
); );
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, maker, remainingFeeAmount); await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, maker, remainingFeeAmount);
@ -475,13 +464,12 @@ describe('OrderStateWatcher', () => {
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
const validOrderState = orderState as OrderStateValid; const validOrderState = orderState as OrderStateValid;
const orderRelevantState = validOrderState.orderRelevantState; const orderRelevantState = validOrderState.orderRelevantState;
expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal( expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
fillableAmount, fillableAmount,
); );
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.token.setProxyAllowanceAsync( await zeroEx.token.setProxyAllowanceAsync(
@ -504,12 +492,11 @@ describe('OrderStateWatcher', () => {
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.false(); expect(orderState.isValid).to.be.false();
const invalidOrderState = orderState as OrderStateInvalid; const invalidOrderState = orderState as OrderStateInvalid;
expect(invalidOrderState.orderHash).to.be.equal(orderHash); expect(invalidOrderState.orderHash).to.be.equal(orderHash);
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderRemainingFillAmountZero); expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderRemainingFillAmountZero);
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
@ -529,12 +516,11 @@ describe('OrderStateWatcher', () => {
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.false(); expect(orderState.isValid).to.be.false();
const invalidOrderState = orderState as OrderStateInvalid; const invalidOrderState = orderState as OrderStateInvalid;
expect(invalidOrderState.orderHash).to.be.equal(orderHash); expect(invalidOrderState.orderHash).to.be.equal(orderHash);
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderFillRoundingError); expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderFillRoundingError);
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.exchange.cancelOrderAsync( await zeroEx.exchange.cancelOrderAsync(
@ -557,13 +543,12 @@ describe('OrderStateWatcher', () => {
const orderHash = ZeroEx.getOrderHashHex(signedOrder); const orderHash = ZeroEx.getOrderHashHex(signedOrder);
zeroEx.orderStateWatcher.addOrder(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder);
const callback = reportCallbackErrors(done)((orderState: OrderState) => { const callback = reportNoErrorCallbackErrors(done)((orderState: OrderState) => {
expect(orderState.isValid).to.be.true(); expect(orderState.isValid).to.be.true();
const validOrderState = orderState as OrderStateValid; const validOrderState = orderState as OrderStateValid;
expect(validOrderState.orderHash).to.be.equal(orderHash); expect(validOrderState.orderHash).to.be.equal(orderHash);
const orderRelevantState = validOrderState.orderRelevantState; const orderRelevantState = validOrderState.orderRelevantState;
expect(orderRelevantState.cancelledTakerTokenAmount).to.be.bignumber.equal(cancelAmountInBaseUnits); expect(orderRelevantState.cancelledTakerTokenAmount).to.be.bignumber.equal(cancelAmountInBaseUnits);
done();
}); });
zeroEx.orderStateWatcher.subscribe(callback); zeroEx.orderStateWatcher.subscribe(callback);
await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmountInBaseUnits); await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmountInBaseUnits);

View File

@ -11,7 +11,7 @@ import { DoneCallback } from '../src/types';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { reportCallbackErrors } from './utils/report_callback_errors'; import { assertNodeCallbackError } from './utils/report_callback_errors';
import { web3Factory } from './utils/web3_factory'; import { web3Factory } from './utils/web3_factory';
chaiSetup.configure(); chaiSetup.configure();
@ -59,13 +59,7 @@ describe('SubscriptionTest', () => {
it('Should receive the Error when an error occurs while fetching the block', (done: DoneCallback) => { it('Should receive the Error when an error occurs while fetching the block', (done: DoneCallback) => {
(async () => { (async () => {
const errMsg = 'Error fetching block'; const errMsg = 'Error fetching block';
const callback = reportCallbackErrors(done)( const callback = assertNodeCallbackError(done, errMsg);
(err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
expect(err.message).to.be.equal(errMsg);
expect(logEvent).to.be.undefined();
done();
},
);
stubs = [Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync').throws(new Error(errMsg))]; stubs = [Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync').throws(new Error(errMsg))];
zeroEx.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback); zeroEx.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount);
@ -74,13 +68,7 @@ describe('SubscriptionTest', () => {
it('Should receive the Error when an error occurs while reconciling the new block', (done: DoneCallback) => { it('Should receive the Error when an error occurs while reconciling the new block', (done: DoneCallback) => {
(async () => { (async () => {
const errMsg = 'Error fetching logs'; const errMsg = 'Error fetching logs';
const callback = reportCallbackErrors(done)( const callback = assertNodeCallbackError(done, errMsg);
(err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
expect(err.message).to.be.equal(errMsg);
expect(logEvent).to.be.undefined();
done();
},
);
stubs = [Sinon.stub((zeroEx as any)._web3Wrapper, 'getLogsAsync').throws(new Error(errMsg))]; stubs = [Sinon.stub((zeroEx as any)._web3Wrapper, 'getLogsAsync').throws(new Error(errMsg))];
zeroEx.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback); zeroEx.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount);
@ -88,7 +76,7 @@ describe('SubscriptionTest', () => {
}); });
it('Should allow unsubscribeAll to be called successfully after an error', (done: DoneCallback) => { it('Should allow unsubscribeAll to be called successfully after an error', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => _.noop; const callback = (err: Error | null, logEvent?: DecodedLogEvent<ApprovalContractEventArgs>) => _.noop;
zeroEx.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback); zeroEx.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
stubs = [Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync').throws(new Error('JSON RPC error'))]; stubs = [Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync').throws(new Error('JSON RPC error'))];
zeroEx.token.unsubscribeAll(); zeroEx.token.unsubscribeAll();

View File

@ -20,6 +20,7 @@ import { DoneCallback } from '../src/types';
import { chaiSetup } from './utils/chai_setup'; import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { reportNodeCallbackErrors } from './utils/report_callback_errors';
import { TokenUtils } from './utils/token_utils'; import { TokenUtils } from './utils/token_utils';
import { web3Factory } from './utils/web3_factory'; import { web3Factory } from './utils/web3_factory';
@ -386,8 +387,8 @@ describe('TokenWrapper', () => {
// Source: https://github.com/mochajs/mocha/issues/2407 // Source: https://github.com/mochajs/mocha/issues/2407
it('Should receive the Transfer event when tokens are transfered', (done: DoneCallback) => { it('Should receive the Transfer event when tokens are transfered', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<TransferContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
expect(logEvent).to.not.be.undefined(); (logEvent: DecodedLogEvent<TransferContractEventArgs>) => {
expect(logEvent.isRemoved).to.be.false(); expect(logEvent.isRemoved).to.be.false();
expect(logEvent.log.logIndex).to.be.equal(0); expect(logEvent.log.logIndex).to.be.equal(0);
expect(logEvent.log.transactionIndex).to.be.equal(0); expect(logEvent.log.transactionIndex).to.be.equal(0);
@ -396,36 +397,37 @@ describe('TokenWrapper', () => {
expect(args._from).to.be.equal(coinbase); expect(args._from).to.be.equal(coinbase);
expect(args._to).to.be.equal(addressWithoutFunds); expect(args._to).to.be.equal(addressWithoutFunds);
expect(args._value).to.be.bignumber.equal(transferAmount); expect(args._value).to.be.bignumber.equal(transferAmount);
done(); },
}; );
zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callback); zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callback);
await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount); await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount);
})().catch(done); })().catch(done);
}); });
it('Should receive the Approval event when allowance is being set', (done: DoneCallback) => { it('Should receive the Approval event when allowance is being set', (done: DoneCallback) => {
(async () => { (async () => {
const callback = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { const callback = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
expect(logEvent).to.not.be.undefined(); expect(logEvent).to.not.be.undefined();
expect(logEvent.isRemoved).to.be.false(); expect(logEvent.isRemoved).to.be.false();
const args = logEvent.log.args; const args = logEvent.log.args;
expect(args._owner).to.be.equal(coinbase); expect(args._owner).to.be.equal(coinbase);
expect(args._spender).to.be.equal(addressWithoutFunds); expect(args._spender).to.be.equal(addressWithoutFunds);
expect(args._value).to.be.bignumber.equal(allowanceAmount); expect(args._value).to.be.bignumber.equal(allowanceAmount);
done(); },
}; );
zeroEx.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback); zeroEx.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount);
})().catch(done); })().catch(done);
}); });
it('Outstanding subscriptions are cancelled when zeroEx.setProvider called', (done: DoneCallback) => { it('Outstanding subscriptions are cancelled when zeroEx.setProvider called', (done: DoneCallback) => {
(async () => { (async () => {
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { const callbackNeverToBeCalled = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
done(new Error('Expected this subscription to have been cancelled')); done(new Error('Expected this subscription to have been cancelled'));
}; },
);
zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackNeverToBeCalled); zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackNeverToBeCalled);
const callbackToBeCalled = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { const callbackToBeCalled = reportNodeCallbackErrors(done)();
done();
};
const newProvider = web3Factory.getRpcProvider(); const newProvider = web3Factory.getRpcProvider();
zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID); zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackToBeCalled); zeroEx.token.subscribe(tokenAddress, TokenEvents.Transfer, indexFilterValues, callbackToBeCalled);
@ -434,9 +436,11 @@ describe('TokenWrapper', () => {
}); });
it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => { it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => {
(async () => { (async () => {
const callbackNeverToBeCalled = (err: Error, logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => { const callbackNeverToBeCalled = reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
done(new Error('Expected this subscription to have been cancelled')); done(new Error('Expected this subscription to have been cancelled'));
}; },
);
const subscriptionToken = zeroEx.token.subscribe( const subscriptionToken = zeroEx.token.subscribe(
tokenAddress, tokenAddress,
TokenEvents.Transfer, TokenEvents.Transfer,

View File

@ -1,10 +1,22 @@
import { DoneCallback } from '../../src/types'; import * as chai from 'chai';
import * as _ from 'lodash';
export const reportCallbackErrors = (done: DoneCallback) => { import { DoneCallback, OrderState } from '../../src/types';
return (f: (...args: any[]) => void) => {
const wrapped = async (...args: any[]) => { const expect = chai.expect;
export const reportNoErrorCallbackErrors = (done: DoneCallback, expectToBeCalledOnce = true) => {
return <T>(f?: (value: T) => void) => {
const wrapped = (value: T) => {
if (_.isUndefined(f)) {
done();
return;
}
try { try {
f(...args); f(value);
if (expectToBeCalledOnce) {
done();
}
} catch (err) { } catch (err) {
done(err); done(err);
} }
@ -12,3 +24,41 @@ export const reportCallbackErrors = (done: DoneCallback) => {
return wrapped; return wrapped;
}; };
}; };
export const reportNodeCallbackErrors = (done: DoneCallback) => {
return <T>(f?: (value: T) => void) => {
const wrapped = (error: Error | null, value: T | undefined) => {
if (!_.isNull(error)) {
done(error);
} else {
if (_.isUndefined(f)) {
done();
return;
}
try {
f(value as T);
done();
} catch (err) {
done(err);
}
}
};
return wrapped;
};
};
export const assertNodeCallbackError = (done: DoneCallback, errMsg: string) => {
const wrapped = <T>(error: Error | null, value: T | undefined) => {
if (_.isNull(error)) {
done(new Error('Expected callback to receive an error'));
} else {
try {
expect(error.message).to.be.equal(errMsg);
done();
} catch (err) {
done(err);
}
}
};
return wrapped;
};

View File

@ -2,7 +2,7 @@
"extends": "../../tsconfig", "extends": "../../tsconfig",
"compilerOptions": { "compilerOptions": {
"outDir": "lib", "outDir": "lib",
"strict": false "noImplicitThis": false
}, },
"include": [ "include": [
"./src/**/*", "./src/**/*",