From 3f8639bd9c87ad62b076e9be47df9c373f4c9f7d Mon Sep 17 00:00:00 2001 From: Michael Zhu Date: Wed, 25 Sep 2019 15:24:40 -0700 Subject: [PATCH] Update MixinExchangeWrapper to handle protocol fees --- .../contracts/src/MixinExchangeWrapper.sol | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/contracts/exchange-forwarder/contracts/src/MixinExchangeWrapper.sol b/contracts/exchange-forwarder/contracts/src/MixinExchangeWrapper.sol index 9218aa7e93..f2f7caf66d 100644 --- a/contracts/exchange-forwarder/contracts/src/MixinExchangeWrapper.sol +++ b/contracts/exchange-forwarder/contracts/src/MixinExchangeWrapper.sol @@ -67,13 +67,14 @@ contract MixinExchangeWrapper is add(fillOrderCalldata, 32), // pointer to start of input (skip array length in first 32 bytes) mload(fillOrderCalldata), // length of input fillOrderCalldata, // write output over input - 128 // output size is 128 bytes + 160 // output size is 160 bytes ) if success { mstore(fillResults, mload(fillOrderCalldata)) mstore(add(fillResults, 32), mload(add(fillOrderCalldata, 32))) mstore(add(fillResults, 64), mload(add(fillOrderCalldata, 64))) mstore(add(fillResults, 96), mload(add(fillOrderCalldata, 96))) + mstore(add(fillResults, 128), mload(add(fillOrderCalldata, 128))) } } // fillResults values will be 0 by default if call was unsuccessful @@ -107,7 +108,9 @@ contract MixinExchangeWrapper is signature ); - wethSpentAmount = singleFillResults.takerAssetFilledAmount; + wethSpentAmount = singleFillResults.takerAssetFilledAmount.safeAdd( + singleFillResults.protocolFeePaid + ); // Subtract fee from makerAssetFilledAmount for the net amount acquired. makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount.safeSub( @@ -115,11 +118,13 @@ contract MixinExchangeWrapper is ); // WETH fee } else if (order.takerFeeAssetData.equals(order.takerAssetData)) { + uint256 protocolFee = tx.gasprice.safeMul(EXCHANGE.protocolFeeMultiplier()); + // We will first sell WETH as the takerAsset, then use it to pay the takerFee. // This ensures that we reserve enough to pay the fee. uint256 takerAssetFillAmount = LibMath.getPartialAmountCeil( order.takerAssetAmount, - order.takerAssetAmount.safeAdd(order.takerFee), + order.takerAssetAmount.safeAdd(order.takerFee).safeAdd(protocolFee), remainingTakerAssetFillAmount ); @@ -132,6 +137,8 @@ contract MixinExchangeWrapper is // WETH is also spent on the taker fee, so we add it here. wethSpentAmount = singleFillResults.takerAssetFilledAmount.safeAdd( singleFillResults.takerFeePaid + ).safeAdd( + singleFillResults.protocolFeePaid ); makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount; @@ -234,6 +241,8 @@ contract MixinExchangeWrapper is // WETH is also spent on the taker fee, so we add it here. wethSpentAmount = singleFillResults.takerAssetFilledAmount.safeAdd( singleFillResults.takerFeePaid + ).safeAdd( + singleFillResults.protocolFeePaid ); makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount; @@ -253,7 +262,9 @@ contract MixinExchangeWrapper is signature ); - wethSpentAmount = singleFillResults.takerAssetFilledAmount; + wethSpentAmount = singleFillResults.takerAssetFilledAmount.safeAdd( + singleFillResults.protocolFeePaid + ); // Subtract fee from makerAssetFilledAmount for the net amount acquired. makerAssetAcquiredAmount = singleFillResults.makerAssetFilledAmount.safeSub(