From d90756e8ef2634737d41633f5427446b47223e98 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 10 Nov 2017 10:07:29 -0500 Subject: Test that the orderStateWatcher doesn't emit an event when an irrelevant blockchain event is received. --- test/order_state_watcher_test.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/order_state_watcher_test.ts b/test/order_state_watcher_test.ts index 10ed6bd77..1231d7e16 100644 --- a/test/order_state_watcher_test.ts +++ b/test/order_state_watcher_test.ts @@ -121,6 +121,28 @@ describe('OrderStateWatcher', () => { await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, new BigNumber(0)); })().catch(done); }); + it('should not emit an orderState event when irrelevant Transfer event received', (done: DoneCallback) => { + (async () => { + 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 for irrelevant order'); + }); + zeroEx.orderStateWatcher.subscribe(callback); + const notTheMaker = userAddresses[0]; + const anyRecipient = taker; + 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); + })().catch(done); + }); it('should emit orderStateInvalid when maker moves balance backing watched order', (done: DoneCallback) => { (async () => { signedOrder = await fillScenarios.createFillableSignedOrderAsync( -- cgit v1.2.3 From 2bf65fda1f6b3018edbb4158574465326d4918be Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 10 Nov 2017 16:34:21 -0500 Subject: Add tests for the numConfirmations config to ensure that the events are being emitted for the confirmation depth specified --- test/order_state_watcher_test.ts | 70 ++++++++++++++++++++++++++++++++++++-- test/utils/blockchain_lifecycle.ts | 3 ++ test/utils/rpc.ts | 7 ++++ 3 files changed, 78 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); + }); + }); }); }); diff --git a/test/utils/blockchain_lifecycle.ts b/test/utils/blockchain_lifecycle.ts index 9fdf0e856..9a44ccd6f 100644 --- a/test/utils/blockchain_lifecycle.ts +++ b/test/utils/blockchain_lifecycle.ts @@ -20,4 +20,7 @@ export class BlockchainLifecycle { throw new Error(`Snapshot with id #${snapshotId} failed to revert`); } } + public async mineABlock(): Promise { + await this.rpc.mineBlockAsync(); + } } diff --git a/test/utils/rpc.ts b/test/utils/rpc.ts index f28a85340..1fc9f2428 100644 --- a/test/utils/rpc.ts +++ b/test/utils/rpc.ts @@ -26,6 +26,13 @@ export class RPC { const didRevert = await this.sendAsync(payload); return didRevert; } + public async mineBlockAsync(): Promise { + const method = 'evm_mine'; + const params: any[] = []; + const payload = this.toPayload(method, params); + const didRevert = await this.sendAsync(payload); + return didRevert; + } private toPayload(method: string, params: any[] = []): string { const payload = JSON.stringify({ id: this.id, -- cgit v1.2.3 From 12298ea392525ed1de87115003e7d89267489c88 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Sun, 12 Nov 2017 18:11:30 -0500 Subject: Don't return anything --- test/utils/rpc.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/utils/rpc.ts b/test/utils/rpc.ts index 1fc9f2428..299e72e79 100644 --- a/test/utils/rpc.ts +++ b/test/utils/rpc.ts @@ -30,8 +30,7 @@ export class RPC { const method = 'evm_mine'; const params: any[] = []; const payload = this.toPayload(method, params); - const didRevert = await this.sendAsync(payload); - return didRevert; + await this.sendAsync(payload); } private toPayload(method: string, params: any[] = []): string { const payload = JSON.stringify({ -- cgit v1.2.3