added optimization utils
This commit is contained in:
@@ -77,6 +77,8 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
|
||||
const signatures = _.map(orders, o => o.signature);
|
||||
|
||||
const optimizedOrders = swapQuoteConsumerUtils.optimizeOrdersForMarketExchangeOperation(orders, quote.type);
|
||||
|
||||
let params: ExchangeSmartContractParams;
|
||||
let methodName: string;
|
||||
|
||||
@@ -84,7 +86,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
const { makerAssetFillAmount } = quote;
|
||||
|
||||
params = {
|
||||
orders,
|
||||
orders: optimizedOrders,
|
||||
signatures,
|
||||
makerAssetFillAmount,
|
||||
type: MarketOperation.Buy,
|
||||
@@ -95,7 +97,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
const { takerAssetFillAmount } = quote;
|
||||
|
||||
params = {
|
||||
orders,
|
||||
orders: optimizedOrders,
|
||||
signatures,
|
||||
takerAssetFillAmount,
|
||||
type: MarketOperation.Sell,
|
||||
|
@@ -12,6 +12,7 @@ export const assert = {
|
||||
sharedAssert.isHexString(`${variableName}.makerAssetData`, swapQuote.makerAssetData);
|
||||
sharedAssert.doesConformToSchema(`${variableName}.orders`, swapQuote.orders, schemas.signedOrdersSchema);
|
||||
sharedAssert.doesConformToSchema(`${variableName}.feeOrders`, swapQuote.feeOrders, schemas.signedOrdersSchema);
|
||||
assert.isValidOrdersForSwapQuote(`${variableName}.orders`, swapQuote.orders, swapQuote.makerAssetData, swapQuote.takerAssetData);
|
||||
assert.isValidSwapQuoteInfo(`${variableName}.bestCaseQuoteInfo`, swapQuote.bestCaseQuoteInfo);
|
||||
assert.isValidSwapQuoteInfo(`${variableName}.worstCaseQuoteInfo`, swapQuote.worstCaseQuoteInfo);
|
||||
if (swapQuote.type === MarketOperation.Buy) {
|
||||
@@ -20,6 +21,12 @@ export const assert = {
|
||||
sharedAssert.isBigNumber(`${variableName}.takerAssetFillAmount`, swapQuote.takerAssetFillAmount);
|
||||
}
|
||||
},
|
||||
isValidOrdersForSwapQuote(variableName: string, orders: SignedOrder[], makerAssetData: string, takerAssetData: string): void {
|
||||
_.every(orders, (order: SignedOrder, index: number) => {
|
||||
assert.assert(order.takerAssetData === takerAssetData, `Expected ${variableName}[${index}].takerAssetData to be ${takerAssetData}`);
|
||||
assert.assert(order.makerAssetData === makerAssetData, `Expected ${variableName}[${index}].makerAssetData to be ${makerAssetData}`);
|
||||
});
|
||||
},
|
||||
isValidForwarderSwapQuote(variableName: string, swapQuote: SwapQuote, wethAssetData: string): void {
|
||||
assert.isValidSwapQuote(variableName, swapQuote);
|
||||
assert.isValidForwarderSignedOrders(`${variableName}.orders`, swapQuote.orders, wethAssetData);
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { ContractWrappers } from '@0x/contract-wrappers';
|
||||
import { SignedOrder } from '@0x/types';
|
||||
import { SignedOrder, MarketOperation } from '@0x/types';
|
||||
import { BigNumber } from '@0x/utils';
|
||||
import { SupportedProvider, Web3Wrapper } from '@0x/web3-wrapper';
|
||||
import { Provider } from 'ethereum-types';
|
||||
@@ -72,6 +72,17 @@ export const swapQuoteConsumerUtils = {
|
||||
isValidForwarderSignedOrder(order: SignedOrder, wethAssetData: string): boolean {
|
||||
return order.takerAssetData === wethAssetData;
|
||||
},
|
||||
optimizeOrdersForMarketExchangeOperation(orders: SignedOrder[], operation: MarketOperation): SignedOrder[] {
|
||||
return _.map(orders, (order: SignedOrder, index: number) => {
|
||||
const optimizedOrder = _.clone(order);
|
||||
if (operation === MarketOperation.Sell && index !== 0) {
|
||||
optimizedOrder.takerAssetData = constants.NULL_ADDRESS;
|
||||
} else if (index !== 0) {
|
||||
optimizedOrder.makerAssetData = constants.NULL_ADDRESS;
|
||||
}
|
||||
return optimizedOrder;
|
||||
});
|
||||
},
|
||||
async getConsumerForSwapQuoteAsync(
|
||||
quote: SwapQuote,
|
||||
contractWrappers: ContractWrappers,
|
||||
|
Reference in New Issue
Block a user