diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-05-18 05:02:21 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-05-19 08:01:06 +0800 |
commit | bb74789b42b3605f0ec9fb8daba95b6b28a9f263 (patch) | |
tree | 52999995f7dc08217378fea342491e31eb93d361 /packages/contracts | |
parent | dbbd32d2ce0329f5219f2c477589e8a2b9a73472 (diff) | |
download | dexon-sol-tools-bb74789b42b3605f0ec9fb8daba95b6b28a9f263.tar dexon-sol-tools-bb74789b42b3605f0ec9fb8daba95b6b28a9f263.tar.gz dexon-sol-tools-bb74789b42b3605f0ec9fb8daba95b6b28a9f263.tar.bz2 dexon-sol-tools-bb74789b42b3605f0ec9fb8daba95b6b28a9f263.tar.lz dexon-sol-tools-bb74789b42b3605f0ec9fb8daba95b6b28a9f263.tar.xz dexon-sol-tools-bb74789b42b3605f0ec9fb8daba95b6b28a9f263.tar.zst dexon-sol-tools-bb74789b42b3605f0ec9fb8daba95b6b28a9f263.zip |
Changed calculateFillResults to public visibility so that it can be used by the Forwarding Contract.
Diffstat (limited to 'packages/contracts')
3 files changed, 131 insertions, 131 deletions
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol index 7e9c4f8fa..29d03fdb8 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol @@ -144,65 +144,62 @@ contract MixinExchangeCore is return updateCancelledState(order, orderInfo.orderStatus, orderInfo.orderHash); } - /// @dev Validates context for fillOrder. Succeeds or throws. - /// @param order to be filled. - /// @param orderStatus Status of order to be filled. - /// @param orderHash Hash of order to be filled. - /// @param takerAddress Address of order taker. - /// @param orderFilledAmount Amount of order already filled. - /// @param takerAssetFillAmount Desired amount of order to fill by taker. - /// @param signature Proof that the orders was created by its maker. - function assertValidFill( - Order memory order, - uint8 orderStatus, - bytes32 orderHash, - address takerAddress, - uint256 orderFilledAmount, - uint256 takerAssetFillAmount, - bytes memory signature - ) - internal + /// @dev Gets information about an order: status, hash, and amount filled. + /// @param order Order to gather information on. + /// @return OrderInfo Information about the order and its state. + /// See LibOrder.OrderInfo for a complete description. + function getOrderInfo(Order memory order) + public + view + returns (LibOrder.OrderInfo memory orderInfo) { - // Ensure order is valid - // An order can only be filled if its status is FILLABLE; - // however, only invalid statuses result in a throw. - // See LibStatus for a complete description of order statuses. - require( - orderStatus != uint8(Status.ORDER_INVALID_MAKER_ASSET_AMOUNT), - INVALID_ORDER_MAKER_ASSET_AMOUNT - ); - require( - orderStatus != uint8(Status.ORDER_INVALID_TAKER_ASSET_AMOUNT), - INVALID_ORDER_TAKER_ASSET_AMOUNT - ); + // Compute the order hash + orderInfo.orderHash = getOrderHash(order); - // Validate Maker signature (check only if first time seen) - if (orderFilledAmount == 0) { - require( - isValidSignature(orderHash, order.makerAddress, signature), - SIGNATURE_VALIDATION_FAILED - ); + // If order.makerAssetAmount is zero, we also reject the order. + // While the Exchange contract handles them correctly, they create + // edge cases in the supporting infrastructure because they have + // an 'infinite' price when computed by a simple division. + if (order.makerAssetAmount == 0) { + orderInfo.orderStatus = uint8(Status.ORDER_INVALID_MAKER_ASSET_AMOUNT); + return orderInfo; } - // Validate sender is allowed to fill this order - if (order.senderAddress != address(0)) { - require( - order.senderAddress == msg.sender, - INVALID_SENDER - ); + // If order.takerAssetAmount is zero, then the order will always + // be considered filled because 0 == takerAssetAmount == orderFilledAmount + // Instead of distinguishing between unfilled and filled zero taker + // amount orders, we choose not to support them. + if (order.takerAssetAmount == 0) { + orderInfo.orderStatus = uint8(Status.ORDER_INVALID_TAKER_ASSET_AMOUNT); + return orderInfo; } - // Validate taker is allowed to fill this order - if (order.takerAddress != address(0)) { - require( - order.takerAddress == takerAddress, - INVALID_CONTEXT - ); + // Validate order expiration + if (block.timestamp >= order.expirationTimeSeconds) { + orderInfo.orderStatus = uint8(Status.ORDER_EXPIRED); + return orderInfo; } - require( - takerAssetFillAmount > 0, - GT_ZERO_AMOUNT_REQUIRED - ); + + // Check if order has been cancelled + if (cancelled[orderInfo.orderHash]) { + orderInfo.orderStatus = uint8(Status.ORDER_CANCELLED); + return orderInfo; + } + if (makerEpoch[order.makerAddress] > order.salt) { + orderInfo.orderStatus = uint8(Status.ORDER_CANCELLED); + return orderInfo; + } + + // Fetch filled amount and validate order availability + orderInfo.orderFilledAmount = filled[orderInfo.orderHash]; + if (orderInfo.orderFilledAmount >= order.takerAssetAmount) { + orderInfo.orderStatus = uint8(Status.ORDER_FULLY_FILLED); + return orderInfo; + } + + // All other statuses are ruled out: order is Fillable + orderInfo.orderStatus = uint8(Status.ORDER_FILLABLE); + return orderInfo; } /// @dev Calculates amounts filled and fees paid by maker and taker. @@ -218,7 +215,7 @@ contract MixinExchangeCore is uint256 orderFilledAmount, uint256 takerAssetFillAmount ) - internal + public pure returns ( uint8 status, @@ -275,6 +272,67 @@ contract MixinExchangeCore is return (status, fillResults); } + /// @dev Validates context for fillOrder. Succeeds or throws. + /// @param order to be filled. + /// @param orderStatus Status of order to be filled. + /// @param orderHash Hash of order to be filled. + /// @param takerAddress Address of order taker. + /// @param orderFilledAmount Amount of order already filled. + /// @param takerAssetFillAmount Desired amount of order to fill by taker. + /// @param signature Proof that the orders was created by its maker. + function assertValidFill( + Order memory order, + uint8 orderStatus, + bytes32 orderHash, + address takerAddress, + uint256 orderFilledAmount, + uint256 takerAssetFillAmount, + bytes memory signature + ) + internal + { + // Ensure order is valid + // An order can only be filled if its status is FILLABLE; + // however, only invalid statuses result in a throw. + // See LibStatus for a complete description of order statuses. + require( + orderStatus != uint8(Status.ORDER_INVALID_MAKER_ASSET_AMOUNT), + INVALID_ORDER_MAKER_ASSET_AMOUNT + ); + require( + orderStatus != uint8(Status.ORDER_INVALID_TAKER_ASSET_AMOUNT), + INVALID_ORDER_TAKER_ASSET_AMOUNT + ); + + // Validate Maker signature (check only if first time seen) + if (orderFilledAmount == 0) { + require( + isValidSignature(orderHash, order.makerAddress, signature), + SIGNATURE_VALIDATION_FAILED + ); + } + + // Validate sender is allowed to fill this order + if (order.senderAddress != address(0)) { + require( + order.senderAddress == msg.sender, + INVALID_SENDER + ); + } + + // Validate taker is allowed to fill this order + if (order.takerAddress != address(0)) { + require( + order.takerAddress == takerAddress, + INVALID_CONTEXT + ); + } + require( + takerAssetFillAmount > 0, + GT_ZERO_AMOUNT_REQUIRED + ); + } + /// @dev Updates state with results of a fill order. /// @param order that was filled. /// @param takerAddress Address of taker who filled the order. @@ -385,62 +443,4 @@ contract MixinExchangeCore is return stateUpdated; } - - /// @dev Gets information about an order: status, hash, and amount filled. - /// @param order Order to gather information on. - /// @return OrderInfo Information about the order and its state. - /// See LibOrder.OrderInfo for a complete description. - function getOrderInfo(Order memory order) - public - view - returns (LibOrder.OrderInfo memory orderInfo) - { - // Compute the order hash - orderInfo.orderHash = getOrderHash(order); - - // If order.makerAssetAmount is zero, we also reject the order. - // While the Exchange contract handles them correctly, they create - // edge cases in the supporting infrastructure because they have - // an 'infinite' price when computed by a simple division. - if (order.makerAssetAmount == 0) { - orderInfo.orderStatus = uint8(Status.ORDER_INVALID_MAKER_ASSET_AMOUNT); - return orderInfo; - } - - // If order.takerAssetAmount is zero, then the order will always - // be considered filled because 0 == takerAssetAmount == orderFilledAmount - // Instead of distinguishing between unfilled and filled zero taker - // amount orders, we choose not to support them. - if (order.takerAssetAmount == 0) { - orderInfo.orderStatus = uint8(Status.ORDER_INVALID_TAKER_ASSET_AMOUNT); - return orderInfo; - } - - // Validate order expiration - if (block.timestamp >= order.expirationTimeSeconds) { - orderInfo.orderStatus = uint8(Status.ORDER_EXPIRED); - return orderInfo; - } - - // Check if order has been cancelled - if (cancelled[orderInfo.orderHash]) { - orderInfo.orderStatus = uint8(Status.ORDER_CANCELLED); - return orderInfo; - } - if (makerEpoch[order.makerAddress] > order.salt) { - orderInfo.orderStatus = uint8(Status.ORDER_CANCELLED); - return orderInfo; - } - - // Fetch filled amount and validate order availability - orderInfo.orderFilledAmount = filled[orderInfo.orderHash]; - if (orderInfo.orderFilledAmount >= order.takerAssetAmount) { - orderInfo.orderStatus = uint8(Status.ORDER_FULLY_FILLED); - return orderInfo; - } - - // All other statuses are ruled out: order is Fillable - orderInfo.orderStatus = uint8(Status.ORDER_FILLABLE); - return orderInfo; - } } diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchangeCore.sol b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchangeCore.sol index 958a89c39..8a0a73556 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchangeCore.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchangeCore.sol @@ -57,4 +57,24 @@ contract IExchangeCore { public view returns (LibOrder.OrderInfo memory orderInfo); + + /// @dev Calculates amounts filled and fees paid by maker and taker. + /// @param order to be filled. + /// @param orderStatus Status of order to be filled. + /// @param orderFilledAmount Amount of order already filled. + /// @param takerAssetFillAmount Desired amount of order to fill by taker. + /// @return status Return status of calculating fill amounts. Returns Status.SUCCESS on success. + /// @return fillResults Amounts filled and fees paid by maker and taker. + function calculateFillResults( + LibOrder.Order memory order, + uint8 orderStatus, + uint256 orderFilledAmount, + uint256 takerAssetFillAmount + ) + public + pure + returns ( + uint8 status, + LibFillResults.FillResults memory fillResults + ); } diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MExchangeCore.sol b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MExchangeCore.sol index fe6c155a3..2f928372b 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MExchangeCore.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MExchangeCore.sol @@ -75,26 +75,6 @@ contract MExchangeCore is ) internal; - /// @dev Calculates amounts filled and fees paid by maker and taker. - /// @param order to be filled. - /// @param orderStatus Status of order to be filled. - /// @param orderFilledAmount Amount of order already filled. - /// @param takerAssetFillAmount Desired amount of order to fill by taker. - /// @return status Return status of calculating fill amounts. Returns Status.SUCCESS on success. - /// @return fillResults Amounts filled and fees paid by maker and taker. - function calculateFillResults( - LibOrder.Order memory order, - uint8 orderStatus, - uint256 orderFilledAmount, - uint256 takerAssetFillAmount - ) - internal - pure - returns ( - uint8 status, - LibFillResults.FillResults memory fillResults - ); - /// @dev Updates state with results of a fill order. /// @param order that was filled. /// @param takerAddress Address of taker who filled the order. |