aboutsummaryrefslogtreecommitdiffstats
path: root/test/order_state_watcher_test.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-11 05:34:21 +0800
committerFabio Berger <me@fabioberger.com>2017-11-11 05:34:21 +0800
commit2bf65fda1f6b3018edbb4158574465326d4918be (patch)
tree5d0ab098003463d6df35fef4bef8b113d0939908 /test/order_state_watcher_test.ts
parentd90756e8ef2634737d41633f5427446b47223e98 (diff)
downloaddexon-0x-contracts-2bf65fda1f6b3018edbb4158574465326d4918be.tar.gz
dexon-0x-contracts-2bf65fda1f6b3018edbb4158574465326d4918be.tar.zst
dexon-0x-contracts-2bf65fda1f6b3018edbb4158574465326d4918be.zip
Add tests for the numConfirmations config to ensure that the events are being emitted for the confirmation depth specified
Diffstat (limited to 'test/order_state_watcher_test.ts')
-rw-r--r--test/order_state_watcher_test.ts70
1 files changed, 68 insertions, 2 deletions
diff --git a/test/order_state_watcher_test.ts b/test/order_state_watcher_test.ts
index 1231d7e16..269956400 100644
--- a/test/order_state_watcher_test.ts
+++ b/test/order_state_watcher_test.ts
@@ -12,6 +12,7 @@ import {
ZeroEx,
LogEvent,
DecodedLogEvent,
+ ZeroExConfig,
OrderState,
SignedOrder,
OrderStateValid,
@@ -21,10 +22,14 @@ import {
import { TokenUtils } from './utils/token_utils';
import { FillScenarios } from './utils/fill_scenarios';
import { DoneCallback } from '../src/types';
+import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
import {reportCallbackErrors} from './utils/report_callback_errors';
+const TIMEOUT_MS = 150;
+
chaiSetup.configure();
const expect = chai.expect;
+const blockchainLifecycle = new BlockchainLifecycle();
describe('OrderStateWatcher', () => {
let web3: Web3;
@@ -137,10 +142,9 @@ describe('OrderStateWatcher', () => {
const transferAmount = new BigNumber(2);
const notTheMakerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, notTheMaker);
await zeroEx.token.transferAsync(makerToken.address, notTheMaker, anyRecipient, transferAmount);
- const timeoutInMs = 150;
setTimeout(() => {
done();
- }, timeoutInMs);
+ }, TIMEOUT_MS);
})().catch(done);
});
it('should emit orderStateInvalid when maker moves balance backing watched order', (done: DoneCallback) => {
@@ -269,5 +273,67 @@ describe('OrderStateWatcher', () => {
await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmountInBaseUnits);
})().catch(done);
});
+ describe('check numConfirmations behavior', () => {
+ before(() => {
+ const configs: ZeroExConfig = {
+ orderWatcherConfig: {
+ numConfirmations: 1,
+ },
+ };
+ zeroEx = new ZeroEx(web3.currentProvider, configs);
+ });
+ it('should emit orderState when watching at 1 confirmation deep and event is one block deep',
+ (done: DoneCallback) => {
+ (async () => {
+ fillScenarios = new FillScenarios(
+ zeroEx, userAddresses, tokens, zrxTokenAddress, exchangeContractAddress,
+ );
+
+ signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerToken.address, takerToken.address, maker, taker, fillableAmount,
+ );
+ const orderHash = ZeroEx.getOrderHashHex(signedOrder);
+ zeroEx.orderStateWatcher.addOrder(signedOrder);
+ const callback = reportCallbackErrors(done)((orderState: OrderState) => {
+ expect(orderState.isValid).to.be.false();
+ const invalidOrderState = orderState as OrderStateInvalid;
+ expect(invalidOrderState.orderHash).to.be.equal(orderHash);
+ expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.InsufficientMakerBalance);
+ done();
+ });
+ zeroEx.orderStateWatcher.subscribe(callback);
+
+ const anyRecipient = taker;
+ const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
+ await zeroEx.token.transferAsync(makerToken.address, maker, anyRecipient, makerBalance);
+ blockchainLifecycle.mineABlock();
+ })().catch(done);
+ });
+ it('shouldn\'t emit orderState when watching at 1 confirmation deep and event is in mempool',
+ (done: DoneCallback) => {
+ (async () => {
+ fillScenarios = new FillScenarios(
+ zeroEx, userAddresses, tokens, zrxTokenAddress, exchangeContractAddress,
+ );
+
+ signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerToken.address, takerToken.address, maker, taker, fillableAmount,
+ );
+ const orderHash = ZeroEx.getOrderHashHex(signedOrder);
+ zeroEx.orderStateWatcher.addOrder(signedOrder);
+ const callback = reportCallbackErrors(done)((orderState: OrderState) => {
+ throw new Error('OrderState callback fired when it shouldn\'t have');
+ });
+ zeroEx.orderStateWatcher.subscribe(callback);
+
+ const anyRecipient = taker;
+ const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
+ await zeroEx.token.transferAsync(makerToken.address, maker, anyRecipient, makerBalance);
+ setTimeout(() => {
+ done();
+ }, TIMEOUT_MS);
+ })().catch(done);
+ });
+ });
});
});