aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-watcher
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-06-08 00:03:50 +0800
committerFabio Berger <me@fabioberger.com>2018-06-08 00:03:50 +0800
commit73cc2a140c778a5396c2766562a3fc648337e18a (patch)
treee949314b9476232966750172f13790d4f1167291 /packages/order-watcher
parent6058a74da54e916e970912d942954d55922cef95 (diff)
downloaddexon-0x-contracts-73cc2a140c778a5396c2766562a3fc648337e18a.tar
dexon-0x-contracts-73cc2a140c778a5396c2766562a3fc648337e18a.tar.gz
dexon-0x-contracts-73cc2a140c778a5396c2766562a3fc648337e18a.tar.bz2
dexon-0x-contracts-73cc2a140c778a5396c2766562a3fc648337e18a.tar.lz
dexon-0x-contracts-73cc2a140c778a5396c2766562a3fc648337e18a.tar.xz
dexon-0x-contracts-73cc2a140c778a5396c2766562a3fc648337e18a.tar.zst
dexon-0x-contracts-73cc2a140c778a5396c2766562a3fc648337e18a.zip
- Refactor remainingFillableCalculator so it can be used for the maker and taker side
- Moved the tests over from order-watcher - Did some token -> asset renaming
Diffstat (limited to 'packages/order-watcher')
-rw-r--r--packages/order-watcher/test/remaining_fillable_calculator_test.ts235
1 files changed, 0 insertions, 235 deletions
diff --git a/packages/order-watcher/test/remaining_fillable_calculator_test.ts b/packages/order-watcher/test/remaining_fillable_calculator_test.ts
deleted file mode 100644
index ea116175b..000000000
--- a/packages/order-watcher/test/remaining_fillable_calculator_test.ts
+++ /dev/null
@@ -1,235 +0,0 @@
-import { ECSignature, SignedOrder } from '@0xproject/types';
-import { BigNumber } from '@0xproject/utils';
-import { Web3Wrapper } from '@0xproject/web3-wrapper';
-import * as chai from 'chai';
-import 'make-promises-safe';
-import 'mocha';
-
-import { RemainingFillableCalculator } from '@0xproject/order-utils';
-
-import { chaiSetup } from './utils/chai_setup';
-
-chaiSetup.configure();
-const expect = chai.expect;
-
-describe('RemainingFillableCalculator', () => {
- let calculator: RemainingFillableCalculator;
- let signedOrder: SignedOrder;
- let transferrableMakerTokenAmount: BigNumber;
- let transferrableMakerFeeTokenAmount: BigNumber;
- let remainingMakerTokenAmount: BigNumber;
- let makerAmount: BigNumber;
- let takerAmount: BigNumber;
- let makerFeeAmount: BigNumber;
- let isMakerTokenZRX: boolean;
- const makerToken: string = '0x1';
- const takerToken: string = '0x2';
- const decimals: number = 4;
- const zero: BigNumber = new BigNumber(0);
- const zeroAddress = '0x0';
- const signature: ECSignature = { v: 27, r: '', s: '' };
- beforeEach(async () => {
- [makerAmount, takerAmount, makerFeeAmount] = [
- Web3Wrapper.toBaseUnitAmount(new BigNumber(50), decimals),
- Web3Wrapper.toBaseUnitAmount(new BigNumber(5), decimals),
- Web3Wrapper.toBaseUnitAmount(new BigNumber(1), decimals),
- ];
- [transferrableMakerTokenAmount, transferrableMakerFeeTokenAmount] = [
- Web3Wrapper.toBaseUnitAmount(new BigNumber(50), decimals),
- Web3Wrapper.toBaseUnitAmount(new BigNumber(5), decimals),
- ];
- });
- function buildSignedOrder(): SignedOrder {
- return {
- ecSignature: signature,
- exchangeContractAddress: zeroAddress,
- feeRecipient: zeroAddress,
- maker: zeroAddress,
- taker: zeroAddress,
- makerFee: makerFeeAmount,
- takerFee: zero,
- makerTokenAmount: makerAmount,
- takerTokenAmount: takerAmount,
- makerTokenAddress: makerToken,
- takerTokenAddress: takerToken,
- salt: zero,
- expirationUnixTimestampSec: zero,
- };
- }
- describe('Maker token is NOT ZRX', () => {
- before(async () => {
- isMakerTokenZRX = false;
- });
- it('calculates the correct amount when unfilled and funds available', () => {
- signedOrder = buildSignedOrder();
- remainingMakerTokenAmount = signedOrder.makerTokenAmount;
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount);
- });
- it('calculates the correct amount when partially filled and funds available', () => {
- signedOrder = buildSignedOrder();
- remainingMakerTokenAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(1), decimals);
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount);
- });
- it('calculates the amount to be 0 when all fee funds are transferred', () => {
- signedOrder = buildSignedOrder();
- transferrableMakerFeeTokenAmount = zero;
- remainingMakerTokenAmount = signedOrder.makerTokenAmount;
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(zero);
- });
- it('calculates the correct amount when balance is less than remaining fillable', () => {
- signedOrder = buildSignedOrder();
- const partiallyFilledAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(2), decimals);
- remainingMakerTokenAmount = signedOrder.makerTokenAmount.minus(partiallyFilledAmount);
- transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(partiallyFilledAmount);
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(transferrableMakerTokenAmount);
- });
- describe('Order to Fee Ratio is < 1', () => {
- beforeEach(async () => {
- [makerAmount, takerAmount, makerFeeAmount] = [
- Web3Wrapper.toBaseUnitAmount(new BigNumber(3), decimals),
- Web3Wrapper.toBaseUnitAmount(new BigNumber(6), decimals),
- Web3Wrapper.toBaseUnitAmount(new BigNumber(6), decimals),
- ];
- });
- it('calculates the correct amount when funds unavailable', () => {
- signedOrder = buildSignedOrder();
- remainingMakerTokenAmount = signedOrder.makerTokenAmount;
- const transferredAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(2), decimals);
- transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(transferredAmount);
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(transferrableMakerTokenAmount);
- });
- });
- describe('Ratio is not evenly divisble', () => {
- beforeEach(async () => {
- [makerAmount, takerAmount, makerFeeAmount] = [
- Web3Wrapper.toBaseUnitAmount(new BigNumber(3), decimals),
- Web3Wrapper.toBaseUnitAmount(new BigNumber(7), decimals),
- Web3Wrapper.toBaseUnitAmount(new BigNumber(7), decimals),
- ];
- });
- it('calculates the correct amount when funds unavailable', () => {
- signedOrder = buildSignedOrder();
- remainingMakerTokenAmount = signedOrder.makerTokenAmount;
- const transferredAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(2), decimals);
- transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(transferredAmount);
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- const calculatedFillableAmount = calculator.computeRemainingMakerFillable();
- expect(calculatedFillableAmount.lessThanOrEqualTo(transferrableMakerTokenAmount)).to.be.true();
- expect(calculatedFillableAmount).to.be.bignumber.greaterThan(new BigNumber(0));
- const orderToFeeRatio = signedOrder.makerTokenAmount.dividedBy(signedOrder.makerFee);
- const calculatedFeeAmount = calculatedFillableAmount.dividedBy(orderToFeeRatio);
- expect(calculatedFeeAmount).to.be.bignumber.lessThan(transferrableMakerFeeTokenAmount);
- });
- });
- });
- describe('Maker Token is ZRX', () => {
- before(async () => {
- isMakerTokenZRX = true;
- });
- it('calculates the correct amount when unfilled and funds available', () => {
- signedOrder = buildSignedOrder();
- transferrableMakerTokenAmount = makerAmount.plus(makerFeeAmount);
- transferrableMakerFeeTokenAmount = transferrableMakerTokenAmount;
- remainingMakerTokenAmount = signedOrder.makerTokenAmount;
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount);
- });
- it('calculates the correct amount when partially filled and funds available', () => {
- signedOrder = buildSignedOrder();
- remainingMakerTokenAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(1), decimals);
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(remainingMakerTokenAmount);
- });
- it('calculates the amount to be 0 when all fee funds are transferred', () => {
- signedOrder = buildSignedOrder();
- transferrableMakerTokenAmount = zero;
- transferrableMakerFeeTokenAmount = zero;
- remainingMakerTokenAmount = signedOrder.makerTokenAmount;
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- expect(calculator.computeRemainingMakerFillable()).to.be.bignumber.equal(zero);
- });
- it('calculates the correct amount when balance is less than remaining fillable', () => {
- signedOrder = buildSignedOrder();
- const partiallyFilledAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(2), decimals);
- remainingMakerTokenAmount = signedOrder.makerTokenAmount.minus(partiallyFilledAmount);
- transferrableMakerTokenAmount = remainingMakerTokenAmount.minus(partiallyFilledAmount);
- transferrableMakerFeeTokenAmount = transferrableMakerTokenAmount;
-
- const orderToFeeRatio = signedOrder.makerTokenAmount.dividedToIntegerBy(signedOrder.makerFee);
- const expectedFillableAmount = new BigNumber(450980);
- calculator = new RemainingFillableCalculator(
- signedOrder,
- isMakerTokenZRX,
- transferrableMakerTokenAmount,
- transferrableMakerFeeTokenAmount,
- remainingMakerTokenAmount,
- );
- const calculatedFillableAmount = calculator.computeRemainingMakerFillable();
- const numberOfFillsInRatio = calculatedFillableAmount.dividedToIntegerBy(orderToFeeRatio);
- const calculatedFillableAmountPlusFees = calculatedFillableAmount.plus(numberOfFillsInRatio);
- expect(calculatedFillableAmountPlusFees).to.be.bignumber.lessThan(transferrableMakerTokenAmount);
- expect(calculatedFillableAmountPlusFees).to.be.bignumber.lessThan(remainingMakerTokenAmount);
- expect(calculatedFillableAmount).to.be.bignumber.equal(expectedFillableAmount);
- expect(numberOfFillsInRatio.decimalPlaces()).to.be.equal(0);
- });
- });
-});