Add MixinAssetProxy to reuse redundant code

This commit is contained in:
Amir Bandeali
2018-05-02 11:30:26 -07:00
parent 0cb357a0e9
commit 3355a39fe0
4 changed files with 117 additions and 98 deletions

View File

@@ -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]
);
}
}
}

View File

@@ -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;
}

View File

@@ -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.

View File

@@ -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.