From 3355a39fe0359c76efc263b3ffe421c68d7a9495 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Wed, 2 May 2018 11:30:26 -0700 Subject: Add MixinAssetProxy to reuse redundant code --- .../protocol/AssetProxy/MixinAssetProxy.sol | 75 ++++++++++++++++++++++ .../protocol/AssetProxy/mixins/MAssetProxy.sol | 35 ++++++++++ .../protocol/AssetProxy/proxies/ERC20Proxy.sol | 53 ++------------- .../protocol/AssetProxy/proxies/ERC721Proxy.sol | 52 +-------------- 4 files changed, 117 insertions(+), 98 deletions(-) create mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAssetProxy.sol create mode 100644 packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAssetProxy.sol (limited to 'packages') diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAssetProxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAssetProxy.sol new file mode 100644 index 000000000..23dce6a8b --- /dev/null +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAssetProxy.sol @@ -0,0 +1,75 @@ +/* + + 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/MAssetProxy.sol"; +import "./IAssetProxy.sol"; +import "../../utils/Authorizable/Authorizable.sol"; + +contract MixinAssetProxy is + IAssetProxy, + MAssetProxy, + Authorizable +{ + + /// @dev Transfers assets. Either succeeds or throws. + /// @param assetMetadata Encoded byte array. + /// @param from Address to transfer asset from. + /// @param to Address to transfer asset to. + /// @param amount Amount of asset to transfer. + function transferFrom( + bytes assetMetadata, + address from, + address to, + uint256 amount) + external + onlyAuthorized + { + transferFromInternal( + assetMetadata, + from, + to, + amount + ); + } + + /// @dev Makes multiple transfers of assets. Either succeeds or throws. + /// @param assetMetadata Array of byte arrays encoded for the respective asset proxy. + /// @param from Array of addresses to transfer assets from. + /// @param to Array of addresses to transfer assets to. + /// @param amounts Array of amounts of assets to transfer. + function batchTransferFrom( + bytes[] memory assetMetadata, + address[] memory from, + address[] memory to, + uint256[] memory amounts) + public + onlyAuthorized + { + for (uint256 i = 0; i < assetMetadata.length; i++) { + transferFromInternal( + assetMetadata[i], + from[i], + to[i], + amounts[i] + ); + } + } +} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAssetProxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAssetProxy.sol new file mode 100644 index 000000000..8e9b3bc65 --- /dev/null +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAssetProxy.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.21; +pragma experimental ABIEncoderV2; + +contract MAssetProxy { + + /// @dev Internal version of `transferFrom`. + /// @param assetMetadata Encoded byte array. + /// @param from Address to transfer asset from. + /// @param to Address to transfer asset to. + /// @param amount Amount of asset to transfer. + function transferFromInternal( + bytes memory assetMetadata, + address from, + address to, + uint256 amount) + internal; +} diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC20Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC20Proxy.sol index d3cc0e804..713e8a6e6 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC20Proxy.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC20Proxy.sol @@ -19,65 +19,20 @@ pragma solidity ^0.4.21; pragma experimental ABIEncoderV2; -import "../IAssetProxy.sol"; + import "../../../utils/LibBytes/LibBytes.sol"; -import "../../../utils/Authorizable/Authorizable.sol"; import "../../../tokens/ERC20Token/IERC20Token.sol"; +import "../MixinAssetProxy.sol"; contract ERC20Proxy is LibBytes, - Authorizable, - IAssetProxy + MixinAssetProxy { uint8 constant PROXY_ID = 1; - /// @dev Transfers ERC20 tokens. Either succeeds or throws. - /// @param assetMetadata ERC20-encoded byte array. - /// @param from Address to transfer asset from. - /// @param to Address to transfer asset to. - /// @param amount Amount of asset to transfer. - function transferFrom( - bytes assetMetadata, - address from, - address to, - uint256 amount) - external - onlyAuthorized - { - transferFromInternal( - assetMetadata, - from, - to, - amount - ); - } - - /// @dev Makes multiple transfers of assets. Either succeeds or throws. - /// @param assetMetadata Array of byte arrays encoded for the respective asset proxy. - /// @param from Array of addresses to transfer assets from. - /// @param to Array of addresses to transfer assets to. - /// @param amounts Array of amounts of assets to transfer. - function batchTransferFrom( - bytes[] memory assetMetadata, - address[] memory from, - address[] memory to, - uint256[] memory amounts) - public - onlyAuthorized - { - for (uint256 i = 0; i < assetMetadata.length; i++) { - transferFromInternal( - assetMetadata[i], - from[i], - to[i], - amounts[i] - ); - } - } - /// @dev Internal version of `transferFrom`. - /// @param assetMetadata ERC20-encoded byte array. + /// @param assetMetadata Encoded byte array. /// @param from Address to transfer asset from. /// @param to Address to transfer asset to. /// @param amount Amount of asset to transfer. diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC721Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC721Proxy.sol index f0fac0259..6a4475d48 100644 --- a/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC721Proxy.sol +++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/proxies/ERC721Proxy.sol @@ -19,65 +19,19 @@ pragma solidity ^0.4.21; pragma experimental ABIEncoderV2; -import "../IAssetProxy.sol"; import "../../../utils/LibBytes/LibBytes.sol"; -import "../../../utils/Authorizable/Authorizable.sol"; import "../../../tokens/ERC721Token/ERC721Token.sol"; +import "../MixinAssetProxy.sol"; contract ERC721Proxy is LibBytes, - Authorizable, - IAssetProxy + MixinAssetProxy { uint8 constant PROXY_ID = 2; - /// @dev Transfers ERC721 tokens. Either succeeds or throws. - /// @param assetMetadata ERC721-encoded byte array - /// @param from Address to transfer asset from. - /// @param to Address to transfer asset to. - /// @param amount Amount of asset to transfer. - function transferFrom( - bytes assetMetadata, - address from, - address to, - uint256 amount) - external - onlyAuthorized - { - transferFromInternal( - assetMetadata, - from, - to, - amount - ); - } - - /// @dev Makes multiple transfers of assets. Either succeeds or throws. - /// @param assetMetadata Array of byte arrays encoded for the respective asset proxy. - /// @param from Array of addresses to transfer assets from. - /// @param to Array of addresses to transfer assets to. - /// @param amounts Array of amounts of assets to transfer. - function batchTransferFrom( - bytes[] memory assetMetadata, - address[] memory from, - address[] memory to, - uint256[] memory amounts) - public - onlyAuthorized - { - for (uint256 i = 0; i < assetMetadata.length; i++) { - transferFromInternal( - assetMetadata[i], - from[i], - to[i], - amounts[i] - ); - } - } - /// @dev Internal version of `transferFrom`. - /// @param assetMetadata ERC20-encoded byte array. + /// @param assetMetadata Encoded byte array. /// @param from Address to transfer asset from. /// @param to Address to transfer asset to. /// @param amount Amount of asset to transfer. -- cgit v1.2.3