aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/test/rate_utils_test.ts
blob: 2e299c209bce5674427452080070745c97cec979 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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));
        });
    });
});