diff options
Diffstat (limited to 'packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol')
-rw-r--r-- | packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol | 121 |
1 files changed, 62 insertions, 59 deletions
diff --git a/packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol b/packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol index 0bca7dc80..a575c9675 100644 --- a/packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol +++ b/packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol @@ -16,106 +16,71 @@ */ -pragma solidity ^0.4.24; +pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../utils/LibBytes/LibBytes.sol"; import "../protocol/Exchange/libs/LibFillResults.sol"; import "../protocol/Exchange/libs/LibMath.sol"; import "../protocol/Exchange/libs/LibOrder.sol"; -import "./MixinConstants.sol"; +import "./mixins/MConstants.sol"; +import "./mixins/MExpectedResults.sol"; + contract MixinExpectedResults is LibMath, LibFillResults, - MixinConstants + MConstants, + MExpectedResults { - /// @dev Simulates the 0x Exchange fillOrder validation and calculations, without performing any state changes. - /// @param order An Order struct containing order specifications. - /// @param takerAssetFillAmount A number representing the amount of this order to fill. - /// @return fillResults Amounts filled and fees paid by maker and taker. - function calculateFillResults( - LibOrder.Order memory order, - uint256 takerAssetFillAmount - ) - internal - view - returns (FillResults memory fillResults) - { - LibOrder.OrderInfo memory orderInfo = EXCHANGE.getOrderInfo(order); - if (orderInfo.orderStatus != uint8(LibOrder.OrderStatus.FILLABLE)) { - return fillResults; - } - uint256 remainingTakerAssetAmount = safeSub(order.takerAssetAmount, orderInfo.orderTakerAssetFilledAmount); - uint256 takerAssetFilledAmount = min256(takerAssetFillAmount, remainingTakerAssetAmount); - - fillResults.takerAssetFilledAmount = takerAssetFilledAmount; - fillResults.makerAssetFilledAmount = getPartialAmount( - takerAssetFilledAmount, - order.takerAssetAmount, - order.makerAssetAmount - ); - fillResults.makerFeePaid = getPartialAmount( - takerAssetFilledAmount, - order.takerAssetAmount, - order.makerFee - ); - fillResults.takerFeePaid = getPartialAmount( - takerAssetFilledAmount, - order.takerAssetAmount, - order.takerFee - ); - return fillResults; - } - - /// @dev Calculates a FillResults total for selling takerAssetFillAmount over all orders. + /// @dev Calculates a total FillResults for buying makerAssetFillAmount over all orders. /// Including the fees required to be paid. /// @param orders An array of Order struct containing order specifications. - /// @param takerAssetFillAmount A number representing the amount of this order to fill. + /// @param makerAssetFillAmount A number representing the amount of this order to fill. /// @return totalFillResults Amounts filled and fees paid by maker and taker. - function calculateMarketSellResults( + function calculateMarketBuyResults( LibOrder.Order[] memory orders, - uint256 takerAssetFillAmount + uint256 makerAssetFillAmount ) - internal + public view returns (FillResults memory totalFillResults) { for (uint256 i = 0; i < orders.length; i++) { - uint256 remainingTakerAssetFillAmount = safeSub(takerAssetFillAmount, totalFillResults.takerAssetFilledAmount); + uint256 remainingMakerAssetFillAmount = safeSub(makerAssetFillAmount, totalFillResults.makerAssetFilledAmount); + uint256 remainingTakerAssetFillAmount = getPartialAmount( + orders[i].takerAssetAmount, + orders[i].makerAssetAmount, + remainingMakerAssetFillAmount + ); FillResults memory singleFillResult = calculateFillResults(orders[i], remainingTakerAssetFillAmount); addFillResults(totalFillResults, singleFillResult); - if (totalFillResults.takerAssetFilledAmount == takerAssetFillAmount) { + if (totalFillResults.makerAssetFilledAmount == makerAssetFillAmount) { break; } } return totalFillResults; } - /// @dev Calculates a total FillResults for buying makerAssetFillAmount over all orders. + /// @dev Calculates a FillResults total for selling takerAssetFillAmount over all orders. /// Including the fees required to be paid. /// @param orders An array of Order struct containing order specifications. - /// @param makerAssetFillAmount A number representing the amount of this order to fill. + /// @param takerAssetFillAmount A number representing the amount of this order to fill. /// @return totalFillResults Amounts filled and fees paid by maker and taker. - function calculateMarketBuyResults( + function calculateMarketSellResults( LibOrder.Order[] memory orders, - uint256 makerAssetFillAmount + uint256 takerAssetFillAmount ) public view returns (FillResults memory totalFillResults) { for (uint256 i = 0; i < orders.length; i++) { - uint256 remainingMakerAssetFillAmount = safeSub(makerAssetFillAmount, totalFillResults.makerAssetFilledAmount); - uint256 remainingTakerAssetFillAmount = getPartialAmount( - orders[i].takerAssetAmount, - orders[i].makerAssetAmount, - remainingMakerAssetFillAmount - ); + uint256 remainingTakerAssetFillAmount = safeSub(takerAssetFillAmount, totalFillResults.takerAssetFilledAmount); FillResults memory singleFillResult = calculateFillResults(orders[i], remainingTakerAssetFillAmount); addFillResults(totalFillResults, singleFillResult); - if (totalFillResults.makerAssetFilledAmount == makerAssetFillAmount) { + if (totalFillResults.takerAssetFilledAmount == takerAssetFillAmount) { break; } } @@ -155,4 +120,42 @@ contract MixinExpectedResults is } return totalFillResults; } + + /// @dev Simulates the 0x Exchange fillOrder validation and calculations, without performing any state changes. + /// @param order An Order struct containing order specifications. + /// @param takerAssetFillAmount A number representing the amount of this order to fill. + /// @return fillResults Amounts filled and fees paid by maker and taker. + function calculateFillResults( + LibOrder.Order memory order, + uint256 takerAssetFillAmount + ) + internal + view + returns (FillResults memory fillResults) + { + LibOrder.OrderInfo memory orderInfo = EXCHANGE.getOrderInfo(order); + if (orderInfo.orderStatus != uint8(LibOrder.OrderStatus.FILLABLE)) { + return fillResults; + } + uint256 remainingTakerAssetAmount = safeSub(order.takerAssetAmount, orderInfo.orderTakerAssetFilledAmount); + uint256 takerAssetFilledAmount = min256(takerAssetFillAmount, remainingTakerAssetAmount); + + fillResults.takerAssetFilledAmount = takerAssetFilledAmount; + fillResults.makerAssetFilledAmount = getPartialAmount( + takerAssetFilledAmount, + order.takerAssetAmount, + order.makerAssetAmount + ); + fillResults.makerFeePaid = getPartialAmount( + takerAssetFilledAmount, + order.takerAssetAmount, + order.makerFee + ); + fillResults.takerFeePaid = getPartialAmount( + takerAssetFilledAmount, + order.takerAssetAmount, + order.takerFee + ); + return fillResults; + } } |