aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-13 07:11:45 +0800
committerGitHub <noreply@github.com>2017-11-13 07:11:45 +0800
commit6daf70b745b561f9a767886e2b40891da7ebc184 (patch)
tree63cfebd8e95b3dc15a8f593ffa105a9b3bf6412e /test
parentabb23631df4fd775d2f9c96332e16c0732b2955e (diff)
parent12298ea392525ed1de87115003e7d89267489c88 (diff)
downloaddexon-sol-tools-6daf70b745b561f9a767886e2b40891da7ebc184.tar
dexon-sol-tools-6daf70b745b561f9a767886e2b40891da7ebc184.tar.gz
dexon-sol-tools-6daf70b745b561f9a767886e2b40891da7ebc184.tar.bz2
dexon-sol-tools-6daf70b745b561f9a767886e2b40891da7ebc184.tar.lz
dexon-sol-tools-6daf70b745b561f9a767886e2b40891da7ebc184.tar.xz
dexon-sol-tools-6daf70b745b561f9a767886e2b40891da7ebc184.tar.zst
dexon-sol-tools-6daf70b745b561f9a767886e2b40891da7ebc184.zip
Merge pull request #207 from 0xProject/orderWatcherTests
Additional order watcher tests
Diffstat (limited to 'test')
-rw-r--r--test/order_state_watcher_test.ts88
-rw-r--r--test/utils/blockchain_lifecycle.ts3
-rw-r--r--test/utils/rpc.ts6
3 files changed, 97 insertions, 0 deletions
diff --git a/test/order_state_watcher_test.ts b/test/order_state_watcher_test.ts
index 3421353e3..d8ac0af49 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;
@@ -121,6 +126,27 @@ 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);
+ setTimeout(() => {
+ done();
+ }, TIMEOUT_MS);
+ })().catch(done);
+ });
it('should emit orderStateInvalid when maker moves balance backing watched order', (done: DoneCallback) => {
(async () => {
signedOrder = await fillScenarios.createFillableSignedOrderAsync(
@@ -247,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<void> {
+ await this.rpc.mineBlockAsync();
+ }
}
diff --git a/test/utils/rpc.ts b/test/utils/rpc.ts
index f28a85340..299e72e79 100644
--- a/test/utils/rpc.ts
+++ b/test/utils/rpc.ts
@@ -26,6 +26,12 @@ export class RPC {
const didRevert = await this.sendAsync(payload);
return didRevert;
}
+ public async mineBlockAsync(): Promise<void> {
+ const method = 'evm_mine';
+ const params: any[] = [];
+ const payload = this.toPayload(method, params);
+ await this.sendAsync(payload);
+ }
private toPayload(method: string, params: any[] = []): string {
const payload = JSON.stringify({
id: this.id,