aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-10-31 00:38:10 +0800
committerFabio Berger <me@fabioberger.com>2017-10-31 00:49:16 +0800
commita896904ae7c453f51b1f46de2be3a28416db72d1 (patch)
treec50c3638b0b9bee982816bb48a4c935de798476a /test
parent6bfcd253f8e9689ce787899a42f80914b067a4f1 (diff)
downloaddexon-sol-tools-a896904ae7c453f51b1f46de2be3a28416db72d1.tar
dexon-sol-tools-a896904ae7c453f51b1f46de2be3a28416db72d1.tar.gz
dexon-sol-tools-a896904ae7c453f51b1f46de2be3a28416db72d1.tar.bz2
dexon-sol-tools-a896904ae7c453f51b1f46de2be3a28416db72d1.tar.lz
dexon-sol-tools-a896904ae7c453f51b1f46de2be3a28416db72d1.tar.xz
dexon-sol-tools-a896904ae7c453f51b1f46de2be3a28416db72d1.tar.zst
dexon-sol-tools-a896904ae7c453f51b1f46de2be3a28416db72d1.zip
Add naive order state watcher implementation
Revalidate all orders upon event received and emit order states even if not changed
Diffstat (limited to 'test')
-rw-r--r--test/order_watcher_test.ts78
1 files changed, 69 insertions, 9 deletions
diff --git a/test/order_watcher_test.ts b/test/order_watcher_test.ts
index e62b1aab2..3ce60d863 100644
--- a/test/order_watcher_test.ts
+++ b/test/order_watcher_test.ts
@@ -9,10 +9,15 @@ 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,
+ OrderStateValid,
} from '../src';
+import {TokenUtils} from './utils/token_utils';
+import {FillScenarios} from './utils/fill_scenarios';
import {DoneCallback} from '../src/types';
chaiSetup.configure();
@@ -21,22 +26,77 @@ const expect = chai.expect;
describe('EventWatcher', () => {
let web3: Web3;
let stubs: Sinon.SinonStub[] = [];
- let orderStateWatcher: OrderStateWatcher;
+ let zeroEx: ZeroEx;
+ let tokens: Token[];
+ let tokenUtils: TokenUtils;
+ let fillScenarios: FillScenarios;
+ let userAddresses: string[];
+ let zrxTokenAddress: string;
+ let exchangeContractAddress: string;
+ let makerToken: Token;
+ let takerToken: Token;
+ let maker: string;
+ let taker: string;
+ let web3Wrapper: Web3Wrapper;
+ 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();
- const mempoolPollingIntervalMs = 10;
- const orderStateWatcherConfig = {
- mempoolPollingIntervalMs,
- };
- orderStateWatcher = new OrderStateWatcher(web3.currentProvider, orderStateWatcherConfig);
+ zeroEx = new ZeroEx(web3.currentProvider);
+ exchangeContractAddress = await zeroEx.exchange.getContractAddressAsync();
+ userAddresses = await zeroEx.getAvailableAddressesAsync();
+ [, maker, taker] = userAddresses;
+ tokens = await zeroEx.tokenRegistry.getTokensAsync();
+ tokenUtils = new TokenUtils(tokens);
+ zrxTokenAddress = tokenUtils.getProtocolTokenOrThrow().address;
+ fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens, zrxTokenAddress, exchangeContractAddress);
+ [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 = [];
- orderStateWatcher.unsubscribe();
+ zeroEx.orderStateWatcher.unsubscribe();
});
- it.skip('TODO', () => {
- // TODO
+ it('should receive OrderState when order state is changed', (done: DoneCallback) => {
+ (async () => {
+ const 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);
+ done();
+ };
+ zeroEx.orderStateWatcher.subscribe(callback);
+ })().catch(done);
});
});