From a8a0a5cbfb7299cd22181c8713e1da6dec412dae Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sun, 22 Apr 2018 15:30:08 -0700 Subject: Combine Exchange with AssetProxyDispatcher --- .../current/protocol/AssetProxy/IAssetProxy.sol | 36 +++++++ .../protocol/AssetProxy/proxies/ERC20Proxy.sol | 50 ++++++++++ .../protocol/AssetProxy/proxies/ERC721Proxy.sol | 58 ++++++++++++ .../AssetProxyDispatcher/AssetProxyDispatcher.sol | 87 ----------------- .../protocol/AssetProxyDispatcher/IAssetProxy.sol | 36 ------- .../AssetProxyDispatcher/IAssetProxyDispatcher.sol | 56 ----------- .../AssetProxyDispatcher/proxies/ERC20Proxy.sol | 50 ---------- .../AssetProxyDispatcher/proxies/ERC721Proxy.sol | 58 ------------ .../current/protocol/Exchange/Exchange.sol | 12 ++- .../Exchange/MixinAssetProxyDispatcher.sol | 87 +++++++++++++++++ .../current/protocol/Exchange/MixinSettlement.sol | 88 ++++++++++++++++++ .../protocol/Exchange/MixinSettlementProxy.sol | 103 --------------------- .../Exchange/mixins/MAssetProxyDispatcher.sol | 60 ++++++++++++ 13 files changed, 386 insertions(+), 395 deletions(-) create mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxy/IAssetProxy.sol create mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC20Proxy.sol create mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC721Proxy.sol delete mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/AssetProxyDispatcher.sol delete mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/IAssetProxy.sol delete mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/IAssetProxyDispatcher.sol delete mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC20Proxy.sol delete mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC721Proxy.sol create mode 100644 packages/contracts/src/contracts/current/protocol/Exchange/MixinAssetProxyDispatcher.sol create mode 100644 packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlement.sol delete mode 100644 packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol create mode 100644 packages/contracts/src/contracts/current/protocol/Exchange/mixins/MAssetProxyDispatcher.sol (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/IAssetProxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/IAssetProxy.sol new file mode 100644 index 000000000..60e74723d --- /dev/null +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/IAssetProxy.sol @@ -0,0 +1,36 @@ +/* + + 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.21; + +import "../../utils/Authorizable/IAuthorizable.sol"; + +contract IAssetProxy is IAuthorizable { + + /// @dev Transfers assets. Either succeeds or throws. + /// @param assetMetadata Byte array encoded for the respective asset proxy. + /// @param from Address to transfer token from. + /// @param to Address to transfer token to. + /// @param amount Amount of token to transfer. + function transferFrom( + bytes assetMetadata, + address from, + address to, + uint256 amount) + external; +} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC20Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC20Proxy.sol new file mode 100644 index 000000000..eef3a39db --- /dev/null +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC20Proxy.sol @@ -0,0 +1,50 @@ +/* + + 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.21; + +import "../IAssetProxy.sol"; +import "../../../utils/LibBytes/LibBytes.sol"; +import "../../../utils/Authorizable/Authorizable.sol"; +import "../../../tokens/ERC20Token/IERC20Token.sol"; + +contract ERC20Proxy is + LibBytes, + Authorizable, + IAssetProxy +{ + + /// @dev Transfers ERC20 tokens. Either succeeds or throws. + /// @param assetMetadata ERC20-encoded byte array. + /// @param from Address to transfer token from. + /// @param to Address to transfer token to. + /// @param amount Amount of token to transfer. + function transferFrom( + bytes assetMetadata, + address from, + address to, + uint256 amount) + external + onlyAuthorized + { + require(assetMetadata.length == 21); + address token = readAddress(assetMetadata, 1); + bool success = IERC20Token(token).transferFrom(from, to, amount); + require(success == true); + } +} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC721Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC721Proxy.sol new file mode 100644 index 000000000..466d0b871 --- /dev/null +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC721Proxy.sol @@ -0,0 +1,58 @@ +/* + + 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.21; + +import "../IAssetProxy.sol"; +import "../../../utils/LibBytes/LibBytes.sol"; +import "../../../utils/Authorizable/Authorizable.sol"; +import "zeppelin-solidity/contracts/token/ERC721/ERC721Token.sol"; + +contract ERC721Proxy is + LibBytes, + Authorizable, + IAssetProxy +{ + + /// @dev Transfers ERC721 tokens. Either succeeds or throws. + /// @param assetMetadata ERC721-encoded byte array + /// @param from Address to transfer token from. + /// @param to Address to transfer token to. + /// @param amount Amount of token to transfer. + function transferFrom( + bytes assetMetadata, + address from, + address to, + uint256 amount) + external + onlyAuthorized + { + // There exists only 1 of each token. + require(amount == 1); + + // Decode metadata + require(assetMetadata.length == 53); + address token = readAddress(assetMetadata, 1); + uint256 tokenId = readUint256(assetMetadata, 21); + + // Either succeeds or throws. + // @TODO: Call safeTransferFrom if there is additional + // data stored in `assetMetadata`. + ERC721Token(token).transferFrom(from, to, tokenId); + } +} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/AssetProxyDispatcher.sol b/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/AssetProxyDispatcher.sol deleted file mode 100644 index 43fd7596e..000000000 --- a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/AssetProxyDispatcher.sol +++ /dev/null @@ -1,87 +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.21; - -import "./IAssetProxyDispatcher.sol"; -import "./IAssetProxy.sol"; -import "../../utils/Ownable/Ownable.sol"; -import "../../utils/Authorizable/Authorizable.sol"; - -contract AssetProxyDispatcher is - Ownable, - Authorizable, - IAssetProxyDispatcher -{ - // Mapping from Asset Proxy Id's to their respective Asset Proxy - mapping (uint8 => IAssetProxy) public assetProxies; - - /// @dev Transfers assets. Either succeeds or throws. - /// @param assetMetadata Byte array encoded for the respective asset proxy. - /// @param from Address to transfer token from. - /// @param to Address to transfer token to. - /// @param amount Amount of token to transfer. - function transferFrom( - bytes assetMetadata, - address from, - address to, - uint256 amount) - external - onlyAuthorized - { - // Lookup asset proxy - require(assetMetadata.length >= 1); - uint8 assetProxyId = uint8(assetMetadata[0]); - IAssetProxy assetProxy = assetProxies[assetProxyId]; - - // transferFrom will either succeed or throw. - assetProxy.transferFrom(assetMetadata, from, to, amount); - } - - /// @dev Registers an asset proxy to an asset proxy id. - /// An id can only be assigned to a single proxy at a given time, - /// however, an asset proxy may be registered to multiple ids. - /// @param assetProxyId Id to register`newAssetProxy` under. - /// @param newAssetProxy Address of new asset proxy to register, or 0x0 to unset assetProxyId. - /// @param oldAssetProxy Existing asset proxy to overwrite, or 0x0 if assetProxyId is currently unused. - function registerAssetProxy( - uint8 assetProxyId, - address newAssetProxy, - address oldAssetProxy) - external - onlyOwner - { - // Ensure the existing asset proxy is not unintentionally overwritten - require(oldAssetProxy == address(assetProxies[assetProxyId])); - - // Add asset proxy and log registration - assetProxies[assetProxyId] = IAssetProxy(newAssetProxy); - emit AssetProxySet(assetProxyId, newAssetProxy, oldAssetProxy); - } - - /// @dev Gets an asset proxy. - /// @param assetProxyId Id of the asset proxy. - /// @return The asset proxy registered to assetProxyId. Returns 0x0 if no proxy is registered. - function getAssetProxy(uint8 assetProxyId) - external view - returns (address) - { - IAssetProxy assetProxy = assetProxies[assetProxyId]; - return address(assetProxy); - } -} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/IAssetProxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/IAssetProxy.sol deleted file mode 100644 index 60e74723d..000000000 --- a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/IAssetProxy.sol +++ /dev/null @@ -1,36 +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.21; - -import "../../utils/Authorizable/IAuthorizable.sol"; - -contract IAssetProxy is IAuthorizable { - - /// @dev Transfers assets. Either succeeds or throws. - /// @param assetMetadata Byte array encoded for the respective asset proxy. - /// @param from Address to transfer token from. - /// @param to Address to transfer token to. - /// @param amount Amount of token to transfer. - function transferFrom( - bytes assetMetadata, - address from, - address to, - uint256 amount) - external; -} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/IAssetProxyDispatcher.sol b/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/IAssetProxyDispatcher.sol deleted file mode 100644 index 88bc0d353..000000000 --- a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/IAssetProxyDispatcher.sol +++ /dev/null @@ -1,56 +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.21; - -import "../../utils/Ownable/IOwnable.sol"; -import "../../utils/Authorizable/IAuthorizable.sol"; -import "./IAssetProxy.sol"; - -contract IAssetProxyDispatcher is - IOwnable, - IAuthorizable, - IAssetProxy -{ - - // Logs registration of new asset proxy - event AssetProxySet( - uint8 id, - address newAssetProxy, - address oldAssetProxy - ); - - /// @dev Registers an asset proxy to an asset proxy id. - /// An id can only be assigned to a single proxy at a given time, - /// however, an asset proxy may be registered to multiple ids. - /// @param assetProxyId Id to register`newAssetProxy` under. - /// @param newAssetProxy Address of new asset proxy to register, or 0x0 to unset assetProxyId. - /// @param oldAssetProxy Existing asset proxy to overwrite, or 0x0 if assetProxyId is currently unused. - function registerAssetProxy( - uint8 assetProxyId, - address newAssetProxy, - address oldAssetProxy) - external; - - /// @dev Gets an asset proxy. - /// @param assetProxyId Id of the asset proxy. - /// @return The asset proxy registered to assetProxyId. Returns 0x0 if no proxy is registered. - function getAssetProxy(uint8 assetProxyId) - external view - returns (address); -} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC20Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC20Proxy.sol deleted file mode 100644 index eef3a39db..000000000 --- a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC20Proxy.sol +++ /dev/null @@ -1,50 +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.21; - -import "../IAssetProxy.sol"; -import "../../../utils/LibBytes/LibBytes.sol"; -import "../../../utils/Authorizable/Authorizable.sol"; -import "../../../tokens/ERC20Token/IERC20Token.sol"; - -contract ERC20Proxy is - LibBytes, - Authorizable, - IAssetProxy -{ - - /// @dev Transfers ERC20 tokens. Either succeeds or throws. - /// @param assetMetadata ERC20-encoded byte array. - /// @param from Address to transfer token from. - /// @param to Address to transfer token to. - /// @param amount Amount of token to transfer. - function transferFrom( - bytes assetMetadata, - address from, - address to, - uint256 amount) - external - onlyAuthorized - { - require(assetMetadata.length == 21); - address token = readAddress(assetMetadata, 1); - bool success = IERC20Token(token).transferFrom(from, to, amount); - require(success == true); - } -} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC721Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC721Proxy.sol deleted file mode 100644 index 466d0b871..000000000 --- a/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC721Proxy.sol +++ /dev/null @@ -1,58 +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.21; - -import "../IAssetProxy.sol"; -import "../../../utils/LibBytes/LibBytes.sol"; -import "../../../utils/Authorizable/Authorizable.sol"; -import "zeppelin-solidity/contracts/token/ERC721/ERC721Token.sol"; - -contract ERC721Proxy is - LibBytes, - Authorizable, - IAssetProxy -{ - - /// @dev Transfers ERC721 tokens. Either succeeds or throws. - /// @param assetMetadata ERC721-encoded byte array - /// @param from Address to transfer token from. - /// @param to Address to transfer token to. - /// @param amount Amount of token to transfer. - function transferFrom( - bytes assetMetadata, - address from, - address to, - uint256 amount) - external - onlyAuthorized - { - // There exists only 1 of each token. - require(amount == 1); - - // Decode metadata - require(assetMetadata.length == 53); - address token = readAddress(assetMetadata, 1); - uint256 tokenId = readUint256(assetMetadata, 21); - - // Either succeeds or throws. - // @TODO: Call safeTransferFrom if there is additional - // data stored in `assetMetadata`. - ERC721Token(token).transferFrom(from, to, tokenId); - } -} diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol b/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol index 995cbcf76..5ccb06b88 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol @@ -21,24 +21,26 @@ pragma experimental ABIEncoderV2; import "./MixinExchangeCore.sol"; import "./MixinSignatureValidator.sol"; -import "./MixinSettlementProxy.sol"; +import "./MixinSettlement.sol"; import "./MixinWrapperFunctions.sol"; +import "./MixinAssetProxyDispatcher.sol"; contract Exchange is MixinExchangeCore, MixinSignatureValidator, - MixinSettlementProxy, - MixinWrapperFunctions + MixinSettlement, + MixinWrapperFunctions, + MixinAssetProxyDispatcher { string constant public VERSION = "2.0.1-alpha"; function Exchange( - address _assetProxyDispatcher, bytes memory _zrxProxyData) public MixinExchangeCore() MixinSignatureValidator() - MixinSettlementProxy(_assetProxyDispatcher, _zrxProxyData) + MixinSettlement(_zrxProxyData) MixinWrapperFunctions() + MixinAssetProxyDispatcher() {} } diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinAssetProxyDispatcher.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinAssetProxyDispatcher.sol new file mode 100644 index 000000000..eab8e04a1 --- /dev/null +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinAssetProxyDispatcher.sol @@ -0,0 +1,87 @@ +/* + + 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.21; + +import "./mixins/MAssetProxyDispatcher.sol"; +import "../AssetProxy/IAssetProxy.sol"; +import "../../utils/Ownable/Ownable.sol"; + +contract MixinAssetProxyDispatcher is + Ownable, + MAssetProxyDispatcher +{ + // Mapping from Asset Proxy Id's to their respective Asset Proxy + mapping (uint8 => IAssetProxy) public assetProxies; + + /// @dev Forwards arguments to assetProxy and calls `transferFrom`. Either succeeds or throws. + /// @param assetMetadata Byte array encoded for the respective asset proxy. + /// @param from Address to transfer token from. + /// @param to Address to transfer token to. + /// @param amount Amount of token to transfer. + function dispatchTransferFrom( + bytes memory assetMetadata, + address from, + address to, + uint256 amount) + internal + { + // Do nothing if no amount should be transferred. + if (amount > 0) { + // Lookup asset proxy + require(assetMetadata.length >= 1); + uint8 assetProxyId = uint8(assetMetadata[0]); + IAssetProxy assetProxy = assetProxies[assetProxyId]; + + // transferFrom will either succeed or throw. + assetProxy.transferFrom(assetMetadata, from, to, amount); + } + } + + /// @dev Registers an asset proxy to an asset proxy id. + /// An id can only be assigned to a single proxy at a given time, + /// however, an asset proxy may be registered to multiple ids. + /// @param assetProxyId Id to register`newAssetProxy` under. + /// @param newAssetProxy Address of new asset proxy to register, or 0x0 to unset assetProxyId. + /// @param oldAssetProxy Existing asset proxy to overwrite, or 0x0 if assetProxyId is currently unused. + function registerAssetProxy( + uint8 assetProxyId, + address newAssetProxy, + address oldAssetProxy) + external + onlyOwner + { + // Ensure the existing asset proxy is not unintentionally overwritten + require(oldAssetProxy == address(assetProxies[assetProxyId])); + + // Add asset proxy and log registration + assetProxies[assetProxyId] = IAssetProxy(newAssetProxy); + emit AssetProxySet(assetProxyId, newAssetProxy, oldAssetProxy); + } + + /// @dev Gets an asset proxy. + /// @param assetProxyId Id of the asset proxy. + /// @return The asset proxy registered to assetProxyId. Returns 0x0 if no proxy is registered. + function getAssetProxy(uint8 assetProxyId) + external view + returns (address) + { + IAssetProxy assetProxy = assetProxies[assetProxyId]; + return address(assetProxy); + } +} diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlement.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlement.sol new file mode 100644 index 000000000..ae41c7a86 --- /dev/null +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlement.sol @@ -0,0 +1,88 @@ +/* + + 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.21; +pragma experimental ABIEncoderV2; + +import "./mixins/MSettlement.sol"; +import "./mixins/MAssetProxyDispatcher.sol"; +import "./LibPartialAmount.sol"; +import "../AssetProxy/IAssetProxy.sol"; + +/// @dev Provides MixinSettlement +contract MixinSettlement is + MSettlement, + MAssetProxyDispatcher, + LibPartialAmount +{ + bytes ZRX_PROXY_DATA; + + function zrxProxyData() + external view + returns (bytes memory) + { + return ZRX_PROXY_DATA; + } + + function MixinSettlement(bytes memory _zrxProxyData) + public + { + ZRX_PROXY_DATA = _zrxProxyData; + } + + function settleOrder( + Order memory order, + address takerAddress, + uint256 takerAssetFilledAmount) + internal + returns ( + uint256 makerAssetFilledAmount, + uint256 makerFeePaid, + uint256 takerFeePaid + ) + { + makerAssetFilledAmount = getPartialAmount(takerAssetFilledAmount, order.takerAssetAmount, order.makerAssetAmount); + dispatchTransferFrom( + order.makerAssetData, + order.makerAddress, + takerAddress, + makerAssetFilledAmount + ); + dispatchTransferFrom( + order.takerAssetData, + takerAddress, + order.makerAddress, + takerAssetFilledAmount + ); + makerFeePaid = getPartialAmount(takerAssetFilledAmount, order.takerAssetAmount, order.makerFee); + dispatchTransferFrom( + ZRX_PROXY_DATA, + order.makerAddress, + order.feeRecipientAddress, + makerFeePaid + ); + takerFeePaid = getPartialAmount(takerAssetFilledAmount, order.takerAssetAmount, order.takerFee); + dispatchTransferFrom( + ZRX_PROXY_DATA, + takerAddress, + order.feeRecipientAddress, + takerFeePaid + ); + return (makerAssetFilledAmount, makerFeePaid, takerFeePaid); + } +} diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol deleted file mode 100644 index 5fff2cd60..000000000 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol +++ /dev/null @@ -1,103 +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.21; -pragma experimental ABIEncoderV2; - -import "./mixins/MSettlement.sol"; -import "./LibPartialAmount.sol"; -import "../AssetProxyDispatcher/IAssetProxy.sol"; - -/// @dev Provides MixinSettlement -contract MixinSettlementProxy is - MSettlement, - LibPartialAmount -{ - IAssetProxy ASSET_PROXY_DISPATCHER; - bytes ZRX_PROXY_DATA; - - function assetProxyDispatcher() - public view - returns (address) - { - return address(ASSET_PROXY_DISPATCHER); - } - - function zrxProxyData() - external view - returns (bytes memory) - { - return ZRX_PROXY_DATA; - } - - function MixinSettlementProxy( - address _assetProxyDispatcher, - bytes memory _zrxProxyData) - public - { - ASSET_PROXY_DISPATCHER = IAssetProxy(_assetProxyDispatcher); - ZRX_PROXY_DATA = _zrxProxyData; - } - - function settleOrder( - Order memory order, - address takerAddress, - uint256 takerAssetFilledAmount) - internal - returns ( - uint256 makerAssetFilledAmount, - uint256 makerFeePaid, - uint256 takerFeePaid - ) - { - makerAssetFilledAmount = getPartialAmount(takerAssetFilledAmount, order.takerAssetAmount, order.makerAssetAmount); - ASSET_PROXY_DISPATCHER.transferFrom( - order.makerAssetData, - order.makerAddress, - takerAddress, - makerAssetFilledAmount - ); - ASSET_PROXY_DISPATCHER.transferFrom( - order.takerAssetData, - takerAddress, - order.makerAddress, - takerAssetFilledAmount - ); - if (order.feeRecipientAddress != address(0)) { - if (order.makerFee > 0) { - makerFeePaid = getPartialAmount(takerAssetFilledAmount, order.takerAssetAmount, order.makerFee); - ASSET_PROXY_DISPATCHER.transferFrom( - ZRX_PROXY_DATA, - order.makerAddress, - order.feeRecipientAddress, - makerFeePaid - ); - } - if (order.takerFee > 0) { - takerFeePaid = getPartialAmount(takerAssetFilledAmount, order.takerAssetAmount, order.takerFee); - ASSET_PROXY_DISPATCHER.transferFrom( - ZRX_PROXY_DATA, - takerAddress, - order.feeRecipientAddress, - takerFeePaid - ); - } - } - return (makerAssetFilledAmount, makerFeePaid, takerFeePaid); - } -} diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MAssetProxyDispatcher.sol b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MAssetProxyDispatcher.sol new file mode 100644 index 000000000..a46be9d0f --- /dev/null +++ b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MAssetProxyDispatcher.sol @@ -0,0 +1,60 @@ +/* + + 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.21; + +contract MAssetProxyDispatcher { + + // Logs registration of new asset proxy + event AssetProxySet( + uint8 id, + address newAssetProxy, + address oldAssetProxy + ); + + /// @dev Forwards arguments to assetProxy and calls `transferFrom`. Either succeeds or throws. + /// @param assetMetadata Byte array encoded for the respective asset proxy. + /// @param from Address to transfer token from. + /// @param to Address to transfer token to. + /// @param amount Amount of token to transfer. + function dispatchTransferFrom( + bytes memory assetMetadata, + address from, + address to, + uint256 amount) + internal; + + /// @dev Registers an asset proxy to an asset proxy id. + /// An id can only be assigned to a single proxy at a given time, + /// however, an asset proxy may be registered to multiple ids. + /// @param assetProxyId Id to register`newAssetProxy` under. + /// @param newAssetProxy Address of new asset proxy to register, or 0x0 to unset assetProxyId. + /// @param oldAssetProxy Existing asset proxy to overwrite, or 0x0 if assetProxyId is currently unused. + function registerAssetProxy( + uint8 assetProxyId, + address newAssetProxy, + address oldAssetProxy) + external; + + /// @dev Gets an asset proxy. + /// @param assetProxyId Id of the asset proxy. + /// @return The asset proxy registered to assetProxyId. Returns 0x0 if no proxy is registered. + function getAssetProxy(uint8 assetProxyId) + external view + returns (address); +} -- cgit v1.2.3