aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/test
diff options
context:
space:
mode:
authorBrandon Millman <brandon@0xproject.com>2018-08-15 04:45:04 +0800
committerGitHub <noreply@github.com>2018-08-15 04:45:04 +0800
commit3afe405bbe92f9c549a59f26b9c82654f0e304c4 (patch)
tree5d2b70b635244b8deac4e34cbfabf97c3808b7cc /packages/order-utils/test
parent7eff195d61072007e64ef934e1411f8100656be8 (diff)
parent99b744ba52f538752bb0966e6d8b50d9f5a2f032 (diff)
downloaddexon-0x-contracts-3afe405bbe92f9c549a59f26b9c82654f0e304c4.tar.gz
dexon-0x-contracts-3afe405bbe92f9c549a59f26b9c82654f0e304c4.tar.zst
dexon-0x-contracts-3afe405bbe92f9c549a59f26b9c82654f0e304c4.zip
Merge pull request #953 from 0xProject/feature/order-utils/order-sorting-utils
[order-utils] Add rate and sorting utilities
Diffstat (limited to 'packages/order-utils/test')
-rw-r--r--packages/order-utils/test/rate_utils_test.ts55
-rw-r--r--packages/order-utils/test/sorting_utils_test.ts67
2 files changed, 122 insertions, 0 deletions
diff --git a/packages/order-utils/test/rate_utils_test.ts b/packages/order-utils/test/rate_utils_test.ts
new file mode 100644
index 000000000..2e299c209
--- /dev/null
+++ b/packages/order-utils/test/rate_utils_test.ts
@@ -0,0 +1,55 @@
+import { BigNumber } from '@0xproject/utils';
+import * as chai from 'chai';
+import 'mocha';
+
+import { rateUtils } from '../src';
+
+import { chaiSetup } from './utils/chai_setup';
+import { testOrderFactory } from './utils/test_order_factory';
+
+chaiSetup.configure();
+const expect = chai.expect;
+
+describe('rateUtils', () => {
+ const testOrder = testOrderFactory.generateTestSignedOrder({
+ makerAssetAmount: new BigNumber(100),
+ takerAssetAmount: new BigNumber(100),
+ takerFee: new BigNumber(20),
+ });
+ describe('#getFeeAdjustedRateOfOrder', () => {
+ it('throws when feeRate is less than zero', async () => {
+ const feeRate = new BigNumber(-1);
+ expect(() => rateUtils.getFeeAdjustedRateOfOrder(testOrder, feeRate)).to.throw(
+ 'Expected feeRate: -1 to be greater than or equal to 0',
+ );
+ });
+ it('correctly calculates fee adjusted rate when feeRate is provided', async () => {
+ const feeRate = new BigNumber(2); // ZRX costs 2 units of takerAsset per 1 unit of ZRX
+ const feeAdjustedRate = rateUtils.getFeeAdjustedRateOfOrder(testOrder, feeRate);
+ // the order actually takes 100 + (2 * 20) takerAsset units to fill 100 units of makerAsset
+ expect(feeAdjustedRate).to.bignumber.equal(new BigNumber(1.4));
+ });
+ it('correctly calculates fee adjusted rate when no feeRate is provided', async () => {
+ const feeAdjustedRate = rateUtils.getFeeAdjustedRateOfOrder(testOrder);
+ // because no feeRate was provided we just assume 0 fees
+ // the order actually takes 100 takerAsset units to fill 100 units of makerAsset
+ expect(feeAdjustedRate).to.bignumber.equal(new BigNumber(1));
+ });
+ });
+ describe('#getFeeAdjustedRateOfFeeOrder', () => {
+ it('throws when takerFee exceeds makerAssetAmount', async () => {
+ const badOrder = testOrderFactory.generateTestSignedOrder({
+ makerAssetAmount: new BigNumber(100),
+ takerFee: new BigNumber(101),
+ });
+ expect(() => rateUtils.getFeeAdjustedRateOfFeeOrder(badOrder)).to.throw(
+ 'Expected takerFee: "101" to be less than makerAssetAmount: "100"',
+ );
+ });
+ it('correctly calculates fee adjusted rate', async () => {
+ const feeAdjustedRate = rateUtils.getFeeAdjustedRateOfFeeOrder(testOrder);
+ // the order actually takes 100 takerAsset units to fill (100 - 20) units of makerAsset
+ expect(feeAdjustedRate).to.bignumber.equal(new BigNumber(1.25));
+ });
+ });
+});
diff --git a/packages/order-utils/test/sorting_utils_test.ts b/packages/order-utils/test/sorting_utils_test.ts
new file mode 100644
index 000000000..016432505
--- /dev/null
+++ b/packages/order-utils/test/sorting_utils_test.ts
@@ -0,0 +1,67 @@
+import { BigNumber } from '@0xproject/utils';
+import * as chai from 'chai';
+import 'mocha';
+
+import { sortingUtils } from '../src';
+
+import { chaiSetup } from './utils/chai_setup';
+import { testOrderFactory } from './utils/test_order_factory';
+
+chaiSetup.configure();
+const expect = chai.expect;
+
+describe('sortingUtils', () => {
+ describe('#sortOrdersByFeeAdjustedRate', () => {
+ const feeRate = new BigNumber(1); // ZRX costs 1 unit of takerAsset per 1 unit of ZRX
+ // rate: 2 takerAsset / makerAsset
+ const testOrder1 = testOrderFactory.generateTestSignedOrder({
+ makerAssetAmount: new BigNumber(100),
+ takerAssetAmount: new BigNumber(200),
+ });
+ // rate: 1 takerAsset / makerAsset
+ const testOrder2 = testOrderFactory.generateTestSignedOrder({
+ makerAssetAmount: new BigNumber(100),
+ takerAssetAmount: new BigNumber(100),
+ });
+ // rate: 2.5 takerAsset / makerAsset
+ const testOrder3 = testOrderFactory.generateTestSignedOrder({
+ makerAssetAmount: new BigNumber(100),
+ takerAssetAmount: new BigNumber(200),
+ takerFee: new BigNumber(50),
+ });
+ it('correctly sorts by fee adjusted rate when feeRate is Provided', async () => {
+ const orders = [testOrder1, testOrder2, testOrder3];
+ const sortedOrders = sortingUtils.sortOrdersByFeeAdjustedRate(orders, feeRate);
+ expect(sortedOrders).to.deep.equal([testOrder2, testOrder1, testOrder3]);
+ });
+ it('correctly sorts by fee adjusted rate when no feeRate is Provided', async () => {
+ const orders = [testOrder1, testOrder2, testOrder3];
+ const sortedOrders = sortingUtils.sortOrdersByFeeAdjustedRate(orders);
+ expect(sortedOrders).to.deep.equal([testOrder2, testOrder1, testOrder3]);
+ });
+ });
+ describe('#sortFeeOrdersByFeeAdjustedRate', () => {
+ // rate: 200 takerAsset / makerAsset
+ const testOrder1 = testOrderFactory.generateTestSignedOrder({
+ makerAssetAmount: new BigNumber(100),
+ takerAssetAmount: new BigNumber(200),
+ takerFee: new BigNumber(99),
+ });
+ // rate: 1 takerAsset / makerAsset
+ const testOrder2 = testOrderFactory.generateTestSignedOrder({
+ makerAssetAmount: new BigNumber(100),
+ takerAssetAmount: new BigNumber(100),
+ });
+ // rate: 4 takerAsset / makerAsset
+ const testOrder3 = testOrderFactory.generateTestSignedOrder({
+ makerAssetAmount: new BigNumber(100),
+ takerAssetAmount: new BigNumber(200),
+ takerFee: new BigNumber(50),
+ });
+ it('correctly sorts by fee adjusted rate', async () => {
+ const orders = [testOrder1, testOrder2, testOrder3];
+ const sortedOrders = sortingUtils.sortFeeOrdersByFeeAdjustedRate(orders);
+ expect(sortedOrders).to.deep.equal([testOrder2, testOrder3, testOrder1]);
+ });
+ });
+});