From dde57b1ecab2a4a92237a9268c5d7385efa74f7c Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sat, 30 Nov 2019 14:38:55 -0800 Subject: [PATCH] Make affiliate fee a flat amount --- .../contracts/src/MixinExchangeWrapper.sol | 6 ++++- .../contracts/src/MixinForwarderCore.sol | 24 ++++++++---------- .../contracts/src/MixinWeth.sol | 25 +++++++++---------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/contracts/exchange-forwarder/contracts/src/MixinExchangeWrapper.sol b/contracts/exchange-forwarder/contracts/src/MixinExchangeWrapper.sol index 16782c5cc3..c86285acb7 100644 --- a/contracts/exchange-forwarder/contracts/src/MixinExchangeWrapper.sol +++ b/contracts/exchange-forwarder/contracts/src/MixinExchangeWrapper.sol @@ -109,6 +109,7 @@ contract MixinExchangeWrapper is // Subtract fee from makerAssetFilledAmount for the net amount acquired. makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount .safeSub(singleFillResults.takerFeePaid); + // WETH fee } else if (_areUnderlyingAssetsEqual(order.takerFeeAssetData, order.takerAssetData)) { @@ -132,6 +133,7 @@ contract MixinExchangeWrapper is .safeAdd(singleFillResults.protocolFeePaid); makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount; + // Unsupported fee } else { LibRichErrors.rrevert(LibForwarderRichErrors.UnsupportedFeeError(order.takerFeeAssetData)); @@ -256,6 +258,7 @@ contract MixinExchangeWrapper is .safeAdd(singleFillResults.protocolFeePaid); makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount; + // Percentage fee } else if (_areUnderlyingAssetsEqual(order.takerFeeAssetData, order.makerAssetData)) { // Calculate the remaining amount of takerAsset to sell @@ -278,6 +281,7 @@ contract MixinExchangeWrapper is // Subtract fee from makerAssetFilledAmount for the net amount acquired. makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount .safeSub(singleFillResults.takerFeePaid); + // Unsupported fee } else { LibRichErrors.rrevert(LibForwarderRichErrors.UnsupportedFeeError(order.takerFeeAssetData)); @@ -295,7 +299,7 @@ contract MixinExchangeWrapper is /// @param signatures Proofs that orders have been signed by makers. /// @return totalWethSpentAmount Total amount of WETH spent on the given orders. /// @return totalMakerAssetAcquiredAmount Total amount of maker asset acquired from the given orders. - function _marketBuyExactAmountWithWeth( + function _marketBuyFillOrKill( LibOrder.Order[] memory orders, uint256 makerAssetBuyAmount, bytes[] memory signatures diff --git a/contracts/exchange-forwarder/contracts/src/MixinForwarderCore.sol b/contracts/exchange-forwarder/contracts/src/MixinForwarderCore.sol index 8b1ba3b804..f34e1f6cdc 100644 --- a/contracts/exchange-forwarder/contracts/src/MixinForwarderCore.sol +++ b/contracts/exchange-forwarder/contracts/src/MixinForwarderCore.sol @@ -63,7 +63,7 @@ contract MixinForwarderCore is /// as possible, accounting for order and forwarder fees. /// @param orders Array of order specifications used containing desired makerAsset and WETH as takerAsset. /// @param signatures Proofs that orders have been created by makers. - /// @param feePercentage Percentage of WETH sold that will payed as fee to forwarding contract feeRecipient. + /// @param ethFeeAmount Amount of ETH, denominatoed in Wei, that is payed to feeRecipient. /// @param feeRecipient Address that will receive ETH when orders are filled. /// @return wethSpentAmount Amount of WETH spent on the given set of orders. /// @return makerAssetAcquiredAmount Amount of maker asset acquired from the given set of orders. @@ -71,7 +71,7 @@ contract MixinForwarderCore is function marketSellOrdersWithEth( LibOrder.Order[] memory orders, bytes[] memory signatures, - uint256 feePercentage, + uint256 ethFeeAmount, address payable feeRecipient ) public @@ -86,11 +86,7 @@ contract MixinForwarderCore is _convertEthToWeth(); // Calculate amount of WETH that won't be spent on the forwarder fee. - uint256 wethSellAmount = LibMath.getPartialAmountFloor( - PERCENTAGE_DENOMINATOR, - feePercentage.safeAdd(PERCENTAGE_DENOMINATOR), - msg.value - ); + uint256 wethSellAmount = msg.value.safeSub(ethFeeAmount); // Spends up to wethSellAmount to fill orders, transfers purchased assets to msg.sender, // and pays WETH order fees. @@ -103,11 +99,11 @@ contract MixinForwarderCore is signatures ); - // Transfer feePercentage of total ETH spent on orders to feeRecipient. + // Transfer ethFeeAmount to feeRecipient. // Refund remaining ETH to msg.sender. ethFeePaid = _transferEthFeeAndRefund( wethSpentAmount, - feePercentage, + ethFeeAmount, feeRecipient ); } @@ -119,7 +115,7 @@ contract MixinForwarderCore is /// @param orders Array of order specifications used containing desired makerAsset and WETH as takerAsset. /// @param makerAssetBuyAmount Desired amount of makerAsset to purchase. /// @param signatures Proofs that orders have been created by makers. - /// @param feePercentage Percentage of WETH sold that will payed as fee to forwarding contract feeRecipient. + /// @param ethFeeAmount Amount of ETH, denominatoed in Wei, that is payed to feeRecipient. /// @param feeRecipient Address that will receive ETH when orders are filled. /// @return wethSpentAmount Amount of WETH spent on the given set of orders. /// @return makerAssetAcquiredAmount Amount of maker asset acquired from the given set of orders. @@ -128,7 +124,7 @@ contract MixinForwarderCore is LibOrder.Order[] memory orders, uint256 makerAssetBuyAmount, bytes[] memory signatures, - uint256 feePercentage, + uint256 ethFeeAmount, address payable feeRecipient ) public @@ -146,17 +142,17 @@ contract MixinForwarderCore is ( wethSpentAmount, makerAssetAcquiredAmount - ) = _marketBuyExactAmountWithWeth( + ) = _marketBuyFillOrKill( orders, makerAssetBuyAmount, signatures ); - // Transfer feePercentage of total ETH spent on orders to feeRecipient. + // Transfer ethFeeAmount to feeRecipient. // Refund remaining ETH to msg.sender. ethFeePaid = _transferEthFeeAndRefund( wethSpentAmount, - feePercentage, + ethFeeAmount, feeRecipient ); } diff --git a/contracts/exchange-forwarder/contracts/src/MixinWeth.sol b/contracts/exchange-forwarder/contracts/src/MixinWeth.sol index 13f15e6f46..3d96458bc6 100644 --- a/contracts/exchange-forwarder/contracts/src/MixinWeth.sol +++ b/contracts/exchange-forwarder/contracts/src/MixinWeth.sol @@ -55,18 +55,24 @@ contract MixinWeth is /// @dev Transfers feePercentage of WETH spent on primary orders to feeRecipient. /// Refunds any excess ETH to msg.sender. /// @param wethSpent Amount of WETH spent when filling orders. - /// @param feePercentage Percentage of WETH sold that will payed as fee to forwarding contract feeRecipient. + /// @param ethFeeAmount Amount of ETH, denominatoed in Wei, that is payed to feeRecipient. /// @param feeRecipient Address that will receive ETH when orders are filled. /// @return ethFee Amount paid to feeRecipient as a percentage fee on the total WETH sold. function _transferEthFeeAndRefund( uint256 wethSpent, - uint256 feePercentage, + uint256 ethFeeAmount, address payable feeRecipient ) internal returns (uint256 ethFee) { // Ensure feePercentage is less than 5%. + uint256 feePercentage = LibMath.getPartialAmountFloor( + ethFeeAmount, + wethSpent, + PERCENTAGE_DENOMINATOR + ); + if (feePercentage > MAX_FEE_PERCENTAGE) { LibRichErrors.rrevert(LibForwarderRichErrors.FeePercentageTooLargeError( feePercentage @@ -84,15 +90,8 @@ contract MixinWeth is // Calculate amount of WETH that hasn't been spent. uint256 wethRemaining = msg.value.safeSub(wethSpent); - // Calculate ETH fee to pay to feeRecipient. - ethFee = LibMath.getPartialAmountFloor( - feePercentage, - PERCENTAGE_DENOMINATOR, - wethSpent - ); - // Ensure fee is less than amount of WETH remaining. - if (ethFee > wethRemaining) { + if (ethFeeAmount > wethRemaining) { LibRichErrors.rrevert(LibForwarderRichErrors.InsufficientEthForFeeError( ethFee, wethRemaining @@ -105,12 +104,12 @@ contract MixinWeth is ETHER_TOKEN.withdraw(wethRemaining); // Pay ETH to feeRecipient - if (ethFee > 0) { - feeRecipient.transfer(ethFee); + if (ethFeeAmount > 0) { + feeRecipient.transfer(ethFeeAmount); } // Refund remaining ETH to msg.sender. - uint256 ethRefund = wethRemaining.safeSub(ethFee); + uint256 ethRefund = wethRemaining.safeSub(ethFeeAmount); if (ethRefund > 0) { msg.sender.transfer(ethRefund); }