From 4f006fdc5c3cc24bb5d008f4c363d5225fe6728a Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 1 Aug 2018 14:27:17 -0700 Subject: Create marketBuyOrdersOptimizations --- .../src/contract_wrappers/forwarder_wrapper.ts | 27 ++++++++++---- packages/contract-wrappers/src/utils/constants.ts | 1 + .../src/utils/market_orders_optimization_utils.ts | 43 ++++++++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 packages/contract-wrappers/src/utils/market_orders_optimization_utils.ts (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index 90cfbe2af..7d51889cf 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -11,6 +11,7 @@ import { txOptsSchema } from '../schemas/tx_opts_schema'; import { TransactionOpts } from '../types'; import { assert } from '../utils/assert'; import { constants } from '../utils/constants'; +import { marketOrdersOptimizationUtils } from '../utils/market_orders_optimization_utils'; import { ContractWrapper } from './contract_wrapper'; import { ForwarderContract } from './generated/forwarder'; @@ -75,14 +76,19 @@ export class ForwarderWrapper extends ContractWrapper { this.getZRXTokenAddress(), this.getEtherTokenAddress(), ); + // lowercase input addresses const normalizedTakerAddress = takerAddress.toLowerCase(); const normalizedFeeRecipientAddress = feeRecipientAddress.toLowerCase(); + // optimize orders + const optimizedMarketOrders = marketOrdersOptimizationUtils.optimizeMarketOrders(signedOrders); + const optimizedFeeOrders = marketOrdersOptimizationUtils.optimizeFeeOrders(signedFeeOrders); + // send transaction const forwarderContractInstance = await this._getForwarderContractAsync(); const txHash = await forwarderContractInstance.marketSellOrdersWithEth.sendTransactionAsync( - signedOrders, - _.map(signedOrders, order => order.signature), - signedFeeOrders, - _.map(signedFeeOrders, order => order.signature), + optimizedMarketOrders, + _.map(optimizedMarketOrders, order => order.signature), + optimizedFeeOrders, + _.map(optimizedFeeOrders, order => order.signature), feePercentage, feeRecipientAddress, { @@ -138,15 +144,20 @@ export class ForwarderWrapper extends ContractWrapper { this.getZRXTokenAddress(), this.getEtherTokenAddress(), ); + // lowercase input addresses const normalizedTakerAddress = takerAddress.toLowerCase(); const normalizedFeeRecipientAddress = feeRecipientAddress.toLowerCase(); + // optimize orders + const optimizedMarketOrders = marketOrdersOptimizationUtils.optimizeMarketOrders(signedOrders); + const optimizedFeeOrders = marketOrdersOptimizationUtils.optimizeFeeOrders(signedFeeOrders); + // send transaction const forwarderContractInstance = await this._getForwarderContractAsync(); const txHash = await forwarderContractInstance.marketBuyOrdersWithEth.sendTransactionAsync( - signedOrders, + optimizedMarketOrders, makerAssetFillAmount, - _.map(signedOrders, order => order.signature), - signedFeeOrders, - _.map(signedFeeOrders, order => order.signature), + _.map(optimizedMarketOrders, order => order.signature), + optimizedFeeOrders, + _.map(optimizedFeeOrders, order => order.signature), feePercentage, feeRecipientAddress, { diff --git a/packages/contract-wrappers/src/utils/constants.ts b/packages/contract-wrappers/src/utils/constants.ts index d436efefc..2df11538c 100644 --- a/packages/contract-wrappers/src/utils/constants.ts +++ b/packages/contract-wrappers/src/utils/constants.ts @@ -2,6 +2,7 @@ import { BigNumber } from '@0xproject/utils'; export const constants = { NULL_ADDRESS: '0x0000000000000000000000000000000000000000', + NULL_BYTES: '0x', TESTRPC_NETWORK_ID: 50, INVALID_JUMP_PATTERN: 'invalid JUMP at', REVERT: 'revert', diff --git a/packages/contract-wrappers/src/utils/market_orders_optimization_utils.ts b/packages/contract-wrappers/src/utils/market_orders_optimization_utils.ts new file mode 100644 index 000000000..0041f1a64 --- /dev/null +++ b/packages/contract-wrappers/src/utils/market_orders_optimization_utils.ts @@ -0,0 +1,43 @@ +import { SignedOrder } from '@0xproject/types'; +import * as _ from 'lodash'; + +import { constants } from './constants'; + +export const marketOrdersOptimizationUtils = { + /** + * Takes an array of orders and outputs an array of equivalent orders where all takerAssetData are '0x' and + * all makerAssetData are '0x' except for that of the first order, which retains its original value + * @param orders An array of SignedOrder objects + * @returns optimized orders + */ + optimizeMarketOrders(orders: SignedOrder[]): SignedOrder[] { + const optimizedOrders = _.map(orders, (order, index) => { + const makerAssetData = index === 0 ? order.makerAssetData : constants.NULL_BYTES; + const takerAssetData = constants.NULL_BYTES; + return { + ...order, + makerAssetData, + takerAssetData, + }; + }); + return optimizedOrders; + }, + /** + * Takes an array of orders and outputs an array of equivalent orders where all takerAssetData are '0x' and + * all makerAssetData are '0x' + * @param orders An array of SignedOrder objects + * @returns optimized orders + */ + optimizeFeeOrders(orders: SignedOrder[]): SignedOrder[] { + const optimizedOrders = _.map(orders, order => { + const makerAssetData = constants.NULL_BYTES; + const takerAssetData = constants.NULL_BYTES; + return { + ...order, + makerAssetData, + takerAssetData, + }; + }); + return optimizedOrders; + }, +}; -- cgit v1.2.3