diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-07-09 11:29:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-09 11:29:31 +0800 |
commit | 14c96e0b97725244dcd7f671dac97f18d473432f (patch) | |
tree | 46e8364474df902b390c2d7267f664d72dc73391 | |
parent | ef03d1ae13a3a1ceb56ff2a0682ce909e50d3bf6 (diff) | |
parent | f1613d36ac1381a978a97fd0ee7cd7bc95e7d66a (diff) | |
download | dexon-sol-tools-14c96e0b97725244dcd7f671dac97f18d473432f.tar dexon-sol-tools-14c96e0b97725244dcd7f671dac97f18d473432f.tar.gz dexon-sol-tools-14c96e0b97725244dcd7f671dac97f18d473432f.tar.bz2 dexon-sol-tools-14c96e0b97725244dcd7f671dac97f18d473432f.tar.lz dexon-sol-tools-14c96e0b97725244dcd7f671dac97f18d473432f.tar.xz dexon-sol-tools-14c96e0b97725244dcd7f671dac97f18d473432f.tar.zst dexon-sol-tools-14c96e0b97725244dcd7f671dac97f18d473432f.zip |
Merge pull request #839 from 0xProject/refactor/contracts/forwarder-interfaces
Refactor forwarder file structure
20 files changed, 844 insertions, 373 deletions
diff --git a/packages/contracts/src/2.0.0/forwarder/Forwarder.sol b/packages/contracts/src/2.0.0/forwarder/Forwarder.sol index 38128d93e..fc17a4c72 100644 --- a/packages/contracts/src/2.0.0/forwarder/Forwarder.sol +++ b/packages/contracts/src/2.0.0/forwarder/Forwarder.sol @@ -19,22 +19,22 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "./MixinWethFees.sol"; -import "./MixinMarketSellTokens.sol"; -import "./MixinMarketBuyTokens.sol"; +import "./MixinFees.sol"; +import "./MixinForwarderCore.sol"; import "./MixinConstants.sol"; -import "../utils/Ownable/Ownable.sol"; +import "./MixinMarketBuyZrx.sol"; +import "./MixinExpectedResults.sol"; +import "./MixinTransfer.sol"; contract Forwarder is - Ownable, MixinConstants, - MixinWethFees, + MixinExpectedResults, + MixinFees, MixinMarketBuyZrx, - MixinMarketBuyTokens, - MixinMarketSellTokens + MixinTransfer, + MixinForwarderCore { - uint256 constant internal MAX_UINT = 2**256 - 1; constructor ( address _exchange, @@ -45,7 +45,6 @@ contract Forwarder is bytes memory _wethAssetData ) public - Ownable() MixinConstants( _exchange, _etherToken, @@ -53,30 +52,6 @@ contract Forwarder is _zrxAssetData, _wethAssetData ) - { - setERC20ProxyApproval(_erc20AssetProxyId); - } - - /// @dev Default payabale function, this allows us to withdraw WETH - function () - public - payable - { - require( - msg.sender == address(ETHER_TOKEN), - "DEFAULT_FUNCTION_WETH_CONTRACT_ONLY" - ); - } - - /// @dev Sets the allowances to the proxy for this contract - function setERC20ProxyApproval(bytes4 erc20AssetProxyId) - public - onlyOwner - { - address proxyAddress = EXCHANGE.getAssetProxy(erc20AssetProxyId); - if (proxyAddress != address(0)) { - ETHER_TOKEN.approve(proxyAddress, MAX_UINT); - ZRX_TOKEN.approve(proxyAddress, MAX_UINT); - } - } + MixinForwarderCore() + {} } diff --git a/packages/contracts/src/2.0.0/forwarder/MixinConstants.sol b/packages/contracts/src/2.0.0/forwarder/MixinConstants.sol index ea82e9d64..2b064d579 100644 --- a/packages/contracts/src/2.0.0/forwarder/MixinConstants.sol +++ b/packages/contracts/src/2.0.0/forwarder/MixinConstants.sol @@ -18,20 +18,12 @@ pragma solidity 0.4.24; -import "../protocol/Exchange/Exchange.sol"; -import { WETH9 as EtherToken } from "../tokens/WETH9/WETH9.sol"; -import "../tokens/ERC20Token/IERC20Token.sol"; +import "./mixins/MConstants.sol"; -contract MixinConstants { - - // solhint-disable var-name-mixedcase - Exchange internal EXCHANGE; - EtherToken internal ETHER_TOKEN; - IERC20Token internal ZRX_TOKEN; - bytes internal ZRX_ASSET_DATA; - bytes internal WETH_ASSET_DATA; - // solhint-enable var-name-mixedcase +contract MixinConstants is + MConstants +{ constructor ( address _exchange, @@ -42,11 +34,10 @@ contract MixinConstants { ) public { - EXCHANGE = Exchange(_exchange); - ETHER_TOKEN = EtherToken(_etherToken); + EXCHANGE = IExchange(_exchange); + ETHER_TOKEN = IEtherToken(_etherToken); ZRX_TOKEN = IERC20Token(_zrxToken); ZRX_ASSET_DATA = _zrxAssetData; WETH_ASSET_DATA = _wethAssetData; } - -}
\ No newline at end of file +} diff --git a/packages/contracts/src/2.0.0/forwarder/MixinERC721.sol b/packages/contracts/src/2.0.0/forwarder/MixinERC721.sol deleted file mode 100644 index b5be223b6..000000000 --- a/packages/contracts/src/2.0.0/forwarder/MixinERC721.sol +++ /dev/null @@ -1,66 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; -pragma experimental ABIEncoderV2; - -import "../utils/LibBytes/LibBytes.sol"; -import "../tokens/ERC721Token/IERC721Token.sol"; - - -contract MixinERC721 { - - using LibBytes for bytes; - - bytes4 constant internal ERC721_RECEIVED = bytes4(keccak256("onERC721Received(address,uint256,bytes)")); - bytes4 constant internal ERC721_RECEIVED_OPERATOR = bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); - - function onERC721Received(address, uint256, bytes memory) - public - pure - returns(bytes4) - { - return ERC721_RECEIVED; - } - - function onERC721Received(address, address, uint256, bytes memory) - public - pure - returns(bytes4) - { - return ERC721_RECEIVED_OPERATOR; - } - - function transferERC721Token( - bytes memory assetData, - address to - ) - internal - { - // Decode asset data. - address token = assetData.readAddress(16); - uint256 tokenId = assetData.readUint256(36); - bytes memory receiverData = assetData.readBytesWithLength(100); - IERC721Token(token).safeTransferFrom( - address(this), - to, - tokenId, - receiverData - ); - } -} diff --git a/packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol b/packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol index 6406d1d37..a575c9675 100644 --- a/packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol +++ b/packages/contracts/src/2.0.0/forwarder/MixinExpectedResults.sol @@ -23,13 +23,15 @@ 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 Calculates a total FillResults for buying makerAssetFillAmount over all orders. @@ -61,6 +63,30 @@ contract MixinExpectedResults is return totalFillResults; } + /// @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 takerAssetFillAmount A number representing the amount of this order to fill. + /// @return totalFillResults Amounts filled and fees paid by maker and taker. + function calculateMarketSellResults( + LibOrder.Order[] memory orders, + uint256 takerAssetFillAmount + ) + public + view + returns (FillResults memory totalFillResults) + { + for (uint256 i = 0; i < orders.length; i++) { + uint256 remainingTakerAssetFillAmount = safeSub(takerAssetFillAmount, totalFillResults.takerAssetFilledAmount); + FillResults memory singleFillResult = calculateFillResults(orders[i], remainingTakerAssetFillAmount); + addFillResults(totalFillResults, singleFillResult); + if (totalFillResults.takerAssetFilledAmount == takerAssetFillAmount) { + break; + } + } + return totalFillResults; + } + /// @dev Calculates fill results for buyFeeTokens. This handles fees on buying ZRX /// so the end result is the expected amount of ZRX (not less after fees). /// @param orders An array of Order struct containing order specifications. @@ -132,28 +158,4 @@ contract MixinExpectedResults is ); return fillResults; } - - /// @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 takerAssetFillAmount A number representing the amount of this order to fill. - /// @return totalFillResults Amounts filled and fees paid by maker and taker. - function calculateMarketSellResults( - LibOrder.Order[] memory orders, - uint256 takerAssetFillAmount - ) - internal - view - returns (FillResults memory totalFillResults) - { - for (uint256 i = 0; i < orders.length; i++) { - uint256 remainingTakerAssetFillAmount = safeSub(takerAssetFillAmount, totalFillResults.takerAssetFilledAmount); - FillResults memory singleFillResult = calculateFillResults(orders[i], remainingTakerAssetFillAmount); - addFillResults(totalFillResults, singleFillResult); - if (totalFillResults.takerAssetFilledAmount == takerAssetFillAmount) { - break; - } - } - return totalFillResults; - } } diff --git a/packages/contracts/src/2.0.0/forwarder/MixinWethFees.sol b/packages/contracts/src/2.0.0/forwarder/MixinFees.sol index 12392be79..8ea00a1d5 100644 --- a/packages/contracts/src/2.0.0/forwarder/MixinWethFees.sol +++ b/packages/contracts/src/2.0.0/forwarder/MixinFees.sol @@ -18,19 +18,31 @@ pragma solidity 0.4.24; -import { WETH9 as EtherToken } from "../tokens/WETH9/WETH9.sol"; import "../protocol/Exchange/libs/LibMath.sol"; -import "./MixinConstants.sol"; +import "./mixins/MConstants.sol"; +import "./mixins/MFees.sol"; -contract MixinWethFees is +contract MixinFees is LibMath, - MixinConstants + MConstants, + MFees { - uint16 constant public PERCENTAGE_DENOMINATOR = 10000; // 9800 == 98%, 10000 == 100% - uint16 constant public MAX_FEE = 1000; // 10% - uint16 constant public ALLOWABLE_EXCHANGE_PERCENTAGE = 9500; // 95% + uint16 constant public PERCENTAGE_DENOMINATOR = 10000; // 9800 == 98%, 10000 == 100% + uint16 constant public MAX_FEE = 1000; // 10% + uint16 constant public ALLOWABLE_EXCHANGE_PERCENTAGE = 9500; // 95% + + /// @dev Default payabale function, this allows us to withdraw WETH + function () + public + payable + { + require( + msg.sender == address(ETHER_TOKEN), + "DEFAULT_FUNCTION_WETH_CONTRACT_ONLY" + ); + } /// @dev Pays the feeRecipient feeProportion of the total takerEthAmount, denominated in ETH /// @param takerEthAmount The total amount that was transacted in WETH, fees are calculated from this value. diff --git a/packages/contracts/src/2.0.0/forwarder/MixinMarketBuyTokens.sol b/packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol index 165fec61f..eadeaf5ba 100644 --- a/packages/contracts/src/2.0.0/forwarder/MixinMarketBuyTokens.sol +++ b/packages/contracts/src/2.0.0/forwarder/MixinForwarderCore.sol @@ -20,25 +20,118 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../utils/LibBytes/LibBytes.sol"; -import "./MixinWethFees.sol"; -import "./MixinMarketBuyZrx.sol"; -import "./MixinExpectedResults.sol"; -import "./MixinERC20.sol"; -import "./MixinERC721.sol"; -import "./MixinConstants.sol"; +import "./mixins/MFees.sol"; +import "./mixins/MMarketBuyZrx.sol"; +import "./mixins/MExpectedResults.sol"; +import "./mixins/MTransfer.sol"; +import "./mixins/MConstants.sol"; +import "./mixins/MForwarderCore.sol"; import "../protocol/Exchange/libs/LibOrder.sol"; +import "../protocol/Exchange/libs/LibFillResults.sol"; -contract MixinMarketBuyTokens is - MixinConstants, - MixinWethFees, - MixinMarketBuyZrx, - MixinExpectedResults, - MixinERC20, - MixinERC721 +contract MixinForwarderCore is + LibFillResults, + MConstants, + MExpectedResults, + MFees, + MMarketBuyZrx, + MTransfer, + MForwarderCore { - bytes4 public constant ERC20_DATA_ID = bytes4(keccak256("ERC20Token(address)")); - bytes4 public constant ERC721_DATA_ID = bytes4(keccak256("ERC721Token(address,uint256,bytes)")); + bytes4 constant internal ERC20_DATA_ID = bytes4(keccak256("ERC20Token(address)")); + bytes4 constant internal ERC721_DATA_ID = bytes4(keccak256("ERC721Token(address,uint256,bytes)")); + uint256 constant internal MAX_UINT = 2**256 - 1; + + constructor () + public + { + address proxyAddress = EXCHANGE.getAssetProxy(ERC20_DATA_ID); + if (proxyAddress != address(0)) { + ETHER_TOKEN.approve(proxyAddress, MAX_UINT); + ZRX_TOKEN.approve(proxyAddress, MAX_UINT); + } + } + + /// @dev Market sells ETH for ERC20 tokens, performing fee abstraction if required. This does not support ERC721 tokens. This function is payable + /// and will convert all incoming ETH into WETH and perform the trade on behalf of the caller. + /// This function allows for a deduction of a proportion of incoming ETH sent to the feeRecipient. + /// The caller is sent all tokens from the operation. + /// If the purchased token amount does not meet an acceptable threshold then this function reverts. + /// @param orders An array of Order struct containing order specifications. + /// @param signatures An array of Proof that order has been created by maker. + /// @param feeOrders An array of Order struct containing order specifications for fees. + /// @param feeSignatures An array of Proof that order has been created by maker for the fee orders. + /// @param feeProportion A proportion deducted off the incoming ETH and sent to feeRecipient. The maximum value for this + /// is 1000, aka 10%. Supports up to 2 decimal places. I.e 0.59% is 59. + /// @param feeRecipient An address of the fee recipient whom receives feeProportion of ETH. + /// @return FillResults amounts filled and fees paid by maker and taker. + function marketSellEthForERC20( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + LibOrder.Order[] memory feeOrders, + bytes[] memory feeSignatures, + uint16 feeProportion, + address feeRecipient + ) + public + payable + returns (FillResults memory totalFillResults) + { + uint256 takerEthAmount = msg.value; + require( + takerEthAmount > 0, + "VALUE_GREATER_THAN_ZERO" + ); + // Deduct the fee from the total amount of ETH sent in + uint256 ethFeeAmount = payEthFee( + takerEthAmount, + feeProportion, + feeRecipient + ); + uint256 wethSellAmount = safeSub(takerEthAmount, ethFeeAmount); + + // Deposit the remaining to be used for trading + ETHER_TOKEN.deposit.value(wethSellAmount)(); + // Populate the known assetData, as it is always WETH the caller can provide null bytes to save gas + // marketSellOrders fills the remaining + address makerTokenAddress = LibBytes.readAddress(orders[0].makerAssetData, 16); + orders[0].takerAssetData = WETH_ASSET_DATA; + if (makerTokenAddress == address(ZRX_TOKEN)) { + // If this is ZRX then we market sell from the orders, rather than a 2 step of buying ZRX fees from feeOrders + // then buying ZRX from orders + totalFillResults = marketSellEthForZRXInternal( + orders, + signatures, + wethSellAmount + ); + } else { + totalFillResults = marketSellEthForERC20Internal( + orders, + signatures, + feeOrders, + feeSignatures, + wethSellAmount + ); + } + // Prevent accidental WETH owned by this contract and it being spent + require( + takerEthAmount >= totalFillResults.takerAssetFilledAmount, + "INVALID_MSG_VALUE" + ); + // Ensure no WETH is left in this contract + require( + wethSellAmount == totalFillResults.takerAssetFilledAmount, + "UNACCEPTABLE_THRESHOLD" + ); + // Transfer all tokens to msg.sender + transferERC20Token( + makerTokenAddress, + msg.sender, + totalFillResults.makerAssetFilledAmount + ); + return totalFillResults; + } /// @dev Buys the exact amount of assets (ERC20 and ERC721), performing fee abstraction if required. /// All order assets must be of the same type. Deducts a proportional fee to fee recipient. @@ -113,6 +206,85 @@ contract MixinMarketBuyTokens is return totalFillResults; } + /// @dev Market sells WETH for ERC20 tokens. + /// @param orders An array of Order struct containing order specifications. + /// @param signatures An array of Proof that order has been created by maker. + /// @param feeOrders An array of Order struct containing order specifications for fees. + /// @param feeSignatures An array of Proof that order has been created by maker for the fee orders. + /// @param wethSellAmount The amount of WETH to sell. + /// @return FillResults amounts filled and fees paid by maker and taker. + function marketSellEthForERC20Internal( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + LibOrder.Order[] memory feeOrders, + bytes[] memory feeSignatures, + uint256 wethSellAmount + ) + internal + returns (FillResults memory totalFillResults) + { + uint256 remainingWethSellAmount = wethSellAmount; + FillResults memory calculatedMarketSellResults = calculateMarketSellResults(orders, wethSellAmount); + if (calculatedMarketSellResults.takerFeePaid > 0) { + // Fees are required for these orders. Buy enough ZRX to cover the future market buy + FillResults memory feeTokensResults = marketBuyZrxInternal( + feeOrders, + feeSignatures, + calculatedMarketSellResults.takerFeePaid + ); + // Ensure the token abstraction was fair if fees were proportionally too high, we fail + require( + isAcceptableThreshold( + wethSellAmount, + safeSub(wethSellAmount, feeTokensResults.takerAssetFilledAmount) + ), + "UNACCEPTABLE_THRESHOLD" + ); + remainingWethSellAmount = safeSub(remainingWethSellAmount, feeTokensResults.takerAssetFilledAmount); + totalFillResults.takerFeePaid = feeTokensResults.takerFeePaid; + totalFillResults.takerAssetFilledAmount = feeTokensResults.takerAssetFilledAmount; + } + // Make our market sell to buy the requested tokens with the remaining balance + FillResults memory requestedTokensResults = EXCHANGE.marketSellOrders( + orders, + remainingWethSellAmount, + signatures + ); + // Update our return FillResult with the market sell + addFillResults(totalFillResults, requestedTokensResults); + return totalFillResults; + } + + /// @dev Market sells WETH for ZRX tokens. + /// @param orders An array of Order struct containing order specifications. + /// @param signatures An array of Proof that order has been created by maker. + /// @param wethSellAmount The amount of WETH to sell. + /// @return FillResults amounts filled and fees paid by maker and taker. + function marketSellEthForZRXInternal( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + uint256 wethSellAmount + ) + internal + returns (FillResults memory totalFillResults) + { + // Make our market sell to buy the requested tokens with the remaining balance + totalFillResults = EXCHANGE.marketSellOrders( + orders, + wethSellAmount, + signatures + ); + // Exchange does not special case ZRX in the makerAssetFilledAmount, if fees were deducted then using this amount + // for future transfers is invalid. + uint256 zrxAmountBought = safeSub(totalFillResults.makerAssetFilledAmount, totalFillResults.takerFeePaid); + require( + isAcceptableThreshold(totalFillResults.makerAssetFilledAmount, zrxAmountBought), + "UNACCEPTABLE_THRESHOLD" + ); + totalFillResults.makerAssetFilledAmount = zrxAmountBought; + return totalFillResults; + } + /// @dev Buys an exact amount of an ERC20 token using WETH. /// @param orders Orders to fill. The maker asset is the ERC20 token to buy. The taker asset is WETH. /// @param signatures Proof that the orders were created by their respective makers. @@ -127,8 +299,8 @@ contract MixinMarketBuyTokens is bytes[] memory feeSignatures, uint256 makerTokenFillAmount ) - private - returns (FillResults memory totalFillResults) + internal + returns (LibFillResults.FillResults memory totalFillResults) { // We read the maker token address to check if it is ZRX and later use it for transfer address makerTokenAddress = LibBytes.readAddress(orders[0].makerAssetData, 16); @@ -189,7 +361,7 @@ contract MixinMarketBuyTokens is ); } // Transfer all purchased tokens to msg.sender - transferToken( + transferERC20Token( makerTokenAddress, msg.sender, marketBuyResults.makerAssetFilledAmount @@ -209,8 +381,8 @@ contract MixinMarketBuyTokens is LibOrder.Order[] memory feeOrders, bytes[] memory feeSignatures ) - private - returns (FillResults memory totalFillResults) + internal + returns (LibFillResults.FillResults memory totalFillResults) { uint256 totalZrxFeeAmount; uint256 ordersLength = orders.length; diff --git a/packages/contracts/src/2.0.0/forwarder/MixinMarketBuyZrx.sol b/packages/contracts/src/2.0.0/forwarder/MixinMarketBuyZrx.sol index cc98cb63d..e272f8aad 100644 --- a/packages/contracts/src/2.0.0/forwarder/MixinMarketBuyZrx.sol +++ b/packages/contracts/src/2.0.0/forwarder/MixinMarketBuyZrx.sol @@ -19,17 +19,18 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../protocol/Exchange/Exchange.sol"; import "../protocol/Exchange/libs/LibFillResults.sol"; import "../protocol/Exchange/libs/LibOrder.sol"; import "../protocol/Exchange/libs/LibMath.sol"; -import "./MixinConstants.sol"; +import "./mixins/MConstants.sol"; +import "./mixins/MMarketBuyZrx.sol"; contract MixinMarketBuyZrx is LibMath, LibFillResults, - MixinConstants + MConstants, + MMarketBuyZrx { /// @dev Buys zrxBuyAmount of ZRX fee tokens, taking into account the fees on buying fee tokens. This will guarantee diff --git a/packages/contracts/src/2.0.0/forwarder/MixinMarketSellTokens.sol b/packages/contracts/src/2.0.0/forwarder/MixinMarketSellTokens.sol deleted file mode 100644 index f4f509846..000000000 --- a/packages/contracts/src/2.0.0/forwarder/MixinMarketSellTokens.sol +++ /dev/null @@ -1,197 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; -pragma experimental ABIEncoderV2; - -import "../protocol/Exchange/libs/LibOrder.sol"; -import "../utils/LibBytes/LibBytes.sol"; -import "./MixinWethFees.sol"; -import "./MixinExpectedResults.sol"; -import "./MixinERC20.sol"; -import "./MixinConstants.sol"; -import "./MixinMarketBuyZrx.sol"; - - -contract MixinMarketSellTokens is - MixinConstants, - MixinWethFees, - MixinMarketBuyZrx, - MixinExpectedResults, - MixinERC20 -{ - /// @dev Market sells ETH for ERC20 tokens, performing fee abstraction if required. This does not support ERC721 tokens. This function is payable - /// and will convert all incoming ETH into WETH and perform the trade on behalf of the caller. - /// This function allows for a deduction of a proportion of incoming ETH sent to the feeRecipient. - /// The caller is sent all tokens from the operation. - /// If the purchased token amount does not meet an acceptable threshold then this function reverts. - /// @param orders An array of Order struct containing order specifications. - /// @param signatures An array of Proof that order has been created by maker. - /// @param feeOrders An array of Order struct containing order specifications for fees. - /// @param feeSignatures An array of Proof that order has been created by maker for the fee orders. - /// @param feeProportion A proportion deducted off the incoming ETH and sent to feeRecipient. The maximum value for this - /// is 1000, aka 10%. Supports up to 2 decimal places. I.e 0.59% is 59. - /// @param feeRecipient An address of the fee recipient whom receives feeProportion of ETH. - /// @return FillResults amounts filled and fees paid by maker and taker. - function marketSellEthForERC20( - LibOrder.Order[] memory orders, - bytes[] memory signatures, - LibOrder.Order[] memory feeOrders, - bytes[] memory feeSignatures, - uint16 feeProportion, - address feeRecipient - ) - public - payable - returns (FillResults memory totalFillResults) - { - uint256 takerEthAmount = msg.value; - require( - takerEthAmount > 0, - "VALUE_GREATER_THAN_ZERO" - ); - // Deduct the fee from the total amount of ETH sent in - uint256 ethFeeAmount = payEthFee( - takerEthAmount, - feeProportion, - feeRecipient - ); - uint256 wethSellAmount = safeSub(takerEthAmount, ethFeeAmount); - - // Deposit the remaining to be used for trading - ETHER_TOKEN.deposit.value(wethSellAmount)(); - // Populate the known assetData, as it is always WETH the caller can provide null bytes to save gas - // marketSellOrders fills the remaining - address makerTokenAddress = LibBytes.readAddress(orders[0].makerAssetData, 16); - orders[0].takerAssetData = WETH_ASSET_DATA; - if (makerTokenAddress == address(ZRX_TOKEN)) { - // If this is ZRX then we market sell from the orders, rather than a 2 step of buying ZRX fees from feeOrders - // then buying ZRX from orders - totalFillResults = marketSellEthForZRXInternal( - orders, - signatures, - wethSellAmount - ); - } else { - totalFillResults = marketSellEthForERC20Internal( - orders, - signatures, - feeOrders, - feeSignatures, - wethSellAmount - ); - } - // Prevent accidental WETH owned by this contract and it being spent - require( - takerEthAmount >= totalFillResults.takerAssetFilledAmount, - "INVALID_MSG_VALUE" - ); - // Ensure no WETH is left in this contract - require( - wethSellAmount == totalFillResults.takerAssetFilledAmount, - "UNACCEPTABLE_THRESHOLD" - ); - // Transfer all tokens to msg.sender - transferToken( - makerTokenAddress, - msg.sender, - totalFillResults.makerAssetFilledAmount - ); - return totalFillResults; - } - - /// @dev Market sells WETH for ERC20 tokens. - /// @param orders An array of Order struct containing order specifications. - /// @param signatures An array of Proof that order has been created by maker. - /// @param feeOrders An array of Order struct containing order specifications for fees. - /// @param feeSignatures An array of Proof that order has been created by maker for the fee orders. - /// @param wethSellAmount The amount of WETH to sell. - /// @return FillResults amounts filled and fees paid by maker and taker. - function marketSellEthForERC20Internal( - LibOrder.Order[] memory orders, - bytes[] memory signatures, - LibOrder.Order[] memory feeOrders, - bytes[] memory feeSignatures, - uint256 wethSellAmount - ) - internal - returns (FillResults memory totalFillResults) - { - uint256 remainingWethSellAmount = wethSellAmount; - FillResults memory calculatedMarketSellResults = calculateMarketSellResults(orders, wethSellAmount); - if (calculatedMarketSellResults.takerFeePaid > 0) { - // Fees are required for these orders. Buy enough ZRX to cover the future market buy - FillResults memory feeTokensResults = marketBuyZrxInternal( - feeOrders, - feeSignatures, - calculatedMarketSellResults.takerFeePaid - ); - // Ensure the token abstraction was fair if fees were proportionally too high, we fail - require( - isAcceptableThreshold( - wethSellAmount, - safeSub(wethSellAmount, feeTokensResults.takerAssetFilledAmount) - ), - "UNACCEPTABLE_THRESHOLD" - ); - remainingWethSellAmount = safeSub(remainingWethSellAmount, feeTokensResults.takerAssetFilledAmount); - totalFillResults.takerFeePaid = feeTokensResults.takerFeePaid; - totalFillResults.takerAssetFilledAmount = feeTokensResults.takerAssetFilledAmount; - } - // Make our market sell to buy the requested tokens with the remaining balance - FillResults memory requestedTokensResults = EXCHANGE.marketSellOrders( - orders, - remainingWethSellAmount, - signatures - ); - // Update our return FillResult with the market sell - addFillResults(totalFillResults, requestedTokensResults); - return totalFillResults; - } - - /// @dev Market sells WETH for ZRX tokens. - /// @param orders An array of Order struct containing order specifications. - /// @param signatures An array of Proof that order has been created by maker. - /// @param wethSellAmount The amount of WETH to sell. - /// @return FillResults amounts filled and fees paid by maker and taker. - function marketSellEthForZRXInternal( - LibOrder.Order[] memory orders, - bytes[] memory signatures, - uint256 wethSellAmount - ) - internal - returns (FillResults memory totalFillResults) - { - // Make our market sell to buy the requested tokens with the remaining balance - totalFillResults = EXCHANGE.marketSellOrders( - orders, - wethSellAmount, - signatures - ); - // Exchange does not special case ZRX in the makerAssetFilledAmount, if fees were deducted then using this amount - // for future transfers is invalid. - uint256 zrxAmountBought = safeSub(totalFillResults.makerAssetFilledAmount, totalFillResults.takerFeePaid); - require( - isAcceptableThreshold(totalFillResults.makerAssetFilledAmount, zrxAmountBought), - "UNACCEPTABLE_THRESHOLD" - ); - totalFillResults.makerAssetFilledAmount = zrxAmountBought; - return totalFillResults; - } - -} diff --git a/packages/contracts/src/2.0.0/forwarder/MixinERC20.sol b/packages/contracts/src/2.0.0/forwarder/MixinTransfer.sol index 23c130d69..6c49330f2 100644 --- a/packages/contracts/src/2.0.0/forwarder/MixinERC20.sol +++ b/packages/contracts/src/2.0.0/forwarder/MixinTransfer.sol @@ -18,12 +18,47 @@ pragma solidity 0.4.24; +import "../utils/LibBytes/LibBytes.sol"; +import "../tokens/ERC721Token/IERC721Token.sol"; +import "./mixins/MTransfer.sol"; -contract MixinERC20 { + +contract MixinTransfer is + MTransfer +{ + + using LibBytes for bytes; bytes4 constant internal ERC20_TRANSFER_SELECTOR = bytes4(keccak256("transfer(address,uint256)")); + bytes4 constant internal ERC721_RECEIVED = bytes4(keccak256("onERC721Received(address,uint256,bytes)")); + bytes4 constant internal ERC721_RECEIVED_OPERATOR = bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); - function transferToken( + function onERC721Received( + address, + uint256, + bytes memory + ) + public + pure + returns(bytes4) + { + return ERC721_RECEIVED; + } + + function onERC721Received( + address, + address, + uint256, + bytes memory + ) + public + pure + returns(bytes4) + { + return ERC721_RECEIVED_OPERATOR; + } + + function transferERC20Token( address token, address to, uint256 amount @@ -64,4 +99,22 @@ contract MixinERC20 { "TRANSFER_FAILED" ); } + + function transferERC721Token( + bytes memory assetData, + address to + ) + internal + { + // Decode asset data. + address token = assetData.readAddress(16); + uint256 tokenId = assetData.readUint256(36); + bytes memory receiverData = assetData.readBytesWithLength(100); + IERC721Token(token).safeTransferFrom( + address(this), + to, + tokenId, + receiverData + ); + } } diff --git a/packages/contracts/src/2.0.0/forwarder/interfaces/IExpectedResults.sol b/packages/contracts/src/2.0.0/forwarder/interfaces/IExpectedResults.sol new file mode 100644 index 000000000..89187b750 --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/interfaces/IExpectedResults.sol @@ -0,0 +1,66 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "../../protocol/Exchange/libs/LibFillResults.sol"; +import "../../protocol/Exchange/libs/LibOrder.sol"; + + +contract IExpectedResults { + + /// @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 makerAssetFillAmount A number representing the amount of this order to fill. + /// @return totalFillResults Amounts filled and fees paid by maker and taker. + function calculateMarketBuyResults( + LibOrder.Order[] memory orders, + uint256 makerAssetFillAmount + ) + public + view + returns (LibFillResults.FillResults memory totalFillResults); + + /// @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 takerAssetFillAmount A number representing the amount of this order to fill. + /// @return totalFillResults Amounts filled and fees paid by maker and taker. + function calculateMarketSellResults( + LibOrder.Order[] memory orders, + uint256 takerAssetFillAmount + ) + public + view + returns (LibFillResults.FillResults memory totalFillResults); + + /// @dev Calculates fill results for buyFeeTokens. This handles fees on buying ZRX + /// so the end result is the expected amount of ZRX (not less after fees). + /// @param orders An array of Order struct containing order specifications. + /// @param zrxFillAmount A number representing the amount zrx to buy + /// @return totalFillResults Expected fill result amounts from buying fees + function calculateMarketBuyZrxResults( + LibOrder.Order[] memory orders, + uint256 zrxFillAmount + ) + public + view + returns (LibFillResults.FillResults memory totalFillResults); +} diff --git a/packages/contracts/src/2.0.0/forwarder/interfaces/IForwarder.sol b/packages/contracts/src/2.0.0/forwarder/interfaces/IForwarder.sol new file mode 100644 index 000000000..745dd29a9 --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/interfaces/IForwarder.sol @@ -0,0 +1,30 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "./IForwarderCore.sol"; +import "./IExpectedResults.sol"; + + +// solhint-disable no-empty-blocks +contract IForwarder is + IForwarderCore, + IExpectedResults +{} diff --git a/packages/contracts/src/2.0.0/forwarder/interfaces/IForwarderCore.sol b/packages/contracts/src/2.0.0/forwarder/interfaces/IForwarderCore.sol new file mode 100644 index 000000000..7ac2a8af3 --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/interfaces/IForwarderCore.sol @@ -0,0 +1,79 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "../../protocol/Exchange/libs/LibOrder.sol"; +import "../../protocol/Exchange/libs/LibFillResults.sol"; + + +contract IForwarderCore { + + /// @dev Market sells ETH for ERC20 tokens, performing fee abstraction if required. This does not support ERC721 tokens. This function is payable + /// and will convert all incoming ETH into WETH and perform the trade on behalf of the caller. + /// This function allows for a deduction of a proportion of incoming ETH sent to the feeRecipient. + /// The caller is sent all tokens from the operation. + /// If the purchased token amount does not meet an acceptable threshold then this function reverts. + /// @param orders An array of Order struct containing order specifications. + /// @param signatures An array of Proof that order has been created by maker. + /// @param feeOrders An array of Order struct containing order specifications for fees. + /// @param feeSignatures An array of Proof that order has been created by maker for the fee orders. + /// @param feeProportion A proportion deducted off the incoming ETH and sent to feeRecipient. The maximum value for this + /// is 1000, aka 10%. Supports up to 2 decimal places. I.e 0.59% is 59. + /// @param feeRecipient An address of the fee recipient whom receives feeProportion of ETH. + /// @return FillResults amounts filled and fees paid by maker and taker. + function marketSellEthForERC20( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + LibOrder.Order[] memory feeOrders, + bytes[] memory feeSignatures, + uint16 feeProportion, + address feeRecipient + ) + public + payable + returns (LibFillResults.FillResults memory totalFillResults); + + /// @dev Buys the exact amount of assets (ERC20 and ERC721), performing fee abstraction if required. + /// All order assets must be of the same type. Deducts a proportional fee to fee recipient. + /// This function is payable and will convert all incoming ETH into WETH and perform the trade on behalf of the caller. + /// The caller is sent all assets from the fill of orders. This function will revert unless the requested amount of assets are purchased. + /// Any excess ETH sent will be returned to the caller + /// @param orders An array of Order struct containing order specifications. + /// @param signatures An array of Proof that order has been created by maker. + /// @param feeOrders An array of Order struct containing order specifications for fees. + /// @param makerTokenFillAmount The amount of maker asset to buy. + /// @param feeSignatures An array of Proof that order has been created by maker for the fee orders. + /// @param feeProportion A proportion deducted off the ETH spent and sent to feeRecipient. The maximum value for this + /// is 1000, aka 10%. Supports up to 2 decimal places. I.e 0.59% is 59. + /// @param feeRecipient An address of the fee recipient whom receives feeProportion of ETH. + /// @return FillResults amounts filled and fees paid by maker and taker. + function marketBuyTokensWithEth( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + LibOrder.Order[] memory feeOrders, + bytes[] memory feeSignatures, + uint256 makerTokenFillAmount, + uint16 feeProportion, + address feeRecipient + ) + public + payable + returns (LibFillResults.FillResults memory totalFillResults); +} diff --git a/packages/contracts/src/2.0.0/forwarder/mixins/MConstants.sol b/packages/contracts/src/2.0.0/forwarder/mixins/MConstants.sol new file mode 100644 index 000000000..348bf169e --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/mixins/MConstants.sol @@ -0,0 +1,35 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "../../protocol/Exchange/interfaces/IExchange.sol"; +import "../../tokens/EtherToken/IEtherToken.sol"; +import "../../tokens/ERC20Token/IERC20Token.sol"; + + +contract MConstants { + + // solhint-disable var-name-mixedcase + IExchange internal EXCHANGE; + IEtherToken internal ETHER_TOKEN; + IERC20Token internal ZRX_TOKEN; + bytes internal ZRX_ASSET_DATA; + bytes internal WETH_ASSET_DATA; + // solhint-enable var-name-mixedcase +} diff --git a/packages/contracts/src/2.0.0/forwarder/mixins/MExpectedResults.sol b/packages/contracts/src/2.0.0/forwarder/mixins/MExpectedResults.sol new file mode 100644 index 000000000..cf03bb32e --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/mixins/MExpectedResults.sol @@ -0,0 +1,42 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "../../protocol/Exchange/libs/LibFillResults.sol"; +import "../../protocol/Exchange/libs/LibOrder.sol"; +import "../interfaces/IExpectedResults.sol"; + + +contract MExpectedResults is + IExpectedResults +{ + + /// @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 (LibFillResults.FillResults memory fillResults); +} diff --git a/packages/contracts/src/2.0.0/forwarder/mixins/MFees.sol b/packages/contracts/src/2.0.0/forwarder/mixins/MFees.sol new file mode 100644 index 000000000..f332637ea --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/mixins/MFees.sol @@ -0,0 +1,63 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + + +contract MFees { + + /// @dev Pays the feeRecipient feeProportion of the total takerEthAmount, denominated in ETH + /// @param takerEthAmount The total amount that was transacted in WETH, fees are calculated from this value. + /// @param feeProportion The proportion of fees + /// @param feeRecipient The recipient of the fees + /// @return ethFeeAmount Amount of ETH paid to feeRecipient as fee. + function payEthFee( + uint256 takerEthAmount, + uint16 feeProportion, + address feeRecipient + ) + internal + returns (uint256 ethFeeAmount); + + /// @dev Withdraws the remaining WETH, deduct and pay fees from this amount based on the takerTokenAmount to the feeRecipient. + /// If a user overpaid ETH initially, the fees are calculated from the amount traded and deducted from withdrawAmount. + /// Any remaining ETH is sent back to the user. + /// @param ethWithdrawAmount The amount to withdraw from the WETH contract. + /// @param wethAmountSold The total amount that was transacted in WETH, fees are calculated from this value. + /// @param feeProportion The proportion of fees + /// @param feeRecipient The recipient of the fees + function withdrawPayAndDeductEthFee( + uint256 ethWithdrawAmount, + uint256 wethAmountSold, + uint16 feeProportion, + address feeRecipient + ) + internal; + + /// @dev Checks whether the amount of tokens sold against the amount of tokens requested + /// is within a certain threshold. This ensures the caller gets a fair deal when + /// performing any token fee abstraction. Threshold is 95%. If fee abstraction costs more than + /// 5% of the total transaction, we return false. + /// @param requestedSellAmount The amount the user requested, or sent in to a payable function + /// @param tokenAmountSold The amount of the token that was sold after fee abstraction + /// @return bool of whether this is within an acceptable threshold + function isAcceptableThreshold(uint256 requestedSellAmount, uint256 tokenAmountSold) + internal + pure + returns (bool); +} diff --git a/packages/contracts/src/2.0.0/forwarder/mixins/MForwarderCore.sol b/packages/contracts/src/2.0.0/forwarder/mixins/MForwarderCore.sol new file mode 100644 index 000000000..4a54e76b1 --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/mixins/MForwarderCore.sol @@ -0,0 +1,92 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "../../protocol/Exchange/libs/LibOrder.sol"; +import "../../protocol/Exchange/libs/LibFillResults.sol"; +import "../interfaces/IForwarderCore.sol"; + + +contract MForwarderCore is + IForwarderCore +{ + + /// @dev Market sells WETH for ERC20 tokens. + /// @param orders An array of Order struct containing order specifications. + /// @param signatures An array of Proof that order has been created by maker. + /// @param feeOrders An array of Order struct containing order specifications for fees. + /// @param feeSignatures An array of Proof that order has been created by maker for the fee orders. + /// @param wethSellAmount The amount of WETH to sell. + /// @return FillResults amounts filled and fees paid by maker and taker. + function marketSellEthForERC20Internal( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + LibOrder.Order[] memory feeOrders, + bytes[] memory feeSignatures, + uint256 wethSellAmount + ) + internal + returns (LibFillResults.FillResults memory totalFillResults); + + /// @dev Market sells WETH for ZRX tokens. + /// @param orders An array of Order struct containing order specifications. + /// @param signatures An array of Proof that order has been created by maker. + /// @param wethSellAmount The amount of WETH to sell. + /// @return FillResults amounts filled and fees paid by maker and taker. + function marketSellEthForZRXInternal( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + uint256 wethSellAmount + ) + internal + returns (LibFillResults.FillResults memory totalFillResults); + + /// @dev Buys an exact amount of an ERC20 token using WETH. + /// @param orders Orders to fill. The maker asset is the ERC20 token to buy. The taker asset is WETH. + /// @param signatures Proof that the orders were created by their respective makers. + /// @param feeOrders to fill. The maker asset is ZRX and the taker asset is WETH. + /// @param feeSignatures Proof that the feeOrders were created by their respective makers. + /// @param makerTokenFillAmount Amount of the ERC20 token to buy. + /// @return totalFillResults Aggregated fill results of buying the ERC20 and ZRX tokens. + function marketBuyERC20TokensInternal( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + LibOrder.Order[] memory feeOrders, + bytes[] memory feeSignatures, + uint256 makerTokenFillAmount + ) + internal + returns (LibFillResults.FillResults memory totalFillResults); + + /// @dev Buys an all of the ERC721 tokens in the orders. + /// @param orders Orders to fill. The maker asset is the ERC721 token to buy. The taker asset is WETH. + /// @param signatures Proof that the orders were created by their respective makers. + /// @param feeOrders to fill. The maker asset is ZRX and the taker asset is WETH. + /// @param feeSignatures Proof that the feeOrders were created by their respective makers. + /// @return totalFillResults Aggregated fill results of buying the ERC721 tokens and ZRX tokens. + function batchBuyERC721TokensInternal( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + LibOrder.Order[] memory feeOrders, + bytes[] memory feeSignatures + ) + internal + returns (LibFillResults.FillResults memory totalFillResults); +} diff --git a/packages/contracts/src/2.0.0/forwarder/mixins/MMarketBuyZrx.sol b/packages/contracts/src/2.0.0/forwarder/mixins/MMarketBuyZrx.sol new file mode 100644 index 000000000..3501ef001 --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/mixins/MMarketBuyZrx.sol @@ -0,0 +1,42 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "../../protocol/Exchange/libs/LibFillResults.sol"; +import "../../protocol/Exchange/libs/LibOrder.sol"; + + +contract MMarketBuyZrx { + + /// @dev Buys zrxBuyAmount of ZRX fee tokens, taking into account the fees on buying fee tokens. This will guarantee + /// At least zrxBuyAmount of ZRX fee tokens are purchased (sometimes slightly over due to rounding issues). + /// It is possible that a request to buy 200 ZRX fee tokens will require purchasing 202 ZRX tokens + /// As 2 ZRX is required to purchase the 200 ZRX fee tokens. This guarantees at least 200 ZRX for future purchases. + /// @param orders An array of Order struct containing order specifications for fees. + /// @param signatures An array of Proof that order has been created by maker for the fee orders. + /// @param zrxBuyAmount The number of requested ZRX fee tokens. + /// @return totalFillResults Amounts filled and fees paid by maker and taker. makerTokenAmount is the zrx amount deducted of fees + function marketBuyZrxInternal( + LibOrder.Order[] memory orders, + bytes[] memory signatures, + uint256 zrxBuyAmount + ) + internal + returns (LibFillResults.FillResults memory totalFillResults); +} diff --git a/packages/contracts/src/2.0.0/forwarder/mixins/MTransfer.sol b/packages/contracts/src/2.0.0/forwarder/mixins/MTransfer.sol new file mode 100644 index 000000000..418a6288b --- /dev/null +++ b/packages/contracts/src/2.0.0/forwarder/mixins/MTransfer.sol @@ -0,0 +1,46 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + + +contract MTransfer { + + function onERC721Received(address, uint256, bytes memory) + public + pure + returns(bytes4); + + function onERC721Received(address, address, uint256, bytes memory) + public + pure + returns(bytes4); + + function transferERC20Token( + address token, + address to, + uint256 amount + ) + internal; + + function transferERC721Token( + bytes memory assetData, + address to + ) + internal; +} diff --git a/packages/contracts/src/2.0.0/tokens/EtherToken/IEtherToken.sol b/packages/contracts/src/2.0.0/tokens/EtherToken/IEtherToken.sol new file mode 100644 index 000000000..9e2e68766 --- /dev/null +++ b/packages/contracts/src/2.0.0/tokens/EtherToken/IEtherToken.sol @@ -0,0 +1,33 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "../ERC20Token/IERC20Token.sol"; + + +contract IEtherToken is + IERC20Token +{ + function deposit() + public + payable; + + function withdraw(uint256 amount) + public; +} diff --git a/packages/contracts/src/2.0.0/tokens/WETH9/WETH9.sol b/packages/contracts/src/2.0.0/tokens/EtherToken/WETH9.sol index 1fdb04de5..1fdb04de5 100644 --- a/packages/contracts/src/2.0.0/tokens/WETH9/WETH9.sol +++ b/packages/contracts/src/2.0.0/tokens/EtherToken/WETH9.sol |