Make affiliate fee a flat amount

This commit is contained in:
Amir Bandeali 2019-11-30 14:38:55 -08:00
parent 264b06938e
commit dde57b1eca
3 changed files with 27 additions and 28 deletions

View File

@ -109,6 +109,7 @@ contract MixinExchangeWrapper is
// Subtract fee from makerAssetFilledAmount for the net amount acquired. // Subtract fee from makerAssetFilledAmount for the net amount acquired.
makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount
.safeSub(singleFillResults.takerFeePaid); .safeSub(singleFillResults.takerFeePaid);
// WETH fee // WETH fee
} else if (_areUnderlyingAssetsEqual(order.takerFeeAssetData, order.takerAssetData)) { } else if (_areUnderlyingAssetsEqual(order.takerFeeAssetData, order.takerAssetData)) {
@ -132,6 +133,7 @@ contract MixinExchangeWrapper is
.safeAdd(singleFillResults.protocolFeePaid); .safeAdd(singleFillResults.protocolFeePaid);
makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount; makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount;
// Unsupported fee // Unsupported fee
} else { } else {
LibRichErrors.rrevert(LibForwarderRichErrors.UnsupportedFeeError(order.takerFeeAssetData)); LibRichErrors.rrevert(LibForwarderRichErrors.UnsupportedFeeError(order.takerFeeAssetData));
@ -256,6 +258,7 @@ contract MixinExchangeWrapper is
.safeAdd(singleFillResults.protocolFeePaid); .safeAdd(singleFillResults.protocolFeePaid);
makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount; makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount;
// Percentage fee // Percentage fee
} else if (_areUnderlyingAssetsEqual(order.takerFeeAssetData, order.makerAssetData)) { } else if (_areUnderlyingAssetsEqual(order.takerFeeAssetData, order.makerAssetData)) {
// Calculate the remaining amount of takerAsset to sell // Calculate the remaining amount of takerAsset to sell
@ -278,6 +281,7 @@ contract MixinExchangeWrapper is
// Subtract fee from makerAssetFilledAmount for the net amount acquired. // Subtract fee from makerAssetFilledAmount for the net amount acquired.
makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount
.safeSub(singleFillResults.takerFeePaid); .safeSub(singleFillResults.takerFeePaid);
// Unsupported fee // Unsupported fee
} else { } else {
LibRichErrors.rrevert(LibForwarderRichErrors.UnsupportedFeeError(order.takerFeeAssetData)); LibRichErrors.rrevert(LibForwarderRichErrors.UnsupportedFeeError(order.takerFeeAssetData));
@ -295,7 +299,7 @@ contract MixinExchangeWrapper is
/// @param signatures Proofs that orders have been signed by makers. /// @param signatures Proofs that orders have been signed by makers.
/// @return totalWethSpentAmount Total amount of WETH spent on the given orders. /// @return totalWethSpentAmount Total amount of WETH spent on the given orders.
/// @return totalMakerAssetAcquiredAmount Total amount of maker asset acquired from the given orders. /// @return totalMakerAssetAcquiredAmount Total amount of maker asset acquired from the given orders.
function _marketBuyExactAmountWithWeth( function _marketBuyFillOrKill(
LibOrder.Order[] memory orders, LibOrder.Order[] memory orders,
uint256 makerAssetBuyAmount, uint256 makerAssetBuyAmount,
bytes[] memory signatures bytes[] memory signatures

View File

@ -63,7 +63,7 @@ contract MixinForwarderCore is
/// as possible, accounting for order and forwarder fees. /// as possible, accounting for order and forwarder fees.
/// @param orders Array of order specifications used containing desired makerAsset and WETH as takerAsset. /// @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 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. /// @param feeRecipient Address that will receive ETH when orders are filled.
/// @return wethSpentAmount Amount of WETH spent on the given set of orders. /// @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. /// @return makerAssetAcquiredAmount Amount of maker asset acquired from the given set of orders.
@ -71,7 +71,7 @@ contract MixinForwarderCore is
function marketSellOrdersWithEth( function marketSellOrdersWithEth(
LibOrder.Order[] memory orders, LibOrder.Order[] memory orders,
bytes[] memory signatures, bytes[] memory signatures,
uint256 feePercentage, uint256 ethFeeAmount,
address payable feeRecipient address payable feeRecipient
) )
public public
@ -86,11 +86,7 @@ contract MixinForwarderCore is
_convertEthToWeth(); _convertEthToWeth();
// Calculate amount of WETH that won't be spent on the forwarder fee. // Calculate amount of WETH that won't be spent on the forwarder fee.
uint256 wethSellAmount = LibMath.getPartialAmountFloor( uint256 wethSellAmount = msg.value.safeSub(ethFeeAmount);
PERCENTAGE_DENOMINATOR,
feePercentage.safeAdd(PERCENTAGE_DENOMINATOR),
msg.value
);
// Spends up to wethSellAmount to fill orders, transfers purchased assets to msg.sender, // Spends up to wethSellAmount to fill orders, transfers purchased assets to msg.sender,
// and pays WETH order fees. // and pays WETH order fees.
@ -103,11 +99,11 @@ contract MixinForwarderCore is
signatures signatures
); );
// Transfer feePercentage of total ETH spent on orders to feeRecipient. // Transfer ethFeeAmount to feeRecipient.
// Refund remaining ETH to msg.sender. // Refund remaining ETH to msg.sender.
ethFeePaid = _transferEthFeeAndRefund( ethFeePaid = _transferEthFeeAndRefund(
wethSpentAmount, wethSpentAmount,
feePercentage, ethFeeAmount,
feeRecipient feeRecipient
); );
} }
@ -119,7 +115,7 @@ contract MixinForwarderCore is
/// @param orders Array of order specifications used containing desired makerAsset and WETH as takerAsset. /// @param orders Array of order specifications used containing desired makerAsset and WETH as takerAsset.
/// @param makerAssetBuyAmount Desired amount of makerAsset to purchase. /// @param makerAssetBuyAmount Desired amount of makerAsset to purchase.
/// @param signatures Proofs that orders have been created by makers. /// @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. /// @param feeRecipient Address that will receive ETH when orders are filled.
/// @return wethSpentAmount Amount of WETH spent on the given set of orders. /// @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. /// @return makerAssetAcquiredAmount Amount of maker asset acquired from the given set of orders.
@ -128,7 +124,7 @@ contract MixinForwarderCore is
LibOrder.Order[] memory orders, LibOrder.Order[] memory orders,
uint256 makerAssetBuyAmount, uint256 makerAssetBuyAmount,
bytes[] memory signatures, bytes[] memory signatures,
uint256 feePercentage, uint256 ethFeeAmount,
address payable feeRecipient address payable feeRecipient
) )
public public
@ -146,17 +142,17 @@ contract MixinForwarderCore is
( (
wethSpentAmount, wethSpentAmount,
makerAssetAcquiredAmount makerAssetAcquiredAmount
) = _marketBuyExactAmountWithWeth( ) = _marketBuyFillOrKill(
orders, orders,
makerAssetBuyAmount, makerAssetBuyAmount,
signatures signatures
); );
// Transfer feePercentage of total ETH spent on orders to feeRecipient. // Transfer ethFeeAmount to feeRecipient.
// Refund remaining ETH to msg.sender. // Refund remaining ETH to msg.sender.
ethFeePaid = _transferEthFeeAndRefund( ethFeePaid = _transferEthFeeAndRefund(
wethSpentAmount, wethSpentAmount,
feePercentage, ethFeeAmount,
feeRecipient feeRecipient
); );
} }

View File

@ -55,18 +55,24 @@ contract MixinWeth is
/// @dev Transfers feePercentage of WETH spent on primary orders to feeRecipient. /// @dev Transfers feePercentage of WETH spent on primary orders to feeRecipient.
/// Refunds any excess ETH to msg.sender. /// Refunds any excess ETH to msg.sender.
/// @param wethSpent Amount of WETH spent when filling orders. /// @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. /// @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. /// @return ethFee Amount paid to feeRecipient as a percentage fee on the total WETH sold.
function _transferEthFeeAndRefund( function _transferEthFeeAndRefund(
uint256 wethSpent, uint256 wethSpent,
uint256 feePercentage, uint256 ethFeeAmount,
address payable feeRecipient address payable feeRecipient
) )
internal internal
returns (uint256 ethFee) returns (uint256 ethFee)
{ {
// Ensure feePercentage is less than 5%. // Ensure feePercentage is less than 5%.
uint256 feePercentage = LibMath.getPartialAmountFloor(
ethFeeAmount,
wethSpent,
PERCENTAGE_DENOMINATOR
);
if (feePercentage > MAX_FEE_PERCENTAGE) { if (feePercentage > MAX_FEE_PERCENTAGE) {
LibRichErrors.rrevert(LibForwarderRichErrors.FeePercentageTooLargeError( LibRichErrors.rrevert(LibForwarderRichErrors.FeePercentageTooLargeError(
feePercentage feePercentage
@ -84,15 +90,8 @@ contract MixinWeth is
// Calculate amount of WETH that hasn't been spent. // Calculate amount of WETH that hasn't been spent.
uint256 wethRemaining = msg.value.safeSub(wethSpent); 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. // Ensure fee is less than amount of WETH remaining.
if (ethFee > wethRemaining) { if (ethFeeAmount > wethRemaining) {
LibRichErrors.rrevert(LibForwarderRichErrors.InsufficientEthForFeeError( LibRichErrors.rrevert(LibForwarderRichErrors.InsufficientEthForFeeError(
ethFee, ethFee,
wethRemaining wethRemaining
@ -105,12 +104,12 @@ contract MixinWeth is
ETHER_TOKEN.withdraw(wethRemaining); ETHER_TOKEN.withdraw(wethRemaining);
// Pay ETH to feeRecipient // Pay ETH to feeRecipient
if (ethFee > 0) { if (ethFeeAmount > 0) {
feeRecipient.transfer(ethFee); feeRecipient.transfer(ethFeeAmount);
} }
// Refund remaining ETH to msg.sender. // Refund remaining ETH to msg.sender.
uint256 ethRefund = wethRemaining.safeSub(ethFee); uint256 ethRefund = wethRemaining.safeSub(ethFeeAmount);
if (ethRefund > 0) { if (ethRefund > 0) {
msg.sender.transfer(ethRefund); msg.sender.transfer(ethRefund);
} }