diff options
Diffstat (limited to 'test/order_watcher_test.ts')
-rw-r--r-- | test/order_watcher_test.ts | 107 |
1 files changed, 63 insertions, 44 deletions
diff --git a/test/order_watcher_test.ts b/test/order_watcher_test.ts index 3ce60d863..11138567c 100644 --- a/test/order_watcher_test.ts +++ b/test/order_watcher_test.ts @@ -1,31 +1,32 @@ import 'mocha'; import * as chai from 'chai'; import * as _ from 'lodash'; -import * as Sinon from 'sinon'; import * as Web3 from 'web3'; import BigNumber from 'bignumber.js'; -import {chaiSetup} from './utils/chai_setup'; -import {web3Factory} from './utils/web3_factory'; -import {Web3Wrapper} from '../src/web3_wrapper'; -import {OrderStateWatcher} from '../src/mempool/order_state_watcher'; +import { chaiSetup } from './utils/chai_setup'; +import { web3Factory } from './utils/web3_factory'; +import { Web3Wrapper } from '../src/web3_wrapper'; +import { OrderStateWatcher } from '../src/mempool/order_state_watcher'; import { Token, ZeroEx, LogEvent, DecodedLogEvent, OrderState, + SignedOrder, OrderStateValid, + OrderStateInvalid, + ExchangeContractErrs, } from '../src'; -import {TokenUtils} from './utils/token_utils'; -import {FillScenarios} from './utils/fill_scenarios'; -import {DoneCallback} from '../src/types'; +import { TokenUtils } from './utils/token_utils'; +import { FillScenarios } from './utils/fill_scenarios'; +import { DoneCallback } from '../src/types'; chaiSetup.configure(); const expect = chai.expect; -describe('EventWatcher', () => { +describe.only('EventWatcher', () => { let web3: Web3; - let stubs: Sinon.SinonStub[] = []; let zeroEx: ZeroEx; let tokens: Token[]; let tokenUtils: TokenUtils; @@ -38,22 +39,8 @@ describe('EventWatcher', () => { let maker: string; let taker: string; let web3Wrapper: Web3Wrapper; + let signedOrder: SignedOrder; const fillableAmount = new BigNumber(5); - const fakeLog = { - address: '0xcdb594a32b1cc3479d8746279712c39d18a07fc0', - blockHash: '0x2d5cec6e3239d40993b74008f684af82b69f238697832e4c4d58e0ba5a2fa99e', - blockNumber: '0x34', - data: '0x0000000000000000000000000000000000000000000000000000000000000028', - logIndex: '0x00', - topics: [ - '0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925', - '0x0000000000000000000000006ecbe1db9ef729cbe972c83fb886247691fb6beb', - '0x000000000000000000000000871dd7c2b4b25e1aa18728e9d5f2af4c4e431f5c', - ], - transactionHash: '0xa550fbe937985c383ed7ed077cf6011960a3c2d38ea39dea209426546f0e95cb', - transactionIndex: '0x00', - type: 'mined', - }; before(async () => { web3 = web3Factory.create(); zeroEx = new ZeroEx(web3.currentProvider); @@ -67,36 +54,68 @@ describe('EventWatcher', () => { [makerToken, takerToken] = tokenUtils.getNonProtocolTokens(); web3Wrapper = (zeroEx as any)._web3Wrapper; }); - beforeEach(() => { - const getLogsStub = Sinon.stub(web3Wrapper, 'getLogsAsync'); - getLogsStub.onCall(0).returns([fakeLog]); - }); - afterEach(() => { - // clean up any stubs after the test has completed - _.each(stubs, s => s.restore()); - stubs = []; + afterEach(async () => { zeroEx.orderStateWatcher.unsubscribe(); + zeroEx.orderStateWatcher.removeOrder(signedOrder); }); - it('should receive OrderState when order state is changed', (done: DoneCallback) => { + it('should emit orderStateInvalid when maker allowance set to 0 for watched order', (done: DoneCallback) => { (async () => { - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerToken.address, takerToken.address, maker, taker, fillableAmount, ); const orderHash = ZeroEx.getOrderHashHex(signedOrder); zeroEx.orderStateWatcher.addOrder(signedOrder); const callback = (orderState: OrderState) => { - expect(orderState.isValid).to.be.true(); - expect(orderState.orderHash).to.be.equal(orderHash); - const orderRelevantState = (orderState as OrderStateValid).orderRelevantState; - expect(orderRelevantState.makerBalance).to.be.bignumber.equal(fillableAmount); - expect(orderRelevantState.makerProxyAllowance).to.be.bignumber.equal(fillableAmount); - expect(orderRelevantState.makerFeeBalance).to.be.bignumber.equal(0); - expect(orderRelevantState.makerFeeProxyAllowance).to.be.bignumber.equal(0); - expect(orderRelevantState.filledTakerTokenAmount).to.be.bignumber.equal(0); - expect(orderRelevantState.canceledTakerTokenAmount).to.be.bignumber.equal(0); + 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.InsufficientMakerAllowance); done(); }; zeroEx.orderStateWatcher.subscribe(callback); + await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, new BigNumber(0)); + })().catch(done); + }); + it('should emit orderStateInvalid when maker moves balance backing watched order', (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 = (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); + })().catch(done); + }); + it('should emit orderStateInvalid when watched order fully filled', (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 = (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.OrderRemainingFillAmountZero); + done(); + }; + zeroEx.orderStateWatcher.subscribe(callback); + + const shouldThrowOnInsufficientBalanceOrAllowance = true; + await zeroEx.exchange.fillOrderAsync( + signedOrder, fillableAmount, shouldThrowOnInsufficientBalanceOrAllowance, taker, + ); })().catch(done); }); }); |