From 9bb738bb2f2b26b61049315ca26a96511460062e Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 29 May 2017 22:45:05 +0200 Subject: Finish ecSignature renaming --- test/utils/blockchain_lifecycle.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/utils/blockchain_lifecycle.ts b/test/utils/blockchain_lifecycle.ts index 68e169ac0..50eb57b95 100644 --- a/test/utils/blockchain_lifecycle.ts +++ b/test/utils/blockchain_lifecycle.ts @@ -17,4 +17,4 @@ export class BlockchainLifecycle { throw new Error(`Snapshot with id #${this.snapshotId} failed to revert`); } } -}; +} -- cgit v1.2.3 From 72e3b0e2904551db6866bcf4d13cdc9b44722175 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 12:01:32 +0200 Subject: Add first order fixture --- test/fixtures/orders/5_MKR_for_42_MLN.json | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/fixtures/orders/5_MKR_for_42_MLN.json (limited to 'test') diff --git a/test/fixtures/orders/5_MKR_for_42_MLN.json b/test/fixtures/orders/5_MKR_for_42_MLN.json new file mode 100644 index 000000000..2c2a3d73e --- /dev/null +++ b/test/fixtures/orders/5_MKR_for_42_MLN.json @@ -0,0 +1,35 @@ +{ + "maker": { + "address": "0xffa119a5761eb93eacfe5d2b0e8944648c3d7164", + "token": { + "name": "MakerDAO", + "symbol": "MKR", + "decimals": 18, + "address": "0x1dad4783cf3fe3085c1426157ab175a6119a04ba" + }, + "amount": "5000000000000000000", + "feeAmount": "0" + }, + "taker": { + "address": "", + "token": { + "name": "Melon Token", + "symbol": "MLN", + "decimals": 18, + "address": "0x323b5d4c32345ced77393b3530b1eed0f346429d" + }, + "amount": "42000000000000000000", + "feeAmount": "0" + }, + "expiration": "1496181600", + "feeRecipient": "0x0000000000000000000000000000000000000000", + "salt": "28894038927316056783595066163529969528517282468995826064292810658599101267743", + "signature": { + "v": 28, + "r": "0x1e84173b09bc51e4e3f923718d747bd91c3584cfa3556d79294891ddd740e819", + "s": "0x36c0f453a01487f49e16157703cacb6cee08d337549f805c25d3f78221823eed", + "hash": "0x3f64711f39393f7f60a0980e3effba087c15bb9b1cbf678ede63a69798e5dc14" + }, + "exchangeContract": "0x9ce1a5e2311f9b8b8e6b40ed20b5b090de4a4c4d", + "networkId": 42 +} \ No newline at end of file -- cgit v1.2.3 From acd5fba1336659053be470e4041b803150588cc5 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 12:56:51 +0200 Subject: Add order utils --- test/utils/order.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test/utils/order.ts (limited to 'test') diff --git a/test/utils/order.ts b/test/utils/order.ts new file mode 100644 index 000000000..dc0be670c --- /dev/null +++ b/test/utils/order.ts @@ -0,0 +1,21 @@ +import {SignedOrder} from '../../lib/src/types'; +import * as BigNumber from 'bignumber.js'; +import * as _ from 'lodash'; + +export function signedOrderFromJSON(signedOrderJSON: any): SignedOrder { + const signedOrder = { + maker: signedOrderJSON.maker.address, + taker: _.isEmpty(signedOrderJSON.taker.address) ? undefined : signedOrderJSON.taker.address, + makerTokenAddress: signedOrderJSON.maker.token.address, + takerTokenAddress: signedOrderJSON.taker.token.address, + makerTokenAmount: new BigNumber(signedOrderJSON.maker.amount), + takerTokenAmount: new BigNumber(signedOrderJSON.taker.amount), + makerFee: new BigNumber(signedOrderJSON.maker.feeAmount), + takerFee: new BigNumber(signedOrderJSON.taker.feeAmount), + expirationUnixTimestampSec: new BigNumber(signedOrderJSON.expiration), + feeRecipient: signedOrderJSON.feeRecipient, + ecSignature: signedOrderJSON.signature, + salt: new BigNumber(signedOrderJSON.salt), + }; + return signedOrder; +} -- cgit v1.2.3 From 18d65f5f55bcdbbb1e1058ac9e8fe193804b1a06 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 14:36:14 +0200 Subject: Append _test to exchange_wrapper --- test/exchange_wrapper.ts | 95 -------------------------------------- test/exchange_wrapper_test.ts | 105 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 95 deletions(-) delete mode 100644 test/exchange_wrapper.ts create mode 100644 test/exchange_wrapper_test.ts (limited to 'test') diff --git a/test/exchange_wrapper.ts b/test/exchange_wrapper.ts deleted file mode 100644 index 4bfc49374..000000000 --- a/test/exchange_wrapper.ts +++ /dev/null @@ -1,95 +0,0 @@ -import 'mocha'; -import * as chai from 'chai'; -import chaiAsPromised = require('chai-as-promised'); -import * as Web3 from 'web3'; -import {web3Factory} from './utils/web3_factory'; -import {ZeroEx} from '../src/0x.js'; -import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; - -const expect = chai.expect; -chai.use(chaiAsPromised); -const blockchainLifecycle = new BlockchainLifecycle(); - -describe('ExchangeWrapper', () => { - let zeroEx: ZeroEx; - before(async () => { - const web3 = web3Factory.create(); - zeroEx = new ZeroEx(web3); - }); - beforeEach(async () => { - await blockchainLifecycle.startAsync(); - }); - afterEach(async () => { - await blockchainLifecycle.revertAsync(); - }); - describe('#isValidSignatureAsync', () => { - // The Exchange smart contract `isValidSignature` method only validates orderHashes and assumes - // the length of the data is exactly 32 bytes. Thus for these tests, we use data of this size. - const dataHex = '0x6927e990021d23b1eb7b8789f6a6feaf98fe104bb0cf8259421b79f9a34222b0'; - const signature = { - v: 27, - r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33', - s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254', - }; - const address = '0x5409ed021d9299bf6814279a6a1411a7e866a631'; - describe('should throw if passed a malformed signature', () => { - it('malformed v', async () => { - const malformedSignature = { - v: 34, - r: signature.r, - s: signature.s, - }; - expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected; - }); - it('r lacks 0x prefix', () => { - const malformedR = signature.r.replace('0x', ''); - const malformedSignature = { - v: signature.v, - r: malformedR, - s: signature.s, - }; - expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected; - }); - it('r is too short', () => { - const malformedR = signature.r.substr(10); - const malformedSignature = { - v: signature.v, - r: malformedR, - s: signature.s.replace('0', 'z'), - }; - expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected; - }); - it('s is not hex', () => { - const malformedS = signature.s.replace('0', 'z'); - const malformedSignature = { - v: signature.v, - r: signature.r, - s: malformedS, - }; - expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected; - }); - }); - it('should return false if the data doesn\'t pertain to the signature & address', async () => { - const isValid = await zeroEx.exchange.isValidSignatureAsync('0x0', signature, address); - expect(isValid).to.be.false; - }); - it('should return false if the address doesn\'t pertain to the signature & dataHex', async () => { - const validUnrelatedAddress = '0x8b0292B11a196601eD2ce54B665CaFEca0347D42'; - const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, signature, validUnrelatedAddress); - expect(isValid).to.be.false; - }); - it('should return false if the signature doesn\'t pertain to the dataHex & address', async () => { - const wrongSignature = Object.assign({}, signature, {v: 28}); - const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, wrongSignature, address); - expect(isValid).to.be.false; - }); - it('should return true if the signature does pertain to the dataHex & address', async () => { - const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, signature, address); - expect(isValid).to.be.true; - }); - }); -}); diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts new file mode 100644 index 000000000..bb09a0344 --- /dev/null +++ b/test/exchange_wrapper_test.ts @@ -0,0 +1,105 @@ +import 'mocha'; +import * as chai from 'chai'; +import chaiAsPromised = require('chai-as-promised'); +import {web3Factory} from './utils/web3_factory'; +import {ZeroEx} from '../src/0x.js'; +import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; +import * as OrderJSON from './fixtures/orders/5_MKR_for_42_MLN.json'; +import * as BigNumber from 'bignumber.js'; +import {signedOrderFromJSON} from './utils/order'; + +const expect = chai.expect; +chai.use(chaiAsPromised); +const blockchainLifecycle = new BlockchainLifecycle(); + +describe('ExchangeWrapper', () => { + let zeroEx: ZeroEx; + before(async () => { + const web3 = web3Factory.create(); + zeroEx = new ZeroEx(web3); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + describe('#isValidSignatureAsync', () => { + // The Exchange smart contract `isValidSignature` method only validates orderHashes and assumes + // the length of the data is exactly 32 bytes. Thus for these tests, we use data of this size. + const dataHex = '0x6927e990021d23b1eb7b8789f6a6feaf98fe104bb0cf8259421b79f9a34222b0'; + const signature = { + v: 27, + r: '0x61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc33', + s: '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254', + }; + const address = '0x5409ed021d9299bf6814279a6a1411a7e866a631'; + describe('should throw if passed a malformed signature', () => { + it('malformed v', async () => { + const malformedSignature = { + v: 34, + r: signature.r, + s: signature.s, + }; + expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) + .to.be.rejected; + }); + it('r lacks 0x prefix', () => { + const malformedR = signature.r.replace('0x', ''); + const malformedSignature = { + v: signature.v, + r: malformedR, + s: signature.s, + }; + expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) + .to.be.rejected; + }); + it('r is too short', () => { + const malformedR = signature.r.substr(10); + const malformedSignature = { + v: signature.v, + r: malformedR, + s: signature.s.replace('0', 'z'), + }; + expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) + .to.be.rejected; + }); + it('s is not hex', () => { + const malformedS = signature.s.replace('0', 'z'); + const malformedSignature = { + v: signature.v, + r: signature.r, + s: malformedS, + }; + expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) + .to.be.rejected; + }); + }); + it('should return false if the data doesn\'t pertain to the signature & address', async () => { + const isValid = await zeroEx.exchange.isValidSignatureAsync('0x0', signature, address); + expect(isValid).to.be.false; + }); + it('should return false if the address doesn\'t pertain to the signature & dataHex', async () => { + const validUnrelatedAddress = '0x8b0292B11a196601eD2ce54B665CaFEca0347D42'; + const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, signature, validUnrelatedAddress); + expect(isValid).to.be.false; + }); + it('should return false if the signature doesn\'t pertain to the dataHex & address', async () => { + const wrongSignature = Object.assign({}, signature, {v: 28}); + const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, wrongSignature, address); + expect(isValid).to.be.false; + }); + it('should return true if the signature does pertain to the dataHex & address', async () => { + const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, signature, address); + expect(isValid).to.be.true; + }); + }); + describe('#fillOrderAsync', () => { + const fillAmount = new BigNumber(1); + const signedOrder = signedOrderFromJSON(OrderJSON); + it('fillsOrder', async () => { + const orderFillResponse = await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); + console.log(orderFillResponse); + }); + }); +}); -- cgit v1.2.3 From dfcf49464b1a93b6e5df39289e9d14b2e60e62d2 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 14:49:04 +0200 Subject: Temporarily remove test --- test/exchange_wrapper_test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index bb09a0344..b97a62100 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -98,8 +98,7 @@ describe('ExchangeWrapper', () => { const fillAmount = new BigNumber(1); const signedOrder = signedOrderFromJSON(OrderJSON); it('fillsOrder', async () => { - const orderFillResponse = await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); - console.log(orderFillResponse); + // const orderFillResponse = await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); }); }); }); -- cgit v1.2.3 From be4248f3459e120cc2a67d2d8a7238445571056c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 14:59:24 +0200 Subject: Fix import --- test/utils/order.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/utils/order.ts b/test/utils/order.ts index dc0be670c..690f50ef5 100644 --- a/test/utils/order.ts +++ b/test/utils/order.ts @@ -1,4 +1,4 @@ -import {SignedOrder} from '../../lib/src/types'; +import {SignedOrder} from '../../src/types'; import * as BigNumber from 'bignumber.js'; import * as _ from 'lodash'; -- cgit v1.2.3 From 18d6b3b86fbbf9fa85238279bd40fa6a0bd01d25 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 15:57:22 +0200 Subject: replace test fixture with the one from testrpc --- test/fixtures/orders/5_MKR_for_42_MLN.json | 35 ------------------------------ test/fixtures/orders/5_MLN_for_42_GLM.json | 35 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 35 deletions(-) delete mode 100644 test/fixtures/orders/5_MKR_for_42_MLN.json create mode 100644 test/fixtures/orders/5_MLN_for_42_GLM.json (limited to 'test') diff --git a/test/fixtures/orders/5_MKR_for_42_MLN.json b/test/fixtures/orders/5_MKR_for_42_MLN.json deleted file mode 100644 index 2c2a3d73e..000000000 --- a/test/fixtures/orders/5_MKR_for_42_MLN.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "maker": { - "address": "0xffa119a5761eb93eacfe5d2b0e8944648c3d7164", - "token": { - "name": "MakerDAO", - "symbol": "MKR", - "decimals": 18, - "address": "0x1dad4783cf3fe3085c1426157ab175a6119a04ba" - }, - "amount": "5000000000000000000", - "feeAmount": "0" - }, - "taker": { - "address": "", - "token": { - "name": "Melon Token", - "symbol": "MLN", - "decimals": 18, - "address": "0x323b5d4c32345ced77393b3530b1eed0f346429d" - }, - "amount": "42000000000000000000", - "feeAmount": "0" - }, - "expiration": "1496181600", - "feeRecipient": "0x0000000000000000000000000000000000000000", - "salt": "28894038927316056783595066163529969528517282468995826064292810658599101267743", - "signature": { - "v": 28, - "r": "0x1e84173b09bc51e4e3f923718d747bd91c3584cfa3556d79294891ddd740e819", - "s": "0x36c0f453a01487f49e16157703cacb6cee08d337549f805c25d3f78221823eed", - "hash": "0x3f64711f39393f7f60a0980e3effba087c15bb9b1cbf678ede63a69798e5dc14" - }, - "exchangeContract": "0x9ce1a5e2311f9b8b8e6b40ed20b5b090de4a4c4d", - "networkId": 42 -} \ No newline at end of file diff --git a/test/fixtures/orders/5_MLN_for_42_GLM.json b/test/fixtures/orders/5_MLN_for_42_GLM.json new file mode 100644 index 000000000..36b20a9b3 --- /dev/null +++ b/test/fixtures/orders/5_MLN_for_42_GLM.json @@ -0,0 +1,35 @@ +{ + "maker": { + "address": "0x5409ed021d9299bf6814279a6a1411a7e866a631", + "token": { + "name": "Melon Token", + "symbol": "MLN", + "decimals": 18, + "address": "0x07f96aa816c1f244cbc6ef114bb2b023ba54a2eb" + }, + "amount": "5000000000000000000", + "feeAmount": "0" + }, + "taker": { + "address": "", + "token": { + "name": "Golem Network Token", + "symbol": "GNT", + "decimals": 18, + "address": "0x1e2f9e10d02a6b8f8f69fcbf515e75039d2ea30d" + }, + "amount": "42000000000000000000", + "feeAmount": "0" + }, + "expiration": "2524604400", + "feeRecipient": "0x0000000000000000000000000000000000000000", + "salt": "80413416917564919552159131224076944404960040388740544082559161373345429797193", + "signature": { + "v": 27, + "r": "0xd2b64004d731e04864e28d49d15c42abd2801272219ee61f71e5b7e4e79a7b40", + "s": "0x22fc752de2a2489af154fcb285cb10e373faaf72f7b00aabf481872ca450fb8f", + "hash": "0xc066ae8f00083071f7e53b2737e1462ec5e11f5305b081f06e35a591e49121ee" + }, + "exchangeContract": "0xb69e673309512a9d726f87304c6984054f87a93b", + "networkId": 50 +} \ No newline at end of file -- cgit v1.2.3 From 90caa189ccdf4f17f3c60d0872bb6efd9ab646f1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 16:19:35 +0200 Subject: Add first passing test --- test/exchange_wrapper_test.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index b97a62100..e00edefbe 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -4,7 +4,7 @@ import chaiAsPromised = require('chai-as-promised'); import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -import * as OrderJSON from './fixtures/orders/5_MKR_for_42_MLN.json'; +import * as OrderJSON from './fixtures/orders/5_MLN_for_42_GLM.json'; import * as BigNumber from 'bignumber.js'; import {signedOrderFromJSON} from './utils/order'; @@ -95,10 +95,11 @@ describe('ExchangeWrapper', () => { }); }); describe('#fillOrderAsync', () => { - const fillAmount = new BigNumber(1); const signedOrder = signedOrderFromJSON(OrderJSON); - it('fillsOrder', async () => { - // const orderFillResponse = await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); + it('should throw when the fill amount is zero', async () => { + const fillAmount = new BigNumber(0); + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) + .to.be.rejectedWith('This order has already been filled or cancelled'); }); }); }); -- cgit v1.2.3 From 68f39765aaac456f89c636e2dff0fc576ac0eb1e Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 17:05:29 +0200 Subject: Remove fixture --- test/fixtures/orders/5_MLN_for_42_GLM.json | 35 ------------------------------ 1 file changed, 35 deletions(-) delete mode 100644 test/fixtures/orders/5_MLN_for_42_GLM.json (limited to 'test') diff --git a/test/fixtures/orders/5_MLN_for_42_GLM.json b/test/fixtures/orders/5_MLN_for_42_GLM.json deleted file mode 100644 index 36b20a9b3..000000000 --- a/test/fixtures/orders/5_MLN_for_42_GLM.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "maker": { - "address": "0x5409ed021d9299bf6814279a6a1411a7e866a631", - "token": { - "name": "Melon Token", - "symbol": "MLN", - "decimals": 18, - "address": "0x07f96aa816c1f244cbc6ef114bb2b023ba54a2eb" - }, - "amount": "5000000000000000000", - "feeAmount": "0" - }, - "taker": { - "address": "", - "token": { - "name": "Golem Network Token", - "symbol": "GNT", - "decimals": 18, - "address": "0x1e2f9e10d02a6b8f8f69fcbf515e75039d2ea30d" - }, - "amount": "42000000000000000000", - "feeAmount": "0" - }, - "expiration": "2524604400", - "feeRecipient": "0x0000000000000000000000000000000000000000", - "salt": "80413416917564919552159131224076944404960040388740544082559161373345429797193", - "signature": { - "v": 27, - "r": "0xd2b64004d731e04864e28d49d15c42abd2801272219ee61f71e5b7e4e79a7b40", - "s": "0x22fc752de2a2489af154fcb285cb10e373faaf72f7b00aabf481872ca450fb8f", - "hash": "0xc066ae8f00083071f7e53b2737e1462ec5e11f5305b081f06e35a591e49121ee" - }, - "exchangeContract": "0xb69e673309512a9d726f87304c6984054f87a93b", - "networkId": 50 -} \ No newline at end of file -- cgit v1.2.3 From be13cf127c00c762d03c5eaf17a11c2775701530 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 May 2017 17:35:17 +0200 Subject: Refactor getOrderHash to accept order as an object --- test/0x.js_test.ts | 47 +++++++++++++++++-------------------------- test/exchange_wrapper_test.ts | 7 +++---- test/utils/order.ts | 35 +++++++++++++++++++------------- 3 files changed, 43 insertions(+), 46 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 5d23d7094..a84785f4b 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -7,6 +7,7 @@ import * as Sinon from 'sinon'; import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; +import {Order} from '../src/types'; // Use BigNumber chai add-on chai.use(ChaiBigNumber()); @@ -43,38 +44,28 @@ describe('ZeroEx library', () => { }); describe('#getOrderHash', () => { const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7'; + const order: Order = { + maker: constants.NULL_ADDRESS, + feeRecipient: constants.NULL_ADDRESS, + makerTokenAddress: constants.NULL_ADDRESS, + takerTokenAddress: constants.NULL_ADDRESS, + salt: new BigNumber(0), + makerFee: new BigNumber(0), + takerFee: new BigNumber(0), + makerTokenAmount: new BigNumber(0), + takerTokenAmount: new BigNumber(0), + expirationUnixTimestampSec: new BigNumber(0), + }; + const exchangeAddress = constants.NULL_ADDRESS; it('defaults takerAddress to NULL address', () => { - const orderHash = ZeroEx.getOrderHashHex( - constants.NULL_ADDRESS, - constants.NULL_ADDRESS, - '', - constants.NULL_ADDRESS, - constants.NULL_ADDRESS, - constants.NULL_ADDRESS, - new BigNumber(0), - new BigNumber(0), - new BigNumber(0), - new BigNumber(0), - new BigNumber(0), - new BigNumber(0), - ); + const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); expect(orderHash).to.be.equal(expectedOrderHash); }); it('calculates the order hash', () => { - const orderHash = ZeroEx.getOrderHashHex( - constants.NULL_ADDRESS, - constants.NULL_ADDRESS, - constants.NULL_ADDRESS, - constants.NULL_ADDRESS, - constants.NULL_ADDRESS, - constants.NULL_ADDRESS, - new BigNumber(0), - new BigNumber(0), - new BigNumber(0), - new BigNumber(0), - new BigNumber(0), - new BigNumber(0), - ); + const orderWithZeroTaker = _.assign(order, { + taker: constants.NULL_ADDRESS, + }); + const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, orderWithZeroTaker); expect(orderHash).to.be.equal(expectedOrderHash); }); }); diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index e00edefbe..355a5d01b 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -4,9 +4,8 @@ import chaiAsPromised = require('chai-as-promised'); import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -import * as OrderJSON from './fixtures/orders/5_MLN_for_42_GLM.json'; import * as BigNumber from 'bignumber.js'; -import {signedOrderFromJSON} from './utils/order'; +import {createSignedOrder} from './utils/order'; const expect = chai.expect; chai.use(chaiAsPromised); @@ -94,8 +93,8 @@ describe('ExchangeWrapper', () => { expect(isValid).to.be.true; }); }); - describe('#fillOrderAsync', () => { - const signedOrder = signedOrderFromJSON(OrderJSON); + describe('#fillOrderAsync', async () => { + const signedOrder = await createSignedOrder(zeroEx); it('should throw when the fill amount is zero', async () => { const fillAmount = new BigNumber(0); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) diff --git a/test/utils/order.ts b/test/utils/order.ts index 690f50ef5..a69c1b62f 100644 --- a/test/utils/order.ts +++ b/test/utils/order.ts @@ -1,21 +1,28 @@ import {SignedOrder} from '../../src/types'; import * as BigNumber from 'bignumber.js'; import * as _ from 'lodash'; +import {ZeroEx} from '../../src/0x.js'; +import {constants} from './constants'; -export function signedOrderFromJSON(signedOrderJSON: any): SignedOrder { - const signedOrder = { - maker: signedOrderJSON.maker.address, - taker: _.isEmpty(signedOrderJSON.taker.address) ? undefined : signedOrderJSON.taker.address, - makerTokenAddress: signedOrderJSON.maker.token.address, - takerTokenAddress: signedOrderJSON.taker.token.address, - makerTokenAmount: new BigNumber(signedOrderJSON.maker.amount), - takerTokenAmount: new BigNumber(signedOrderJSON.taker.amount), - makerFee: new BigNumber(signedOrderJSON.maker.feeAmount), - takerFee: new BigNumber(signedOrderJSON.taker.feeAmount), - expirationUnixTimestampSec: new BigNumber(signedOrderJSON.expiration), - feeRecipient: signedOrderJSON.feeRecipient, - ecSignature: signedOrderJSON.signature, - salt: new BigNumber(signedOrderJSON.salt), +export async function createSignedOrder(zeroEx: ZeroEx): Promise { + // TODO: fetch properly + const EXCHANGE_ADDRESS = '0xb69e673309512a9d726f87304c6984054f87a93b'; + const INF_TIMESTAMP = 2524604400; + const order = { + maker: '0x5409ed021d9299bf6814279a6a1411a7e866a631', + taker: undefined, + makerFee: new BigNumber(0), + takerFee: new BigNumber(0), + makerTokenAmount: new BigNumber(5000000000000000000), + takerTokenAmount: new BigNumber(42000000000000000000), + makerTokenAddress: '0x07f96aa816c1f244cbc6ef114bb2b023ba54a2eb', + takerTokenAddress: '0x1e2f9e10d02a6b8f8f69fcbf515e75039d2ea30d', + salt: ZeroEx.generatePseudoRandomSalt(), + feeRecipient: constants.NULL_ADDRESS, + expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), }; + const orderHash = ZeroEx.getOrderHashHex(EXCHANGE_ADDRESS, order); + const ecSignature = await zeroEx.signOrderHashAsync(orderHash); + const signedOrder: SignedOrder = _.assign(order, {ecSignature}); return signedOrder; } -- cgit v1.2.3 From 7f269cd791e4f0ed1515e90478de6b35683ce2bf Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 31 May 2017 16:03:06 +0200 Subject: Add createSignedOrder function --- test/utils/order.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/utils/order.ts b/test/utils/order.ts index a69c1b62f..2de87bac1 100644 --- a/test/utils/order.ts +++ b/test/utils/order.ts @@ -1,28 +1,45 @@ -import {SignedOrder} from '../../src/types'; +import {SignedOrder, Token} from '../../src/types'; import * as BigNumber from 'bignumber.js'; import * as _ from 'lodash'; import {ZeroEx} from '../../src/0x.js'; import {constants} from './constants'; -export async function createSignedOrder(zeroEx: ZeroEx): Promise { +export async function createSignedOrder( + zeroEx: ZeroEx, + tokens: Token[], + makerTokenAmount: BigNumber.BigNumber|number, + makerTokenSymbol: string, + takerTokenAmount: BigNumber.BigNumber|number, + takerTokenSymbol: string): Promise { // TODO: fetch properly const EXCHANGE_ADDRESS = '0xb69e673309512a9d726f87304c6984054f87a93b'; const INF_TIMESTAMP = 2524604400; + const makerToken = _.find(tokens, {symbol: makerTokenSymbol}); + const takerToken = _.find(tokens, {symbol: takerTokenSymbol}); + if (_.isUndefined(makerToken)) { + throw new Error(`Token ${makerTokenSymbol} not found`); + } + if (_.isUndefined(takerToken)) { + throw new Error(`Token ${takerTokenSymbol} not found`); + } const order = { maker: '0x5409ed021d9299bf6814279a6a1411a7e866a631', taker: undefined, makerFee: new BigNumber(0), takerFee: new BigNumber(0), - makerTokenAmount: new BigNumber(5000000000000000000), - takerTokenAmount: new BigNumber(42000000000000000000), - makerTokenAddress: '0x07f96aa816c1f244cbc6ef114bb2b023ba54a2eb', - takerTokenAddress: '0x1e2f9e10d02a6b8f8f69fcbf515e75039d2ea30d', + makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount, + takerTokenAmount: _.isNumber(takerTokenAmount) ? new BigNumber(takerTokenAmount) : takerTokenAmount, + makerTokenAddress: makerToken.address, + takerTokenAddress: takerToken.address, salt: ZeroEx.generatePseudoRandomSalt(), feeRecipient: constants.NULL_ADDRESS, expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), }; + console.log(order.makerTokenAmount.toString()); + console.log(order.takerTokenAmount.toString()); const orderHash = ZeroEx.getOrderHashHex(EXCHANGE_ADDRESS, order); const ecSignature = await zeroEx.signOrderHashAsync(orderHash); const signedOrder: SignedOrder = _.assign(order, {ecSignature}); + console.log(signedOrder); return signedOrder; } -- cgit v1.2.3 From 84c652d205af0fd289149762661536895eaaafef Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 31 May 2017 16:03:16 +0200 Subject: Revert tests --- test/exchange_wrapper_test.ts | 50 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ede4198d8..ad82e6e82 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -2,18 +2,25 @@ import 'mocha'; import * as chai from 'chai'; import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; +import promisify = require('es6-promisify'); +import * as _ from 'lodash'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import * as BigNumber from 'bignumber.js'; import {createSignedOrder} from './utils/order'; +import {Token} from '../src/types'; +import * as Web3 from 'web3'; const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(); describe('ExchangeWrapper', () => { let zeroEx: ZeroEx; + let userAddresses: string[]; + let web3: Web3; before(async () => { - const web3 = web3Factory.create(); + web3 = web3Factory.create(); zeroEx = new ZeroEx(web3); + userAddresses = await promisify(web3.eth.getAccounts)(); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -91,12 +98,41 @@ describe('ExchangeWrapper', () => { expect(isValid).to.be.true; }); }); - describe('#fillOrderAsync', async () => { - const signedOrder = await createSignedOrder(zeroEx); - it('should throw when the fill amount is zero', async () => { - const fillAmount = new BigNumber(0); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) - .to.be.rejectedWith('This order has already been filled or cancelled'); + describe('#fillOrderAsync', () => { + let tokens: Token[]; + const giveMeTokens = async (toAddress: string, + amountInBaseUnits: BigNumber.BigNumber|number, + symbol: string) => { + const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; + const token = _.find(tokens, {symbol}); + if (_.isUndefined(token)) { + throw new Error(`Token ${symbol} not found`); + } else { + await zeroEx.token.transferAsync(token.address, userAddresses[0], toAddress, amount); + } + }; + before('fetch tokens', async () => { + tokens = await zeroEx.tokenRegistry.getTokensAsync(); + }); + describe('failed fills', () => { + it('should throw when the fill amount is zero', async () => { + const signedOrder = await createSignedOrder(zeroEx, tokens, 5, 'MLN', 5, 'GNT'); + const fillAmount = new BigNumber(0); + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) + .to.be.rejectedWith('This order has already been filled or cancelled'); + }); + }); + describe('successful fills', () => { + afterEach('reset default account', () => { + web3.eth.defaultAccount = userAddresses[0]; + }); + it('should fill the valid order', async () => { + const signedOrder = await createSignedOrder(zeroEx, tokens, 5, 'MLN', 5, 'GNT'); + await giveMeTokens(userAddresses[0], 5, 'GNT'); + const fillAmount = new BigNumber(0); + web3.eth.defaultAccount = userAddresses[1]; + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)).to.become(undefined); + }); }); }); }); -- cgit v1.2.3 From 7d29fb217445497d81ef62d0feb821c8e16ca88a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 31 May 2017 16:06:14 +0200 Subject: Remove debug output --- test/utils/order.ts | 3 --- 1 file changed, 3 deletions(-) (limited to 'test') diff --git a/test/utils/order.ts b/test/utils/order.ts index 2de87bac1..d775dc616 100644 --- a/test/utils/order.ts +++ b/test/utils/order.ts @@ -35,11 +35,8 @@ export async function createSignedOrder( feeRecipient: constants.NULL_ADDRESS, expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), }; - console.log(order.makerTokenAmount.toString()); - console.log(order.takerTokenAmount.toString()); const orderHash = ZeroEx.getOrderHashHex(EXCHANGE_ADDRESS, order); const ecSignature = await zeroEx.signOrderHashAsync(orderHash); const signedOrder: SignedOrder = _.assign(order, {ecSignature}); - console.log(signedOrder); return signedOrder; } -- cgit v1.2.3 From 1955e90bbbfeb7d2aeebfd84132f684d20e17400 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 31 May 2017 16:15:16 +0200 Subject: Fix linter errors --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ad82e6e82..24383906f 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -89,7 +89,7 @@ describe('ExchangeWrapper', () => { expect(isValid).to.be.false; }); it('should return false if the signature doesn\'t pertain to the dataHex & address', async () => { - const wrongSignature = Object.assign({}, signature, {v: 28}); + const wrongSignature = {...signature, v: 28}; const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, wrongSignature, address); expect(isValid).to.be.false; }); -- cgit v1.2.3 From 82a46b4938902009896bef1d296b887f66b284fd Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 31 May 2017 17:45:15 +0200 Subject: Add setAllowance function --- test/exchange_wrapper_test.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 24383906f..5138e140f 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -21,6 +21,7 @@ describe('ExchangeWrapper', () => { web3 = web3Factory.create(); zeroEx = new ZeroEx(web3); userAddresses = await promisify(web3.eth.getAccounts)(); + web3.eth.defaultAccount = userAddresses[0]; }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -100,7 +101,7 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; - const giveMeTokens = async (toAddress: string, + const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, symbol: string) => { const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; @@ -111,6 +112,17 @@ describe('ExchangeWrapper', () => { await zeroEx.token.transferAsync(token.address, userAddresses[0], toAddress, amount); } }; + const setAllowance = async (ownerAddress: string, + amountInBaseUnits: BigNumber.BigNumber|number, + symbol: string) => { + const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; + const token = _.find(tokens, {symbol}); + if (_.isUndefined(token)) { + throw new Error(`Token ${symbol} not found`); + } else { + await zeroEx.token.setProxyAllowanceAsync(token.address, ownerAddress, amount); + } + }; before('fetch tokens', async () => { tokens = await zeroEx.tokenRegistry.getTokensAsync(); }); @@ -127,11 +139,13 @@ describe('ExchangeWrapper', () => { web3.eth.defaultAccount = userAddresses[0]; }); it('should fill the valid order', async () => { + await setAllowance(userAddresses[0], 5, 'MLN'); + await setBalance(userAddresses[1], 5, 'GNT'); + await setAllowance(userAddresses[1], 5, 'GNT'); const signedOrder = await createSignedOrder(zeroEx, tokens, 5, 'MLN', 5, 'GNT'); - await giveMeTokens(userAddresses[0], 5, 'GNT'); - const fillAmount = new BigNumber(0); + const fillAmount = new BigNumber(5); web3.eth.defaultAccount = userAddresses[1]; - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)).to.become(undefined); + await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); }); }); }); -- cgit v1.2.3 From 28dee77db84549db24b95d9aaaca949a2fdec29c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 31 May 2017 21:15:15 +0200 Subject: Temp push --- test/exchange_wrapper_test.ts | 49 +++++++++++++++++++-------------- test/utils/order.ts | 64 ++++++++++++++++++++----------------------- 2 files changed, 58 insertions(+), 55 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 5138e140f..a5a327dc4 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -6,7 +6,7 @@ import promisify = require('es6-promisify'); import * as _ from 'lodash'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import * as BigNumber from 'bignumber.js'; -import {createSignedOrder} from './utils/order'; +import {orderFactory} from './utils/order'; import {Token} from '../src/types'; import * as Web3 from 'web3'; @@ -101,34 +101,31 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; + let addressBySymbol: {[symbol: string]: string} = {}; + let networkId: number; const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, - symbol: string) => { + tokenAddress: string) => { const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; - const token = _.find(tokens, {symbol}); - if (_.isUndefined(token)) { - throw new Error(`Token ${symbol} not found`); - } else { - await zeroEx.token.transferAsync(token.address, userAddresses[0], toAddress, amount); - } + await zeroEx.token.transferAsync(tokenAddress, userAddresses[0], toAddress, amount); }; const setAllowance = async (ownerAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, - symbol: string) => { + tokenAddress: string) => { const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; - const token = _.find(tokens, {symbol}); - if (_.isUndefined(token)) { - throw new Error(`Token ${symbol} not found`); - } else { - await zeroEx.token.setProxyAllowanceAsync(token.address, ownerAddress, amount); - } + await zeroEx.token.setProxyAllowanceAsync(tokenAddress, ownerAddress, amount); }; before('fetch tokens', async () => { tokens = await zeroEx.tokenRegistry.getTokensAsync(); + _.forEach(tokens, token => { + addressBySymbol[token.symbol] = token.address; + }); + networkId = await promisify(web3.version.getNetwork)(); }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { - const signedOrder = await createSignedOrder(zeroEx, tokens, 5, 'MLN', 5, 'GNT'); + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, userAddresses[0], + 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(0); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) .to.be.rejectedWith('This order has already been filled or cancelled'); @@ -139,13 +136,23 @@ describe('ExchangeWrapper', () => { web3.eth.defaultAccount = userAddresses[0]; }); it('should fill the valid order', async () => { - await setAllowance(userAddresses[0], 5, 'MLN'); - await setBalance(userAddresses[1], 5, 'GNT'); - await setAllowance(userAddresses[1], 5, 'GNT'); - const signedOrder = await createSignedOrder(zeroEx, tokens, 5, 'MLN', 5, 'GNT'); + const maker = userAddresses[0]; + const taker = userAddresses[1]; + await setAllowance(maker, 5, addressBySymbol.MLN); + await setBalance(taker, 5, addressBySymbol.GNT); + await setAllowance(taker, 5, addressBySymbol.GNT); + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, + 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); - web3.eth.defaultAccount = userAddresses[1]; + web3.eth.defaultAccount = taker; + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, maker)).to.be.bignumber.greaterThan(5); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(0); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(5); + expect(await zeroEx.token.getProxyAllowanceAsync(addressBySymbol.MLN, maker)).to.be.bignumber.equal(5); + expect(await zeroEx.token.getProxyAllowanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(5); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(0); }); }); }); diff --git a/test/utils/order.ts b/test/utils/order.ts index d775dc616..8a7d8430e 100644 --- a/test/utils/order.ts +++ b/test/utils/order.ts @@ -3,40 +3,36 @@ import * as BigNumber from 'bignumber.js'; import * as _ from 'lodash'; import {ZeroEx} from '../../src/0x.js'; import {constants} from './constants'; +import * as ExchangeArtifacts from '../../src/artifacts/Exchange.json'; -export async function createSignedOrder( - zeroEx: ZeroEx, - tokens: Token[], - makerTokenAmount: BigNumber.BigNumber|number, - makerTokenSymbol: string, - takerTokenAmount: BigNumber.BigNumber|number, - takerTokenSymbol: string): Promise { - // TODO: fetch properly - const EXCHANGE_ADDRESS = '0xb69e673309512a9d726f87304c6984054f87a93b'; - const INF_TIMESTAMP = 2524604400; - const makerToken = _.find(tokens, {symbol: makerTokenSymbol}); - const takerToken = _.find(tokens, {symbol: takerTokenSymbol}); - if (_.isUndefined(makerToken)) { - throw new Error(`Token ${makerTokenSymbol} not found`); +export const orderFactory = { + async createSignedOrderAsync( + zeroEx: ZeroEx, + networkId: number, + maker: string, + makerTokenAmount: BigNumber.BigNumber|number, + makerTokenAddress: string, + takerTokenAmount: BigNumber.BigNumber|number, + takerTokenAddress: string): Promise { + // TODO refactor and check + const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address; + const INF_TIMESTAMP = 2524604400; + const order = { + maker, + taker: undefined, + makerFee: new BigNumber(0), + takerFee: new BigNumber(0), + makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount, + takerTokenAmount: _.isNumber(takerTokenAmount) ? new BigNumber(takerTokenAmount) : takerTokenAmount, + makerTokenAddress, + takerTokenAddress, + salt: ZeroEx.generatePseudoRandomSalt(), + feeRecipient: constants.NULL_ADDRESS, + expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), + }; + const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); + const ecSignature = await zeroEx.signOrderHashAsync(orderHash); + const signedOrder: SignedOrder = _.assign(order, {ecSignature}); + return signedOrder; } - if (_.isUndefined(takerToken)) { - throw new Error(`Token ${takerTokenSymbol} not found`); - } - const order = { - maker: '0x5409ed021d9299bf6814279a6a1411a7e866a631', - taker: undefined, - makerFee: new BigNumber(0), - takerFee: new BigNumber(0), - makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount, - takerTokenAmount: _.isNumber(takerTokenAmount) ? new BigNumber(takerTokenAmount) : takerTokenAmount, - makerTokenAddress: makerToken.address, - takerTokenAddress: takerToken.address, - salt: ZeroEx.generatePseudoRandomSalt(), - feeRecipient: constants.NULL_ADDRESS, - expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), - }; - const orderHash = ZeroEx.getOrderHashHex(EXCHANGE_ADDRESS, order); - const ecSignature = await zeroEx.signOrderHashAsync(orderHash); - const signedOrder: SignedOrder = _.assign(order, {ecSignature}); - return signedOrder; } -- cgit v1.2.3 From f450f538a0af0b4c85eefdd81c4983f40d230a33 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 11:07:36 +0200 Subject: Add setDefaultAccount --- test/exchange_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index a5a327dc4..ea541ca8e 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -133,7 +133,7 @@ describe('ExchangeWrapper', () => { }); describe('successful fills', () => { afterEach('reset default account', () => { - web3.eth.defaultAccount = userAddresses[0]; + zeroEx.setDefaultAccount(userAddresses[0]); }); it('should fill the valid order', async () => { const maker = userAddresses[0]; @@ -144,12 +144,12 @@ describe('ExchangeWrapper', () => { const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); - web3.eth.defaultAccount = taker; expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, maker)).to.be.bignumber.greaterThan(5); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(0); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(5); expect(await zeroEx.token.getProxyAllowanceAsync(addressBySymbol.MLN, maker)).to.be.bignumber.equal(5); expect(await zeroEx.token.getProxyAllowanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(5); + zeroEx.setDefaultAccount(taker); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(0); -- cgit v1.2.3 From cc7592e9315f9cd9eba0a4049c4382545e8154e1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 11:07:58 +0200 Subject: Use const --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ea541ca8e..36ad34fab 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -101,7 +101,7 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; - let addressBySymbol: {[symbol: string]: string} = {}; + const addressBySymbol: {[symbol: string]: string} = {}; let networkId: number; const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, -- cgit v1.2.3 From 0a68a69d0ca75b45c4ebf46ff35f513f1c4344ec Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 11:15:52 +0200 Subject: Allign parameters --- test/exchange_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 36ad34fab..0c8182c73 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -104,8 +104,8 @@ describe('ExchangeWrapper', () => { const addressBySymbol: {[symbol: string]: string} = {}; let networkId: number; const setBalance = async (toAddress: string, - amountInBaseUnits: BigNumber.BigNumber|number, - tokenAddress: string) => { + amountInBaseUnits: BigNumber.BigNumber|number, + tokenAddress: string) => { const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; await zeroEx.token.transferAsync(tokenAddress, userAddresses[0], toAddress, amount); }; -- cgit v1.2.3 From de3a2ff6726351b9d8400a6757a9ce2a18c3c3f6 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 11:40:08 +0200 Subject: Add dirty-chai --- test/exchange_wrapper_test.ts | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 0c8182c73..c0d5b292b 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -9,7 +9,11 @@ import * as BigNumber from 'bignumber.js'; import {orderFactory} from './utils/order'; import {Token} from '../src/types'; import * as Web3 from 'web3'; +import * as dirtyChai from 'dirty-chai'; +import ChaiBigNumber = require('chai-bignumber'); +chai.use(dirtyChai); +chai.use(ChaiBigNumber()); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(); -- cgit v1.2.3 From dbb30aa1dd5db685cf9fea0e23029a77a66290da Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 11:46:57 +0200 Subject: Fix linter errors --- test/0x.js_test.ts | 30 ++++++++++++++++-------------- test/exchange_wrapper_test.ts | 16 ++++++++-------- 2 files changed, 24 insertions(+), 22 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index a84785f4b..32040bd33 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -3,6 +3,7 @@ import * as chai from 'chai'; import 'mocha'; import * as BigNumber from 'bignumber.js'; import ChaiBigNumber = require('chai-bignumber'); +import * as dirtyChai from 'dirty-chai'; import * as Sinon from 'sinon'; import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; @@ -11,6 +12,7 @@ import {Order} from '../src/types'; // Use BigNumber chai add-on chai.use(ChaiBigNumber()); +chai.use(dirtyChai); const expect = chai.expect; describe('ZeroEx library', () => { @@ -21,8 +23,8 @@ describe('ZeroEx library', () => { // Instantiate the contract instances with the current provider await (zeroEx.exchange as any).getExchangeContractAsync(); await (zeroEx.tokenRegistry as any).getTokenRegistryContractAsync(); - expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.undefined; - expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.undefined; + expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.undefined(); + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.undefined(); const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider @@ -30,8 +32,8 @@ describe('ZeroEx library', () => { zeroEx.setProvider(newProvider); // Check that contractInstances with old provider are removed after provider update - expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.undefined; - expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.undefined; + expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.undefined(); + expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.undefined(); // Check that all nested web3 instances return the updated provider const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); @@ -118,47 +120,47 @@ describe('ZeroEx library', () => { }); it('should return false if the data doesn\'t pertain to the signature & address', () => { const isValid = ZeroEx.isValidSignature('0x0', signature, address); - expect(isValid).to.be.false; + expect(isValid).to.be.false(); }); it('should return false if the address doesn\'t pertain to the signature & data', () => { const validUnrelatedAddress = '0x8b0292B11a196601eD2ce54B665CaFEca0347D42'; const isValid = ZeroEx.isValidSignature(data, signature, validUnrelatedAddress); - expect(isValid).to.be.false; + expect(isValid).to.be.false(); }); it('should return false if the signature doesn\'t pertain to the data & address', () => { const wrongSignature = _.assign({}, signature, {v: 28}); const isValid = ZeroEx.isValidSignature(data, wrongSignature, address); - expect(isValid).to.be.false; + expect(isValid).to.be.false(); }); it('should return true if the signature does pertain to the data & address', () => { const isValid = ZeroEx.isValidSignature(data, signature, address); - expect(isValid).to.be.true; + expect(isValid).to.be.true(); }); }); describe('#generateSalt', () => { it('generates different salts', () => { const equal = ZeroEx.generatePseudoRandomSalt().eq(ZeroEx.generatePseudoRandomSalt()); - expect(equal).to.be.false; + expect(equal).to.be.false(); }); it('generates salt in range [0..2^256)', () => { const salt = ZeroEx.generatePseudoRandomSalt(); - expect(salt.greaterThanOrEqualTo(0)).to.be.true; + expect(salt.greaterThanOrEqualTo(0)).to.be.true(); const twoPow256 = new BigNumber(2).pow(256); - expect(salt.lessThan(twoPow256)).to.be.true; + expect(salt.lessThan(twoPow256)).to.be.true(); }); }); describe('#isValidOrderHash', () => { it('returns false if the value is not a hex string', () => { const isValid = ZeroEx.isValidOrderHash('not a hex'); - expect(isValid).to.be.false; + expect(isValid).to.be.false(); }); it('returns false if the length is wrong', () => { const isValid = ZeroEx.isValidOrderHash('0xdeadbeef'); - expect(isValid).to.be.false; + expect(isValid).to.be.false(); }); it('returns true if order hash is correct', () => { const isValid = ZeroEx.isValidOrderHash('0x' + Array(65).join('0')); - expect(isValid).to.be.true; + expect(isValid).to.be.true(); }); }); describe('#toUnitAmount', () => { diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index c0d5b292b..ca040ab28 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -51,7 +51,7 @@ describe('ExchangeWrapper', () => { s: signature.s, }; expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected; + .to.be.rejected(); }); it('r lacks 0x prefix', () => { const malformedR = signature.r.replace('0x', ''); @@ -61,7 +61,7 @@ describe('ExchangeWrapper', () => { s: signature.s, }; expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected; + .to.be.rejected(); }); it('r is too short', () => { const malformedR = signature.r.substr(10); @@ -71,7 +71,7 @@ describe('ExchangeWrapper', () => { s: signature.s.replace('0', 'z'), }; expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected; + .to.be.rejected(); }); it('s is not hex', () => { const malformedS = signature.s.replace('0', 'z'); @@ -81,26 +81,26 @@ describe('ExchangeWrapper', () => { s: malformedS, }; expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) - .to.be.rejected; + .to.be.rejected(); }); }); it('should return false if the data doesn\'t pertain to the signature & address', async () => { const isValid = await zeroEx.exchange.isValidSignatureAsync('0x0', signature, address); - expect(isValid).to.be.false; + expect(isValid).to.be.false(); }); it('should return false if the address doesn\'t pertain to the signature & dataHex', async () => { const validUnrelatedAddress = '0x8b0292B11a196601eD2ce54B665CaFEca0347D42'; const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, signature, validUnrelatedAddress); - expect(isValid).to.be.false; + expect(isValid).to.be.false(); }); it('should return false if the signature doesn\'t pertain to the dataHex & address', async () => { const wrongSignature = {...signature, v: 28}; const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, wrongSignature, address); - expect(isValid).to.be.false; + expect(isValid).to.be.false(); }); it('should return true if the signature does pertain to the dataHex & address', async () => { const isValid = await zeroEx.exchange.isValidSignatureAsync(dataHex, signature, address); - expect(isValid).to.be.true; + expect(isValid).to.be.true(); }); }); describe('#fillOrderAsync', () => { -- cgit v1.2.3 From 448be396cba7265d6ea0d69c9388e32db25773e8 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 14:52:25 +0200 Subject: Remove unnecessary checks --- test/exchange_wrapper_test.ts | 5 ----- 1 file changed, 5 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ca040ab28..548ef0c36 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -148,11 +148,6 @@ describe('ExchangeWrapper', () => { const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, maker)).to.be.bignumber.greaterThan(5); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(0); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(5); - expect(await zeroEx.token.getProxyAllowanceAsync(addressBySymbol.MLN, maker)).to.be.bignumber.equal(5); - expect(await zeroEx.token.getProxyAllowanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(5); zeroEx.setDefaultAccount(taker); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); -- cgit v1.2.3 From ef7ac6ac2df34cc45bce461bbbf0e83be5187aed Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 14:53:26 +0200 Subject: Rename order to order_factory --- test/exchange_wrapper_test.ts | 2 +- test/utils/order.ts | 38 -------------------------------------- test/utils/order_factory.ts | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 39 deletions(-) delete mode 100644 test/utils/order.ts create mode 100644 test/utils/order_factory.ts (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 548ef0c36..352eb9e05 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -6,7 +6,7 @@ import promisify = require('es6-promisify'); import * as _ from 'lodash'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import * as BigNumber from 'bignumber.js'; -import {orderFactory} from './utils/order'; +import {orderFactory} from './utils/order_factory'; import {Token} from '../src/types'; import * as Web3 from 'web3'; import * as dirtyChai from 'dirty-chai'; diff --git a/test/utils/order.ts b/test/utils/order.ts deleted file mode 100644 index 8a7d8430e..000000000 --- a/test/utils/order.ts +++ /dev/null @@ -1,38 +0,0 @@ -import {SignedOrder, Token} from '../../src/types'; -import * as BigNumber from 'bignumber.js'; -import * as _ from 'lodash'; -import {ZeroEx} from '../../src/0x.js'; -import {constants} from './constants'; -import * as ExchangeArtifacts from '../../src/artifacts/Exchange.json'; - -export const orderFactory = { - async createSignedOrderAsync( - zeroEx: ZeroEx, - networkId: number, - maker: string, - makerTokenAmount: BigNumber.BigNumber|number, - makerTokenAddress: string, - takerTokenAmount: BigNumber.BigNumber|number, - takerTokenAddress: string): Promise { - // TODO refactor and check - const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address; - const INF_TIMESTAMP = 2524604400; - const order = { - maker, - taker: undefined, - makerFee: new BigNumber(0), - takerFee: new BigNumber(0), - makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount, - takerTokenAmount: _.isNumber(takerTokenAmount) ? new BigNumber(takerTokenAmount) : takerTokenAmount, - makerTokenAddress, - takerTokenAddress, - salt: ZeroEx.generatePseudoRandomSalt(), - feeRecipient: constants.NULL_ADDRESS, - expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), - }; - const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); - const ecSignature = await zeroEx.signOrderHashAsync(orderHash); - const signedOrder: SignedOrder = _.assign(order, {ecSignature}); - return signedOrder; - } -} diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts new file mode 100644 index 000000000..8a7d8430e --- /dev/null +++ b/test/utils/order_factory.ts @@ -0,0 +1,38 @@ +import {SignedOrder, Token} from '../../src/types'; +import * as BigNumber from 'bignumber.js'; +import * as _ from 'lodash'; +import {ZeroEx} from '../../src/0x.js'; +import {constants} from './constants'; +import * as ExchangeArtifacts from '../../src/artifacts/Exchange.json'; + +export const orderFactory = { + async createSignedOrderAsync( + zeroEx: ZeroEx, + networkId: number, + maker: string, + makerTokenAmount: BigNumber.BigNumber|number, + makerTokenAddress: string, + takerTokenAmount: BigNumber.BigNumber|number, + takerTokenAddress: string): Promise { + // TODO refactor and check + const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address; + const INF_TIMESTAMP = 2524604400; + const order = { + maker, + taker: undefined, + makerFee: new BigNumber(0), + takerFee: new BigNumber(0), + makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount, + takerTokenAmount: _.isNumber(takerTokenAmount) ? new BigNumber(takerTokenAmount) : takerTokenAmount, + makerTokenAddress, + takerTokenAddress, + salt: ZeroEx.generatePseudoRandomSalt(), + feeRecipient: constants.NULL_ADDRESS, + expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), + }; + const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); + const ecSignature = await zeroEx.signOrderHashAsync(orderHash); + const signedOrder: SignedOrder = _.assign(order, {ecSignature}); + return signedOrder; + } +} -- cgit v1.2.3 From 75d0b69db294a58b8fdca330d4724ecf758d65f2 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 14:57:34 +0200 Subject: Add taker parameter to createSignedOrder --- test/exchange_wrapper_test.ts | 6 ++++-- test/utils/order_factory.ts | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 352eb9e05..b2fb1894b 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -128,7 +128,9 @@ describe('ExchangeWrapper', () => { }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, userAddresses[0], + const maker = userAddresses[0]; + const taker = userAddresses[1]; + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(0); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) @@ -145,7 +147,7 @@ describe('ExchangeWrapper', () => { await setAllowance(maker, 5, addressBySymbol.MLN); await setBalance(taker, 5, addressBySymbol.GNT); await setAllowance(taker, 5, addressBySymbol.GNT); - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); zeroEx.setDefaultAccount(taker); diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index 8a7d8430e..e41e973ee 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -10,6 +10,7 @@ export const orderFactory = { zeroEx: ZeroEx, networkId: number, maker: string, + taker: string, makerTokenAmount: BigNumber.BigNumber|number, makerTokenAddress: string, takerTokenAmount: BigNumber.BigNumber|number, @@ -19,7 +20,7 @@ export const orderFactory = { const INF_TIMESTAMP = 2524604400; const order = { maker, - taker: undefined, + taker, makerFee: new BigNumber(0), takerFee: new BigNumber(0), makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount, -- cgit v1.2.3 From 07cdfa655be81862f7ff40fe5ac6fdb38d780370 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 15:12:27 +0200 Subject: Add FILL_AMOUNT_IS_ZERO check --- test/exchange_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index b2fb1894b..4867427ae 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -7,7 +7,7 @@ import * as _ from 'lodash'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import * as BigNumber from 'bignumber.js'; import {orderFactory} from './utils/order_factory'; -import {Token} from '../src/types'; +import {FillOrderValidationErrs, Token} from '../src/types'; import * as Web3 from 'web3'; import * as dirtyChai from 'dirty-chai'; import ChaiBigNumber = require('chai-bignumber'); @@ -134,7 +134,7 @@ describe('ExchangeWrapper', () => { 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(0); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) - .to.be.rejectedWith('This order has already been filled or cancelled'); + .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); }); describe('successful fills', () => { -- cgit v1.2.3 From d5d20db439a4a6fe913462bd216a776cc4300450 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 15:18:13 +0200 Subject: Add NOT_A_TAKER check --- test/exchange_wrapper_test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 4867427ae..c5cbc58be 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -129,13 +129,22 @@ describe('ExchangeWrapper', () => { describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { const maker = userAddresses[0]; - const taker = userAddresses[1]; + const taker = userAddresses[0]; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(0); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); + it('should throw when sender is not a taker', async () => { + const maker = userAddresses[0]; + const taker = userAddresses[1]; + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, + 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); + const fillAmount = new BigNumber(5); + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) + .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); + }); }); describe('successful fills', () => { afterEach('reset default account', () => { -- cgit v1.2.3 From 21d00f04d46a8067801ff539fb21cbbed416c8b3 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 15:21:24 +0200 Subject: Fix tests --- test/0x.js_test.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 32040bd33..a548d8268 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -48,6 +48,7 @@ describe('ZeroEx library', () => { const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7'; const order: Order = { maker: constants.NULL_ADDRESS, + taker: constants.NULL_ADDRESS, feeRecipient: constants.NULL_ADDRESS, makerTokenAddress: constants.NULL_ADDRESS, takerTokenAddress: constants.NULL_ADDRESS, @@ -59,15 +60,8 @@ describe('ZeroEx library', () => { expirationUnixTimestampSec: new BigNumber(0), }; const exchangeAddress = constants.NULL_ADDRESS; - it('defaults takerAddress to NULL address', () => { - const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); - expect(orderHash).to.be.equal(expectedOrderHash); - }); it('calculates the order hash', () => { - const orderWithZeroTaker = _.assign(order, { - taker: constants.NULL_ADDRESS, - }); - const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, orderWithZeroTaker); + const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); expect(orderHash).to.be.equal(expectedOrderHash); }); }); -- cgit v1.2.3 From d8e35c364ea94b606810b340fb02d8706e257c3c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 15:46:27 +0200 Subject: Add EXPIRED test --- test/exchange_wrapper_test.ts | 38 ++++++++++++++++++++++---------------- test/utils/order_factory.ts | 10 +++++++--- 2 files changed, 29 insertions(+), 19 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index c5cbc58be..f929df149 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -107,6 +107,9 @@ describe('ExchangeWrapper', () => { let tokens: Token[]; const addressBySymbol: {[symbol: string]: string} = {}; let networkId: number; + let maker: string; + let taker: string; + const fillAmount = new BigNumber(5); const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, tokenAddress: string) => { @@ -126,40 +129,43 @@ describe('ExchangeWrapper', () => { }); networkId = await promisify(web3.version.getNetwork)(); }); + beforeEach('get ready for fill', async () => { + [maker, taker] = userAddresses; + zeroEx.setDefaultAccount(taker); + await setAllowance(maker, 5, addressBySymbol.MLN); + await setBalance(taker, 5, addressBySymbol.GNT); + await setAllowance(taker, 5, addressBySymbol.GNT); + }); + afterEach('reset sender', () => { + zeroEx.setDefaultAccount(userAddresses[0]); + }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { - const maker = userAddresses[0]; - const taker = userAddresses[0]; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); - const fillAmount = new BigNumber(0); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { - const maker = userAddresses[0]; - const taker = userAddresses[1]; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); - const fillAmount = new BigNumber(5); + const notTaker = userAddresses[2]; + zeroEx.setDefaultAccount(notTaker); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); + it('should throw when order is expired', async () => { + const OLD_TIMESTAMP = new BigNumber(42); + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, + 5, addressBySymbol.MLN, 5, addressBySymbol.GNT, OLD_TIMESTAMP); + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) + .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); + }); }); describe('successful fills', () => { - afterEach('reset default account', () => { - zeroEx.setDefaultAccount(userAddresses[0]); - }); it('should fill the valid order', async () => { - const maker = userAddresses[0]; - const taker = userAddresses[1]; - await setAllowance(maker, 5, addressBySymbol.MLN); - await setBalance(taker, 5, addressBySymbol.GNT); - await setAllowance(taker, 5, addressBySymbol.GNT); const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); - const fillAmount = new BigNumber(5); - zeroEx.setDefaultAccount(taker); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(0); diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index e41e973ee..c6c6ed927 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -14,10 +14,14 @@ export const orderFactory = { makerTokenAmount: BigNumber.BigNumber|number, makerTokenAddress: string, takerTokenAmount: BigNumber.BigNumber|number, - takerTokenAddress: string): Promise { + takerTokenAddress: string, + expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { // TODO refactor and check const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address; - const INF_TIMESTAMP = 2524604400; + const INF_TIMESTAMP = new BigNumber(2524604400); + expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSec) ? + INF_TIMESTAMP : + expirationUnixTimestampSec; const order = { maker, taker, @@ -29,7 +33,7 @@ export const orderFactory = { takerTokenAddress, salt: ZeroEx.generatePseudoRandomSalt(), feeRecipient: constants.NULL_ADDRESS, - expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), + expirationUnixTimestampSec, }; const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); const ecSignature = await zeroEx.signOrderHashAsync(orderHash); -- cgit v1.2.3 From 771c88ec791cb6bedf2ddd3418e89dec1581e326 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 16:11:25 +0200 Subject: Revert "Add EXPIRED test" This reverts commit d8e35c364ea94b606810b340fb02d8706e257c3c. --- test/exchange_wrapper_test.ts | 38 ++++++++++++++++---------------------- test/utils/order_factory.ts | 10 +++------- 2 files changed, 19 insertions(+), 29 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index f929df149..c5cbc58be 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -107,9 +107,6 @@ describe('ExchangeWrapper', () => { let tokens: Token[]; const addressBySymbol: {[symbol: string]: string} = {}; let networkId: number; - let maker: string; - let taker: string; - const fillAmount = new BigNumber(5); const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, tokenAddress: string) => { @@ -129,43 +126,40 @@ describe('ExchangeWrapper', () => { }); networkId = await promisify(web3.version.getNetwork)(); }); - beforeEach('get ready for fill', async () => { - [maker, taker] = userAddresses; - zeroEx.setDefaultAccount(taker); - await setAllowance(maker, 5, addressBySymbol.MLN); - await setBalance(taker, 5, addressBySymbol.GNT); - await setAllowance(taker, 5, addressBySymbol.GNT); - }); - afterEach('reset sender', () => { - zeroEx.setDefaultAccount(userAddresses[0]); - }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { + const maker = userAddresses[0]; + const taker = userAddresses[0]; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); + const fillAmount = new BigNumber(0); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { + const maker = userAddresses[0]; + const taker = userAddresses[1]; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); - const notTaker = userAddresses[2]; - zeroEx.setDefaultAccount(notTaker); + const fillAmount = new BigNumber(5); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); - it('should throw when order is expired', async () => { - const OLD_TIMESTAMP = new BigNumber(42); - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, - 5, addressBySymbol.MLN, 5, addressBySymbol.GNT, OLD_TIMESTAMP); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) - .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); - }); }); describe('successful fills', () => { + afterEach('reset default account', () => { + zeroEx.setDefaultAccount(userAddresses[0]); + }); it('should fill the valid order', async () => { + const maker = userAddresses[0]; + const taker = userAddresses[1]; + await setAllowance(maker, 5, addressBySymbol.MLN); + await setBalance(taker, 5, addressBySymbol.GNT); + await setAllowance(taker, 5, addressBySymbol.GNT); const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); + const fillAmount = new BigNumber(5); + zeroEx.setDefaultAccount(taker); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(0); diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index c6c6ed927..e41e973ee 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -14,14 +14,10 @@ export const orderFactory = { makerTokenAmount: BigNumber.BigNumber|number, makerTokenAddress: string, takerTokenAmount: BigNumber.BigNumber|number, - takerTokenAddress: string, - expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { + takerTokenAddress: string): Promise { // TODO refactor and check const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address; - const INF_TIMESTAMP = new BigNumber(2524604400); - expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSec) ? - INF_TIMESTAMP : - expirationUnixTimestampSec; + const INF_TIMESTAMP = 2524604400; const order = { maker, taker, @@ -33,7 +29,7 @@ export const orderFactory = { takerTokenAddress, salt: ZeroEx.generatePseudoRandomSalt(), feeRecipient: constants.NULL_ADDRESS, - expirationUnixTimestampSec, + expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), }; const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); const ecSignature = await zeroEx.signOrderHashAsync(orderHash); -- cgit v1.2.3 From 52bdc0d3432ef917cca15522cea78255d03d82f6 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 16:17:15 +0200 Subject: Add timestamp parameter to order factory --- test/utils/order_factory.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index e41e973ee..c6c6ed927 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -14,10 +14,14 @@ export const orderFactory = { makerTokenAmount: BigNumber.BigNumber|number, makerTokenAddress: string, takerTokenAmount: BigNumber.BigNumber|number, - takerTokenAddress: string): Promise { + takerTokenAddress: string, + expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { // TODO refactor and check const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address; - const INF_TIMESTAMP = 2524604400; + const INF_TIMESTAMP = new BigNumber(2524604400); + expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSec) ? + INF_TIMESTAMP : + expirationUnixTimestampSec; const order = { maker, taker, @@ -29,7 +33,7 @@ export const orderFactory = { takerTokenAddress, salt: ZeroEx.generatePseudoRandomSalt(), feeRecipient: constants.NULL_ADDRESS, - expirationUnixTimestampSec: new BigNumber(INF_TIMESTAMP), + expirationUnixTimestampSec, }; const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); const ecSignature = await zeroEx.signOrderHashAsync(orderHash); -- cgit v1.2.3 From 44f11442424c88d1130ee398d0714636c5ade045 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 16:21:01 +0200 Subject: Add comment for fillOrderAsync method, rename fillAmount to fillTakerAmountInBaseUnits and remove default value for shouldCheckTransfer --- test/exchange_wrapper_test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index c5cbc58be..f324c5492 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -107,6 +107,7 @@ describe('ExchangeWrapper', () => { let tokens: Token[]; const addressBySymbol: {[symbol: string]: string} = {}; let networkId: number; + const shouldCheckTransfer = false; const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, tokenAddress: string) => { @@ -133,7 +134,7 @@ describe('ExchangeWrapper', () => { const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(0); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { @@ -142,7 +143,7 @@ describe('ExchangeWrapper', () => { const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); }); @@ -160,7 +161,7 @@ describe('ExchangeWrapper', () => { 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); zeroEx.setDefaultAccount(taker); - await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount); + await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(0); }); -- cgit v1.2.3 From 5ded2bd52ba911e50e2cb3606a2e0e7a32cbd778 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 17:00:43 +0200 Subject: Refactor tests --- test/exchange_wrapper_test.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index f324c5492..4c61320e3 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -108,6 +108,8 @@ describe('ExchangeWrapper', () => { const addressBySymbol: {[symbol: string]: string} = {}; let networkId: number; const shouldCheckTransfer = false; + let maker: string; + let taker: string; const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, tokenAddress: string) => { @@ -127,19 +129,23 @@ describe('ExchangeWrapper', () => { }); networkId = await promisify(web3.version.getNetwork)(); }); + beforeEach('setup', () => { + maker = userAddresses[0]; + taker = userAddresses[1]; + }); + afterEach('reset default account', () => { + zeroEx.setDefaultAccount(userAddresses[0]); + }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { - const maker = userAddresses[0]; - const taker = userAddresses[0]; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(0); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) + zeroEx.setDefaultAccount(taker); + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { - const maker = userAddresses[0]; - const taker = userAddresses[1]; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); @@ -148,12 +154,7 @@ describe('ExchangeWrapper', () => { }); }); describe('successful fills', () => { - afterEach('reset default account', () => { - zeroEx.setDefaultAccount(userAddresses[0]); - }); it('should fill the valid order', async () => { - const maker = userAddresses[0]; - const taker = userAddresses[1]; await setAllowance(maker, 5, addressBySymbol.MLN); await setBalance(taker, 5, addressBySymbol.GNT); await setAllowance(taker, 5, addressBySymbol.GNT); -- cgit v1.2.3 From af61b5a9089b2575c8f0a44e1f0bceac41bb891f Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 17:03:15 +0200 Subject: Add shouldCheckTransfer --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 4c61320e3..e8a217d0f 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -142,7 +142,7 @@ describe('ExchangeWrapper', () => { 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(0); zeroEx.setDefaultAccount(taker); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount)) + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { -- cgit v1.2.3 From 59bc13c188cc54443e1514340310142c8442a88c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:03:39 +0200 Subject: remove comment --- test/0x.js_test.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index a548d8268..f2bb40d75 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -10,7 +10,6 @@ import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; import {Order} from '../src/types'; -// Use BigNumber chai add-on chai.use(ChaiBigNumber()); chai.use(dirtyChai); const expect = chai.expect; -- cgit v1.2.3 From 60d658a0249cdfb54d0763eb322cf6b16a1af36e Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:03:49 +0200 Subject: put external imports at the top --- test/exchange_wrapper_test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index f324c5492..251bd03ec 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -1,16 +1,16 @@ +import * as _ from 'lodash'; import 'mocha'; import * as chai from 'chai'; +import * as Web3 from 'web3'; +import * as BigNumber from 'bignumber.js'; +import * as dirtyChai from 'dirty-chai'; +import ChaiBigNumber = require('chai-bignumber'); +import promisify = require('es6-promisify'); import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; -import promisify = require('es6-promisify'); -import * as _ from 'lodash'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -import * as BigNumber from 'bignumber.js'; import {orderFactory} from './utils/order_factory'; import {FillOrderValidationErrs, Token} from '../src/types'; -import * as Web3 from 'web3'; -import * as dirtyChai from 'dirty-chai'; -import ChaiBigNumber = require('chai-bignumber'); chai.use(dirtyChai); chai.use(ChaiBigNumber()); -- cgit v1.2.3 From 00c597411fb1f14cc7f2d759a52638c4c2a61830 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:07:43 +0200 Subject: reorder import --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 04fd6a7e1..3f2054e0e 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -1,5 +1,5 @@ -import * as _ from 'lodash'; import 'mocha'; +import * as _ from 'lodash'; import * as chai from 'chai'; import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; -- cgit v1.2.3 From 2c65a658e7660654e2ee334e43b13c2dce0e134d Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:07:53 +0200 Subject: remove legacy code --- test/exchange_wrapper_test.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 3f2054e0e..70f4deec3 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -25,7 +25,6 @@ describe('ExchangeWrapper', () => { web3 = web3Factory.create(); zeroEx = new ZeroEx(web3); userAddresses = await promisify(web3.eth.getAccounts)(); - web3.eth.defaultAccount = userAddresses[0]; }); beforeEach(async () => { await blockchainLifecycle.startAsync(); -- cgit v1.2.3 From bcc8e43ab889b98bc2ebe2f4f6c172cbef925838 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:08:44 +0200 Subject: reorder variabled --- test/exchange_wrapper_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 70f4deec3..4d28dd2bd 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -104,11 +104,11 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; - const addressBySymbol: {[symbol: string]: string} = {}; - let networkId: number; - const shouldCheckTransfer = false; let maker: string; let taker: string; + let networkId: number; + const addressBySymbol: {[symbol: string]: string} = {}; + const shouldCheckTransfer = false; const setBalance = async (toAddress: string, amountInBaseUnits: BigNumber.BigNumber|number, tokenAddress: string) => { -- cgit v1.2.3 From 9a3cd3d06c5cb74e772685501bedee7e3fce5990 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 17:09:02 +0200 Subject: Add test, for throw when order is expired --- test/exchange_wrapper_test.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 4d28dd2bd..92e2514dd 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -104,6 +104,10 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; + const addressBySymbol: {[symbol: string]: string} = {}; + let networkId: number; + const fillAmount = new BigNumber(5); + const shouldCheckTransfer = false; let maker: string; let taker: string; let networkId: number; @@ -139,18 +143,25 @@ describe('ExchangeWrapper', () => { it('should throw when the fill amount is zero', async () => { const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); - const fillAmount = new BigNumber(0); + const zeroFillAmount = new BigNumber(0); zeroEx.setDefaultAccount(taker); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) + expect(zeroEx.exchange.fillOrderAsync(signedOrder, zeroFillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); - const fillAmount = new BigNumber(5); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); + it('should throw when order is expired', async () => { + const OLD_TIMESTAMP = new BigNumber(42); + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, + 5, addressBySymbol.MLN, 5, addressBySymbol.GNT, OLD_TIMESTAMP); + zeroEx.setDefaultAccount(taker); + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) + .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); + }); }); describe('successful fills', () => { it('should fill the valid order', async () => { -- cgit v1.2.3 From d1f352810f2228b2be143557f86e55c90193eea4 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 17:13:15 +0200 Subject: give some balance and allowance by default --- test/exchange_wrapper_test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 92e2514dd..2becd2cbe 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -132,9 +132,12 @@ describe('ExchangeWrapper', () => { }); networkId = await promisify(web3.version.getNetwork)(); }); - beforeEach('setup', () => { + beforeEach('setup', async () => { maker = userAddresses[0]; taker = userAddresses[1]; + await setAllowance(maker, 5, addressBySymbol.MLN); + await setBalance(taker, 5, addressBySymbol.GNT); + await setAllowance(taker, 5, addressBySymbol.GNT); }); afterEach('reset default account', () => { zeroEx.setDefaultAccount(userAddresses[0]); @@ -165,12 +168,8 @@ describe('ExchangeWrapper', () => { }); describe('successful fills', () => { it('should fill the valid order', async () => { - await setAllowance(maker, 5, addressBySymbol.MLN); - await setBalance(taker, 5, addressBySymbol.GNT); - await setAllowance(taker, 5, addressBySymbol.GNT); const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); - const fillAmount = new BigNumber(5); zeroEx.setDefaultAccount(taker); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); -- cgit v1.2.3 From cc0d6411071cb36a224c8f4f0cb742edb4193482 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:16:16 +0200 Subject: Add async suffix and fix alignment --- test/exchange_wrapper_test.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 4d28dd2bd..d6dca0a63 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -109,15 +109,15 @@ describe('ExchangeWrapper', () => { let networkId: number; const addressBySymbol: {[symbol: string]: string} = {}; const shouldCheckTransfer = false; - const setBalance = async (toAddress: string, - amountInBaseUnits: BigNumber.BigNumber|number, - tokenAddress: string) => { + const setBalanceAsync = async (toAddress: string, + amountInBaseUnits: BigNumber.BigNumber|number, + tokenAddress: string) => { const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; await zeroEx.token.transferAsync(tokenAddress, userAddresses[0], toAddress, amount); }; - const setAllowance = async (ownerAddress: string, - amountInBaseUnits: BigNumber.BigNumber|number, - tokenAddress: string) => { + const setAllowanceAsync = async (ownerAddress: string, + amountInBaseUnits: BigNumber.BigNumber|number, + tokenAddress: string) => { const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; await zeroEx.token.setProxyAllowanceAsync(tokenAddress, ownerAddress, amount); }; @@ -154,9 +154,9 @@ describe('ExchangeWrapper', () => { }); describe('successful fills', () => { it('should fill the valid order', async () => { - await setAllowance(maker, 5, addressBySymbol.MLN); - await setBalance(taker, 5, addressBySymbol.GNT); - await setAllowance(taker, 5, addressBySymbol.GNT); + await setAllowanceAsync(maker, 5, addressBySymbol.MLN); + await setBalanceAsync(taker, 5, addressBySymbol.GNT); + await setAllowanceAsync(taker, 5, addressBySymbol.GNT); const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); const fillAmount = new BigNumber(5); -- cgit v1.2.3 From 3e76497f4e47cb897b1afde509c3d2eb424a4f84 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:19:06 +0200 Subject: remove duplicate declarations --- test/exchange_wrapper_test.ts | 3 --- 1 file changed, 3 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 9c3683a98..9a519e92e 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -104,10 +104,7 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; - const addressBySymbol: {[symbol: string]: string} = {}; - let networkId: number; const fillAmount = new BigNumber(5); - const shouldCheckTransfer = false; let maker: string; let taker: string; let networkId: number; -- cgit v1.2.3 From 1e8eb054bc99baf9d10cafe9d5d388857484a801 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:25:33 +0200 Subject: get rid of literal amount params --- test/exchange_wrapper_test.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 9a519e92e..cb7482c99 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -141,23 +141,29 @@ describe('ExchangeWrapper', () => { }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { + const makerAmount = 5; + const takerAmount = 5; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, - 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); + makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); const zeroFillAmount = new BigNumber(0); zeroEx.setDefaultAccount(taker); expect(zeroEx.exchange.fillOrderAsync(signedOrder, zeroFillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { + const makerAmount = 5; + const takerAmount = 5; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, - 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); + makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { const OLD_TIMESTAMP = new BigNumber(42); + const makerAmount = 5; + const takerAmount = 5; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, - 5, addressBySymbol.MLN, 5, addressBySymbol.GNT, OLD_TIMESTAMP); + makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT, OLD_TIMESTAMP); zeroEx.setDefaultAccount(taker); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); @@ -165,12 +171,16 @@ describe('ExchangeWrapper', () => { }); describe('successful fills', () => { it('should fill the valid order', async () => { + const makerAmount = 5; + const takerAmount = 5; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, - 5, addressBySymbol.MLN, 5, addressBySymbol.GNT); + makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); zeroEx.setDefaultAccount(taker); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)).to.be.bignumber.equal(5); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)).to.be.bignumber.equal(0); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)) + .to.be.bignumber.equal(fillAmount); + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)) + .to.be.bignumber.equal(0); }); }); }); -- cgit v1.2.3 From 7ff0f677a12386a119bcadbdabd0721f9fc4cbce Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:27:23 +0200 Subject: move external imports to top --- test/utils/order_factory.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index c6c6ed927..3195803ad 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -1,6 +1,6 @@ -import {SignedOrder, Token} from '../../src/types'; -import * as BigNumber from 'bignumber.js'; import * as _ from 'lodash'; +import * as BigNumber from 'bignumber.js'; +import {SignedOrder, Token} from '../../src/types'; import {ZeroEx} from '../../src/0x.js'; import {constants} from './constants'; import * as ExchangeArtifacts from '../../src/artifacts/Exchange.json'; -- cgit v1.2.3 From ab5d99954946e01bdede26c4157fcfbdf9ae8bb1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 17:23:03 +0200 Subject: Remove duplicate declarations --- test/exchange_wrapper_test.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index cb7482c99..091d23fca 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -110,18 +110,6 @@ describe('ExchangeWrapper', () => { let networkId: number; const addressBySymbol: {[symbol: string]: string} = {}; const shouldCheckTransfer = false; - const setBalanceAsync = async (toAddress: string, - amountInBaseUnits: BigNumber.BigNumber|number, - tokenAddress: string) => { - const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; - await zeroEx.token.transferAsync(tokenAddress, userAddresses[0], toAddress, amount); - }; - const setAllowanceAsync = async (ownerAddress: string, - amountInBaseUnits: BigNumber.BigNumber|number, - tokenAddress: string) => { - const amount = _.isNumber(amountInBaseUnits) ? new BigNumber(amountInBaseUnits) : amountInBaseUnits; - await zeroEx.token.setProxyAllowanceAsync(tokenAddress, ownerAddress, amount); - }; before('fetch tokens', async () => { tokens = await zeroEx.tokenRegistry.getTokensAsync(); _.forEach(tokens, token => { @@ -132,9 +120,9 @@ describe('ExchangeWrapper', () => { beforeEach('setup', async () => { maker = userAddresses[0]; taker = userAddresses[1]; - await setAllowanceAsync(maker, 5, addressBySymbol.MLN); - await setBalanceAsync(taker, 5, addressBySymbol.GNT); - await setAllowanceAsync(taker, 5, addressBySymbol.GNT); + await zeroEx.token.setProxyAllowanceAsync(addressBySymbol.MLN, maker, new BigNumber(5)); + await zeroEx.token.transferAsync(addressBySymbol.GNT, maker, taker, new BigNumber(5)); + await zeroEx.token.setProxyAllowanceAsync(addressBySymbol.GNT, taker, new BigNumber(5)); }); afterEach('reset default account', () => { zeroEx.setDefaultAccount(userAddresses[0]); -- cgit v1.2.3 From 24a745092e0f7a626f5cbcfaef2e56567f0ace1d Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:38:09 +0200 Subject: Stop passing exchangeAddress into getOrderHash and instead get it from the artifacts --- test/0x.js_test.ts | 43 ++++++++++++++++++++++--------------------- test/utils/order_factory.ts | 4 +--- 2 files changed, 23 insertions(+), 24 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index f2bb40d75..8c5e9c929 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -43,27 +43,6 @@ describe('ZeroEx library', () => { expect((tokenRegistryWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); }); }); - describe('#getOrderHash', () => { - const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7'; - const order: Order = { - maker: constants.NULL_ADDRESS, - taker: constants.NULL_ADDRESS, - feeRecipient: constants.NULL_ADDRESS, - makerTokenAddress: constants.NULL_ADDRESS, - takerTokenAddress: constants.NULL_ADDRESS, - salt: new BigNumber(0), - makerFee: new BigNumber(0), - takerFee: new BigNumber(0), - makerTokenAmount: new BigNumber(0), - takerTokenAmount: new BigNumber(0), - expirationUnixTimestampSec: new BigNumber(0), - }; - const exchangeAddress = constants.NULL_ADDRESS; - it('calculates the order hash', () => { - const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); - expect(orderHash).to.be.equal(expectedOrderHash); - }); - }); describe('#isValidSignature', () => { // This test data was borrowed from the JSON RPC documentation // Source: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign @@ -174,6 +153,28 @@ describe('ZeroEx library', () => { expect(baseUnitAmount).to.be.bignumber.equal(expectedUnitAmount); }); }); + describe('#getOrderHashAsync', () => { + const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7'; + const order: Order = { + maker: constants.NULL_ADDRESS, + taker: constants.NULL_ADDRESS, + feeRecipient: constants.NULL_ADDRESS, + makerTokenAddress: constants.NULL_ADDRESS, + takerTokenAddress: constants.NULL_ADDRESS, + salt: new BigNumber(0), + makerFee: new BigNumber(0), + takerFee: new BigNumber(0), + makerTokenAmount: new BigNumber(0), + takerTokenAmount: new BigNumber(0), + expirationUnixTimestampSec: new BigNumber(0), + }; + it('calculates the order hash', async () => { + const web3 = web3Factory.create(); + const zeroEx = new ZeroEx(web3); + const orderHash = await zeroEx.getOrderHashHexAsync(order); + expect(orderHash).to.be.equal(expectedOrderHash); + }); + }); describe('#signOrderHashAsync', () => { let stubs: Sinon.SinonStub[] = []; afterEach(() => { diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index 3195803ad..941056c7e 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -16,8 +16,6 @@ export const orderFactory = { takerTokenAmount: BigNumber.BigNumber|number, takerTokenAddress: string, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { - // TODO refactor and check - const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address; const INF_TIMESTAMP = new BigNumber(2524604400); expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSec) ? INF_TIMESTAMP : @@ -35,7 +33,7 @@ export const orderFactory = { feeRecipient: constants.NULL_ADDRESS, expirationUnixTimestampSec, }; - const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order); + const orderHash = await zeroEx.getOrderHashHexAsync(order); const ecSignature = await zeroEx.signOrderHashAsync(orderHash); const signedOrder: SignedOrder = _.assign(order, {ecSignature}); return signedOrder; -- cgit v1.2.3 From 35b7a60e9704e5ab222dc7552cbe523d12dcb003 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:38:20 +0200 Subject: Add missing comma and semi-colon --- test/utils/order_factory.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index 941056c7e..c0d70f39b 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -37,5 +37,5 @@ export const orderFactory = { const ecSignature = await zeroEx.signOrderHashAsync(orderHash); const signedOrder: SignedOrder = _.assign(order, {ecSignature}); return signedOrder; - } -} + }, +}; -- cgit v1.2.3 From 85d4badbd6141bed4f7f00c08e882eb8db362252 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:39:58 +0200 Subject: remove unused arg --- test/exchange_wrapper_test.ts | 10 ++++------ test/utils/order_factory.ts | 1 - 2 files changed, 4 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 091d23fca..e74cdacaf 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -107,7 +107,6 @@ describe('ExchangeWrapper', () => { const fillAmount = new BigNumber(5); let maker: string; let taker: string; - let networkId: number; const addressBySymbol: {[symbol: string]: string} = {}; const shouldCheckTransfer = false; before('fetch tokens', async () => { @@ -115,7 +114,6 @@ describe('ExchangeWrapper', () => { _.forEach(tokens, token => { addressBySymbol[token.symbol] = token.address; }); - networkId = await promisify(web3.version.getNetwork)(); }); beforeEach('setup', async () => { maker = userAddresses[0]; @@ -131,7 +129,7 @@ describe('ExchangeWrapper', () => { it('should throw when the fill amount is zero', async () => { const makerAmount = 5; const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); const zeroFillAmount = new BigNumber(0); zeroEx.setDefaultAccount(taker); @@ -141,7 +139,7 @@ describe('ExchangeWrapper', () => { it('should throw when sender is not a taker', async () => { const makerAmount = 5; const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); @@ -150,7 +148,7 @@ describe('ExchangeWrapper', () => { const OLD_TIMESTAMP = new BigNumber(42); const makerAmount = 5; const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT, OLD_TIMESTAMP); zeroEx.setDefaultAccount(taker); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) @@ -161,7 +159,7 @@ describe('ExchangeWrapper', () => { it('should fill the valid order', async () => { const makerAmount = 5; const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, networkId, maker, taker, + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); zeroEx.setDefaultAccount(taker); await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer); diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index c0d70f39b..359168ff9 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -8,7 +8,6 @@ import * as ExchangeArtifacts from '../../src/artifacts/Exchange.json'; export const orderFactory = { async createSignedOrderAsync( zeroEx: ZeroEx, - networkId: number, maker: string, taker: string, makerTokenAmount: BigNumber.BigNumber|number, -- cgit v1.2.3 From 69a596352d47502e810adc10ffd0c89b7fa2d1d0 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:49:53 +0200 Subject: Fix getOrderHashAsync test --- test/0x.js_test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 8c5e9c929..efc703ea1 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -154,6 +154,7 @@ describe('ZeroEx library', () => { }); }); describe('#getOrderHashAsync', () => { + const exchangeContractAddress = constants.NULL_ADDRESS; const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7'; const order: Order = { maker: constants.NULL_ADDRESS, @@ -168,9 +169,21 @@ describe('ZeroEx library', () => { takerTokenAmount: new BigNumber(0), expirationUnixTimestampSec: new BigNumber(0), }; + let stubs: Sinon.SinonStub[] = []; + afterEach(() => { + // clean up any stubs after the test has completed + _.each(stubs, s => s.restore()); + stubs = []; + }); it('calculates the order hash', async () => { const web3 = web3Factory.create(); const zeroEx = new ZeroEx(web3); + + stubs = [ + Sinon.stub((zeroEx as any), 'getExchangeAddressAsync') + .returns(Promise.resolve(exchangeContractAddress)), + ]; + const orderHash = await zeroEx.getOrderHashHexAsync(order); expect(orderHash).to.be.equal(expectedOrderHash); }); -- cgit v1.2.3 From aef3ed9fd7e1d8c72a02bb2eec970597cb353a4d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 17:51:49 +0200 Subject: Follow constants convention --- test/exchange_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index e74cdacaf..ad5eba699 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -145,11 +145,11 @@ describe('ExchangeWrapper', () => { .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { - const OLD_TIMESTAMP = new BigNumber(42); + const oldTimestamp = new BigNumber(42); const makerAmount = 5; const takerAmount = 5; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT, OLD_TIMESTAMP); + makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT, oldTimestamp); zeroEx.setDefaultAccount(taker); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); -- cgit v1.2.3 From 19e922724e963950c97c009def39019b382fb516 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 17:52:31 +0200 Subject: rename variable --- test/utils/order_factory.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index 359168ff9..0f370ed34 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -15,9 +15,9 @@ export const orderFactory = { takerTokenAmount: BigNumber.BigNumber|number, takerTokenAddress: string, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { - const INF_TIMESTAMP = new BigNumber(2524604400); + const defaultExpirationUnixTimestampSec = new BigNumber(2524604400); // Close to infinite expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSec) ? - INF_TIMESTAMP : + defaultExpirationUnixTimestampSec : expirationUnixTimestampSec; const order = { maker, -- cgit v1.2.3 From 389c18e98e2b5663b1732d7c34f9b21afef66e44 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 17:56:01 +0200 Subject: Add test to check the balance --- test/exchange_wrapper_test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ad5eba699..6b2fd6477 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -154,6 +154,16 @@ describe('ExchangeWrapper', () => { expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); + it('should throw when not enough balance', async () => { + const makerAmount = 10; + const takerAmount = 10; + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, + makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); + zeroEx.setDefaultAccount(taker); + const tooMuch = new BigNumber(6); + expect(zeroEx.exchange.fillOrderAsync(signedOrder, tooMuch, true)) + .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); + }); }); describe('successful fills', () => { it('should fill the valid order', async () => { -- cgit v1.2.3 From 6ced554d9f6cf00d0d9e8f9ff8170af4f1cfbee3 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 18:15:26 +0200 Subject: Revert "Add test to check the balance" This reverts commit 389c18e98e2b5663b1732d7c34f9b21afef66e44. --- test/exchange_wrapper_test.ts | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 6b2fd6477..ad5eba699 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -154,16 +154,6 @@ describe('ExchangeWrapper', () => { expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); - it('should throw when not enough balance', async () => { - const makerAmount = 10; - const takerAmount = 10; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); - zeroEx.setDefaultAccount(taker); - const tooMuch = new BigNumber(6); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, tooMuch, true)) - .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); - }); }); describe('successful fills', () => { it('should fill the valid order', async () => { -- cgit v1.2.3 From 0554f947b12f36a1cd5f7be469123e794b467f0b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 18:21:12 +0200 Subject: Add not enough taker balance tests --- test/exchange_wrapper_test.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ad5eba699..09fb11cb3 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -104,7 +104,7 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; - const fillAmount = new BigNumber(5); + const fillTakerAmountInBaseUnits = new BigNumber(5); let maker: string; let taker: string; const addressBySymbol: {[symbol: string]: string} = {}; @@ -141,19 +141,29 @@ describe('ExchangeWrapper', () => { const takerAmount = 5; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { - const oldTimestamp = new BigNumber(42); + const timestampInThePast = new BigNumber(42); const makerAmount = 5; const takerAmount = 5; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT, oldTimestamp); + makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT, timestampInThePast); zeroEx.setDefaultAccount(taker); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer)) + expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); + it('should throw when not enough balance', async () => { + const makerAmount = 10; + const takerAmount = 10; + const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, + makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); + zeroEx.setDefaultAccount(taker); + const moreThanTheBalance = new BigNumber(6); + expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, true)) + .to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); + }); }); describe('successful fills', () => { it('should fill the valid order', async () => { @@ -162,9 +172,9 @@ describe('ExchangeWrapper', () => { const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); zeroEx.setDefaultAccount(taker); - await zeroEx.exchange.fillOrderAsync(signedOrder, fillAmount, shouldCheckTransfer); + await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)) - .to.be.bignumber.equal(fillAmount); + .to.be.bignumber.equal(fillTakerAmountInBaseUnits); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)) .to.be.bignumber.equal(0); }); -- cgit v1.2.3 From 520248e6787a04b2fcf45d7f185d85214a84bfd5 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 18:31:08 +0200 Subject: Make validateFillOrder asyncronous --- test/exchange_wrapper_test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 09fb11cb3..2746c9e46 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -161,7 +161,8 @@ describe('ExchangeWrapper', () => { makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); zeroEx.setDefaultAccount(taker); const moreThanTheBalance = new BigNumber(6); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, true)) + const checkTransfer = true; + expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, checkTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); }); }); -- cgit v1.2.3 From c42877327e59f983a83c7221f1bec5b31addf5a7 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 18:52:56 +0200 Subject: Change test name --- test/exchange_wrapper_test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 2746c9e46..4847d10cf 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -154,15 +154,14 @@ describe('ExchangeWrapper', () => { expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); - it('should throw when not enough balance', async () => { + it('should throw when taker balance is less than fill amount', async () => { const makerAmount = 10; const takerAmount = 10; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); zeroEx.setDefaultAccount(taker); const moreThanTheBalance = new BigNumber(6); - const checkTransfer = true; - expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, checkTransfer)) + expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); }); }); -- cgit v1.2.3 From a1be87058536f5ab10c68c570c1b9994defe52b1 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 19:47:22 +0200 Subject: Create a FillsScenario utils module and use it in the fillOrder tests --- test/exchange_wrapper_test.ts | 64 ++++++++++++++++++------------------------- test/utils/fill_scenarios.ts | 29 ++++++++++++++++++++ 2 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 test/utils/fill_scenarios.ts (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 4847d10cf..ad6e5496e 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -11,6 +11,7 @@ import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {orderFactory} from './utils/order_factory'; import {FillOrderValidationErrs, Token} from '../src/types'; +import {FillScenarios} from './utils/fill_scenarios'; chai.use(dirtyChai); chai.use(ChaiBigNumber()); @@ -104,62 +105,52 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; + let fillScenarios: FillScenarios; + let takerAddress: string; const fillTakerAmountInBaseUnits = new BigNumber(5); - let maker: string; - let taker: string; const addressBySymbol: {[symbol: string]: string} = {}; const shouldCheckTransfer = false; before('fetch tokens', async () => { + takerAddress = userAddresses[1]; tokens = await zeroEx.tokenRegistry.getTokensAsync(); _.forEach(tokens, token => { addressBySymbol[token.symbol] = token.address; }); - }); - beforeEach('setup', async () => { - maker = userAddresses[0]; - taker = userAddresses[1]; - await zeroEx.token.setProxyAllowanceAsync(addressBySymbol.MLN, maker, new BigNumber(5)); - await zeroEx.token.transferAsync(addressBySymbol.GNT, maker, taker, new BigNumber(5)); - await zeroEx.token.setProxyAllowanceAsync(addressBySymbol.GNT, taker, new BigNumber(5)); + fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens); }); afterEach('reset default account', () => { - zeroEx.setDefaultAccount(userAddresses[0]); + zeroEx.setTransactionSenderAccount(userAddresses[0]); }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { - const makerAmount = 5; - const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, fillableAmount); const zeroFillAmount = new BigNumber(0); - zeroEx.setDefaultAccount(taker); + zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, zeroFillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { - const makerAmount = 5; - const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, fillableAmount); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { - const timestampInThePast = new BigNumber(42); - const makerAmount = 5; - const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT, timestampInThePast); - zeroEx.setDefaultAccount(taker); + const expirationInPast = new BigNumber(42); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, + fillableAmount, + expirationInPast); + zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); it('should throw when taker balance is less than fill amount', async () => { - const makerAmount = 10; - const takerAmount = 10; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); - zeroEx.setDefaultAccount(taker); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, + fillableAmount); + zeroEx.setTransactionSenderAccount(takerAddress); const moreThanTheBalance = new BigNumber(6); expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); @@ -167,15 +158,14 @@ describe('ExchangeWrapper', () => { }); describe('successful fills', () => { it('should fill the valid order', async () => { - const makerAmount = 5; - const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); - zeroEx.setDefaultAccount(taker); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, + fillableAmount); + zeroEx.setTransactionSenderAccount(takerAddress); await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)) + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)) + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, takerAddress)) .to.be.bignumber.equal(0); }); }); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts new file mode 100644 index 000000000..8447c8c15 --- /dev/null +++ b/test/utils/fill_scenarios.ts @@ -0,0 +1,29 @@ +import * as BigNumber from 'bignumber.js'; +import {ZeroEx} from '../../src/0x.js'; +import {Token, SignedOrder} from '../../src/types'; +import {orderFactory} from '../utils/order_factory'; + +export class FillScenarios { + private zeroEx: ZeroEx; + private userAddresses: string[]; + private tokens: Token[]; + constructor(zeroEx: ZeroEx, userAddresses: string[], tokens: Token[]) { + this.zeroEx = zeroEx; + this.userAddresses = userAddresses; + this.tokens = tokens; + } + public async createAFillableSignedOrderAsync(takerAddress: string, fillableAmount: BigNumber.BigNumber, + expirationUnixTimestampSec?: BigNumber.BigNumber): + Promise { + const [makerAddress] = this.userAddresses; + const [makerToken, takerToken] = this.tokens; + await this.zeroEx.token.setProxyAllowanceAsync(makerToken.address, makerAddress, fillableAmount); + await this.zeroEx.token.transferAsync(takerToken.address, makerAddress, takerAddress, fillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync(takerToken.address, takerAddress, fillableAmount); + + const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, + takerAddress, fillableAmount, makerToken.address, fillableAmount, takerToken.address, + expirationUnixTimestampSec); + return signedOrder; + } +} -- cgit v1.2.3 From 511fa537ad3d2a669d5d680efb26012ca1b9c062 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 20:04:12 +0200 Subject: Work --- test/exchange_wrapper_test.ts | 33 ++++++++++++++++++++++++--------- test/utils/fill_scenarios.ts | 12 ++++++------ 2 files changed, 30 insertions(+), 15 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ad6e5496e..debc8cced 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -105,6 +105,8 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; + let makerTokenAddress: string; + let takerTokenAddress: string; let fillScenarios: FillScenarios; let takerAddress: string; const fillTakerAmountInBaseUnits = new BigNumber(5); @@ -116,6 +118,9 @@ describe('ExchangeWrapper', () => { _.forEach(tokens, token => { addressBySymbol[token.symbol] = token.address; }); + const [makerToken, takerToken] = tokens; + makerTokenAddress = makerToken.address; + takerTokenAddress = takerToken.address; fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens); }); afterEach('reset default account', () => { @@ -124,7 +129,9 @@ describe('ExchangeWrapper', () => { describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, fillableAmount); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + ); const zeroFillAmount = new BigNumber(0); zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, zeroFillAmount, shouldCheckTransfer)) @@ -132,24 +139,27 @@ describe('ExchangeWrapper', () => { }); it('should throw when sender is not a taker', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, fillableAmount); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + ); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { const expirationInPast = new BigNumber(42); const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, - fillableAmount, - expirationInPast); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, expirationInPast, + ); zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); it('should throw when taker balance is less than fill amount', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, - fillableAmount); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + ); zeroEx.setTransactionSenderAccount(takerAddress); const moreThanTheBalance = new BigNumber(6); expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, shouldCheckTransfer)) @@ -159,9 +169,14 @@ describe('ExchangeWrapper', () => { describe('successful fills', () => { it('should fill the valid order', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, - fillableAmount); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + ); zeroEx.setTransactionSenderAccount(takerAddress); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) + .to.be.bignumber.equal(0); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) + .to.be.bignumber.equal(fillTakerAmountInBaseUnits); await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 8447c8c15..ac233ad50 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -12,17 +12,17 @@ export class FillScenarios { this.userAddresses = userAddresses; this.tokens = tokens; } - public async createAFillableSignedOrderAsync(takerAddress: string, fillableAmount: BigNumber.BigNumber, + public async createAFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, + takerAddress: string, fillableAmount: BigNumber.BigNumber, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { const [makerAddress] = this.userAddresses; - const [makerToken, takerToken] = this.tokens; - await this.zeroEx.token.setProxyAllowanceAsync(makerToken.address, makerAddress, fillableAmount); - await this.zeroEx.token.transferAsync(takerToken.address, makerAddress, takerAddress, fillableAmount); - await this.zeroEx.token.setProxyAllowanceAsync(takerToken.address, takerAddress, fillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, fillableAmount); + await this.zeroEx.token.transferAsync(takerTokenAddress, makerAddress, takerAddress, fillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, fillableAmount); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, - takerAddress, fillableAmount, makerToken.address, fillableAmount, takerToken.address, + takerAddress, fillableAmount, makerTokenAddress, fillableAmount, takerTokenAddress, expirationUnixTimestampSec); return signedOrder; } -- cgit v1.2.3 From f663a15c9b000d639bd0d58d0c177e6de9d47346 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 21:12:13 +0200 Subject: Fix tests --- test/exchange_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index debc8cced..77c78470b 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -178,9 +178,9 @@ describe('ExchangeWrapper', () => { expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, takerAddress)) + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, takerAddress)) + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) .to.be.bignumber.equal(0); }); }); -- cgit v1.2.3 From a740498c8061bc7a3dafae82929b358e61bac2d4 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 11:23:39 +0200 Subject: Introduce coinBase account Add makerAccount parameter Adjust tests Add more assertions to success test --- test/exchange_wrapper_test.ts | 33 ++++++++++++++++++++------------- test/utils/fill_scenarios.ts | 12 +++++++++--- 2 files changed, 29 insertions(+), 16 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 77c78470b..716adb63a 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -108,16 +108,14 @@ describe('ExchangeWrapper', () => { let makerTokenAddress: string; let takerTokenAddress: string; let fillScenarios: FillScenarios; + let coinBase: string; + let makerAddress: string; let takerAddress: string; const fillTakerAmountInBaseUnits = new BigNumber(5); - const addressBySymbol: {[symbol: string]: string} = {}; const shouldCheckTransfer = false; before('fetch tokens', async () => { - takerAddress = userAddresses[1]; + [coinBase, makerAddress, takerAddress] = userAddresses; tokens = await zeroEx.tokenRegistry.getTokensAsync(); - _.forEach(tokens, token => { - addressBySymbol[token.symbol] = token.address; - }); const [makerToken, takerToken] = tokens; makerTokenAddress = makerToken.address; takerTokenAddress = takerToken.address; @@ -130,7 +128,7 @@ describe('ExchangeWrapper', () => { it('should throw when the fill amount is zero', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const zeroFillAmount = new BigNumber(0); zeroEx.setTransactionSenderAccount(takerAddress); @@ -140,7 +138,7 @@ describe('ExchangeWrapper', () => { it('should throw when sender is not a taker', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); @@ -149,7 +147,7 @@ describe('ExchangeWrapper', () => { const expirationInPast = new BigNumber(42); const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, expirationInPast, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, expirationInPast, ); zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) @@ -158,7 +156,7 @@ describe('ExchangeWrapper', () => { it('should throw when taker balance is less than fill amount', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); zeroEx.setTransactionSenderAccount(takerAddress); const moreThanTheBalance = new BigNumber(6); @@ -170,18 +168,27 @@ describe('ExchangeWrapper', () => { it('should fill the valid order', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); - zeroEx.setTransactionSenderAccount(takerAddress); + + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillableAmount); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) + .to.be.bignumber.equal(0); expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) .to.be.bignumber.equal(0); expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillTakerAmountInBaseUnits); + .to.be.bignumber.equal(fillableAmount); + zeroEx.setTransactionSenderAccount(takerAddress); await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmountInBaseUnits)); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillTakerAmountInBaseUnits); expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(0); + .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmountInBaseUnits)); }); }); }); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index ac233ad50..26840bb0c 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -7,23 +7,29 @@ export class FillScenarios { private zeroEx: ZeroEx; private userAddresses: string[]; private tokens: Token[]; + private coinBase: string; constructor(zeroEx: ZeroEx, userAddresses: string[], tokens: Token[]) { this.zeroEx = zeroEx; this.userAddresses = userAddresses; this.tokens = tokens; + this.coinBase = userAddresses[0]; } public async createAFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, - takerAddress: string, fillableAmount: BigNumber.BigNumber, + makerAddress: string, takerAddress: string, + fillableAmount: BigNumber.BigNumber, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { - const [makerAddress] = this.userAddresses; + await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinBase, makerAddress, fillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, fillableAmount); - await this.zeroEx.token.transferAsync(takerTokenAddress, makerAddress, takerAddress, fillableAmount); + await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinBase, takerAddress, fillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, fillableAmount); + const transactionSenderAccount = this.zeroEx.getTransactionSenderAccount(); + this.zeroEx.setTransactionSenderAccount(makerAddress); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, takerAddress, fillableAmount, makerTokenAddress, fillableAmount, takerTokenAddress, expirationUnixTimestampSec); + this.zeroEx.setTransactionSenderAccount(transactionSenderAccount); return signedOrder; } } -- cgit v1.2.3 From 5155f4980440f1c91006b124406c8b4a7a90e300 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 12:03:52 +0200 Subject: Add test for insufficient balance and make all async tests async --- test/exchange_wrapper_test.ts | 47 +++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 716adb63a..8d39c9e57 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -50,37 +50,37 @@ describe('ExchangeWrapper', () => { r: signature.r, s: signature.s, }; - expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) + return expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) .to.be.rejected(); }); - it('r lacks 0x prefix', () => { + it('r lacks 0x prefix', async () => { const malformedR = signature.r.replace('0x', ''); const malformedSignature = { v: signature.v, r: malformedR, s: signature.s, }; - expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) + return expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) .to.be.rejected(); }); - it('r is too short', () => { + it('r is too short', async () => { const malformedR = signature.r.substr(10); const malformedSignature = { v: signature.v, r: malformedR, s: signature.s.replace('0', 'z'), }; - expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) + return expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) .to.be.rejected(); }); - it('s is not hex', () => { + it('s is not hex', async () => { const malformedS = signature.s.replace('0', 'z'); const malformedSignature = { v: signature.v, r: signature.r, s: malformedS, }; - expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) + return expect(zeroEx.exchange.isValidSignatureAsync(dataHex, malformedSignature, address)) .to.be.rejected(); }); }); @@ -132,16 +132,18 @@ describe('ExchangeWrapper', () => { ); const zeroFillAmount = new BigNumber(0); zeroEx.setTransactionSenderAccount(takerAddress); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, zeroFillAmount, shouldCheckTransfer)) - .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, zeroFillAmount, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) - .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { const expirationInPast = new BigNumber(42); @@ -150,8 +152,9 @@ describe('ExchangeWrapper', () => { makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, expirationInPast, ); zeroEx.setTransactionSenderAccount(takerAddress); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) - .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); it('should throw when taker balance is less than fill amount', async () => { const fillableAmount = new BigNumber(5); @@ -160,8 +163,22 @@ describe('ExchangeWrapper', () => { ); zeroEx.setTransactionSenderAccount(takerAddress); const moreThanTheBalance = new BigNumber(6); - expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, shouldCheckTransfer)) - .to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, moreThanTheBalance, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); + }); + it('should throw when taker allowance is less than fill amount', async () => { + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(1); + await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, + newAllowanceWhichIsLessThanFillAmount); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); }); }); describe('successful fills', () => { -- cgit v1.2.3 From 4caf1b009ea253d08e1cd6ecc4e914606e2d31c1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 12:08:25 +0200 Subject: Add test for valid partial fill --- test/exchange_wrapper_test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 8d39c9e57..0c1f552b6 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -207,6 +207,23 @@ describe('ExchangeWrapper', () => { expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmountInBaseUnits)); }); + it('should partially fill the valid order', async () => { + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + const partialFillAmount = new BigNumber(3); + zeroEx.setTransactionSenderAccount(takerAddress); + await zeroEx.exchange.fillOrderAsync(signedOrder, partialFillAmount, shouldCheckTransfer); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) + .to.be.bignumber.equal(partialFillAmount); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) + .to.be.bignumber.equal(partialFillAmount); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) + .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); + }); }); }); }); -- cgit v1.2.3 From fc7592d73f1613b11e4b48904bfb041cccca5800 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 12:15:50 +0200 Subject: Fix getTransactionSenderAccountIfExistsAsync to return true senderAddress or undefined if non available --- test/utils/fill_scenarios.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 26840bb0c..3b66937e6 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -24,12 +24,12 @@ export class FillScenarios { await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinBase, takerAddress, fillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, fillableAmount); - const transactionSenderAccount = this.zeroEx.getTransactionSenderAccount(); + const transactionSenderAccount = await this.zeroEx.getTransactionSenderAccountIfExistsAsync(); this.zeroEx.setTransactionSenderAccount(makerAddress); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, takerAddress, fillableAmount, makerTokenAddress, fillableAmount, takerTokenAddress, expirationUnixTimestampSec); - this.zeroEx.setTransactionSenderAccount(transactionSenderAccount); + this.zeroEx.setTransactionSenderAccount(transactionSenderAccount as string); return signedOrder; } } -- cgit v1.2.3 From 2a0c6abbe7f9abeacc4fea05bc468413ec1f4732 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 12:20:18 +0200 Subject: Validate maker balance and allowance & tests --- test/exchange_wrapper_test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 0c1f552b6..66ba167cc 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -180,6 +180,31 @@ describe('ExchangeWrapper', () => { signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); }); + it('should throw when maker balance is less than maker fill amount', async () => { + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + const lackingMakerBalance = new BigNumber(3); + await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinBase, lackingMakerBalance); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_BALANCE); + }); + it('should throw when maker allowance is less than maker fill amount', async () => { + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(1); + await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, + newAllowanceWhichIsLessThanFillAmount); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_ALLOWANCE); + }); }); describe('successful fills', () => { it('should fill the valid order', async () => { -- cgit v1.2.3 From e1ee6b84945e729d894f6535be02f3541e43dbf0 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 12:57:21 +0200 Subject: Add check for ROUNDING_ERROR and test for it --- test/exchange_wrapper_test.ts | 31 ++++++++++++++++++++++--------- test/utils/fill_scenarios.ts | 27 ++++++++++++++++++--------- 2 files changed, 40 insertions(+), 18 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 66ba167cc..7f8bad377 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -127,7 +127,7 @@ describe('ExchangeWrapper', () => { describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const zeroFillAmount = new BigNumber(0); @@ -138,7 +138,7 @@ describe('ExchangeWrapper', () => { }); it('should throw when sender is not a taker', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); return expect(zeroEx.exchange.fillOrderAsync( @@ -148,7 +148,7 @@ describe('ExchangeWrapper', () => { it('should throw when order is expired', async () => { const expirationInPast = new BigNumber(42); const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, expirationInPast, ); zeroEx.setTransactionSenderAccount(takerAddress); @@ -158,7 +158,7 @@ describe('ExchangeWrapper', () => { }); it('should throw when taker balance is less than fill amount', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); zeroEx.setTransactionSenderAccount(takerAddress); @@ -169,7 +169,7 @@ describe('ExchangeWrapper', () => { }); it('should throw when taker allowance is less than fill amount', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(1); @@ -182,7 +182,7 @@ describe('ExchangeWrapper', () => { }); it('should throw when maker balance is less than maker fill amount', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const lackingMakerBalance = new BigNumber(3); @@ -194,7 +194,7 @@ describe('ExchangeWrapper', () => { }); it('should throw when maker allowance is less than maker fill amount', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(1); @@ -205,11 +205,24 @@ describe('ExchangeWrapper', () => { signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_ALLOWANCE); }); + it('should throw when there would be a rounding error', async () => { + const makerFillableAmount = new BigNumber(3); + const takerFillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAsymetricFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, + makerFillableAmount, takerFillableAmount, + ); + const fillTakerAmountInBaseUnitsThatCausesRoundingError = new BigNumber(3); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnitsThatCausesRoundingError, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.ROUNDING_ERROR); + }); }); describe('successful fills', () => { it('should fill the valid order', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); @@ -234,7 +247,7 @@ describe('ExchangeWrapper', () => { }); it('should partially fill the valid order', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const partialFillAmount = new BigNumber(3); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 3b66937e6..568b11ef8 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -14,20 +14,29 @@ export class FillScenarios { this.tokens = tokens; this.coinBase = userAddresses[0]; } - public async createAFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, - makerAddress: string, takerAddress: string, - fillableAmount: BigNumber.BigNumber, - expirationUnixTimestampSec?: BigNumber.BigNumber): + public async createFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, + makerAddress: string, takerAddress: string, + fillableAmount: BigNumber.BigNumber, + expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { - await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinBase, makerAddress, fillableAmount); - await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, fillableAmount); - await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinBase, takerAddress, fillableAmount); - await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, fillableAmount); + return this.createAsymetricFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, + fillableAmount, fillableAmount, expirationUnixTimestampSec, + ); + } + public async createAsymetricFillableSignedOrderAsync( + makerTokenAddress: string, takerTokenAddress: string, makerAddress: string, takerAddress: string, + makerFillableAmount: BigNumber.BigNumber, takerFillableAmount: BigNumber.BigNumber, + expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { + await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinBase, makerAddress, makerFillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, makerFillableAmount); + await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinBase, takerAddress, takerFillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, takerFillableAmount); const transactionSenderAccount = await this.zeroEx.getTransactionSenderAccountIfExistsAsync(); this.zeroEx.setTransactionSenderAccount(makerAddress); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, - takerAddress, fillableAmount, makerTokenAddress, fillableAmount, takerTokenAddress, + takerAddress, makerFillableAmount, makerTokenAddress, takerFillableAmount, takerTokenAddress, expirationUnixTimestampSec); this.zeroEx.setTransactionSenderAccount(transactionSenderAccount as string); return signedOrder; -- cgit v1.2.3 From 832d2ff5ca1e82c4580e47c0b101069ee7cc0fca Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 13:08:59 +0200 Subject: Add fill scenario with fees --- test/utils/fill_scenarios.ts | 34 +++++++++++++++++++++++++++++++--- test/utils/order_factory.ts | 17 ++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 568b11ef8..31f93618f 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -2,6 +2,7 @@ import * as BigNumber from 'bignumber.js'; import {ZeroEx} from '../../src/0x.js'; import {Token, SignedOrder} from '../../src/types'; import {orderFactory} from '../utils/order_factory'; +import {constants} from './constants'; export class FillScenarios { private zeroEx: ZeroEx; @@ -24,10 +25,36 @@ export class FillScenarios { fillableAmount, fillableAmount, expirationUnixTimestampSec, ); } + public async createFillableSignedOrderWithFeesAsync( + makerTokenAddress: string, takerTokenAddress: string, + makerFee: BigNumber.BigNumber, takerFee: BigNumber.BigNumber, + makerAddress: string, takerAddress: string, + fillableAmount: BigNumber.BigNumber, + feeRecepient: string, expirationUnixTimestampSec?: BigNumber.BigNumber, + ): Promise { + return this.createAsymetricFillableSignedOrderWithFeesAsync( + makerTokenAddress, takerTokenAddress, makerFee, takerFee, makerAddress, takerAddress, + fillableAmount, fillableAmount, feeRecepient, expirationUnixTimestampSec, + ); + } public async createAsymetricFillableSignedOrderAsync( makerTokenAddress: string, takerTokenAddress: string, makerAddress: string, takerAddress: string, makerFillableAmount: BigNumber.BigNumber, takerFillableAmount: BigNumber.BigNumber, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { + const makerFee = new BigNumber(0); + const takerFee = new BigNumber(0); + const feeRecepient = constants.NULL_ADDRESS; + return this.createAsymetricFillableSignedOrderWithFeesAsync( + makerTokenAddress, takerTokenAddress, makerFee, takerFee, makerAddress, takerAddress, + makerFillableAmount, takerFillableAmount, feeRecepient, expirationUnixTimestampSec, + ); + } + private async createAsymetricFillableSignedOrderWithFeesAsync( + makerTokenAddress: string, takerTokenAddress: string, + makerFee: BigNumber.BigNumber, takerFee: BigNumber.BigNumber, + makerAddress: string, takerAddress: string, + makerFillableAmount: BigNumber.BigNumber, takerFillableAmount: BigNumber.BigNumber, + feeRecepient: string, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinBase, makerAddress, makerFillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, makerFillableAmount); await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinBase, takerAddress, takerFillableAmount); @@ -35,9 +62,10 @@ export class FillScenarios { const transactionSenderAccount = await this.zeroEx.getTransactionSenderAccountIfExistsAsync(); this.zeroEx.setTransactionSenderAccount(makerAddress); - const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, - takerAddress, makerFillableAmount, makerTokenAddress, takerFillableAmount, takerTokenAddress, - expirationUnixTimestampSec); + const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, + makerAddress, takerAddress, makerFee, takerFee, + makerFillableAmount, makerTokenAddress, takerFillableAmount, takerTokenAddress, + feeRecepient, expirationUnixTimestampSec); this.zeroEx.setTransactionSenderAccount(transactionSenderAccount as string); return signedOrder; } diff --git a/test/utils/order_factory.ts b/test/utils/order_factory.ts index 0f370ed34..373dbddc6 100644 --- a/test/utils/order_factory.ts +++ b/test/utils/order_factory.ts @@ -10,10 +10,13 @@ export const orderFactory = { zeroEx: ZeroEx, maker: string, taker: string, - makerTokenAmount: BigNumber.BigNumber|number, + makerFee: BigNumber.BigNumber, + takerFee: BigNumber.BigNumber, + makerTokenAmount: BigNumber.BigNumber, makerTokenAddress: string, - takerTokenAmount: BigNumber.BigNumber|number, + takerTokenAmount: BigNumber.BigNumber, takerTokenAddress: string, + feeRecipient: string, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { const defaultExpirationUnixTimestampSec = new BigNumber(2524604400); // Close to infinite expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSec) ? @@ -22,14 +25,14 @@ export const orderFactory = { const order = { maker, taker, - makerFee: new BigNumber(0), - takerFee: new BigNumber(0), - makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount, - takerTokenAmount: _.isNumber(takerTokenAmount) ? new BigNumber(takerTokenAmount) : takerTokenAmount, + makerFee, + takerFee, + makerTokenAmount, + takerTokenAmount, makerTokenAddress, takerTokenAddress, salt: ZeroEx.generatePseudoRandomSalt(), - feeRecipient: constants.NULL_ADDRESS, + feeRecipient, expirationUnixTimestampSec, }; const orderHash = await zeroEx.getOrderHashHexAsync(order); -- cgit v1.2.3 From a07a9d8571928dbbe82d2cc0917dafe6b446f701 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 15:24:11 +0200 Subject: Add token utils --- test/utils/token_utils.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/utils/token_utils.ts (limited to 'test') diff --git a/test/utils/token_utils.ts b/test/utils/token_utils.ts new file mode 100644 index 000000000..11d334619 --- /dev/null +++ b/test/utils/token_utils.ts @@ -0,0 +1,23 @@ +import * as _ from 'lodash'; +import {Token, ZeroExError} from '../../src/types'; + +const PROTOCOL_TOKEN_SYMBOL = 'ZRX'; + +export class TokenUtils { + private tokens: Token[]; + constructor(tokens: Token[]) { + this.tokens = tokens; + } + public getProtocolTokenOrThrow(): Token { + const zrxToken = _.find(this.tokens, {symbol: PROTOCOL_TOKEN_SYMBOL}); + if (_.isUndefined(zrxToken)) { + throw new Error(ZeroExError.CONTRACT_NOT_DEPLOYED_ON_NETWORK); + } + return zrxToken; + } + public getNonProtocolTokens(): Token[] { + return _.filter(this.tokens, token => { + return token.symbol !== PROTOCOL_TOKEN_SYMBOL; + }); + } +} -- cgit v1.2.3 From d8587875b82ae2fde6dad1334a586c36cda2bfec Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 15:24:42 +0200 Subject: Add success test for fill with fees --- test/exchange_wrapper_test.ts | 25 +++++++++++++++++++++---- test/utils/fill_scenarios.ts | 13 ++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 7f8bad377..ecb5a408b 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -9,9 +9,9 @@ import promisify = require('es6-promisify'); import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -import {orderFactory} from './utils/order_factory'; import {FillOrderValidationErrs, Token} from '../src/types'; import {FillScenarios} from './utils/fill_scenarios'; +import {TokenUtils} from './utils/token_utils'; chai.use(dirtyChai); chai.use(ChaiBigNumber()); @@ -111,15 +111,19 @@ describe('ExchangeWrapper', () => { let coinBase: string; let makerAddress: string; let takerAddress: string; + let feeRecipient: string; + let zrxTokenAddress: string; const fillTakerAmountInBaseUnits = new BigNumber(5); const shouldCheckTransfer = false; before('fetch tokens', async () => { - [coinBase, makerAddress, takerAddress] = userAddresses; + [coinBase, makerAddress, takerAddress, feeRecipient] = userAddresses; tokens = await zeroEx.tokenRegistry.getTokensAsync(); - const [makerToken, takerToken] = tokens; + const tokenUtils = new TokenUtils(tokens); + const [makerToken, takerToken] = tokenUtils.getNonProtocolTokens(); makerTokenAddress = makerToken.address; takerTokenAddress = takerToken.address; - fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens); + zrxTokenAddress = tokenUtils.getProtocolTokenOrThrow().address; + fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens, zrxTokenAddress); }); afterEach('reset default account', () => { zeroEx.setTransactionSenderAccount(userAddresses[0]); @@ -262,6 +266,19 @@ describe('ExchangeWrapper', () => { expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); }); + it('should fill the valid orders with fees', async () => { + const fillableAmount = new BigNumber(5); + const makerFee = new BigNumber(1); + const takerFee = new BigNumber(2); + const signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( + makerTokenAddress, takerTokenAddress, makerFee, takerFee, + makerAddress, takerAddress, fillableAmount, feeRecipient, + ); + zeroEx.setTransactionSenderAccount(takerAddress); + await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); + expect(await zeroEx.token.getBalanceAsync(zrxTokenAddress, feeRecipient)) + .to.be.bignumber.equal(makerFee.plus(takerFee)); + }); }); }); }); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 31f93618f..706f3f281 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -9,11 +9,13 @@ export class FillScenarios { private userAddresses: string[]; private tokens: Token[]; private coinBase: string; - constructor(zeroEx: ZeroEx, userAddresses: string[], tokens: Token[]) { + private zrxTokenAddress: string; + constructor(zeroEx: ZeroEx, userAddresses: string[], tokens: Token[], zrxTokenAddress: string) { this.zeroEx = zeroEx; this.userAddresses = userAddresses; this.tokens = tokens; this.coinBase = userAddresses[0]; + this.zrxTokenAddress = zrxTokenAddress; } public async createFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, makerAddress: string, takerAddress: string, @@ -60,6 +62,15 @@ export class FillScenarios { await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinBase, takerAddress, takerFillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, takerFillableAmount); + if (!makerFee.isZero()) { + await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinBase, makerAddress, makerFee); + await this.zeroEx.token.setProxyAllowanceAsync(this.zrxTokenAddress, makerAddress, makerFee); + } + if (!takerFee.isZero()) { + await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinBase, takerAddress, takerFee); + await this.zeroEx.token.setProxyAllowanceAsync(this.zrxTokenAddress, takerAddress, takerFee); + } + const transactionSenderAccount = await this.zeroEx.getTransactionSenderAccountIfExistsAsync(); this.zeroEx.setTransactionSenderAccount(makerAddress); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, -- cgit v1.2.3 From c650d1ba204a862de81b225118d9bcd1a38ad25d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 16:18:54 +0200 Subject: Add tests and checks for fees balances and allowances --- test/exchange_wrapper_test.ts | 45 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ecb5a408b..6f4105e1e 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -9,7 +9,7 @@ import promisify = require('es6-promisify'); import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -import {FillOrderValidationErrs, Token} from '../src/types'; +import {FillOrderValidationErrs, SignedOrder, Token} from '../src/types'; import {FillScenarios} from './utils/fill_scenarios'; import {TokenUtils} from './utils/token_utils'; @@ -222,6 +222,49 @@ describe('ExchangeWrapper', () => { signedOrder, fillTakerAmountInBaseUnitsThatCausesRoundingError, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.ROUNDING_ERROR); }); + describe('should raise when not enough balance or allowance to pay fees', () => { + const fillableAmount = new BigNumber(5); + const makerFee = new BigNumber(2); + const takerFee = new BigNumber(2); + let signedOrder: SignedOrder; + beforeEach('setup', async () => { + signedOrder = await fillScenarios.createFillableSignedOrderWithFeesAsync( + makerTokenAddress, takerTokenAddress, makerFee, takerFee, + makerAddress, takerAddress, fillableAmount, feeRecipient, + ); + zeroEx.setTransactionSenderAccount(takerAddress); + }); + it('should throw when maker doesn\'t have enough balance to pay fees', async () => { + const lackingBalance = new BigNumber(1); + await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinBase, lackingBalance); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_FEE_BALANCE); + }); + it('should throw when maker doesn\'t have enough allowance to pay fees', async () => { + const newAllowanceWhichIsLessThanFees = makerFee.minus(1); + await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, makerAddress, + newAllowanceWhichIsLessThanFees); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_FEE_ALLOWANCE); + }); + it('should throw when taker doesn\'t have enough balance to pay fees', async () => { + const lackingBalance = new BigNumber(1); + await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinBase, lackingBalance); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_FEE_BALANCE); + }); + it('should throw when taker doesn\'t have enough allowance to pay fees', async () => { + const newAllowanceWhichIsLessThanFees = makerFee.minus(1); + await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, takerAddress, + newAllowanceWhichIsLessThanFees); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_FEE_ALLOWANCE); + }); + }); }); describe('successful fills', () => { it('should fill the valid order', async () => { -- cgit v1.2.3 From 07c61b1f9b75ec6a4589438ec2dff8ee926cc0d3 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 16:25:05 +0200 Subject: Refactor balance & allowance tests --- test/exchange_wrapper_test.ts | 89 ++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 47 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 6f4105e1e..29cdd925d 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -160,54 +160,49 @@ describe('ExchangeWrapper', () => { signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); - it('should throw when taker balance is less than fill amount', async () => { + describe('should throw when not enough balance or allowance to fulfill the order', () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, - ); - zeroEx.setTransactionSenderAccount(takerAddress); - const moreThanTheBalance = new BigNumber(6); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, moreThanTheBalance, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); - }); - it('should throw when taker allowance is less than fill amount', async () => { - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, - ); - const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(1); - await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, - newAllowanceWhichIsLessThanFillAmount); - zeroEx.setTransactionSenderAccount(takerAddress); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); - }); - it('should throw when maker balance is less than maker fill amount', async () => { - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, - ); - const lackingMakerBalance = new BigNumber(3); - await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinBase, lackingMakerBalance); - zeroEx.setTransactionSenderAccount(takerAddress); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_BALANCE); - }); - it('should throw when maker allowance is less than maker fill amount', async () => { - const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, - ); - const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(1); - await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, - newAllowanceWhichIsLessThanFillAmount); - zeroEx.setTransactionSenderAccount(takerAddress); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_ALLOWANCE); + const lackingBalance = new BigNumber(3); + const lackingAllowance = new BigNumber(3); + let signedOrder: SignedOrder; + beforeEach('create fillable signed order', async () => { + signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + }); + it('should throw when taker balance is less than fill amount', async () => { + + await zeroEx.token.transferAsync(takerTokenAddress, takerAddress, coinBase, lackingBalance); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); + }); + it('should throw when taker allowance is less than fill amount', async () => { + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(lackingAllowance); + await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, + newAllowanceWhichIsLessThanFillAmount); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); + }); + it('should throw when maker balance is less than maker fill amount', async () => { + await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinBase, lackingBalance); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_BALANCE); + }); + it('should throw when maker allowance is less than maker fill amount', async () => { + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(lackingAllowance); + await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, + newAllowanceWhichIsLessThanFillAmount); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_ALLOWANCE); + }); }); it('should throw when there would be a rounding error', async () => { const makerFillableAmount = new BigNumber(3); -- cgit v1.2.3 From 3c71506022a827f37916f981a57e3ad8cc0c5abb Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 16:26:47 +0200 Subject: Rename coinBase to coinbase --- test/exchange_wrapper_test.ts | 12 ++++++------ test/utils/fill_scenarios.ts | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 29cdd925d..a72c11bb6 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -108,7 +108,7 @@ describe('ExchangeWrapper', () => { let makerTokenAddress: string; let takerTokenAddress: string; let fillScenarios: FillScenarios; - let coinBase: string; + let coinbase: string; let makerAddress: string; let takerAddress: string; let feeRecipient: string; @@ -116,7 +116,7 @@ describe('ExchangeWrapper', () => { const fillTakerAmountInBaseUnits = new BigNumber(5); const shouldCheckTransfer = false; before('fetch tokens', async () => { - [coinBase, makerAddress, takerAddress, feeRecipient] = userAddresses; + [coinbase, makerAddress, takerAddress, feeRecipient] = userAddresses; tokens = await zeroEx.tokenRegistry.getTokensAsync(); const tokenUtils = new TokenUtils(tokens); const [makerToken, takerToken] = tokenUtils.getNonProtocolTokens(); @@ -172,7 +172,7 @@ describe('ExchangeWrapper', () => { }); it('should throw when taker balance is less than fill amount', async () => { - await zeroEx.token.transferAsync(takerTokenAddress, takerAddress, coinBase, lackingBalance); + await zeroEx.token.transferAsync(takerTokenAddress, takerAddress, coinbase, lackingBalance); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, @@ -188,7 +188,7 @@ describe('ExchangeWrapper', () => { )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); }); it('should throw when maker balance is less than maker fill amount', async () => { - await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinBase, lackingBalance); + await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinbase, lackingBalance); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, @@ -231,7 +231,7 @@ describe('ExchangeWrapper', () => { }); it('should throw when maker doesn\'t have enough balance to pay fees', async () => { const lackingBalance = new BigNumber(1); - await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinBase, lackingBalance); + await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinbase, lackingBalance); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_FEE_BALANCE); @@ -246,7 +246,7 @@ describe('ExchangeWrapper', () => { }); it('should throw when taker doesn\'t have enough balance to pay fees', async () => { const lackingBalance = new BigNumber(1); - await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinBase, lackingBalance); + await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinbase, lackingBalance); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_FEE_BALANCE); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 706f3f281..8260b76ab 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -8,13 +8,13 @@ export class FillScenarios { private zeroEx: ZeroEx; private userAddresses: string[]; private tokens: Token[]; - private coinBase: string; + private coinbase: string; private zrxTokenAddress: string; constructor(zeroEx: ZeroEx, userAddresses: string[], tokens: Token[], zrxTokenAddress: string) { this.zeroEx = zeroEx; this.userAddresses = userAddresses; this.tokens = tokens; - this.coinBase = userAddresses[0]; + this.coinbase = userAddresses[0]; this.zrxTokenAddress = zrxTokenAddress; } public async createFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, @@ -57,17 +57,17 @@ export class FillScenarios { makerAddress: string, takerAddress: string, makerFillableAmount: BigNumber.BigNumber, takerFillableAmount: BigNumber.BigNumber, feeRecepient: string, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { - await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinBase, makerAddress, makerFillableAmount); + await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinbase, makerAddress, makerFillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, makerFillableAmount); - await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinBase, takerAddress, takerFillableAmount); + await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinbase, takerAddress, takerFillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, takerFillableAmount); if (!makerFee.isZero()) { - await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinBase, makerAddress, makerFee); + await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinbase, makerAddress, makerFee); await this.zeroEx.token.setProxyAllowanceAsync(this.zrxTokenAddress, makerAddress, makerFee); } if (!takerFee.isZero()) { - await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinBase, takerAddress, takerFee); + await this.zeroEx.token.transferAsync(this.zrxTokenAddress, this.coinbase, takerAddress, takerFee); await this.zeroEx.token.setProxyAllowanceAsync(this.zrxTokenAddress, takerAddress, takerFee); } -- cgit v1.2.3 From 827a0d4e91169e338cbdc8042d158aaf7fdcf96c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 17:03:36 +0200 Subject: rename fillTakerAmountInBaseUnits to fillTakerAmount --- test/exchange_wrapper_test.ts | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index a72c11bb6..76601d1b3 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -113,7 +113,7 @@ describe('ExchangeWrapper', () => { let takerAddress: string; let feeRecipient: string; let zrxTokenAddress: string; - const fillTakerAmountInBaseUnits = new BigNumber(5); + const fillTakerAmount = new BigNumber(5); const shouldCheckTransfer = false; before('fetch tokens', async () => { [coinbase, makerAddress, takerAddress, feeRecipient] = userAddresses; @@ -146,7 +146,7 @@ describe('ExchangeWrapper', () => { makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { @@ -157,7 +157,7 @@ describe('ExchangeWrapper', () => { ); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); describe('should throw when not enough balance or allowance to fulfill the order', () => { @@ -175,32 +175,32 @@ describe('ExchangeWrapper', () => { await zeroEx.token.transferAsync(takerTokenAddress, takerAddress, coinbase, lackingBalance); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); }); it('should throw when taker allowance is less than fill amount', async () => { - const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(lackingAllowance); + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, newAllowanceWhichIsLessThanFillAmount); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); }); it('should throw when maker balance is less than maker fill amount', async () => { await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinbase, lackingBalance); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_BALANCE); }); it('should throw when maker allowance is less than maker fill amount', async () => { - const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(lackingAllowance); + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, newAllowanceWhichIsLessThanFillAmount); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_ALLOWANCE); }); }); @@ -211,10 +211,10 @@ describe('ExchangeWrapper', () => { makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, makerFillableAmount, takerFillableAmount, ); - const fillTakerAmountInBaseUnitsThatCausesRoundingError = new BigNumber(3); + const fillTakerAmountThatCausesRoundingError = new BigNumber(3); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnitsThatCausesRoundingError, shouldCheckTransfer, + signedOrder, fillTakerAmountThatCausesRoundingError, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.ROUNDING_ERROR); }); describe('should raise when not enough balance or allowance to pay fees', () => { @@ -233,7 +233,7 @@ describe('ExchangeWrapper', () => { const lackingBalance = new BigNumber(1); await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinbase, lackingBalance); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_FEE_BALANCE); }); it('should throw when maker doesn\'t have enough allowance to pay fees', async () => { @@ -241,14 +241,14 @@ describe('ExchangeWrapper', () => { await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, makerAddress, newAllowanceWhichIsLessThanFees); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_FEE_ALLOWANCE); }); it('should throw when taker doesn\'t have enough balance to pay fees', async () => { const lackingBalance = new BigNumber(1); await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinbase, lackingBalance); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_FEE_BALANCE); }); it('should throw when taker doesn\'t have enough allowance to pay fees', async () => { @@ -256,7 +256,7 @@ describe('ExchangeWrapper', () => { await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, takerAddress, newAllowanceWhichIsLessThanFees); return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_FEE_ALLOWANCE); }); }); @@ -277,15 +277,15 @@ describe('ExchangeWrapper', () => { expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillableAmount); zeroEx.setTransactionSenderAccount(takerAddress); - await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); + await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmount, shouldCheckTransfer); expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) - .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmountInBaseUnits)); + .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount)); expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) - .to.be.bignumber.equal(fillTakerAmountInBaseUnits); + .to.be.bignumber.equal(fillTakerAmount); expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillTakerAmountInBaseUnits); + .to.be.bignumber.equal(fillTakerAmount); expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmountInBaseUnits)); + .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount)); }); it('should partially fill the valid order', async () => { const fillableAmount = new BigNumber(5); @@ -313,7 +313,7 @@ describe('ExchangeWrapper', () => { makerAddress, takerAddress, fillableAmount, feeRecipient, ); zeroEx.setTransactionSenderAccount(takerAddress); - await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); + await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmount, shouldCheckTransfer); expect(await zeroEx.token.getBalanceAsync(zrxTokenAddress, feeRecipient)) .to.be.bignumber.equal(makerFee.plus(takerFee)); }); -- cgit v1.2.3 From e5cc0e0562eccf67eff9cf0521c4884ffc3b0f3b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 17:38:47 +0200 Subject: Rename NOT_A_TAKER to TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 76601d1b3..d8934d409 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -147,7 +147,7 @@ describe('ExchangeWrapper', () => { ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); + )).to.be.rejectedWith(FillOrderValidationErrs.TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER); }); it('should throw when order is expired', async () => { const expirationInPast = new BigNumber(42); -- cgit v1.2.3 From 7fd84e29ab8c27cb872cf8cd0f631d4b78abba0e Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 17:40:06 +0200 Subject: Rename EXPIRED to FILL_ORDER_EXPIRED --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index d8934d409..e83134909 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -158,7 +158,7 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); + )).to.be.rejectedWith(FillOrderValidationErrs.FILL_ORDER_EXPIRED); }); describe('should throw when not enough balance or allowance to fulfill the order', () => { const fillableAmount = new BigNumber(5); -- cgit v1.2.3 From abf2cf4c5e2ea6d09d862871adfa16ca108907e2 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:00:37 +0200 Subject: Move FillOrderValidatinErrs to ExchangeContractErrs --- test/exchange_wrapper_test.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index e83134909..c05c5c26f 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -9,7 +9,7 @@ import promisify = require('es6-promisify'); import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -import {FillOrderValidationErrs, SignedOrder, Token} from '../src/types'; +import {ExchangeContractErrs, SignedOrder, Token} from '../src/types'; import {FillScenarios} from './utils/fill_scenarios'; import {TokenUtils} from './utils/token_utils'; @@ -138,7 +138,7 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, zeroFillAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); + )).to.be.rejectedWith(ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO); }); it('should throw when sender is not a taker', async () => { const fillableAmount = new BigNumber(5); @@ -147,7 +147,7 @@ describe('ExchangeWrapper', () => { ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER); + )).to.be.rejectedWith(ExchangeContractErrs.TRANSACTION_SENDER_IS_NOT_FILL_ORDER_TAKER); }); it('should throw when order is expired', async () => { const expirationInPast = new BigNumber(42); @@ -158,7 +158,7 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.FILL_ORDER_EXPIRED); + )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_EXPIRED); }); describe('should throw when not enough balance or allowance to fulfill the order', () => { const fillableAmount = new BigNumber(5); @@ -176,7 +176,7 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_TAKER_BALANCE); }); it('should throw when taker allowance is less than fill amount', async () => { const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); @@ -185,14 +185,14 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_TAKER_ALLOWANCE); }); it('should throw when maker balance is less than maker fill amount', async () => { await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinbase, lackingBalance); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_MAKER_BALANCE); }); it('should throw when maker allowance is less than maker fill amount', async () => { const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); @@ -201,7 +201,7 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_MAKER_ALLOWANCE); }); }); it('should throw when there would be a rounding error', async () => { @@ -215,7 +215,7 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountThatCausesRoundingError, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.ROUNDING_ERROR); + )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR); }); describe('should raise when not enough balance or allowance to pay fees', () => { const fillableAmount = new BigNumber(5); @@ -234,7 +234,7 @@ describe('ExchangeWrapper', () => { await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinbase, lackingBalance); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_FEE_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_MAKER_FEE_BALANCE); }); it('should throw when maker doesn\'t have enough allowance to pay fees', async () => { const newAllowanceWhichIsLessThanFees = makerFee.minus(1); @@ -242,14 +242,14 @@ describe('ExchangeWrapper', () => { newAllowanceWhichIsLessThanFees); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_FEE_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_MAKER_FEE_ALLOWANCE); }); it('should throw when taker doesn\'t have enough balance to pay fees', async () => { const lackingBalance = new BigNumber(1); await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinbase, lackingBalance); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_FEE_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_TAKER_FEE_BALANCE); }); it('should throw when taker doesn\'t have enough allowance to pay fees', async () => { const newAllowanceWhichIsLessThanFees = makerFee.minus(1); @@ -257,7 +257,7 @@ describe('ExchangeWrapper', () => { newAllowanceWhichIsLessThanFees); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_FEE_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_TAKER_FEE_ALLOWANCE); }); }); }); -- cgit v1.2.3 From 76280dd5dbc5a5a2fb5f7230fa73d594f822c7e6 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:10:06 +0200 Subject: Address feedback --- test/exchange_wrapper_test.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index c05c5c26f..87a6fbb85 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -176,7 +176,7 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_TAKER_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_BALANCE); }); it('should throw when taker allowance is less than fill amount', async () => { const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); @@ -185,14 +185,14 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_TAKER_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_ALLOWANCE); }); it('should throw when maker balance is less than maker fill amount', async () => { await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinbase, lackingBalance); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_MAKER_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_BALANCE); }); it('should throw when maker allowance is less than maker fill amount', async () => { const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); @@ -201,7 +201,7 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_MAKER_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_ALLOWANCE); }); }); it('should throw when there would be a rounding error', async () => { @@ -234,7 +234,7 @@ describe('ExchangeWrapper', () => { await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinbase, lackingBalance); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_MAKER_FEE_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_BALANCE); }); it('should throw when maker doesn\'t have enough allowance to pay fees', async () => { const newAllowanceWhichIsLessThanFees = makerFee.minus(1); @@ -242,14 +242,14 @@ describe('ExchangeWrapper', () => { newAllowanceWhichIsLessThanFees); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_MAKER_FEE_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_ALLOWANCE); }); it('should throw when taker doesn\'t have enough balance to pay fees', async () => { const lackingBalance = new BigNumber(1); await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinbase, lackingBalance); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_TAKER_FEE_BALANCE); + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_BALANCE); }); it('should throw when taker doesn\'t have enough allowance to pay fees', async () => { const newAllowanceWhichIsLessThanFees = makerFee.minus(1); @@ -257,7 +257,7 @@ describe('ExchangeWrapper', () => { newAllowanceWhichIsLessThanFees); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, - )).to.be.rejectedWith(ExchangeContractErrs.NOT_ENOUGH_TAKER_FEE_ALLOWANCE); + )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_ALLOWANCE); }); }); }); -- cgit v1.2.3 From 199843718fba26db72db6196be5036339e0cf9f9 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:13:22 +0200 Subject: Remove spaces --- test/exchange_wrapper_test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 87a6fbb85..ed6f68954 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -115,7 +115,7 @@ describe('ExchangeWrapper', () => { let zrxTokenAddress: string; const fillTakerAmount = new BigNumber(5); const shouldCheckTransfer = false; - before('fetch tokens', async () => { + before(async () => { [coinbase, makerAddress, takerAddress, feeRecipient] = userAddresses; tokens = await zeroEx.tokenRegistry.getTokensAsync(); const tokenUtils = new TokenUtils(tokens); @@ -171,7 +171,6 @@ describe('ExchangeWrapper', () => { ); }); it('should throw when taker balance is less than fill amount', async () => { - await zeroEx.token.transferAsync(takerTokenAddress, takerAddress, coinbase, lackingBalance); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( @@ -267,7 +266,6 @@ describe('ExchangeWrapper', () => { const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); - expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) .to.be.bignumber.equal(fillableAmount); expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) -- cgit v1.2.3 From 16c296be1461a3ccc76fbdf68e78d8c8ccd3ed83 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:14:51 +0200 Subject: Add ZRX_TOKEN_NOT_IN_REGISTRY --- test/utils/token_utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/utils/token_utils.ts b/test/utils/token_utils.ts index 11d334619..3d2faa959 100644 --- a/test/utils/token_utils.ts +++ b/test/utils/token_utils.ts @@ -11,7 +11,7 @@ export class TokenUtils { public getProtocolTokenOrThrow(): Token { const zrxToken = _.find(this.tokens, {symbol: PROTOCOL_TOKEN_SYMBOL}); if (_.isUndefined(zrxToken)) { - throw new Error(ZeroExError.CONTRACT_NOT_DEPLOYED_ON_NETWORK); + throw new Error(ZeroExError.ZRX_NOT_IN_TOKEN_REGISTRY); } return zrxToken; } -- cgit v1.2.3 From a52f834debc1727cee97c28f59e0973ba33bde7c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:16:55 +0200 Subject: Fix test name --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ed6f68954..f445e1202 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -203,7 +203,7 @@ describe('ExchangeWrapper', () => { )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_ALLOWANCE); }); }); - it('should throw when there would be a rounding error', async () => { + it('should throw when there a rounding error would have occurred', async () => { const makerFillableAmount = new BigNumber(3); const takerFillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAsymetricFillableSignedOrderAsync( -- cgit v1.2.3 From 7cf60b589b280c578bcde4b9209e70d07c92c241 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:20:17 +0200 Subject: Address feedback --- test/exchange_wrapper_test.ts | 10 +++++----- test/utils/fill_scenarios.ts | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index f445e1202..842d6de32 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -204,11 +204,11 @@ describe('ExchangeWrapper', () => { }); }); it('should throw when there a rounding error would have occurred', async () => { - const makerFillableAmount = new BigNumber(3); - const takerFillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAsymetricFillableSignedOrderAsync( + const makerAmount = new BigNumber(3); + const takerAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAsymmetricFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, - makerFillableAmount, takerFillableAmount, + makerAmount, takerAmount, ); const fillTakerAmountThatCausesRoundingError = new BigNumber(3); zeroEx.setTransactionSenderAccount(takerAddress); @@ -216,7 +216,7 @@ describe('ExchangeWrapper', () => { signedOrder, fillTakerAmountThatCausesRoundingError, shouldCheckTransfer, )).to.be.rejectedWith(ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR); }); - describe('should raise when not enough balance or allowance to pay fees', () => { + describe('should throw when not enough balance or allowance to pay fees', () => { const fillableAmount = new BigNumber(5); const makerFee = new BigNumber(2); const takerFee = new BigNumber(2); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 8260b76ab..f95b06663 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -22,7 +22,7 @@ export class FillScenarios { fillableAmount: BigNumber.BigNumber, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { - return this.createAsymetricFillableSignedOrderAsync( + return this.createAsymmetricFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, fillableAmount, expirationUnixTimestampSec, ); @@ -34,24 +34,24 @@ export class FillScenarios { fillableAmount: BigNumber.BigNumber, feeRecepient: string, expirationUnixTimestampSec?: BigNumber.BigNumber, ): Promise { - return this.createAsymetricFillableSignedOrderWithFeesAsync( + return this.createAsymmetricFillableSignedOrderWithFeesAsync( makerTokenAddress, takerTokenAddress, makerFee, takerFee, makerAddress, takerAddress, fillableAmount, fillableAmount, feeRecepient, expirationUnixTimestampSec, ); } - public async createAsymetricFillableSignedOrderAsync( + public async createAsymmetricFillableSignedOrderAsync( makerTokenAddress: string, takerTokenAddress: string, makerAddress: string, takerAddress: string, makerFillableAmount: BigNumber.BigNumber, takerFillableAmount: BigNumber.BigNumber, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { const makerFee = new BigNumber(0); const takerFee = new BigNumber(0); const feeRecepient = constants.NULL_ADDRESS; - return this.createAsymetricFillableSignedOrderWithFeesAsync( + return this.createAsymmetricFillableSignedOrderWithFeesAsync( makerTokenAddress, takerTokenAddress, makerFee, takerFee, makerAddress, takerAddress, makerFillableAmount, takerFillableAmount, feeRecepient, expirationUnixTimestampSec, ); } - private async createAsymetricFillableSignedOrderWithFeesAsync( + private async createAsymmetricFillableSignedOrderWithFeesAsync( makerTokenAddress: string, takerTokenAddress: string, makerFee: BigNumber.BigNumber, takerFee: BigNumber.BigNumber, makerAddress: string, takerAddress: string, -- cgit v1.2.3 From cc6b27cbb179de3e5ac124bfddac63f66e16737a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:24:24 +0200 Subject: Address feedback --- test/exchange_wrapper_test.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 842d6de32..0fa2f93e0 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -162,7 +162,7 @@ describe('ExchangeWrapper', () => { }); describe('should throw when not enough balance or allowance to fulfill the order', () => { const fillableAmount = new BigNumber(5); - const lackingBalance = new BigNumber(3); + const balanceToSubtractFromMaker = new BigNumber(3); const lackingAllowance = new BigNumber(3); let signedOrder: SignedOrder; beforeEach('create fillable signed order', async () => { @@ -171,7 +171,7 @@ describe('ExchangeWrapper', () => { ); }); it('should throw when taker balance is less than fill amount', async () => { - await zeroEx.token.transferAsync(takerTokenAddress, takerAddress, coinbase, lackingBalance); + await zeroEx.token.transferAsync(takerTokenAddress, takerAddress, coinbase, balanceToSubtractFromMaker); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, @@ -187,7 +187,7 @@ describe('ExchangeWrapper', () => { )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_ALLOWANCE); }); it('should throw when maker balance is less than maker fill amount', async () => { - await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinbase, lackingBalance); + await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, @@ -229,8 +229,8 @@ describe('ExchangeWrapper', () => { zeroEx.setTransactionSenderAccount(takerAddress); }); it('should throw when maker doesn\'t have enough balance to pay fees', async () => { - const lackingBalance = new BigNumber(1); - await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinbase, lackingBalance); + const balanceToSubtractFromMaker = new BigNumber(1); + await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_BALANCE); @@ -244,8 +244,8 @@ describe('ExchangeWrapper', () => { )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_ALLOWANCE); }); it('should throw when taker doesn\'t have enough balance to pay fees', async () => { - const lackingBalance = new BigNumber(1); - await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinbase, lackingBalance); + const balanceToSubtractFromTaker = new BigNumber(1); + await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinbase, balanceToSubtractFromTaker); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_BALANCE); @@ -261,7 +261,7 @@ describe('ExchangeWrapper', () => { }); }); describe('successful fills', () => { - it('should fill the valid order', async () => { + it('should fill a valid order', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, -- cgit v1.2.3 From 3551f0791408a5c91f357130740cb81f72b59f9f Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 18:34:18 +0200 Subject: Fix ordering --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 76601d1b3..92b6e5c35 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -23,9 +23,9 @@ describe('ExchangeWrapper', () => { let userAddresses: string[]; let web3: Web3; before(async () => { - web3 = web3Factory.create(); zeroEx = new ZeroEx(web3); userAddresses = await promisify(web3.eth.getAccounts)(); + web3 = web3Factory.create(); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); -- cgit v1.2.3 From 9620b18fecb59b5ce4c2fec483e426f79852c034 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:34:43 +0200 Subject: Address feedback --- test/utils/fill_scenarios.ts | 5 +++-- test/utils/token_utils.ts | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index f95b06663..a44d6b18a 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -71,13 +71,14 @@ export class FillScenarios { await this.zeroEx.token.setProxyAllowanceAsync(this.zrxTokenAddress, takerAddress, takerFee); } - const transactionSenderAccount = await this.zeroEx.getTransactionSenderAccountIfExistsAsync(); + const prevTransactionSenderAccount = await this.zeroEx.getTransactionSenderAccountIfExistsAsync(); this.zeroEx.setTransactionSenderAccount(makerAddress); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, takerAddress, makerFee, takerFee, makerFillableAmount, makerTokenAddress, takerFillableAmount, takerTokenAddress, feeRecepient, expirationUnixTimestampSec); - this.zeroEx.setTransactionSenderAccount(transactionSenderAccount as string); + // We re-set the transactionSender to avoid introducing side-effects + this.zeroEx.setTransactionSenderAccount(prevTransactionSenderAccount as string); return signedOrder; } } diff --git a/test/utils/token_utils.ts b/test/utils/token_utils.ts index 3d2faa959..14788b299 100644 --- a/test/utils/token_utils.ts +++ b/test/utils/token_utils.ts @@ -16,8 +16,9 @@ export class TokenUtils { return zrxToken; } public getNonProtocolTokens(): Token[] { - return _.filter(this.tokens, token => { + const nonProtocolTokens = _.filter(this.tokens, token => { return token.symbol !== PROTOCOL_TOKEN_SYMBOL; }); + return nonProtocolTokens; } } -- cgit v1.2.3 From 45c4f203dff813ba25c04a3c49f0b7e3ad0f2494 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 2 Jun 2017 18:37:38 +0200 Subject: Fix linter errors --- test/exchange_wrapper_test.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 7b4730f3a..3935ebc89 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -171,7 +171,9 @@ describe('ExchangeWrapper', () => { ); }); it('should throw when taker balance is less than fill amount', async () => { - await zeroEx.token.transferAsync(takerTokenAddress, takerAddress, coinbase, balanceToSubtractFromMaker); + await zeroEx.token.transferAsync( + takerTokenAddress, takerAddress, coinbase, balanceToSubtractFromMaker, + ); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, @@ -187,7 +189,9 @@ describe('ExchangeWrapper', () => { )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_ALLOWANCE); }); it('should throw when maker balance is less than maker fill amount', async () => { - await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker); + await zeroEx.token.transferAsync( + makerTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, + ); zeroEx.setTransactionSenderAccount(takerAddress); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, @@ -230,7 +234,9 @@ describe('ExchangeWrapper', () => { }); it('should throw when maker doesn\'t have enough balance to pay fees', async () => { const balanceToSubtractFromMaker = new BigNumber(1); - await zeroEx.token.transferAsync(zrxTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker); + await zeroEx.token.transferAsync( + zrxTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, + ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_BALANCE); @@ -245,7 +251,9 @@ describe('ExchangeWrapper', () => { }); it('should throw when taker doesn\'t have enough balance to pay fees', async () => { const balanceToSubtractFromTaker = new BigNumber(1); - await zeroEx.token.transferAsync(zrxTokenAddress, takerAddress, coinbase, balanceToSubtractFromTaker); + await zeroEx.token.transferAsync( + zrxTokenAddress, takerAddress, coinbase, balanceToSubtractFromTaker, + ); return expect(zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmount, shouldCheckTransfer, )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_TAKER_FEE_BALANCE); -- cgit v1.2.3 From 3fad55d118b6a2f8f44ba5dec7fdae276c806eb3 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 18:46:47 +0200 Subject: Fix undefined web3 issue --- test/exchange_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 3935ebc89..6327ef004 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -19,13 +19,13 @@ const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(); describe('ExchangeWrapper', () => { + let web3: Web3; let zeroEx: ZeroEx; let userAddresses: string[]; - let web3: Web3; before(async () => { + web3 = web3Factory.create(); zeroEx = new ZeroEx(web3); userAddresses = await promisify(web3.eth.getAccounts)(); - web3 = web3Factory.create(); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); -- cgit v1.2.3 From d4320fec724c5bf34e7dcd006cba8ffe7a3c76d1 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 19:38:28 +0200 Subject: Add includeStack config to chai setup so that we get stackTraces --- test/0x.js_test.ts | 1 + test/exchange_wrapper_test.ts | 1 + test/token_registry_wrapper_test.ts | 1 + test/token_wrapper_test.ts | 1 + 4 files changed, 4 insertions(+) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index efc703ea1..9196d3df3 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -10,6 +10,7 @@ import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; import {Order} from '../src/types'; +chai.config.includeStack = true; chai.use(ChaiBigNumber()); chai.use(dirtyChai); const expect = chai.expect; diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 6327ef004..607cdf052 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -13,6 +13,7 @@ import {ExchangeContractErrs, SignedOrder, Token} from '../src/types'; import {FillScenarios} from './utils/fill_scenarios'; import {TokenUtils} from './utils/token_utils'; +chai.config.includeStack = true; chai.use(dirtyChai); chai.use(ChaiBigNumber()); const expect = chai.expect; diff --git a/test/token_registry_wrapper_test.ts b/test/token_registry_wrapper_test.ts index c91555d8b..195b2f205 100644 --- a/test/token_registry_wrapper_test.ts +++ b/test/token_registry_wrapper_test.ts @@ -10,6 +10,7 @@ import {Token} from '../src/types'; import {SchemaValidator} from '../src/utils/schema_validator'; import {tokenSchema} from '../src/schemas/token_schema'; +chai.config.includeStack = true; const expect = chai.expect; chai.use(chaiAsPromised); const blockchainLifecycle = new BlockchainLifecycle(); diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index 4af07a8b2..cfd87da84 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -8,6 +8,7 @@ import {ZeroEx} from '../src/0x.js'; import {ZeroExError, Token} from '../src/types'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; +chai.config.includeStack = true; const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(); -- cgit v1.2.3