Add additional order factory methods and refactor test to use them

This commit is contained in:
Brandon Millman 2018-10-03 23:14:55 -07:00
parent 250a9a4809
commit 059162a90a
2 changed files with 62 additions and 62 deletions

View File

@ -27,74 +27,37 @@ describe('buyQuoteCalculator', () => {
// the second order has a rate of 2 makerAsset / WETH with a takerFee of 100 ZRX and has 200 / 200 makerAsset units left to fill (completely fillable) // the second order has a rate of 2 makerAsset / WETH with a takerFee of 100 ZRX and has 200 / 200 makerAsset units left to fill (completely fillable)
// generate one order for fees // generate one order for fees
// the fee order has a rate of 1 ZRX / WETH with no taker fee and has 100 ZRX left to fill (completely fillable) // the fee order has a rate of 1 ZRX / WETH with no taker fee and has 100 ZRX left to fill (completely fillable)
const firstOrder = orderFactory.createOrder( const firstOrder = orderFactory.createSignedOrderFromPartial({
NULL_ADDRESS, makerAssetAmount: new BigNumber(400),
new BigNumber(400), takerAssetAmount: new BigNumber(100),
NULL_BYTES,
new BigNumber(100),
NULL_BYTES,
NULL_ADDRESS,
{
takerFee: new BigNumber(200), takerFee: new BigNumber(200),
},
);
const firstRemainingFillAmount = new BigNumber(200);
const secondOrder = orderFactory.createOrder(
NULL_ADDRESS,
new BigNumber(200),
NULL_BYTES,
new BigNumber(100),
NULL_BYTES,
NULL_ADDRESS,
{
takerFee: new BigNumber(100),
},
);
const secondRemainingFillAmount = secondOrder.makerAssetAmount;
const signedOrders = _.map([firstOrder, secondOrder], order => {
return {
...order,
signature: NULL_BYTES,
};
}); });
const firstRemainingFillAmount = new BigNumber(200);
const secondOrder = orderFactory.createSignedOrderFromPartial({
makerAssetAmount: new BigNumber(200),
takerAssetAmount: new BigNumber(100),
takerFee: new BigNumber(100),
});
const secondRemainingFillAmount = secondOrder.makerAssetAmount;
ordersAndFillableAmounts = { ordersAndFillableAmounts = {
orders: signedOrders, orders: [firstOrder, secondOrder],
remainingFillableMakerAssetAmounts: [firstRemainingFillAmount, secondRemainingFillAmount], remainingFillableMakerAssetAmounts: [firstRemainingFillAmount, secondRemainingFillAmount],
}; };
const smallFeeOrder = orderFactory.createOrder( const smallFeeOrder = orderFactory.createSignedOrderFromPartial({
NULL_ADDRESS, makerAssetAmount: new BigNumber(100),
new BigNumber(100), takerAssetAmount: new BigNumber(100),
NULL_BYTES, });
new BigNumber(100),
NULL_BYTES,
NULL_ADDRESS,
);
const signedSmallFeeOrder = {
...smallFeeOrder,
signature: NULL_BYTES,
};
smallFeeOrderAndFillableAmount = { smallFeeOrderAndFillableAmount = {
orders: [signedSmallFeeOrder], orders: [smallFeeOrder],
remainingFillableMakerAssetAmounts: [signedSmallFeeOrder.makerAssetAmount], remainingFillableMakerAssetAmounts: [smallFeeOrder.makerAssetAmount],
};
const largeFeeOrder = orderFactory.createOrder(
NULL_ADDRESS,
new BigNumber(100),
NULL_BYTES,
new BigNumber(200),
NULL_BYTES,
NULL_ADDRESS,
);
const signedLargeFeeOrder = {
...largeFeeOrder,
signature: NULL_BYTES,
}; };
const largeFeeOrder = orderFactory.createSignedOrderFromPartial({
makerAssetAmount: new BigNumber(100),
takerAssetAmount: new BigNumber(200),
});
allFeeOrdersAndFillableAmounts = { allFeeOrdersAndFillableAmounts = {
orders: [signedSmallFeeOrder, signedLargeFeeOrder], orders: [smallFeeOrder, largeFeeOrder],
remainingFillableMakerAssetAmounts: [ remainingFillableMakerAssetAmounts: [smallFeeOrder.makerAssetAmount, largeFeeOrder.makerAssetAmount],
signedSmallFeeOrder.makerAssetAmount,
largeFeeOrder.makerAssetAmount,
],
}; };
}); });
it('should throw if not enough maker asset liquidity', () => { it('should throw if not enough maker asset liquidity', () => {

View File

@ -8,8 +8,21 @@ import { orderHashUtils } from './order_hash';
import { generatePseudoRandomSalt } from './salt'; import { generatePseudoRandomSalt } from './salt';
import { signatureUtils } from './signature_utils'; import { signatureUtils } from './signature_utils';
import { CreateOrderOpts } from './types'; import { CreateOrderOpts } from './types';
export const orderFactory = { export const orderFactory = {
createOrderFromPartial(partialOrder: Partial<Order>): Order {
const defaultOrder = generateEmptyOrder();
return {
...defaultOrder,
...partialOrder,
};
},
createSignedOrderFromPartial(partialSignedOrder: Partial<SignedOrder>): SignedOrder {
const defaultOrder = generateEmptySignedOrder();
return {
...defaultOrder,
...partialSignedOrder,
};
},
createOrder( createOrder(
makerAddress: string, makerAddress: string,
makerAssetAmount: BigNumber, makerAssetAmount: BigNumber,
@ -69,6 +82,30 @@ export const orderFactory = {
}, },
}; };
function generateEmptySignedOrder(): SignedOrder {
return {
...generateEmptyOrder(),
signature: constants.NULL_BYTES,
};
}
function generateEmptyOrder(): Order {
return {
senderAddress: constants.NULL_ADDRESS,
makerAddress: constants.NULL_ADDRESS,
takerAddress: constants.NULL_ADDRESS,
makerFee: constants.ZERO_AMOUNT,
takerFee: constants.ZERO_AMOUNT,
makerAssetAmount: constants.ZERO_AMOUNT,
takerAssetAmount: constants.ZERO_AMOUNT,
makerAssetData: constants.NULL_BYTES,
takerAssetData: constants.NULL_BYTES,
salt: generatePseudoRandomSalt(),
exchangeAddress: constants.NULL_ADDRESS,
feeRecipientAddress: constants.NULL_ADDRESS,
expirationTimeSeconds: constants.INFINITE_TIMESTAMP_SEC,
};
}
function generateDefaultCreateOrderOpts(): { function generateDefaultCreateOrderOpts(): {
takerAddress: string; takerAddress: string;
senderAddress: string; senderAddress: string;