diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-07-20 23:01:18 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-07-23 23:02:51 +0800 |
commit | e20f3a0f97d3d2029aea24e761d4689e0382756f (patch) | |
tree | 4084f55e8d9cfc6e276efb1de274294da17f1eb4 /packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol | |
parent | c5029e61e3bd147d8b177b60da43e76a32e57c6f (diff) | |
download | dexon-0x-contracts-e20f3a0f97d3d2029aea24e761d4689e0382756f.tar dexon-0x-contracts-e20f3a0f97d3d2029aea24e761d4689e0382756f.tar.gz dexon-0x-contracts-e20f3a0f97d3d2029aea24e761d4689e0382756f.tar.bz2 dexon-0x-contracts-e20f3a0f97d3d2029aea24e761d4689e0382756f.tar.lz dexon-0x-contracts-e20f3a0f97d3d2029aea24e761d4689e0382756f.tar.xz dexon-0x-contracts-e20f3a0f97d3d2029aea24e761d4689e0382756f.tar.zst dexon-0x-contracts-e20f3a0f97d3d2029aea24e761d4689e0382756f.zip |
Remove redundant external call by reimplementing fillOrderNoThrow
Diffstat (limited to 'packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol')
-rw-r--r-- | packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol | 134 |
1 files changed, 6 insertions, 128 deletions
diff --git a/packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol b/packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol index 2ec2e2638..e9acef7a2 100644 --- a/packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol +++ b/packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol @@ -22,6 +22,7 @@ pragma experimental ABIEncoderV2; import "./mixins/MWeth.sol"; import "./mixins/MAssets.sol"; import "./mixins/MConstants.sol"; +import "./mixins/MExchangeWrapper.sol"; import "./mixins/MForwarderCore.sol"; import "../utils/LibBytes/LibBytes.sol"; import "../protocol/Exchange/libs/LibOrder.sol"; @@ -35,6 +36,7 @@ contract MixinForwarderCore is MConstants, MWeth, MAssets, + MExchangeWrapper, MForwarderCore { @@ -115,7 +117,7 @@ contract MixinForwarderCore is ); // Buy back all ZRX spent on fees. zrxBuyAmount = orderFillResults.takerFeePaid; - feeOrderFillResults = marketBuyZrx( + feeOrderFillResults = marketBuyZrxWithWeth( feeOrders, zrxBuyAmount, feeSignatures @@ -178,7 +180,7 @@ contract MixinForwarderCore is if (orders[0].makerAssetData.equals(ZRX_ASSET_DATA)) { // If the makerAsset is ZRX, it is not necessary to pay fees out of this // contracts's ZRX balance because fees are factored into the price of the order. - orderFillResults = marketBuyZrx( + orderFillResults = marketBuyZrxWithWeth( orders, makerAssetFillAmount, signatures @@ -188,14 +190,14 @@ contract MixinForwarderCore is } else { // Attemp to purchase desired amount of makerAsset. // ZRX fees are payed with this contract's balance. - orderFillResults = marketBuyAsset( + orderFillResults = marketBuyWithWeth( orders, makerAssetFillAmount, signatures ); // Buy back all ZRX spent on fees. zrxBuyAmount = orderFillResults.takerFeePaid; - feeOrderFillResults = marketBuyZrx( + feeOrderFillResults = marketBuyZrxWithWeth( feeOrders, zrxBuyAmount, feeSignatures @@ -223,130 +225,6 @@ contract MixinForwarderCore is transferPurchasedAssetToSender(orders[0].makerAssetData, makerAssetAmountPurchased); } - /// @param orders Array of order specifications used containing desired makerAsset and WETH as takerAsset. - /// @param wethSellAmount Desired amount of WETH to sell. - /// @param signatures Proofs that orders have been created by makers. - /// @return Amounts filled and fees paid by maker and taker. - function marketSellWeth( - LibOrder.Order[] memory orders, - uint256 wethSellAmount, - bytes[] memory signatures - ) - internal - returns (FillResults memory fillResults) - { - // `marketSellOrders` uses the first order's takerAssetData for all passed in orders. - orders[0].takerAssetData = WETH_ASSET_DATA; - - // All orders are required to have the same makerAssetData. We save on gas by reusing the makerAssetData of the first order. - uint256 ordersLength = orders.length; - for (uint256 i = 0; i < ordersLength; i++) { - orders[i].makerAssetData = orders[0].makerAssetData; - } - - // Sell WETH until entire amount has been sold or all orders have been filled. - fillResults = EXCHANGE.marketSellOrdersNoThrow( - orders, - wethSellAmount, - signatures - ); - - return fillResults; - } - - /// @param orders Array of order specifications used containing desired makerAsset and WETH as takerAsset. - /// @param makerAssetFillAmount Desired amount of makerAsset to buy. - /// @param signatures Proofs that orders have been created by makers. - /// @return Amounts filled and fees paid by maker and taker. - function marketBuyAsset( - LibOrder.Order[] memory orders, - uint256 makerAssetFillAmount, - bytes[] memory signatures - ) - internal - returns (FillResults memory fillResults) - { - bytes memory wethAssetData = WETH_ASSET_DATA; - - // All orders are required to have WETH as takerAssetData. We save on gas by populating the orders here, rather than passing in any extra calldata. - uint256 ordersLength = orders.length; - for (uint256 i = 0; i < ordersLength; i++) { - orders[i].takerAssetData = wethAssetData; - } - - // Purchase makerAsset until entire amount has been bought or all orders have been filled. - fillResults = EXCHANGE.marketBuyOrdersNoThrow( - orders, - makerAssetFillAmount, - signatures - ); - - return fillResults; - } - - /// @dev Buys zrxBuyAmount of ZRX fee tokens, taking into account ZRX fees for each order. This will guarantee - /// that at least zrxBuyAmount of ZRX is purchased (sometimes slightly over due to rounding issues). - /// It is possible that a request to buy 200 ZRX will require purchasing 202 ZRX - /// as 2 ZRX is required to purchase the 200 ZRX fee tokens. This guarantees at least 200 ZRX for future purchases. - /// @param orders Array of order specifications containing ZRX as makerAsset and WETH as takerAsset. - /// @param zrxBuyAmount Desired amount of ZRX to buy. - /// @param signatures Proofs that orders have been created by makers. - /// @return totalFillResults Amounts filled and fees paid by maker and taker. - function marketBuyZrx( - LibOrder.Order[] memory orders, - uint256 zrxBuyAmount, - bytes[] memory signatures - ) - internal - returns (FillResults memory totalFillResults) - { - // Do nothing if zrxBuyAmount == 0 - if (zrxBuyAmount == 0) { - return totalFillResults; - } - - bytes memory zrxAssetData = ZRX_ASSET_DATA; - bytes memory wethAssetData = WETH_ASSET_DATA; - uint256 zrxPurchased = 0; - - uint256 ordersLength = orders.length; - for (uint256 i = 0; i < ordersLength; i++) { - - // All of these are ZRX/WETH, so we can drop the respective assetData from calldata. - orders[i].makerAssetData = zrxAssetData; - orders[i].takerAssetData = wethAssetData; - - // Calculate the remaining amount of ZRX to buy. - uint256 remainingZrxBuyAmount = safeSub(zrxBuyAmount, zrxPurchased); - - // Convert the remaining amount of ZRX to buy into remaining amount - // of WETH to sell, assuming entire amount can be sold in the current order. - uint256 remainingWethSellAmount = getPartialAmount( - orders[i].takerAssetAmount, - safeSub(orders[i].makerAssetAmount, orders[i].takerFee), // our exchange rate after fees - remainingZrxBuyAmount - ); - - // Attempt to sell the remaining amount of WETH. - FillResults memory singleFillResult = EXCHANGE.fillOrderNoThrow( - orders[i], - safeAdd(remainingWethSellAmount, 1), - signatures[i] - ); - - // Update amounts filled and fees paid by maker and taker. - addFillResults(totalFillResults, singleFillResult); - zrxPurchased = safeSub(totalFillResults.makerAssetFilledAmount, totalFillResults.takerFeePaid); - - // Stop execution if the entire amount of ZRX has been bought. - if (zrxPurchased >= zrxBuyAmount) { - break; - } - } - - return totalFillResults; - } - /// @dev Ensures that all ZRX fees have been repurchased and no extra WETH owned by this contract has been sold. /// @param orderFillResults Amounts filled and fees paid for primary orders. /// @param feeOrderFillResults Amounts filled and fees paid for fee orders. |