aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/contracts/src')
-rw-r--r--packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLock/MultiSigWalletWithTimeLock.sol2
-rw-r--r--packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.sol2
-rw-r--r--packages/contracts/src/contracts/current/protocol/AssetProxy/ERC20Proxy.sol83
-rw-r--r--packages/contracts/src/contracts/current/protocol/AssetProxy/ERC721Proxy.sol89
-rw-r--r--packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAssetProxy.sol73
-rw-r--r--packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAuthorizable.sol117
-rw-r--r--packages/contracts/src/contracts/current/protocol/AssetProxy/interfaces/IAssetProxy.sol59
-rw-r--r--packages/contracts/src/contracts/current/protocol/AssetProxy/interfaces/IAuthorizable.sol50
-rw-r--r--packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAssetProxy.sol39
-rw-r--r--packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAuthorizable.sol42
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol614
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/MixinAssetProxyDispatcher.sol106
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol243
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlement.sol99
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol191
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/MixinTransactions.sol102
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol514
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IAssetProxyDispatcher.sol41
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchange.sol34
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchangeCore.sol51
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ISignatureValidator.sol32
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ISigner.sol33
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ITransactions.sol33
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IWrapperFunctions.sol142
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/libs/LibExchangeErrors.sol58
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/libs/LibFillResults.sol47
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/libs/LibMath.sol72
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol84
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/mixins/MAssetProxyDispatcher.sol45
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/mixins/MExchangeCore.sol66
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSettlement.sol41
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSignatureValidator.sol50
-rw-r--r--packages/contracts/src/contracts/current/protocol/Exchange/mixins/MTransactions.sol35
-rw-r--r--packages/contracts/src/contracts/current/test/DummyERC20Token/DummyERC20Token.sol56
-rw-r--r--packages/contracts/src/contracts/current/test/DummyERC721Token/DummyERC721Token.sol58
-rw-r--r--packages/contracts/src/contracts/current/test/DummyToken/DummyToken.sol37
-rw-r--r--packages/contracts/src/contracts/current/test/MaliciousToken/MaliciousToken.sol31
-rw-r--r--packages/contracts/src/contracts/current/test/Mintable/Mintable.sol32
-rw-r--r--packages/contracts/src/contracts/current/test/TestAssetProxyDispatcher/TestAssetProxyDispatcher.sol34
-rw-r--r--packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol133
-rw-r--r--packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol80
-rw-r--r--packages/contracts/src/contracts/current/test/TestSignatureValidator/TestSignatureValidator.sol41
-rw-r--r--packages/contracts/src/contracts/current/tokens/ERC20Token/ERC20Token.sol80
-rw-r--r--packages/contracts/src/contracts/current/tokens/ERC20Token/IERC20Token.sol73
-rw-r--r--packages/contracts/src/contracts/current/tokens/ERC721Token/ERC721Token.sol406
-rw-r--r--packages/contracts/src/contracts/current/tokens/ERC721Token/IERC721Receiver.sol60
-rw-r--r--packages/contracts/src/contracts/current/tokens/ERC721Token/IERC721Token.sol105
-rw-r--r--packages/contracts/src/contracts/current/tokens/Token/Token.sol35
-rw-r--r--packages/contracts/src/contracts/current/tokens/UnlimitedAllowanceToken/UnlimitedAllowanceToken.sol28
-rw-r--r--packages/contracts/src/contracts/current/tokens/ZRXToken/ZRXToken.sol2
-rw-r--r--packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol206
-rw-r--r--packages/contracts/src/contracts/current/utils/Ownable/IOwnable.sol14
-rw-r--r--packages/contracts/src/contracts/current/utils/Ownable/Ownable.sol14
-rw-r--r--packages/contracts/src/contracts/current/utils/SafeMath/SafeMath.sol3
-rw-r--r--packages/contracts/src/contracts/previous/Arbitrage/Arbitrage.sol (renamed from packages/contracts/src/contracts/current/tutorials/Arbitrage/Arbitrage.sol)6
-rw-r--r--packages/contracts/src/contracts/previous/EtherDelta/AccountLevels.sol (renamed from packages/contracts/src/contracts/current/tutorials/EtherDelta/AccountLevels.sol)0
-rw-r--r--packages/contracts/src/contracts/previous/EtherDelta/EtherDelta.sol (renamed from packages/contracts/src/contracts/current/tutorials/EtherDelta/EtherDelta.sol)4
-rw-r--r--packages/contracts/src/contracts/previous/Exchange/Exchange_v1.sol602
-rw-r--r--packages/contracts/src/contracts/previous/Exchange/IExchange_v1.sol226
-rw-r--r--packages/contracts/src/contracts/previous/Ownable/IOwnable_v1.sol18
-rw-r--r--packages/contracts/src/contracts/previous/TokenRegistry/ITokenRegistery.sol195
-rw-r--r--packages/contracts/src/contracts/previous/TokenRegistry/TokenRegistry.sol (renamed from packages/contracts/src/contracts/current/protocol/TokenRegistry/TokenRegistry.sol)4
-rw-r--r--packages/contracts/src/contracts/previous/TokenTransferProxy/TokenTransferProxy_v1.sol (renamed from packages/contracts/src/contracts/current/protocol/TokenTransferProxy/TokenTransferProxy.sol)8
-rw-r--r--packages/contracts/src/contracts/previous/UnlimitedAllowanceToken/UnlimitedAllowanceToken_v1.sol2
-rw-r--r--packages/contracts/src/utils/address_utils.ts12
-rw-r--r--packages/contracts/src/utils/artifacts.ts37
-rw-r--r--packages/contracts/src/utils/asset_proxy_utils.ts38
-rw-r--r--packages/contracts/src/utils/chai_setup.ts13
-rw-r--r--packages/contracts/src/utils/constants.ts42
-rw-r--r--packages/contracts/src/utils/crypto.ts45
-rw-r--r--packages/contracts/src/utils/erc20_wrapper.ts116
-rw-r--r--packages/contracts/src/utils/erc721_wrapper.ts145
-rw-r--r--packages/contracts/src/utils/exchange_wrapper.ts234
-rw-r--r--packages/contracts/src/utils/formatters.ts60
-rw-r--r--packages/contracts/src/utils/log_decoder.ts39
-rw-r--r--packages/contracts/src/utils/multi_sig_wrapper.ts43
-rw-r--r--packages/contracts/src/utils/order_factory.ts37
-rw-r--r--packages/contracts/src/utils/order_utils.ts83
-rw-r--r--packages/contracts/src/utils/signing_utils.ts30
-rw-r--r--packages/contracts/src/utils/token_registry_wrapper.ts60
-rw-r--r--packages/contracts/src/utils/transaction_factory.ts35
-rw-r--r--packages/contracts/src/utils/types.ts145
-rw-r--r--packages/contracts/src/utils/web3_wrapper.ts12
83 files changed, 6369 insertions, 739 deletions
diff --git a/packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLock/MultiSigWalletWithTimeLock.sol b/packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLock/MultiSigWalletWithTimeLock.sol
index a545d9813..e393b565f 100644
--- a/packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLock/MultiSigWalletWithTimeLock.sol
+++ b/packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLock/MultiSigWalletWithTimeLock.sol
@@ -1,6 +1,6 @@
/*
- Copyright 2017 ZeroEx Intl.
+ 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.
diff --git a/packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.sol b/packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.sol
index 3c6a3d2ef..3d44e4c07 100644
--- a/packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.sol
+++ b/packages/contracts/src/contracts/current/multisig/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.sol
@@ -1,6 +1,6 @@
/*
- Copyright 2017 ZeroEx Intl.
+ 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.
diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC20Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC20Proxy.sol
new file mode 100644
index 000000000..c02536d67
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC20Proxy.sol
@@ -0,0 +1,83 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../utils/LibBytes/LibBytes.sol";
+import "../../tokens/ERC20Token/IERC20Token.sol";
+import "./MixinAssetProxy.sol";
+import "./MixinAuthorizable.sol";
+
+contract ERC20Proxy is
+ LibBytes,
+ MixinAssetProxy,
+ MixinAuthorizable
+{
+
+ // Id of this proxy.
+ uint8 constant PROXY_ID = 1;
+
+ // Revert reasons
+ string constant INVALID_METADATA_LENGTH = "Metadata must have a length of 21.";
+ string constant TRANSFER_FAILED = "Transfer failed.";
+ string constant PROXY_ID_MISMATCH = "Proxy id in metadata does not match this proxy id.";
+
+ /// @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
+ {
+ // Data must be intended for this proxy.
+ require(
+ uint8(assetMetadata[0]) == PROXY_ID,
+ PROXY_ID_MISMATCH
+ );
+
+ // Decode metadata.
+ require(
+ assetMetadata.length == 21,
+ INVALID_METADATA_LENGTH
+ );
+ address token = readAddress(assetMetadata, 1);
+
+ // Transfer tokens.
+ bool success = IERC20Token(token).transferFrom(from, to, amount);
+ require(
+ success == true,
+ TRANSFER_FAILED
+ );
+ }
+
+ /// @dev Gets the proxy id associated with the proxy address.
+ /// @return Proxy id.
+ function getProxyId()
+ external
+ view
+ returns (uint8)
+ {
+ return PROXY_ID;
+ }
+}
diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC721Proxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC721Proxy.sol
new file mode 100644
index 000000000..475359087
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/ERC721Proxy.sol
@@ -0,0 +1,89 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../utils/LibBytes/LibBytes.sol";
+import "../../tokens/ERC721Token/ERC721Token.sol";
+import "./MixinAssetProxy.sol";
+import "./MixinAuthorizable.sol";
+
+contract ERC721Proxy is
+ LibBytes,
+ MixinAssetProxy,
+ MixinAuthorizable
+{
+
+ // Id of this proxy.
+ uint8 constant PROXY_ID = 2;
+
+ // Revert reasons
+ string constant INVALID_TRANSFER_AMOUNT = "Transfer amount must equal 1.";
+ string constant INVALID_METADATA_LENGTH = "Metadata must have a length of 53.";
+ string constant PROXY_ID_MISMATCH = "Proxy id in metadata does not match this proxy id.";
+
+ /// @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
+ {
+ // Data must be intended for this proxy.
+ require(
+ uint8(assetMetadata[0]) == PROXY_ID,
+ PROXY_ID_MISMATCH
+ );
+
+ // There exists only 1 of each token.
+ require(
+ amount == 1,
+ INVALID_TRANSFER_AMOUNT
+ );
+
+ // Decode metadata
+ require(
+ assetMetadata.length == 53,
+ INVALID_METADATA_LENGTH
+ );
+ address token = readAddress(assetMetadata, 1);
+ uint256 tokenId = readUint256(assetMetadata, 21);
+
+ // Transfer token.
+ // Either succeeds or throws.
+ // @TODO: Call safeTransferFrom if there is additional
+ // data stored in `assetMetadata`.
+ ERC721Token(token).transferFrom(from, to, tokenId);
+ }
+
+ /// @dev Gets the proxy id associated with the proxy address.
+ /// @return Proxy id.
+ function getProxyId()
+ external
+ view
+ returns (uint8)
+ {
+ return PROXY_ID;
+ }
+}
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..d58cfc2dd
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAssetProxy.sol
@@ -0,0 +1,73 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "./mixins/MAssetProxy.sol";
+import "./mixins/MAuthorizable.sol";
+
+contract MixinAssetProxy is
+ MAuthorizable,
+ MAssetProxy
+{
+
+ /// @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/MixinAuthorizable.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAuthorizable.sol
new file mode 100644
index 000000000..b66b783ea
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinAuthorizable.sol
@@ -0,0 +1,117 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "./mixins/MAuthorizable.sol";
+import "../../utils/Ownable/Ownable.sol";
+
+contract MixinAuthorizable is
+ Ownable,
+ MAuthorizable
+{
+
+ // Revert reasons
+ string constant SENDER_NOT_AUTHORIZED = "Sender not authorized to call this method.";
+ string constant TARGET_NOT_AUTHORIZED = "Target address must be authorized.";
+ string constant TARGET_ALREADY_AUTHORIZED = "Target must not already be authorized.";
+ string constant INDEX_OUT_OF_BOUNDS = "Specified array index is out of bounds.";
+ string constant INDEX_ADDRESS_MISMATCH = "Address found at index does not match target address.";
+
+ /// @dev Only authorized addresses can invoke functions with this modifier.
+ modifier onlyAuthorized {
+ require(
+ authorized[msg.sender],
+ SENDER_NOT_AUTHORIZED
+ );
+ _;
+ }
+
+ mapping (address => bool) public authorized;
+ address[] public authorities;
+
+ /// @dev Authorizes an address.
+ /// @param target Address to authorize.
+ function addAuthorizedAddress(address target)
+ external
+ onlyOwner
+ {
+ require(
+ !authorized[target],
+ TARGET_ALREADY_AUTHORIZED
+ );
+
+ authorized[target] = true;
+ authorities.push(target);
+ emit AuthorizedAddressAdded(target, msg.sender);
+ }
+
+ /// @dev Removes authorizion of an address.
+ /// @param target Address to remove authorization from.
+ function removeAuthorizedAddress(address target)
+ external
+ onlyOwner
+ {
+ require(
+ authorized[target],
+ TARGET_NOT_AUTHORIZED
+ );
+
+ delete authorized[target];
+ for (uint i = 0; i < authorities.length; i++) {
+ if (authorities[i] == target) {
+ authorities[i] = authorities[authorities.length - 1];
+ authorities.length -= 1;
+ break;
+ }
+ }
+ emit AuthorizedAddressRemoved(target, msg.sender);
+ }
+
+ /// @dev Removes authorizion of an address.
+ /// @param target Address to remove authorization from.
+ /// @param index Index of target in authorities array.
+ function removeAuthorizedAddressAtIndex(address target, uint256 index)
+ external
+ {
+ require(
+ index < authorities.length,
+ INDEX_OUT_OF_BOUNDS
+ );
+ require(
+ authorities[index] == target,
+ INDEX_ADDRESS_MISMATCH
+ );
+
+ delete authorized[target];
+ authorities[index] = authorities[authorities.length - 1];
+ authorities.length -= 1;
+ emit AuthorizedAddressRemoved(target, msg.sender);
+ }
+
+ /// @dev Gets all authorized addresses.
+ /// @return Array of authorized addresses.
+ function getAuthorizedAddresses()
+ external
+ view
+ returns (address[] memory)
+ {
+ return authorities;
+ }
+}
diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/interfaces/IAssetProxy.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/interfaces/IAssetProxy.sol
new file mode 100644
index 000000000..eca6524e5
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/interfaces/IAssetProxy.sol
@@ -0,0 +1,59 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "./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 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;
+
+ /// @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;
+
+ /// @dev Gets the proxy id associated with the proxy address.
+ /// @return Proxy id.
+ function getProxyId()
+ external
+ view
+ returns (uint8);
+}
+
diff --git a/packages/contracts/src/contracts/current/protocol/AssetProxy/interfaces/IAuthorizable.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/interfaces/IAuthorizable.sol
new file mode 100644
index 000000000..3120be7ec
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/interfaces/IAuthorizable.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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../../utils/Ownable/IOwnable.sol";
+
+contract IAuthorizable is
+ IOwnable
+{
+
+ /// @dev Gets all authorized addresses.
+ /// @return Array of authorized addresses.
+ function getAuthorizedAddresses()
+ external
+ view
+ returns (address[]);
+
+ /// @dev Authorizes an address.
+ /// @param target Address to authorize.
+ function addAuthorizedAddress(address target)
+ external;
+
+ /// @dev Removes authorizion of an address.
+ /// @param target Address to remove authorization from.
+ function removeAuthorizedAddress(address target)
+ external;
+
+ /// @dev Removes authorizion of an address.
+ /// @param target Address to remove authorization from.
+ /// @param index Index of target in authorities array.
+ function removeAuthorizedAddressAtIndex(address target, uint256 index)
+ external;
+}
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..e0ec8c4e1
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAssetProxy.sol
@@ -0,0 +1,39 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../interfaces/IAssetProxy.sol";
+
+contract MAssetProxy is
+ IAssetProxy
+{
+
+ /// @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/mixins/MAuthorizable.sol b/packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAuthorizable.sol
new file mode 100644
index 000000000..71d1910e5
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/AssetProxy/mixins/MAuthorizable.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.23;
+pragma experimental ABIEncoderV2;
+
+import "../interfaces/IAuthorizable.sol";
+
+contract MAuthorizable is
+ IAuthorizable
+{
+
+ // Event logged when a new address is authorized.
+ event AuthorizedAddressAdded(
+ address indexed target,
+ address indexed caller
+ );
+
+ // Event logged when a currently authorized address is unauthorized.
+ event AuthorizedAddressRemoved(
+ address indexed target,
+ address indexed caller
+ );
+
+ /// @dev Only authorized addresses can invoke functions with this modifier.
+ modifier onlyAuthorized { _; }
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol b/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol
index 8dacf797c..85e2bc47e 100644
--- a/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol
@@ -1,6 +1,6 @@
/*
- Copyright 2017 ZeroEx Intl.
+ 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.
@@ -16,587 +16,33 @@
*/
-pragma solidity ^0.4.14;
-
-import { TokenTransferProxy } from "../TokenTransferProxy/TokenTransferProxy.sol";
-import { Token_v1 as Token } from "../../../previous/Token/Token_v1.sol";
-import { SafeMath_v1 as SafeMath } from "../../../previous/SafeMath/SafeMath_v1.sol";
-
-/// @title Exchange - Facilitates exchange of ERC20 tokens.
-/// @author Amir Bandeali - <amir@0xProject.com>, Will Warren - <will@0xProject.com>
-contract Exchange is SafeMath {
-
- // Error Codes
- enum Errors {
- ORDER_EXPIRED, // Order has already expired
- ORDER_FULLY_FILLED_OR_CANCELLED, // Order has already been fully filled or cancelled
- ROUNDING_ERROR_TOO_LARGE, // Rounding error too large
- INSUFFICIENT_BALANCE_OR_ALLOWANCE // Insufficient balance or allowance for token transfer
- }
-
- string constant public VERSION = "1.0.0";
- uint16 constant public EXTERNAL_QUERY_GAS_LIMIT = 4999; // Changes to state require at least 5000 gas
-
- address public ZRX_TOKEN_CONTRACT;
- address public TOKEN_TRANSFER_PROXY_CONTRACT;
-
- // Mappings of orderHash => amounts of takerTokenAmount filled or cancelled.
- mapping (bytes32 => uint) public filled;
- mapping (bytes32 => uint) public cancelled;
-
- event LogFill(
- address indexed maker,
- address taker,
- address indexed feeRecipient,
- address makerToken,
- address takerToken,
- uint filledMakerTokenAmount,
- uint filledTakerTokenAmount,
- uint paidMakerFee,
- uint paidTakerFee,
- bytes32 indexed tokens, // keccak256(makerToken, takerToken), allows subscribing to a token pair
- bytes32 orderHash
- );
-
- event LogCancel(
- address indexed maker,
- address indexed feeRecipient,
- address makerToken,
- address takerToken,
- uint cancelledMakerTokenAmount,
- uint cancelledTakerTokenAmount,
- bytes32 indexed tokens,
- bytes32 orderHash
- );
-
- event LogError(uint8 indexed errorId, bytes32 indexed orderHash);
-
- struct Order {
- address maker;
- address taker;
- address makerToken;
- address takerToken;
- address feeRecipient;
- uint makerTokenAmount;
- uint takerTokenAmount;
- uint makerFee;
- uint takerFee;
- uint expirationTimestampInSec;
- bytes32 orderHash;
- }
-
- function Exchange(address _zrxToken, address _tokenTransferProxy) {
- ZRX_TOKEN_CONTRACT = _zrxToken;
- TOKEN_TRANSFER_PROXY_CONTRACT = _tokenTransferProxy;
- }
-
- /*
- * Core exchange functions
- */
-
- /// @dev Fills the input order.
- /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
- /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
- /// @param fillTakerTokenAmount Desired amount of takerToken to fill.
- /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfer will fail before attempting.
- /// @param v ECDSA signature parameter v.
- /// @param r ECDSA signature parameters r.
- /// @param s ECDSA signature parameters s.
- /// @return Total amount of takerToken filled in trade.
- function fillOrder(
- address[5] orderAddresses,
- uint[6] orderValues,
- uint fillTakerTokenAmount,
- bool shouldThrowOnInsufficientBalanceOrAllowance,
- uint8 v,
- bytes32 r,
- bytes32 s)
- public
- returns (uint filledTakerTokenAmount)
- {
- Order memory order = Order({
- maker: orderAddresses[0],
- taker: orderAddresses[1],
- makerToken: orderAddresses[2],
- takerToken: orderAddresses[3],
- feeRecipient: orderAddresses[4],
- makerTokenAmount: orderValues[0],
- takerTokenAmount: orderValues[1],
- makerFee: orderValues[2],
- takerFee: orderValues[3],
- expirationTimestampInSec: orderValues[4],
- orderHash: getOrderHash(orderAddresses, orderValues)
- });
-
- require(order.taker == address(0) || order.taker == msg.sender);
- require(order.makerTokenAmount > 0 && order.takerTokenAmount > 0 && fillTakerTokenAmount > 0);
- require(isValidSignature(
- order.maker,
- order.orderHash,
- v,
- r,
- s
- ));
-
- if (block.timestamp >= order.expirationTimestampInSec) {
- LogError(uint8(Errors.ORDER_EXPIRED), order.orderHash);
- return 0;
- }
-
- uint remainingTakerTokenAmount = safeSub(order.takerTokenAmount, getUnavailableTakerTokenAmount(order.orderHash));
- filledTakerTokenAmount = min256(fillTakerTokenAmount, remainingTakerTokenAmount);
- if (filledTakerTokenAmount == 0) {
- LogError(uint8(Errors.ORDER_FULLY_FILLED_OR_CANCELLED), order.orderHash);
- return 0;
- }
-
- if (isRoundingError(filledTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount)) {
- LogError(uint8(Errors.ROUNDING_ERROR_TOO_LARGE), order.orderHash);
- return 0;
- }
-
- if (!shouldThrowOnInsufficientBalanceOrAllowance && !isTransferable(order, filledTakerTokenAmount)) {
- LogError(uint8(Errors.INSUFFICIENT_BALANCE_OR_ALLOWANCE), order.orderHash);
- return 0;
- }
-
- uint filledMakerTokenAmount = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount);
- uint paidMakerFee;
- uint paidTakerFee;
- filled[order.orderHash] = safeAdd(filled[order.orderHash], filledTakerTokenAmount);
- require(transferViaTokenTransferProxy(
- order.makerToken,
- order.maker,
- msg.sender,
- filledMakerTokenAmount
- ));
- require(transferViaTokenTransferProxy(
- order.takerToken,
- msg.sender,
- order.maker,
- filledTakerTokenAmount
- ));
- if (order.feeRecipient != address(0)) {
- if (order.makerFee > 0) {
- paidMakerFee = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.makerFee);
- require(transferViaTokenTransferProxy(
- ZRX_TOKEN_CONTRACT,
- order.maker,
- order.feeRecipient,
- paidMakerFee
- ));
- }
- if (order.takerFee > 0) {
- paidTakerFee = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.takerFee);
- require(transferViaTokenTransferProxy(
- ZRX_TOKEN_CONTRACT,
- msg.sender,
- order.feeRecipient,
- paidTakerFee
- ));
- }
- }
-
- LogFill(
- order.maker,
- msg.sender,
- order.feeRecipient,
- order.makerToken,
- order.takerToken,
- filledMakerTokenAmount,
- filledTakerTokenAmount,
- paidMakerFee,
- paidTakerFee,
- keccak256(order.makerToken, order.takerToken),
- order.orderHash
- );
- return filledTakerTokenAmount;
- }
-
- /// @dev Cancels the input order.
- /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
- /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
- /// @param cancelTakerTokenAmount Desired amount of takerToken to cancel in order.
- /// @return Amount of takerToken cancelled.
- function cancelOrder(
- address[5] orderAddresses,
- uint[6] orderValues,
- uint cancelTakerTokenAmount)
- public
- returns (uint)
- {
- Order memory order = Order({
- maker: orderAddresses[0],
- taker: orderAddresses[1],
- makerToken: orderAddresses[2],
- takerToken: orderAddresses[3],
- feeRecipient: orderAddresses[4],
- makerTokenAmount: orderValues[0],
- takerTokenAmount: orderValues[1],
- makerFee: orderValues[2],
- takerFee: orderValues[3],
- expirationTimestampInSec: orderValues[4],
- orderHash: getOrderHash(orderAddresses, orderValues)
- });
-
- require(order.maker == msg.sender);
- require(order.makerTokenAmount > 0 && order.takerTokenAmount > 0 && cancelTakerTokenAmount > 0);
-
- if (block.timestamp >= order.expirationTimestampInSec) {
- LogError(uint8(Errors.ORDER_EXPIRED), order.orderHash);
- return 0;
- }
-
- uint remainingTakerTokenAmount = safeSub(order.takerTokenAmount, getUnavailableTakerTokenAmount(order.orderHash));
- uint cancelledTakerTokenAmount = min256(cancelTakerTokenAmount, remainingTakerTokenAmount);
- if (cancelledTakerTokenAmount == 0) {
- LogError(uint8(Errors.ORDER_FULLY_FILLED_OR_CANCELLED), order.orderHash);
- return 0;
- }
-
- cancelled[order.orderHash] = safeAdd(cancelled[order.orderHash], cancelledTakerTokenAmount);
-
- LogCancel(
- order.maker,
- order.feeRecipient,
- order.makerToken,
- order.takerToken,
- getPartialAmount(cancelledTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount),
- cancelledTakerTokenAmount,
- keccak256(order.makerToken, order.takerToken),
- order.orderHash
- );
- return cancelledTakerTokenAmount;
- }
-
- /*
- * Wrapper functions
- */
-
- /// @dev Fills an order with specified parameters and ECDSA signature, throws if specified amount not filled entirely.
- /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
- /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
- /// @param fillTakerTokenAmount Desired amount of takerToken to fill.
- /// @param v ECDSA signature parameter v.
- /// @param r ECDSA signature parameters r.
- /// @param s ECDSA signature parameters s.
- function fillOrKillOrder(
- address[5] orderAddresses,
- uint[6] orderValues,
- uint fillTakerTokenAmount,
- uint8 v,
- bytes32 r,
- bytes32 s)
- public
- {
- require(fillOrder(
- orderAddresses,
- orderValues,
- fillTakerTokenAmount,
- false,
- v,
- r,
- s
- ) == fillTakerTokenAmount);
- }
-
- /// @dev Synchronously executes multiple fill orders in a single transaction.
- /// @param orderAddresses Array of address arrays containing individual order addresses.
- /// @param orderValues Array of uint arrays containing individual order values.
- /// @param fillTakerTokenAmounts Array of desired amounts of takerToken to fill in orders.
- /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfers will fail before attempting.
- /// @param v Array ECDSA signature v parameters.
- /// @param r Array of ECDSA signature r parameters.
- /// @param s Array of ECDSA signature s parameters.
- function batchFillOrders(
- address[5][] orderAddresses,
- uint[6][] orderValues,
- uint[] fillTakerTokenAmounts,
- bool shouldThrowOnInsufficientBalanceOrAllowance,
- uint8[] v,
- bytes32[] r,
- bytes32[] s)
- public
- {
- for (uint i = 0; i < orderAddresses.length; i++) {
- fillOrder(
- orderAddresses[i],
- orderValues[i],
- fillTakerTokenAmounts[i],
- shouldThrowOnInsufficientBalanceOrAllowance,
- v[i],
- r[i],
- s[i]
- );
- }
- }
-
- /// @dev Synchronously executes multiple fillOrKill orders in a single transaction.
- /// @param orderAddresses Array of address arrays containing individual order addresses.
- /// @param orderValues Array of uint arrays containing individual order values.
- /// @param fillTakerTokenAmounts Array of desired amounts of takerToken to fill in orders.
- /// @param v Array ECDSA signature v parameters.
- /// @param r Array of ECDSA signature r parameters.
- /// @param s Array of ECDSA signature s parameters.
- function batchFillOrKillOrders(
- address[5][] orderAddresses,
- uint[6][] orderValues,
- uint[] fillTakerTokenAmounts,
- uint8[] v,
- bytes32[] r,
- bytes32[] s)
- public
- {
- for (uint i = 0; i < orderAddresses.length; i++) {
- fillOrKillOrder(
- orderAddresses[i],
- orderValues[i],
- fillTakerTokenAmounts[i],
- v[i],
- r[i],
- s[i]
- );
- }
- }
-
- /// @dev Synchronously executes multiple fill orders in a single transaction until total fillTakerTokenAmount filled.
- /// @param orderAddresses Array of address arrays containing individual order addresses.
- /// @param orderValues Array of uint arrays containing individual order values.
- /// @param fillTakerTokenAmount Desired total amount of takerToken to fill in orders.
- /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfers will fail before attempting.
- /// @param v Array ECDSA signature v parameters.
- /// @param r Array of ECDSA signature r parameters.
- /// @param s Array of ECDSA signature s parameters.
- /// @return Total amount of fillTakerTokenAmount filled in orders.
- function fillOrdersUpTo(
- address[5][] orderAddresses,
- uint[6][] orderValues,
- uint fillTakerTokenAmount,
- bool shouldThrowOnInsufficientBalanceOrAllowance,
- uint8[] v,
- bytes32[] r,
- bytes32[] s)
- public
- returns (uint)
- {
- uint filledTakerTokenAmount = 0;
- for (uint i = 0; i < orderAddresses.length; i++) {
- require(orderAddresses[i][3] == orderAddresses[0][3]); // takerToken must be the same for each order
- filledTakerTokenAmount = safeAdd(filledTakerTokenAmount, fillOrder(
- orderAddresses[i],
- orderValues[i],
- safeSub(fillTakerTokenAmount, filledTakerTokenAmount),
- shouldThrowOnInsufficientBalanceOrAllowance,
- v[i],
- r[i],
- s[i]
- ));
- if (filledTakerTokenAmount == fillTakerTokenAmount) break;
- }
- return filledTakerTokenAmount;
- }
-
- /// @dev Synchronously cancels multiple orders in a single transaction.
- /// @param orderAddresses Array of address arrays containing individual order addresses.
- /// @param orderValues Array of uint arrays containing individual order values.
- /// @param cancelTakerTokenAmounts Array of desired amounts of takerToken to cancel in orders.
- function batchCancelOrders(
- address[5][] orderAddresses,
- uint[6][] orderValues,
- uint[] cancelTakerTokenAmounts)
- public
- {
- for (uint i = 0; i < orderAddresses.length; i++) {
- cancelOrder(
- orderAddresses[i],
- orderValues[i],
- cancelTakerTokenAmounts[i]
- );
- }
- }
-
- /*
- * Constant public functions
- */
-
- /// @dev Calculates Keccak-256 hash of order with specified parameters.
- /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
- /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
- /// @return Keccak-256 hash of order.
- function getOrderHash(address[5] orderAddresses, uint[6] orderValues)
- public
- constant
- returns (bytes32)
- {
- return keccak256(
- address(this),
- orderAddresses[0], // maker
- orderAddresses[1], // taker
- orderAddresses[2], // makerToken
- orderAddresses[3], // takerToken
- orderAddresses[4], // feeRecipient
- orderValues[0], // makerTokenAmount
- orderValues[1], // takerTokenAmount
- orderValues[2], // makerFee
- orderValues[3], // takerFee
- orderValues[4], // expirationTimestampInSec
- orderValues[5] // salt
- );
- }
-
- /// @dev Verifies that an order signature is valid.
- /// @param signer address of signer.
- /// @param hash Signed Keccak-256 hash.
- /// @param v ECDSA signature parameter v.
- /// @param r ECDSA signature parameters r.
- /// @param s ECDSA signature parameters s.
- /// @return Validity of order signature.
- function isValidSignature(
- address signer,
- bytes32 hash,
- uint8 v,
- bytes32 r,
- bytes32 s)
- public
- constant
- returns (bool)
- {
- return signer == ecrecover(
- keccak256("\x19Ethereum Signed Message:\n32", hash),
- v,
- r,
- s
- );
- }
-
- /// @dev Checks if rounding error > 0.1%.
- /// @param numerator Numerator.
- /// @param denominator Denominator.
- /// @param target Value to multiply with numerator/denominator.
- /// @return Rounding error is present.
- function isRoundingError(uint numerator, uint denominator, uint target)
- public
- constant
- returns (bool)
- {
- uint remainder = mulmod(target, numerator, denominator);
- if (remainder == 0) return false; // No rounding error.
-
- uint errPercentageTimes1000000 = safeDiv(
- safeMul(remainder, 1000000),
- safeMul(numerator, target)
- );
- return errPercentageTimes1000000 > 1000;
- }
-
- /// @dev Calculates partial value given a numerator and denominator.
- /// @param numerator Numerator.
- /// @param denominator Denominator.
- /// @param target Value to calculate partial of.
- /// @return Partial value of target.
- function getPartialAmount(uint numerator, uint denominator, uint target)
- public
- constant
- returns (uint)
- {
- return safeDiv(safeMul(numerator, target), denominator);
- }
-
- /// @dev Calculates the sum of values already filled and cancelled for a given order.
- /// @param orderHash The Keccak-256 hash of the given order.
- /// @return Sum of values already filled and cancelled.
- function getUnavailableTakerTokenAmount(bytes32 orderHash)
- public
- constant
- returns (uint)
- {
- return safeAdd(filled[orderHash], cancelled[orderHash]);
- }
-
-
- /*
- * Internal functions
- */
-
- /// @dev Transfers a token using TokenTransferProxy transferFrom function.
- /// @param token Address of token to transferFrom.
- /// @param from Address transfering token.
- /// @param to Address receiving token.
- /// @param value Amount of token to transfer.
- /// @return Success of token transfer.
- function transferViaTokenTransferProxy(
- address token,
- address from,
- address to,
- uint value)
- internal
- returns (bool)
- {
- return TokenTransferProxy(TOKEN_TRANSFER_PROXY_CONTRACT).transferFrom(token, from, to, value);
- }
-
- /// @dev Checks if any order transfers will fail.
- /// @param order Order struct of params that will be checked.
- /// @param fillTakerTokenAmount Desired amount of takerToken to fill.
- /// @return Predicted result of transfers.
- function isTransferable(Order order, uint fillTakerTokenAmount)
- internal
- constant // The called token contracts may attempt to change state, but will not be able to due to gas limits on getBalance and getAllowance.
- returns (bool)
- {
- address taker = msg.sender;
- uint fillMakerTokenAmount = getPartialAmount(fillTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount);
-
- if (order.feeRecipient != address(0)) {
- bool isMakerTokenZRX = order.makerToken == ZRX_TOKEN_CONTRACT;
- bool isTakerTokenZRX = order.takerToken == ZRX_TOKEN_CONTRACT;
- uint paidMakerFee = getPartialAmount(fillTakerTokenAmount, order.takerTokenAmount, order.makerFee);
- uint paidTakerFee = getPartialAmount(fillTakerTokenAmount, order.takerTokenAmount, order.takerFee);
- uint requiredMakerZRX = isMakerTokenZRX ? safeAdd(fillMakerTokenAmount, paidMakerFee) : paidMakerFee;
- uint requiredTakerZRX = isTakerTokenZRX ? safeAdd(fillTakerTokenAmount, paidTakerFee) : paidTakerFee;
-
- if ( getBalance(ZRX_TOKEN_CONTRACT, order.maker) < requiredMakerZRX
- || getAllowance(ZRX_TOKEN_CONTRACT, order.maker) < requiredMakerZRX
- || getBalance(ZRX_TOKEN_CONTRACT, taker) < requiredTakerZRX
- || getAllowance(ZRX_TOKEN_CONTRACT, taker) < requiredTakerZRX
- ) return false;
-
- if (!isMakerTokenZRX && ( getBalance(order.makerToken, order.maker) < fillMakerTokenAmount // Don't double check makerToken if ZRX
- || getAllowance(order.makerToken, order.maker) < fillMakerTokenAmount)
- ) return false;
- if (!isTakerTokenZRX && ( getBalance(order.takerToken, taker) < fillTakerTokenAmount // Don't double check takerToken if ZRX
- || getAllowance(order.takerToken, taker) < fillTakerTokenAmount)
- ) return false;
- } else if ( getBalance(order.makerToken, order.maker) < fillMakerTokenAmount
- || getAllowance(order.makerToken, order.maker) < fillMakerTokenAmount
- || getBalance(order.takerToken, taker) < fillTakerTokenAmount
- || getAllowance(order.takerToken, taker) < fillTakerTokenAmount
- ) return false;
-
- return true;
- }
-
- /// @dev Get token balance of an address.
- /// @param token Address of token.
- /// @param owner Address of owner.
- /// @return Token balance of owner.
- function getBalance(address token, address owner)
- internal
- constant // The called token contract may attempt to change state, but will not be able to due to an added gas limit.
- returns (uint)
- {
- return Token(token).balanceOf.gas(EXTERNAL_QUERY_GAS_LIMIT)(owner); // Limit gas to prevent reentrancy
- }
-
- /// @dev Get allowance of token given to TokenTransferProxy by an address.
- /// @param token Address of token.
- /// @param owner Address of owner.
- /// @return Allowance of token given to TokenTransferProxy by owner.
- function getAllowance(address token, address owner)
- internal
- constant // The called token contract may attempt to change state, but will not be able to due to an added gas limit.
- returns (uint)
- {
- return Token(token).allowance.gas(EXTERNAL_QUERY_GAS_LIMIT)(owner, TOKEN_TRANSFER_PROXY_CONTRACT); // Limit gas to prevent reentrancy
- }
+pragma solidity ^0.4.23;
+pragma experimental ABIEncoderV2;
+
+import "./MixinExchangeCore.sol";
+import "./MixinSignatureValidator.sol";
+import "./MixinSettlement.sol";
+import "./MixinWrapperFunctions.sol";
+import "./MixinAssetProxyDispatcher.sol";
+import "./MixinTransactions.sol";
+
+contract Exchange is
+ MixinWrapperFunctions,
+ MixinExchangeCore,
+ MixinSettlement,
+ MixinSignatureValidator,
+ MixinTransactions,
+ MixinAssetProxyDispatcher
+{
+ string constant public VERSION = "2.0.1-alpha";
+
+ constructor (bytes memory _zrxProxyData)
+ public
+ MixinExchangeCore()
+ MixinSignatureValidator()
+ MixinSettlement(_zrxProxyData)
+ MixinWrapperFunctions()
+ MixinAssetProxyDispatcher()
+ MixinTransactions()
+ {}
}
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..308dace32
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinAssetProxyDispatcher.sol
@@ -0,0 +1,106 @@
+/*
+
+ 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.23;
+
+import "../../utils/Ownable/Ownable.sol";
+import "../AssetProxy/interfaces/IAssetProxy.sol";
+import "./libs/LibExchangeErrors.sol";
+import "./mixins/MAssetProxyDispatcher.sol";
+
+contract MixinAssetProxyDispatcher is
+ LibExchangeErrors,
+ Ownable,
+ MAssetProxyDispatcher
+{
+ // Mapping from Asset Proxy Id's to their respective Asset Proxy
+ mapping (uint8 => IAssetProxy) public assetProxies;
+
+ /// @dev Registers an asset proxy to an asset proxy id.
+ /// An id can only be assigned to a single proxy at a given time.
+ /// @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]),
+ OLD_ASSET_PROXY_MISMATCH
+ );
+
+ IAssetProxy assetProxy = IAssetProxy(newAssetProxy);
+
+ // Ensure that the id of newAssetProxy matches the passed in assetProxyId, unless it is being reset to 0.
+ if (newAssetProxy != address(0)) {
+ uint8 newAssetProxyId = assetProxy.getProxyId();
+ require(
+ newAssetProxyId == assetProxyId,
+ NEW_ASSET_PROXY_MISMATCH
+ );
+ }
+
+ // Add asset proxy and log registration.
+ assetProxies[assetProxyId] = assetProxy;
+ 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)
+ {
+ address assetProxy = address(assetProxies[assetProxyId]);
+ return assetProxy;
+ }
+
+ /// @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,
+ GT_ZERO_LENGTH_REQUIRED
+ );
+ uint8 assetProxyId = uint8(assetMetadata[0]);
+ IAssetProxy assetProxy = assetProxies[assetProxyId];
+
+ // transferFrom will either succeed or throw.
+ assetProxy.transferFrom(assetMetadata, from, to, amount);
+ }
+ }
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol
new file mode 100644
index 000000000..82c7a2ddc
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol
@@ -0,0 +1,243 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "./libs/LibFillResults.sol";
+import "./libs/LibOrder.sol";
+import "./libs/LibMath.sol";
+import "./libs/LibExchangeErrors.sol";
+import "./mixins/MExchangeCore.sol";
+import "./mixins/MSettlement.sol";
+import "./mixins/MSignatureValidator.sol";
+import "./mixins/MTransactions.sol";
+
+contract MixinExchangeCore is
+ LibOrder,
+ LibFillResults,
+ LibMath,
+ LibExchangeErrors,
+ MExchangeCore,
+ MSettlement,
+ MSignatureValidator,
+ MTransactions
+{
+ // Mapping of orderHash => amount of takerAsset already bought by maker
+ mapping (bytes32 => uint256) public filled;
+
+ // Mapping of orderHash => cancelled
+ mapping (bytes32 => bool) public cancelled;
+
+ // Mapping of makerAddress => lowest salt an order can have in order to be fillable
+ // Orders with a salt less than their maker's epoch are considered cancelled
+ mapping (address => uint256) public makerEpoch;
+
+ /// @dev Cancels all orders reated by sender with a salt less than or equal to the specified salt value.
+ /// @param salt Orders created with a salt less or equal to this value will be cancelled.
+ function cancelOrdersUpTo(uint256 salt)
+ external
+ {
+ uint256 newMakerEpoch = salt + 1; // makerEpoch is initialized to 0, so to cancelUpTo we need salt + 1
+ require(
+ newMakerEpoch > makerEpoch[msg.sender], // epoch must be monotonically increasing
+ INVALID_NEW_MAKER_EPOCH
+ );
+ makerEpoch[msg.sender] = newMakerEpoch;
+ emit CancelUpTo(msg.sender, newMakerEpoch);
+ }
+
+ /// @dev Fills the input order.
+ /// @param order Order struct containing order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signature Proof that order has been created by maker.
+ /// @return Amounts filled and fees paid by maker and taker.
+ function fillOrder(
+ Order memory order,
+ uint256 takerAssetFillAmount,
+ bytes memory signature)
+ public
+ returns (FillResults memory fillResults)
+ {
+ // Compute the order hash
+ bytes32 orderHash = getOrderHash(order);
+
+ // Check if order has been cancelled by salt value
+ if (order.salt < makerEpoch[order.makerAddress]) {
+ emit ExchangeError(uint8(Errors.ORDER_CANCELLED), orderHash);
+ return fillResults;
+ }
+
+ // Check if order has been cancelled by orderHash
+ if (cancelled[orderHash]) {
+ emit ExchangeError(uint8(Errors.ORDER_CANCELLED), orderHash);
+ return fillResults;
+ }
+
+ // Validate order and maker only if first time seen
+ // TODO: Read filled and cancelled only once
+ if (filled[orderHash] == 0) {
+ require(
+ order.makerAssetAmount > 0,
+ GT_ZERO_AMOUNT_REQUIRED
+ );
+ require(
+ order.takerAssetAmount > 0,
+ GT_ZERO_AMOUNT_REQUIRED
+ );
+ require(
+ isValidSignature(orderHash, order.makerAddress, signature),
+ SIGNATURE_VALIDATION_FAILED
+ );
+ }
+
+ // Validate sender is allowed to fill this order
+ if (order.senderAddress != address(0)) {
+ require(
+ order.senderAddress == msg.sender,
+ INVALID_SENDER
+ );
+ }
+
+ // Validate taker is allowed to fill this order
+ address takerAddress = getCurrentContextAddress();
+ if (order.takerAddress != address(0)) {
+ require(
+ order.takerAddress == takerAddress,
+ INVALID_CONTEXT
+ );
+ }
+ require(
+ takerAssetFillAmount > 0,
+ GT_ZERO_AMOUNT_REQUIRED
+ );
+
+ // Validate order expiration
+ if (block.timestamp >= order.expirationTimeSeconds) {
+ emit ExchangeError(uint8(Errors.ORDER_EXPIRED), orderHash);
+ return fillResults;
+ }
+
+ // Validate order availability
+ uint256 remainingTakerAssetFillAmount = safeSub(order.takerAssetAmount, filled[orderHash]);
+ if (remainingTakerAssetFillAmount == 0) {
+ emit ExchangeError(uint8(Errors.ORDER_FULLY_FILLED), orderHash);
+ return fillResults;
+ }
+
+ // Validate fill order rounding
+ fillResults.takerAssetFilledAmount = min256(takerAssetFillAmount, remainingTakerAssetFillAmount);
+ if (isRoundingError(fillResults.takerAssetFilledAmount, order.takerAssetAmount, order.makerAssetAmount)) {
+ emit ExchangeError(uint8(Errors.ROUNDING_ERROR_TOO_LARGE), orderHash);
+ fillResults.takerAssetFilledAmount = 0;
+ return fillResults;
+ }
+
+ // Update state
+ filled[orderHash] = safeAdd(filled[orderHash], fillResults.takerAssetFilledAmount);
+
+ // Settle order
+ (fillResults.makerAssetFilledAmount, fillResults.makerFeePaid, fillResults.takerFeePaid) =
+ settleOrder(order, takerAddress, fillResults.takerAssetFilledAmount);
+
+ // Log order
+ emitFillEvent(order, takerAddress, orderHash, fillResults);
+ return fillResults;
+ }
+
+ /// @dev After calling, the order can not be filled anymore.
+ /// @param order Order struct containing order specifications.
+ /// @return True if the order state changed to cancelled.
+ /// False if the transaction was already cancelled or expired.
+ function cancelOrder(Order memory order)
+ public
+ returns (bool)
+ {
+ // Compute the order hash
+ bytes32 orderHash = getOrderHash(order);
+
+ // Validate the order
+ require(
+ order.makerAssetAmount > 0,
+ GT_ZERO_AMOUNT_REQUIRED
+ );
+ require(
+ order.takerAssetAmount > 0,
+ GT_ZERO_AMOUNT_REQUIRED
+ );
+
+ // Validate sender is allowed to cancel this order
+ if (order.senderAddress != address(0)) {
+ require(
+ order.senderAddress == msg.sender,
+ INVALID_SENDER
+ );
+ }
+
+ // Validate transaction signed by maker
+ address makerAddress = getCurrentContextAddress();
+ require(
+ order.makerAddress == makerAddress,
+ INVALID_CONTEXT
+ );
+
+ if (block.timestamp >= order.expirationTimeSeconds) {
+ emit ExchangeError(uint8(Errors.ORDER_EXPIRED), orderHash);
+ return false;
+ }
+
+ if (cancelled[orderHash]) {
+ emit ExchangeError(uint8(Errors.ORDER_CANCELLED), orderHash);
+ return false;
+ }
+
+ cancelled[orderHash] = true;
+
+ emit Cancel(
+ order.makerAddress,
+ order.feeRecipientAddress,
+ orderHash,
+ order.makerAssetData,
+ order.takerAssetData
+ );
+ return true;
+ }
+
+ /// @dev Logs a Fill event with the given arguments.
+ /// The sole purpose of this function is to get around the stack variable limit.
+ function emitFillEvent(
+ Order memory order,
+ address takerAddress,
+ bytes32 orderHash,
+ FillResults memory fillResults)
+ internal
+ {
+ emit Fill(
+ order.makerAddress,
+ takerAddress,
+ order.feeRecipientAddress,
+ fillResults.makerAssetFilledAmount,
+ fillResults.takerAssetFilledAmount,
+ fillResults.makerFeePaid,
+ fillResults.takerFeePaid,
+ orderHash,
+ order.makerAssetData,
+ order.takerAssetData
+ );
+ }
+}
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..30f1bb49b
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlement.sol
@@ -0,0 +1,99 @@
+/*
+
+ 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.23;
+
+import "./mixins/MSettlement.sol";
+import "./mixins/MAssetProxyDispatcher.sol";
+import "./libs/LibOrder.sol";
+import "./libs/LibMath.sol";
+
+contract MixinSettlement is
+ LibMath,
+ MSettlement,
+ MAssetProxyDispatcher
+{
+ // ZRX metadata used for fee transfers.
+ // This will be constant throughout the life of the Exchange contract,
+ // since ZRX will always be transferred via the ERC20 AssetProxy.
+ bytes internal ZRX_PROXY_DATA;
+
+ /// @dev Gets the ZRX metadata used for fee transfers.
+ function zrxProxyData()
+ external
+ view
+ returns (bytes memory)
+ {
+ return ZRX_PROXY_DATA;
+ }
+
+ /// TODO: _zrxProxyData should be a constant in production.
+ /// @dev Constructor sets the metadata that will be used for paying ZRX fees.
+ /// @param _zrxProxyData Byte array containing ERC20 proxy id concatenated with address of ZRX.
+ constructor (bytes memory _zrxProxyData)
+ public
+ {
+ ZRX_PROXY_DATA = _zrxProxyData;
+ }
+
+ /// @dev Settles an order by transferring assets between counterparties.
+ /// @param order Order struct containing order specifications.
+ /// @param takerAddress Address selling takerAsset and buying makerAsset.
+ /// @param takerAssetFilledAmount The amount of takerAsset that will be transferred to the order's maker.
+ /// @return Amount filled by maker and fees paid by maker/taker.
+ function settleOrder(
+ LibOrder.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/MixinSignatureValidator.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol
new file mode 100644
index 000000000..2322625d9
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol
@@ -0,0 +1,191 @@
+/*
+
+ 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.23;
+
+import "./mixins/MSignatureValidator.sol";
+import "./interfaces/ISigner.sol";
+import "./libs/LibExchangeErrors.sol";
+import "../../utils/LibBytes/LibBytes.sol";
+
+contract MixinSignatureValidator is
+ LibBytes,
+ LibExchangeErrors,
+ MSignatureValidator
+{
+
+ // Mapping of hash => signer => signed
+ mapping(bytes32 => mapping(address => bool)) preSigned;
+
+ /// @dev Approves a hash on-chain using any valid signature type.
+ /// After presigning a hash, the preSign signature type will become valid for that hash and signer.
+ /// @param signer Address that should have signed the given hash.
+ /// @param signature Proof that the hash has been signed by signer.
+ function preSign(
+ bytes32 hash,
+ address signer,
+ bytes signature)
+ external
+ {
+ require(
+ isValidSignature(hash, signer, signature),
+ SIGNATURE_VALIDATION_FAILED
+ );
+ preSigned[hash][signer] = true;
+ }
+
+ /// @dev Verifies that a hash has been signed by the given signer.
+ /// @param hash Any 32 byte hash.
+ /// @param signer Address that should have signed the given hash.
+ /// @param signature Proof that the hash has been signed by signer.
+ /// @return True if the address recovered from the provided signature matches the input signer address.
+ function isValidSignature(
+ bytes32 hash,
+ address signer,
+ bytes memory signature)
+ internal
+ view
+ returns (bool isValid)
+ {
+ // TODO: Domain separation: make hash depend on role. (Taker sig should not be valid as maker sig, etc.)
+
+ require(
+ signature.length >= 1,
+ INVALID_SIGNATURE_LENGTH
+ );
+ SignatureType signatureType = SignatureType(uint8(signature[0]));
+
+ // Variables are not scoped in Solidity
+ uint8 v;
+ bytes32 r;
+ bytes32 s;
+ address recovered;
+
+ // Always illegal signature
+ // This is always an implicit option since a signer can create a
+ // signature array with invalid type or length. We may as well make
+ // it an explicit option. This aids testing and analysis. It is
+ // also the initialization value for the enum type.
+ if (signatureType == SignatureType.Illegal) {
+ // NOTE: Reason cannot be assigned to a variable because of https://github.com/ethereum/solidity/issues/4051
+ revert("Illegal signature type.");
+
+ // Always invalid signature
+ // Like Illegal, this is always implicitly available and therefore
+ // offered explicitly. It can be implicitly created by providing
+ // a correctly formatted but incorrect signature.
+ } else if (signatureType == SignatureType.Invalid) {
+ require(
+ signature.length == 1,
+ INVALID_SIGNATURE_LENGTH
+ );
+ isValid = false;
+ return isValid;
+
+ // Implicitly signed by caller
+ // The signer has initiated the call. In the case of non-contract
+ // accounts it means the transaction itself was signed.
+ // Example: let's say for a particular operation three signatures
+ // A, B and C are required. To submit the transaction, A and B can
+ // give a signature to C, who can then submit the transaction using
+ // `Caller` for his own signature. Or A and C can sign and B can
+ // submit using `Caller`. Having `Caller` allows this flexibility.
+ } else if (signatureType == SignatureType.Caller) {
+ require(
+ signature.length == 1,
+ INVALID_SIGNATURE_LENGTH
+ );
+ isValid = signer == msg.sender;
+ return isValid;
+
+ // Signed using web3.eth_sign
+ } else if (signatureType == SignatureType.Ecrecover) {
+ require(
+ signature.length == 66,
+ INVALID_SIGNATURE_LENGTH
+ );
+ v = uint8(signature[1]);
+ r = readBytes32(signature, 2);
+ s = readBytes32(signature, 34);
+ recovered = ecrecover(
+ keccak256("\x19Ethereum Signed Message:\n32", hash),
+ v,
+ r,
+ s
+ );
+ isValid = signer == recovered;
+ return isValid;
+
+ // Signature using EIP712
+ } else if (signatureType == SignatureType.EIP712) {
+ require(
+ signature.length == 66,
+ INVALID_SIGNATURE_LENGTH
+ );
+ v = uint8(signature[1]);
+ r = readBytes32(signature, 2);
+ s = readBytes32(signature, 34);
+ recovered = ecrecover(hash, v, r, s);
+ isValid = signer == recovered;
+ return isValid;
+
+ // Signature from Trezor hardware wallet
+ // It differs from web3.eth_sign in the encoding of message length
+ // (Bitcoin varint encoding vs ascii-decimal, the latter is not
+ // self-terminating which leads to ambiguities).
+ // See also:
+ // https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
+ // https://github.com/trezor/trezor-mcu/blob/master/firmware/ethereum.c#L602
+ // https://github.com/trezor/trezor-mcu/blob/master/firmware/crypto.c#L36
+ } else if (signatureType == SignatureType.Trezor) {
+ require(
+ signature.length == 66,
+ INVALID_SIGNATURE_LENGTH
+ );
+ v = uint8(signature[1]);
+ r = readBytes32(signature, 2);
+ s = readBytes32(signature, 34);
+ recovered = ecrecover(
+ keccak256("\x19Ethereum Signed Message:\n\x41", hash),
+ v,
+ r,
+ s
+ );
+ isValid = signer == recovered;
+ return isValid;
+
+ // Signature verified by signer contract
+ } else if (signatureType == SignatureType.Contract) {
+ isValid = ISigner(signer).isValidSignature(hash, signature);
+ return isValid;
+
+ // Signer signed hash previously using the preSign function
+ } else if (signatureType == SignatureType.PreSigned) {
+ isValid = preSigned[hash][signer];
+ return isValid;
+ }
+
+ // Anything else is illegal (We do not return false because
+ // the signature may actually be valid, just not in a format
+ // that we currently support. In this case returning false
+ // may lead the caller to incorrectly believe that the
+ // signature was invalid.)
+ // NOTE: Reason cannot be assigned to a variable because of https://github.com/ethereum/solidity/issues/4051
+ revert("Unsupported signature type.");
+ }
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinTransactions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinTransactions.sol
new file mode 100644
index 000000000..7f12834a3
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinTransactions.sol
@@ -0,0 +1,102 @@
+/*
+
+ 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/MSignatureValidator.sol";
+import "./mixins/MTransactions.sol";
+import "./libs/LibExchangeErrors.sol";
+
+contract MixinTransactions is
+ LibExchangeErrors,
+ MSignatureValidator,
+ MTransactions
+{
+
+ // Mapping of transaction hash => executed
+ // This prevents transactions from being executed more than once.
+ mapping (bytes32 => bool) public transactions;
+
+ // Address of current transaction signer
+ address public currentContextAddress;
+
+ /// @dev Executes an exchange method call in the context of signer.
+ /// @param salt Arbitrary number to ensure uniqueness of transaction hash.
+ /// @param signer Address of transaction signer.
+ /// @param data AbiV2 encoded calldata.
+ /// @param signature Proof of signer transaction by signer.
+ function executeTransaction(
+ uint256 salt,
+ address signer,
+ bytes data,
+ bytes signature)
+ external
+ {
+ // Prevent reentrancy
+ require(currentContextAddress == address(0));
+
+ // Calculate transaction hash
+ bytes32 transactionHash = keccak256(
+ address(this),
+ salt,
+ data
+ );
+
+ // Validate transaction has not been executed
+ require(
+ !transactions[transactionHash],
+ DUPLICATE_TRANSACTION_HASH
+ );
+
+ // TODO: is SignatureType.Caller necessary if we make this check?
+ if (signer != msg.sender) {
+ // Validate signature
+ require(
+ isValidSignature(transactionHash, signer, signature),
+ SIGNATURE_VALIDATION_FAILED
+ );
+
+ // Set the current transaction signer
+ currentContextAddress = signer;
+ }
+
+ // Execute transaction
+ transactions[transactionHash] = true;
+ require(
+ address(this).delegatecall(data),
+ TRANSACTION_EXECUTION_FAILED
+ );
+
+ // Reset current transaction signer
+ // TODO: Check if gas is paid when currentContextAddress is already 0.
+ currentContextAddress = address(0);
+ }
+
+ /// @dev The current function will be called in the context of this address (either 0x transaction signer or `msg.sender`).
+ /// If calling a fill function, this address will represent the taker.
+ /// If calling a cancel function, this address will represent the maker.
+ /// @return Signer of 0x transaction if entry point is `executeTransaction`.
+ /// `msg.sender` if entry point is any other function.
+ function getCurrentContextAddress()
+ internal
+ view
+ returns (address)
+ {
+ address contextAddress = currentContextAddress == address(0) ? msg.sender : currentContextAddress;
+ return contextAddress;
+ }
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol
new file mode 100644
index 000000000..42517221e
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol
@@ -0,0 +1,514 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../utils/LibBytes/LibBytes.sol";
+import "./mixins/MExchangeCore.sol";
+import "./libs/LibMath.sol";
+import "./libs/LibOrder.sol";
+import "./libs/LibFillResults.sol";
+import "./libs/LibExchangeErrors.sol";
+
+contract MixinWrapperFunctions is
+ LibOrder,
+ LibFillResults,
+ LibMath,
+ LibBytes,
+ LibExchangeErrors,
+ MExchangeCore
+{
+ /// @dev Fills the input order. Reverts if exact takerAssetFillAmount not filled.
+ /// @param order Order struct containing order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signature Proof that order has been created by maker.
+ function fillOrKillOrder(
+ Order memory order,
+ uint256 takerAssetFillAmount,
+ bytes memory signature)
+ public
+ returns (FillResults memory fillResults)
+ {
+ fillResults = fillOrder(
+ order,
+ takerAssetFillAmount,
+ signature
+ );
+ require(
+ fillResults.takerAssetFilledAmount == takerAssetFillAmount,
+ COMPLETE_FILL_FAILED
+ );
+ return fillResults;
+ }
+
+ /// @dev Fills an order with specified parameters and ECDSA signature.
+ /// Returns false if the transaction would otherwise revert.
+ /// @param order Order struct containing order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signature Proof that order has been created by maker.
+ /// @return Amounts filled and fees paid by maker and taker.
+ function fillOrderNoThrow(
+ Order memory order,
+ uint256 takerAssetFillAmount,
+ bytes memory signature)
+ public
+ returns (FillResults memory fillResults)
+ {
+ // We need to call MExchangeCore.fillOrder using a delegatecall in
+ // assembly so that we can intercept a call that throws. For this, we
+ // need the input encoded in memory in the Ethereum ABIv2 format [1].
+
+ // | Area | Offset | Length | Contents |
+ // | -------- |--------|---------|-------------------------------------------- |
+ // | Header | 0x00 | 4 | function selector |
+ // | Params | | 3 * 32 | function parameters: |
+ // | | 0x00 | | 1. offset to order (*) |
+ // | | 0x20 | | 2. takerAssetFillAmount |
+ // | | 0x40 | | 3. offset to signature (*) |
+ // | Data | | 12 * 32 | order: |
+ // | | 0x000 | | 1. senderAddress |
+ // | | 0x020 | | 2. makerAddress |
+ // | | 0x040 | | 3. takerAddress |
+ // | | 0x060 | | 4. feeRecipientAddress |
+ // | | 0x080 | | 5. makerAssetAmount |
+ // | | 0x0A0 | | 6. takerAssetAmount |
+ // | | 0x0C0 | | 7. makerFeeAmount |
+ // | | 0x0E0 | | 8. takerFeeAmount |
+ // | | 0x100 | | 9. expirationTimeSeconds |
+ // | | 0x120 | | 10. salt |
+ // | | 0x140 | | 11. Offset to makerAssetProxyMetadata (*) |
+ // | | 0x160 | | 12. Offset to takerAssetProxyMetadata (*) |
+ // | | 0x180 | 32 | makerAssetProxyMetadata Length |
+ // | | 0x1A0 | ** | makerAssetProxyMetadata Contents |
+ // | | 0x1C0 | 32 | takerAssetProxyMetadata Length |
+ // | | 0x1E0 | ** | takerAssetProxyMetadata Contents |
+ // | | 0x200 | 32 | signature Length |
+ // | | 0x220 | ** | signature Contents |
+
+ // * Offsets are calculated from the beginning of the current area: Header, Params, Data:
+ // An offset stored in the Params area is calculated from the beginning of the Params section.
+ // An offset stored in the Data area is calculated from the beginning of the Data section.
+
+ // ** The length of dynamic array contents are stored in the field immediately preceeding the contents.
+
+ // [1]: https://solidity.readthedocs.io/en/develop/abi-spec.html
+
+ bytes4 fillOrderSelector = this.fillOrder.selector;
+
+ assembly {
+
+ // Areas below may use the following variables:
+ // 1. <area>Start -- Start of this area in memory
+ // 2. <area>End -- End of this area in memory. This value may
+ // be precomputed (before writing contents),
+ // or it may be computed as contents are written.
+ // 3. <area>Offset -- Current offset into area. If an area's End
+ // is precomputed, this variable tracks the
+ // offsets of contents as they are written.
+
+ /////// Setup Header Area ///////
+ // Load free memory pointer
+ let headerAreaStart := mload(0x40)
+ mstore(headerAreaStart, fillOrderSelector)
+ let headerAreaEnd := add(headerAreaStart, 0x4)
+
+ /////// Setup Params Area ///////
+ // This area is preallocated and written to later.
+ // This is because we need to fill in offsets that have not yet been calculated.
+ let paramsAreaStart := headerAreaEnd
+ let paramsAreaEnd := add(paramsAreaStart, 0x60)
+ let paramsAreaOffset := paramsAreaStart
+
+ /////// Setup Data Area ///////
+ let dataAreaStart := paramsAreaEnd
+ let dataAreaEnd := dataAreaStart
+
+ // Offset from the source data we're reading from
+ let sourceOffset := order
+ // arrayLenBytes and arrayLenWords track the length of a dynamically-allocated bytes array.
+ let arrayLenBytes := 0
+ let arrayLenWords := 0
+
+ /////// Write order Struct ///////
+ // Write memory location of Order, relative to the start of the
+ // parameter list, then increment the paramsAreaOffset respectively.
+ mstore(paramsAreaOffset, sub(dataAreaEnd, paramsAreaStart))
+ paramsAreaOffset := add(paramsAreaOffset, 0x20)
+
+ // Write values for each field in the order
+ // It would be nice to use a loop, but we save on gas by writing
+ // the stores sequentially.
+ mstore(dataAreaEnd, mload(sourceOffset)) // makerAddress
+ mstore(add(dataAreaEnd, 0x20), mload(add(sourceOffset, 0x20))) // takerAddress
+ mstore(add(dataAreaEnd, 0x40), mload(add(sourceOffset, 0x40))) // feeRecipientAddress
+ mstore(add(dataAreaEnd, 0x60), mload(add(sourceOffset, 0x60))) // senderAddress
+ mstore(add(dataAreaEnd, 0x80), mload(add(sourceOffset, 0x80))) // makerAssetAmount
+ mstore(add(dataAreaEnd, 0xA0), mload(add(sourceOffset, 0xA0))) // takerAssetAmount
+ mstore(add(dataAreaEnd, 0xC0), mload(add(sourceOffset, 0xC0))) // makerFeeAmount
+ mstore(add(dataAreaEnd, 0xE0), mload(add(sourceOffset, 0xE0))) // takerFeeAmount
+ mstore(add(dataAreaEnd, 0x100), mload(add(sourceOffset, 0x100))) // expirationTimeSeconds
+ mstore(add(dataAreaEnd, 0x120), mload(add(sourceOffset, 0x120))) // salt
+ mstore(add(dataAreaEnd, 0x140), mload(add(sourceOffset, 0x140))) // Offset to makerAssetProxyMetadata
+ mstore(add(dataAreaEnd, 0x160), mload(add(sourceOffset, 0x160))) // Offset to takerAssetProxyMetadata
+ dataAreaEnd := add(dataAreaEnd, 0x180)
+ sourceOffset := add(sourceOffset, 0x180)
+
+ // Write offset to <order.makerAssetProxyMetadata>
+ mstore(add(dataAreaStart, mul(10, 0x20)), sub(dataAreaEnd, dataAreaStart))
+
+ // Calculate length of <order.makerAssetProxyMetadata>
+ arrayLenBytes := mload(sourceOffset)
+ sourceOffset := add(sourceOffset, 0x20)
+ arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20)
+
+ // Write length of <order.makerAssetProxyMetadata>
+ mstore(dataAreaEnd, arrayLenBytes)
+ dataAreaEnd := add(dataAreaEnd, 0x20)
+
+ // Write contents of <order.makerAssetProxyMetadata>
+ for {let i := 0} lt(i, arrayLenWords) {i := add(i, 1)} {
+ mstore(dataAreaEnd, mload(sourceOffset))
+ dataAreaEnd := add(dataAreaEnd, 0x20)
+ sourceOffset := add(sourceOffset, 0x20)
+ }
+
+ // Write offset to <order.takerAssetProxyMetadata>
+ mstore(add(dataAreaStart, mul(11, 0x20)), sub(dataAreaEnd, dataAreaStart))
+
+ // Calculate length of <order.takerAssetProxyMetadata>
+ arrayLenBytes := mload(sourceOffset)
+ sourceOffset := add(sourceOffset, 0x20)
+ arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20)
+
+ // Write length of <order.takerAssetProxyMetadata>
+ mstore(dataAreaEnd, arrayLenBytes)
+ dataAreaEnd := add(dataAreaEnd, 0x20)
+
+ // Write contents of <order.takerAssetProxyMetadata>
+ for {let i := 0} lt(i, arrayLenWords) {i := add(i, 1)} {
+ mstore(dataAreaEnd, mload(sourceOffset))
+ dataAreaEnd := add(dataAreaEnd, 0x20)
+ sourceOffset := add(sourceOffset, 0x20)
+ }
+
+ /////// Write takerAssetFillAmount ///////
+ mstore(paramsAreaOffset, takerAssetFillAmount)
+ paramsAreaOffset := add(paramsAreaOffset, 0x20)
+
+ /////// Write signature ///////
+ // Write offset to paramsArea
+ mstore(paramsAreaOffset, sub(dataAreaEnd, paramsAreaStart))
+
+ // Calculate length of signature
+ sourceOffset := signature
+ arrayLenBytes := mload(sourceOffset)
+ sourceOffset := add(sourceOffset, 0x20)
+ arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20)
+
+ // Write length of signature
+ mstore(dataAreaEnd, arrayLenBytes)
+ dataAreaEnd := add(dataAreaEnd, 0x20)
+
+ // Write contents of signature
+ for {let i := 0} lt(i, arrayLenWords) {i := add(i, 1)} {
+ mstore(dataAreaEnd, mload(sourceOffset))
+ dataAreaEnd := add(dataAreaEnd, 0x20)
+ sourceOffset := add(sourceOffset, 0x20)
+ }
+
+ // Execute delegatecall
+ let success := delegatecall(
+ gas, // forward all gas, TODO: look into gas consumption of assert/throw
+ address, // call address of this contract
+ headerAreaStart, // pointer to start of input
+ sub(dataAreaEnd, headerAreaStart), // length of input
+ headerAreaStart, // write output over input
+ 128 // output size is 128 bytes
+ )
+ switch success
+ case 0 {
+ mstore(fillResults, 0)
+ mstore(add(fillResults, 32), 0)
+ mstore(add(fillResults, 64), 0)
+ mstore(add(fillResults, 96), 0)
+ }
+ case 1 {
+ mstore(fillResults, mload(headerAreaStart))
+ mstore(add(fillResults, 32), mload(add(headerAreaStart, 32)))
+ mstore(add(fillResults, 64), mload(add(headerAreaStart, 64)))
+ mstore(add(fillResults, 96), mload(add(headerAreaStart, 96)))
+ }
+ }
+ return fillResults;
+ }
+
+ /// @dev Synchronously executes multiple calls of fillOrder.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmounts Array of desired amounts of takerAsset to sell in orders.
+ /// @param signatures Proofs that orders have been created by makers.
+ function batchFillOrders(
+ Order[] memory orders,
+ uint256[] memory takerAssetFillAmounts,
+ bytes[] memory signatures)
+ public
+ {
+ for (uint256 i = 0; i < orders.length; i++) {
+ fillOrder(
+ orders[i],
+ takerAssetFillAmounts[i],
+ signatures[i]
+ );
+ }
+ }
+
+ /// @dev Synchronously executes multiple calls of fillOrKill.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmounts Array of desired amounts of takerAsset to sell in orders.
+ /// @param signatures Proofs that orders have been created by makers.
+ function batchFillOrKillOrders(
+ Order[] memory orders,
+ uint256[] memory takerAssetFillAmounts,
+ bytes[] memory signatures)
+ public
+ {
+ for (uint256 i = 0; i < orders.length; i++) {
+ fillOrKillOrder(
+ orders[i],
+ takerAssetFillAmounts[i],
+ signatures[i]
+ );
+ }
+ }
+
+ /// @dev Fills an order with specified parameters and ECDSA signature.
+ /// Returns false if the transaction would otherwise revert.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmounts Array of desired amounts of takerAsset to sell in orders.
+ /// @param signatures Proofs that orders have been created by makers.
+ function batchFillOrdersNoThrow(
+ Order[] memory orders,
+ uint256[] memory takerAssetFillAmounts,
+ bytes[] memory signatures)
+ public
+ {
+ for (uint256 i = 0; i < orders.length; i++) {
+ fillOrderNoThrow(
+ orders[i],
+ takerAssetFillAmounts[i],
+ signatures[i]
+ );
+ }
+ }
+
+ /// @dev Synchronously executes multiple calls of fillOrder until total amount of takerAsset is sold by taker.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signatures Proofs that orders have been created by makers.
+ /// @return Amounts filled and fees paid by makers and taker.
+ function marketSellOrders(
+ Order[] memory orders,
+ uint256 takerAssetFillAmount,
+ bytes[] memory signatures)
+ public
+ returns (FillResults memory totalFillResults)
+ {
+ for (uint256 i = 0; i < orders.length; i++) {
+
+ // Token being sold by taker must be the same for each order
+ // TODO: optimize by only using takerAssetData for first order.
+ require(
+ areBytesEqual(orders[i].takerAssetData, orders[0].takerAssetData),
+ ASSET_DATA_MISMATCH
+ );
+
+ // Calculate the remaining amount of takerAsset to sell
+ uint256 remainingTakerAssetFillAmount = safeSub(takerAssetFillAmount, totalFillResults.takerAssetFilledAmount);
+
+ // Attempt to sell the remaining amount of takerAsset
+ FillResults memory singleFillResults = fillOrder(
+ orders[i],
+ remainingTakerAssetFillAmount,
+ signatures[i]
+ );
+
+ // Update amounts filled and fees paid by maker and taker
+ addFillResults(totalFillResults, singleFillResults);
+
+ // Stop execution if the entire amount of takerAsset has been sold
+ if (totalFillResults.takerAssetFilledAmount == takerAssetFillAmount) {
+ break;
+ }
+ }
+ return totalFillResults;
+ }
+
+ /// @dev Synchronously executes multiple calls of fillOrder until total amount of takerAsset is sold by taker.
+ /// Returns false if the transaction would otherwise revert.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signatures Proofs that orders have been signed by makers.
+ /// @return Amounts filled and fees paid by makers and taker.
+ function marketSellOrdersNoThrow(
+ Order[] memory orders,
+ uint256 takerAssetFillAmount,
+ bytes[] memory signatures)
+ public
+ returns (FillResults memory totalFillResults)
+ {
+ for (uint256 i = 0; i < orders.length; i++) {
+
+ // Token being sold by taker must be the same for each order
+ // TODO: optimize by only using takerAssetData for first order.
+ require(
+ areBytesEqual(orders[i].takerAssetData, orders[0].takerAssetData),
+ ASSET_DATA_MISMATCH
+ );
+
+ // Calculate the remaining amount of takerAsset to sell
+ uint256 remainingTakerAssetFillAmount = safeSub(takerAssetFillAmount, totalFillResults.takerAssetFilledAmount);
+
+ // Attempt to sell the remaining amount of takerAsset
+ FillResults memory singleFillResults = fillOrderNoThrow(
+ orders[i],
+ remainingTakerAssetFillAmount,
+ signatures[i]
+ );
+
+ // Update amounts filled and fees paid by maker and taker
+ addFillResults(totalFillResults, singleFillResults);
+
+ // Stop execution if the entire amount of takerAsset has been sold
+ if (totalFillResults.takerAssetFilledAmount == takerAssetFillAmount) {
+ break;
+ }
+ }
+ return totalFillResults;
+ }
+
+ /// @dev Synchronously executes multiple calls of fillOrder until total amount of makerAsset is bought by taker.
+ /// @param orders Array of order specifications.
+ /// @param makerAssetFillAmount Desired amount of makerAsset to buy.
+ /// @param signatures Proofs that orders have been signed by makers.
+ /// @return Amounts filled and fees paid by makers and taker.
+ function marketBuyOrders(
+ Order[] memory orders,
+ uint256 makerAssetFillAmount,
+ bytes[] memory signatures)
+ public
+ returns (FillResults memory totalFillResults)
+ {
+ for (uint256 i = 0; i < orders.length; i++) {
+
+ // Token being bought by taker must be the same for each order
+ // TODO: optimize by only using makerAssetData for first order.
+ require(
+ areBytesEqual(orders[i].makerAssetData, orders[0].makerAssetData),
+ ASSET_DATA_MISMATCH
+ );
+
+ // Calculate the remaining amount of makerAsset to buy
+ uint256 remainingMakerAssetFillAmount = safeSub(makerAssetFillAmount, totalFillResults.makerAssetFilledAmount);
+
+ // Convert the remaining amount of makerAsset to buy into remaining amount
+ // of takerAsset to sell, assuming entire amount can be sold in the current order
+ uint256 remainingTakerAssetFillAmount = getPartialAmount(
+ orders[i].takerAssetAmount,
+ orders[i].makerAssetAmount,
+ remainingMakerAssetFillAmount
+ );
+
+ // Attempt to sell the remaining amount of takerAsset
+ FillResults memory singleFillResults = fillOrder(
+ orders[i],
+ remainingTakerAssetFillAmount,
+ signatures[i]
+ );
+
+ // Update amounts filled and fees paid by maker and taker
+ addFillResults(totalFillResults, singleFillResults);
+
+ // Stop execution if the entire amount of makerAsset has been bought
+ if (totalFillResults.makerAssetFilledAmount == makerAssetFillAmount) {
+ break;
+ }
+ }
+ return totalFillResults;
+ }
+
+ /// @dev Synchronously executes multiple fill orders in a single transaction until total amount is bought by taker.
+ /// Returns false if the transaction would otherwise revert.
+ /// @param orders Array of order specifications.
+ /// @param makerAssetFillAmount Desired amount of makerAsset to buy.
+ /// @param signatures Proofs that orders have been signed by makers.
+ /// @return Amounts filled and fees paid by makers and taker.
+ function marketBuyOrdersNoThrow(
+ Order[] memory orders,
+ uint256 makerAssetFillAmount,
+ bytes[] memory signatures)
+ public
+ returns (FillResults memory totalFillResults)
+ {
+ for (uint256 i = 0; i < orders.length; i++) {
+
+ // Token being bought by taker must be the same for each order
+ // TODO: optimize by only using makerAssetData for first order.
+ require(
+ areBytesEqual(orders[i].makerAssetData, orders[0].makerAssetData),
+ ASSET_DATA_MISMATCH
+ );
+
+ // Calculate the remaining amount of makerAsset to buy
+ uint256 remainingMakerAssetFillAmount = safeSub(makerAssetFillAmount, totalFillResults.makerAssetFilledAmount);
+
+ // Convert the remaining amount of makerAsset to buy into remaining amount
+ // of takerAsset to sell, assuming entire amount can be sold in the current order
+ uint256 remainingTakerAssetFillAmount = getPartialAmount(
+ orders[i].takerAssetAmount,
+ orders[i].makerAssetAmount,
+ remainingMakerAssetFillAmount
+ );
+
+ // Attempt to sell the remaining amount of takerAsset
+ FillResults memory singleFillResults = fillOrderNoThrow(
+ orders[i],
+ remainingTakerAssetFillAmount,
+ signatures[i]
+ );
+
+ // Update amounts filled and fees paid by maker and taker
+ addFillResults(totalFillResults, singleFillResults);
+
+ // Stop execution if the entire amount of makerAsset has been bought
+ if (totalFillResults.makerAssetFilledAmount == makerAssetFillAmount) {
+ break;
+ }
+ }
+ return totalFillResults;
+ }
+
+ /// @dev Synchronously cancels multiple orders in a single transaction.
+ /// @param orders Array of order specifications.
+ function batchCancelOrders(Order[] memory orders)
+ public
+ {
+ for (uint256 i = 0; i < orders.length; i++) {
+ cancelOrder(orders[i]);
+ }
+ }
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IAssetProxyDispatcher.sol b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IAssetProxyDispatcher.sol
new file mode 100644
index 000000000..4e4ed6be8
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IAssetProxyDispatcher.sol
@@ -0,0 +1,41 @@
+/*
+
+ 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.23;
+
+contract IAssetProxyDispatcher {
+
+ /// @dev Registers an asset proxy to an asset proxy id.
+ /// An id can only be assigned to a single proxy at a given time.
+ /// @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/Exchange/interfaces/IExchange.sol b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchange.sol
new file mode 100644
index 000000000..9fba3491b
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchange.sol
@@ -0,0 +1,34 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "./IExchangeCore.sol";
+import "./ISignatureValidator.sol";
+import "./IAssetProxyDispatcher.sol";
+import "./ITransactions.sol";
+import "./IWrapperFunctions.sol";
+
+contract IExchange is
+ IWrapperFunctions,
+ IExchangeCore,
+ ISignatureValidator,
+ ITransactions,
+ IAssetProxyDispatcher
+{}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchangeCore.sol b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchangeCore.sol
new file mode 100644
index 000000000..0f19525ca
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IExchangeCore.sol
@@ -0,0 +1,51 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../libs/LibOrder.sol";
+import "../libs/LibFillResults.sol";
+
+contract IExchangeCore {
+
+ /// @dev Cancels all orders reated by sender with a salt less than or equal to the specified salt value.
+ /// @param salt Orders created with a salt less or equal to this value will be cancelled.
+ function cancelOrdersUpTo(uint256 salt)
+ external;
+
+ /// @dev Fills the input order.
+ /// @param order Order struct containing order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signature Proof that order has been created by maker.
+ /// @return Amounts filled and fees paid by maker and taker.
+ function fillOrder(
+ LibOrder.Order memory order,
+ uint256 takerAssetFillAmount,
+ bytes memory signature)
+ public
+ returns (LibFillResults.FillResults memory fillResults);
+
+ /// @dev After calling, the order can not be filled anymore.
+ /// @param order Order struct containing order specifications.
+ /// @return True if the order state changed to cancelled.
+ /// False if the transaction was already cancelled or expired.
+ function cancelOrder(LibOrder.Order memory order)
+ public
+ returns (bool);
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ISignatureValidator.sol b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ISignatureValidator.sol
new file mode 100644
index 000000000..b4a238472
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ISignatureValidator.sol
@@ -0,0 +1,32 @@
+/*
+
+ 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.23;
+
+contract ISignatureValidator {
+
+ /// @dev Approves a hash on-chain using any valid signature type.
+ /// After presigning a hash, the preSign signature type will become valid for that hash and signer.
+ /// @param signer Address that should have signed the given hash.
+ /// @param signature Proof that the hash has been signed by signer.
+ function preSign(
+ bytes32 hash,
+ address signer,
+ bytes signature)
+ external;
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ISigner.sol b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ISigner.sol
new file mode 100644
index 000000000..e065cfd8a
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ISigner.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.23;
+
+contract ISigner {
+
+ /// @dev Verifies that a signature is valid.
+ /// @param hash Message hash that is signed.
+ /// @param signature Proof of signing.
+ /// @return Validity of order signature.
+ function isValidSignature(
+ bytes32 hash,
+ bytes signature)
+ external
+ view
+ returns (bool isValid);
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ITransactions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ITransactions.sol
new file mode 100644
index 000000000..76e4cf2fe
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/ITransactions.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.23;
+
+contract ITransactions {
+
+ /// @dev Executes an exchange method call in the context of signer.
+ /// @param salt Arbitrary number to ensure uniqueness of transaction hash.
+ /// @param signer Address of transaction signer.
+ /// @param data AbiV2 encoded calldata.
+ /// @param signature Proof of signer transaction by signer.
+ function executeTransaction(
+ uint256 salt,
+ address signer,
+ bytes data,
+ bytes signature)
+ external;
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IWrapperFunctions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IWrapperFunctions.sol
new file mode 100644
index 000000000..d23170b1c
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/interfaces/IWrapperFunctions.sol
@@ -0,0 +1,142 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "./libs/LibOrder.sol";
+import "./libs/LibFillResults.sol";
+
+contract IWrapperFunctions is
+ LibOrder,
+ LibFillResults,
+ LibMath,
+ LibBytes,
+ LibExchangeErrors,
+ MExchangeCore
+{
+ /// @dev Fills the input order. Reverts if exact takerAssetFillAmount not filled.
+ /// @param order LibOrder.Order struct containing order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signature Proof that order has been created by maker.
+ function fillOrKillOrder(
+ LibOrder.LibOrder.Order memory order,
+ uint256 takerAssetFillAmount,
+ bytes memory signature)
+ public
+ returns (LibFillResults.LibFillResults.FillResults memory fillResults);
+
+ /// @dev Fills an order with specified parameters and ECDSA signature.
+ /// Returns false if the transaction would otherwise revert.
+ /// @param order LibOrder.Order struct containing order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signature Proof that order has been created by maker.
+ /// @return Amounts filled and fees paid by maker and taker.
+ function fillOrderNoThrow(
+ LibOrder.Order memory order,
+ uint256 takerAssetFillAmount,
+ bytes memory signature)
+ public
+ returns (LibFillResults.FillResults memory fillResults);
+
+ /// @dev Synchronously executes multiple calls of fillOrder.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmounts Array of desired amounts of takerAsset to sell in orders.
+ /// @param signatures Proofs that orders have been created by makers.
+ function batchFillOrders(
+ LibOrder.Order[] memory orders,
+ uint256[] memory takerAssetFillAmounts,
+ bytes[] memory signatures)
+ public;
+
+ /// @dev Synchronously executes multiple calls of fillOrKill.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmounts Array of desired amounts of takerAsset to sell in orders.
+ /// @param signatures Proofs that orders have been created by makers.
+ function batchFillOrKillOrders(
+ LibOrder.Order[] memory orders,
+ uint256[] memory takerAssetFillAmounts,
+ bytes[] memory signatures)
+ public;
+
+ /// @dev Fills an order with specified parameters and ECDSA signature.
+ /// Returns false if the transaction would otherwise revert.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmounts Array of desired amounts of takerAsset to sell in orders.
+ /// @param signatures Proofs that orders have been created by makers.
+ function batchFillOrdersNoThrow(
+ LibOrder.Order[] memory orders,
+ uint256[] memory takerAssetFillAmounts,
+ bytes[] memory signatures)
+ public;
+
+ /// @dev Synchronously executes multiple calls of fillOrder until total amount of takerAsset is sold by taker.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signatures Proofs that orders have been created by makers.
+ /// @return Amounts filled and fees paid by makers and taker.
+ function marketSellOrders(
+ LibOrder.Order[] memory orders,
+ uint256 takerAssetFillAmount,
+ bytes[] memory signatures)
+ public
+ returns (LibFillResults.FillResults memory totalFillResults);
+
+ /// @dev Synchronously executes multiple calls of fillOrder until total amount of takerAsset is sold by taker.
+ /// Returns false if the transaction would otherwise revert.
+ /// @param orders Array of order specifications.
+ /// @param takerAssetFillAmount Desired amount of takerAsset to sell.
+ /// @param signatures Proofs that orders have been signed by makers.
+ /// @return Amounts filled and fees paid by makers and taker.
+ function marketSellOrdersNoThrow(
+ LibOrder.Order[] memory orders,
+ uint256 takerAssetFillAmount,
+ bytes[] memory signatures)
+ public
+ returns (LibFillResults.FillResults memory totalFillResults);
+
+ /// @dev Synchronously executes multiple calls of fillOrder until total amount of makerAsset is bought by taker.
+ /// @param orders Array of order specifications.
+ /// @param makerAssetFillAmount Desired amount of makerAsset to buy.
+ /// @param signatures Proofs that orders have been signed by makers.
+ /// @return Amounts filled and fees paid by makers and taker.
+ function marketBuyOrders(
+ LibOrder.Order[] memory orders,
+ uint256 makerAssetFillAmount,
+ bytes[] memory signatures)
+ public
+ returns (LibFillResults.FillResults memory totalFillResults);
+
+ /// @dev Synchronously executes multiple fill orders in a single transaction until total amount is bought by taker.
+ /// Returns false if the transaction would otherwise revert.
+ /// @param orders Array of order specifications.
+ /// @param makerAssetFillAmount Desired amount of makerAsset to buy.
+ /// @param signatures Proofs that orders have been signed by makers.
+ /// @return Amounts filled and fees paid by makers and taker.
+ function marketBuyOrdersNoThrow(
+ LibOrder.Order[] memory orders,
+ uint256 makerAssetFillAmount,
+ bytes[] memory signatures)
+ public
+ returns (LibFillResults.FillResults memory totalFillResults);
+
+ /// @dev Synchronously cancels multiple orders in a single transaction.
+ /// @param orders Array of order specifications.
+ function batchCancelOrders(LibOrder.Order[] memory orders)
+ public;
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibExchangeErrors.sol b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibExchangeErrors.sol
new file mode 100644
index 000000000..6ffbdfdca
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibExchangeErrors.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.23;
+
+contract LibExchangeErrors {
+
+ // Error Codes
+ enum Errors {
+ ORDER_EXPIRED, // Order has already expired
+ ORDER_FULLY_FILLED, // Order has already been fully filled
+ ORDER_CANCELLED, // Order has already been cancelled
+ ROUNDING_ERROR_TOO_LARGE, // Rounding error too large
+ INSUFFICIENT_BALANCE_OR_ALLOWANCE // Insufficient balance or allowance for token transfer
+ }
+
+ event ExchangeError(uint8 indexed errorId, bytes32 indexed orderHash);
+
+ // Core revert reasons
+ string constant GT_ZERO_AMOUNT_REQUIRED = "Amount must be greater than 0.";
+ string constant SIGNATURE_VALIDATION_FAILED = "Signature validation failed.";
+ string constant INVALID_SENDER = "Invalid `msg.sender`.";
+ string constant INVALID_CONTEXT = "Function called in an invalid context.";
+ string constant INVALID_NEW_MAKER_EPOCH = "Specified salt must be greater than or equal to existing makerEpoch.";
+
+ // Transaction revert reasons
+ string constant DUPLICATE_TRANSACTION_HASH = "Transaction has already been executed.";
+ string constant TRANSACTION_EXECUTION_FAILED = "Transaction execution failed.";
+
+ // Wrapper revert reasons
+ string constant COMPLETE_FILL_FAILED = "Desired fill amount could not be completely filled.";
+ string constant ASSET_DATA_MISMATCH = "Asset data must be the same for each order.";
+
+ // Asset proxy dispatcher revert reasons
+ string constant GT_ZERO_LENGTH_REQUIRED = "Length must be greater than 0.";
+ string constant OLD_ASSET_PROXY_MISMATCH = "Old asset proxy does not match asset proxy at given id.";
+ string constant NEW_ASSET_PROXY_MISMATCH = "New asset proxy id does not match given id.";
+
+ // Signature validator revert reasons
+ string constant INVALID_SIGNATURE_LENGTH = "Invalid signature length.";
+ string constant ILLEGAL_SIGNATURE_TYPE = "Illegal signature type.";
+ string constant UNSUPPORTED_SIGNATURE_TYPE = "Unsupported signature type.";
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibFillResults.sol b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibFillResults.sol
new file mode 100644
index 000000000..3b8f2acf9
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibFillResults.sol
@@ -0,0 +1,47 @@
+/*
+
+ 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.23;
+
+import "../../../utils/SafeMath/SafeMath.sol";
+
+contract LibFillResults is
+ SafeMath
+{
+
+ struct FillResults {
+ uint256 makerAssetFilledAmount;
+ uint256 takerAssetFilledAmount;
+ uint256 makerFeePaid;
+ uint256 takerFeePaid;
+ }
+
+ /// @dev Adds properties of both FillResults instances.
+ /// Modifies the first FillResults instance specified.
+ /// @param totalFillResults Fill results instance that will be added onto.
+ /// @param singleFillResults Fill results instance that will be added to totalFillResults.
+ function addFillResults(FillResults memory totalFillResults, FillResults memory singleFillResults)
+ internal
+ pure
+ {
+ totalFillResults.makerAssetFilledAmount = safeAdd(totalFillResults.makerAssetFilledAmount, singleFillResults.makerAssetFilledAmount);
+ totalFillResults.takerAssetFilledAmount = safeAdd(totalFillResults.takerAssetFilledAmount, singleFillResults.takerAssetFilledAmount);
+ totalFillResults.makerFeePaid = safeAdd(totalFillResults.makerFeePaid, singleFillResults.makerFeePaid);
+ totalFillResults.takerFeePaid = safeAdd(totalFillResults.takerFeePaid, singleFillResults.takerFeePaid);
+ }
+} \ No newline at end of file
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibMath.sol b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibMath.sol
new file mode 100644
index 000000000..b48602d19
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibMath.sol
@@ -0,0 +1,72 @@
+/*
+
+ 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.23;
+
+import "../../../utils/SafeMath/SafeMath.sol";
+
+contract LibMath is
+ SafeMath
+{
+
+ /// @dev Calculates partial value given a numerator and denominator.
+ /// @param numerator Numerator.
+ /// @param denominator Denominator.
+ /// @param target Value to calculate partial of.
+ /// @return Partial value of target.
+ function getPartialAmount(
+ uint256 numerator,
+ uint256 denominator,
+ uint256 target)
+ internal
+ pure
+ returns (uint256 partialAmount)
+ {
+ partialAmount = safeDiv(
+ safeMul(numerator, target),
+ denominator
+ );
+ return partialAmount;
+ }
+
+ /// @dev Checks if rounding error > 0.1%.
+ /// @param numerator Numerator.
+ /// @param denominator Denominator.
+ /// @param target Value to multiply with numerator/denominator.
+ /// @return Rounding error is present.
+ function isRoundingError(
+ uint256 numerator,
+ uint256 denominator,
+ uint256 target)
+ internal
+ pure
+ returns (bool isError)
+ {
+ uint256 remainder = mulmod(target, numerator, denominator);
+ if (remainder == 0) {
+ return false; // No rounding error.
+ }
+
+ uint256 errPercentageTimes1000000 = safeDiv(
+ safeMul(remainder, 1000000),
+ safeMul(numerator, target)
+ );
+ isError = errPercentageTimes1000000 > 1000;
+ return isError;
+ }
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol
new file mode 100644
index 000000000..0fe8c8c96
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/libs/LibOrder.sol
@@ -0,0 +1,84 @@
+/*
+
+ 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.23;
+
+contract LibOrder {
+
+ bytes32 constant ORDER_SCHEMA_HASH = keccak256(
+ "address exchangeAddress",
+ "address makerAddress",
+ "address takerAddress",
+ "address feeRecipientAddress",
+ "address senderAddress",
+ "uint256 makerAssetAmount",
+ "uint256 takerAssetAmount",
+ "uint256 makerFee",
+ "uint256 takerFee",
+ "uint256 expirationTimeSeconds",
+ "uint256 salt",
+ "bytes makerAssetData",
+ "bytes takerAssetData"
+ );
+
+ struct Order {
+ address makerAddress;
+ address takerAddress;
+ address feeRecipientAddress;
+ address senderAddress;
+ uint256 makerAssetAmount;
+ uint256 takerAssetAmount;
+ uint256 makerFee;
+ uint256 takerFee;
+ uint256 expirationTimeSeconds;
+ uint256 salt;
+ bytes makerAssetData;
+ bytes takerAssetData;
+ }
+
+ /// @dev Calculates Keccak-256 hash of the order.
+ /// @param order The order structure.
+ /// @return Keccak-256 EIP712 hash of the order.
+ function getOrderHash(Order memory order)
+ internal
+ view
+ returns (bytes32 orderHash)
+ {
+ // TODO: EIP712 is not finalized yet
+ // Source: https://github.com/ethereum/EIPs/pull/712
+ orderHash = keccak256(
+ ORDER_SCHEMA_HASH,
+ keccak256(
+ address(this),
+ order.makerAddress,
+ order.takerAddress,
+ order.feeRecipientAddress,
+ order.senderAddress,
+ order.makerAssetAmount,
+ order.takerAssetAmount,
+ order.makerFee,
+ order.takerFee,
+ order.expirationTimeSeconds,
+ order.salt,
+ order.makerAssetData,
+ order.takerAssetData
+ )
+ );
+ return orderHash;
+ }
+}
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..77ee77797
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MAssetProxyDispatcher.sol
@@ -0,0 +1,45 @@
+/*
+
+ 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.23;
+
+import "../interfaces/IAssetProxyDispatcher.sol";
+
+contract MAssetProxyDispatcher is
+ IAssetProxyDispatcher
+{
+
+ // 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;
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MExchangeCore.sol b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MExchangeCore.sol
new file mode 100644
index 000000000..5b78e70da
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MExchangeCore.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.23;
+
+import "../libs/LibOrder.sol";
+import "../libs/LibFillResults.sol";
+import "../interfaces/IExchangeCore.sol";
+
+contract MExchangeCore is
+ IExchangeCore
+{
+
+ // Fill event is emitted whenever an order is filled.
+ event Fill(
+ address indexed makerAddress,
+ address takerAddress,
+ address indexed feeRecipientAddress,
+ uint256 makerAssetFilledAmount,
+ uint256 takerAssetFilledAmount,
+ uint256 makerFeePaid,
+ uint256 takerFeePaid,
+ bytes32 indexed orderHash,
+ bytes makerAssetData,
+ bytes takerAssetData
+ );
+
+ // Cancel event is emitted whenever an individual order is cancelled.
+ event Cancel(
+ address indexed makerAddress,
+ address indexed feeRecipientAddress,
+ bytes32 indexed orderHash,
+ bytes makerAssetData,
+ bytes takerAssetData
+ );
+
+ // CancelUpTo event is emitted whenever `cancelOrdersUpTo` is executed succesfully.
+ event CancelUpTo(
+ address indexed makerAddress,
+ uint256 makerEpoch
+ );
+
+ /// @dev Logs a Fill event with the given arguments.
+ /// The sole purpose of this function is to get around the stack variable limit.
+ function emitFillEvent(
+ LibOrder.Order memory order,
+ address takerAddress,
+ bytes32 orderHash,
+ LibFillResults.FillResults memory fillResults)
+ internal;
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSettlement.sol b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSettlement.sol
new file mode 100644
index 000000000..5e2edbf99
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSettlement.sol
@@ -0,0 +1,41 @@
+/*
+
+ 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.23;
+
+import "../libs/LibOrder.sol";
+
+contract MSettlement {
+
+ /// @dev Settles an order by transfering assets between counterparties.
+ /// @param order Order struct containing order specifications.
+ /// @param takerAddress Address selling takerAsset and buying makerAsset.
+ /// @param takerAssetFilledAmount The amount of takerAsset that will be transfered to the order's maker.
+ /// @return Amount filled by maker and fees paid by maker/taker.
+ function settleOrder(
+ LibOrder.Order memory order,
+ address takerAddress,
+ uint256 takerAssetFilledAmount)
+ internal
+ returns (
+ uint256 makerAssetFilledAmount,
+ uint256 makerFeePaid,
+ uint256 takerFeePaid
+ );
+}
+
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSignatureValidator.sol b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSignatureValidator.sol
new file mode 100644
index 000000000..ba26c07f6
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSignatureValidator.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.23;
+
+import "../interfaces/ISignatureValidator.sol";
+
+contract MSignatureValidator is
+ ISignatureValidator
+{
+ // Allowed signature types.
+ enum SignatureType {
+ Illegal, // Default value
+ Invalid,
+ Caller,
+ Ecrecover,
+ EIP712,
+ Trezor,
+ Contract,
+ PreSigned
+ }
+
+ /// @dev Verifies that a signature is valid.
+ /// @param hash Message hash that is signed.
+ /// @param signer Address of signer.
+ /// @param signature Proof of signing.
+ /// @return Validity of order signature.
+ function isValidSignature(
+ bytes32 hash,
+ address signer,
+ bytes memory signature)
+ internal
+ view
+ returns (bool isValid);
+}
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MTransactions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MTransactions.sol
new file mode 100644
index 000000000..159ed1527
--- /dev/null
+++ b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MTransactions.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.23;
+
+import "../interfaces/ITransactions.sol";
+
+contract MTransactions is
+ ITransactions
+{
+
+ /// @dev The current function will be called in the context of this address (either 0x transaction signer or `msg.sender`).
+ /// If calling a fill function, this address will represent the taker.
+ /// If calling a cancel function, this address will represent the maker.
+ /// @return Signer of 0x transaction if entry point is `executeTransaction`.
+ /// `msg.sender` if entry point is any other function.
+ function getCurrentContextAddress()
+ internal
+ view
+ returns (address);
+}
diff --git a/packages/contracts/src/contracts/current/test/DummyERC20Token/DummyERC20Token.sol b/packages/contracts/src/contracts/current/test/DummyERC20Token/DummyERC20Token.sol
new file mode 100644
index 000000000..ab5311e0c
--- /dev/null
+++ b/packages/contracts/src/contracts/current/test/DummyERC20Token/DummyERC20Token.sol
@@ -0,0 +1,56 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../Mintable/Mintable.sol";
+import "../../utils/Ownable/Ownable.sol";
+
+contract DummyERC20Token is Mintable, Ownable {
+ string public name;
+ string public symbol;
+ uint256 public decimals;
+
+ constructor (
+ string _name,
+ string _symbol,
+ uint256 _decimals,
+ uint256 _totalSupply)
+ public
+ {
+ name = _name;
+ symbol = _symbol;
+ decimals = _decimals;
+ totalSupply = _totalSupply;
+ balances[msg.sender] = _totalSupply;
+ }
+
+ function setBalance(address _target, uint256 _value)
+ public
+ onlyOwner
+ {
+ uint256 currBalance = balanceOf(_target);
+ if (_value < currBalance) {
+ totalSupply = safeSub(totalSupply, safeSub(currBalance, _value));
+ } else {
+ totalSupply = safeAdd(totalSupply, safeSub(_value, currBalance));
+ }
+ balances[_target] = _value;
+ }
+}
diff --git a/packages/contracts/src/contracts/current/test/DummyERC721Token/DummyERC721Token.sol b/packages/contracts/src/contracts/current/test/DummyERC721Token/DummyERC721Token.sol
new file mode 100644
index 000000000..22ebbd3c1
--- /dev/null
+++ b/packages/contracts/src/contracts/current/test/DummyERC721Token/DummyERC721Token.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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../tokens/ERC721Token/ERC721Token.sol";
+import "../../utils/Ownable/Ownable.sol";
+
+contract DummyERC721Token is
+ Ownable,
+ ERC721Token
+{
+
+ /**
+ * @dev Constructor passes its arguments to the base ERC721Token constructor
+ * @param name of token
+ * @param symbol of token
+ */
+ constructor (
+ string name,
+ string symbol)
+ public
+ ERC721Token(name, symbol)
+ {}
+
+ /**
+ * @dev Function to mint a new token
+ * @dev Reverts if the given token ID already exists
+ * @param to address the beneficiary that will own the minted token
+ * @param tokenId uint256 ID of the token to be minted by the msg.sender
+ */
+ function mint(address to, uint256 tokenId)
+ public
+ onlyOwner
+ {
+ require(
+ !exists(tokenId),
+ "Token with tokenId already exists."
+ );
+ _mint(to, tokenId);
+ }
+}
diff --git a/packages/contracts/src/contracts/current/test/DummyToken/DummyToken.sol b/packages/contracts/src/contracts/current/test/DummyToken/DummyToken.sol
deleted file mode 100644
index ab04f4d16..000000000
--- a/packages/contracts/src/contracts/current/test/DummyToken/DummyToken.sol
+++ /dev/null
@@ -1,37 +0,0 @@
-pragma solidity ^0.4.18;
-
-import { Mintable } from "../Mintable/Mintable.sol";
-import { Ownable } from "../../utils/Ownable/Ownable.sol";
-
-contract DummyToken is Mintable, Ownable {
- string public name;
- string public symbol;
- uint public decimals;
-
- function DummyToken(
- string _name,
- string _symbol,
- uint _decimals,
- uint _totalSupply)
- public
- {
- name = _name;
- symbol = _symbol;
- decimals = _decimals;
- totalSupply = _totalSupply;
- balances[msg.sender] = _totalSupply;
- }
-
- function setBalance(address _target, uint _value)
- public
- onlyOwner
- {
- uint currBalance = balanceOf(_target);
- if (_value < currBalance) {
- totalSupply = safeSub(totalSupply, safeSub(currBalance, _value));
- } else {
- totalSupply = safeAdd(totalSupply, safeSub(_value, currBalance));
- }
- balances[_target] = _value;
- }
-}
diff --git a/packages/contracts/src/contracts/current/test/MaliciousToken/MaliciousToken.sol b/packages/contracts/src/contracts/current/test/MaliciousToken/MaliciousToken.sol
deleted file mode 100644
index 9e502616c..000000000
--- a/packages/contracts/src/contracts/current/test/MaliciousToken/MaliciousToken.sol
+++ /dev/null
@@ -1,31 +0,0 @@
-pragma solidity ^0.4.18;
-
-import { ERC20Token } from "../../tokens/ERC20Token/ERC20Token.sol";
-
-contract MaliciousToken is ERC20Token {
- uint8 stateToUpdate = 1; // Not null so that change only requires 5000 gas
-
- function updateState()
- internal
- {
- stateToUpdate++;
- }
-
- function balanceOf(address _owner)
- public
- constant
- returns (uint)
- {
- updateState();
- return balances[_owner];
- }
-
- function allowance(address _owner, address _spender)
- public
- constant
- returns (uint)
- {
- updateState();
- return allowed[_owner][_spender];
- }
-}
diff --git a/packages/contracts/src/contracts/current/test/Mintable/Mintable.sol b/packages/contracts/src/contracts/current/test/Mintable/Mintable.sol
index cf7ee35a5..fd944f244 100644
--- a/packages/contracts/src/contracts/current/test/Mintable/Mintable.sol
+++ b/packages/contracts/src/contracts/current/test/Mintable/Mintable.sol
@@ -1,17 +1,39 @@
-pragma solidity ^0.4.18;
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
-import { UnlimitedAllowanceToken } from "../../tokens/UnlimitedAllowanceToken/UnlimitedAllowanceToken.sol";
-import { SafeMath } from "../../utils/SafeMath/SafeMath.sol";
+import "../../tokens/UnlimitedAllowanceToken/UnlimitedAllowanceToken.sol";
+import "../../utils/SafeMath/SafeMath.sol";
/*
* Mintable
* Base contract that creates a mintable UnlimitedAllowanceToken
*/
contract Mintable is UnlimitedAllowanceToken, SafeMath {
- function mint(uint _value)
+ function mint(uint256 _value)
public
{
- require(_value <= 100000000000000000000);
+ require(
+ _value <= 100000000000000000000,
+ "Minting more than 100000000000000000000 is not allowed."
+ );
balances[msg.sender] = safeAdd(_value, balances[msg.sender]);
totalSupply = safeAdd(totalSupply, _value);
}
diff --git a/packages/contracts/src/contracts/current/test/TestAssetProxyDispatcher/TestAssetProxyDispatcher.sol b/packages/contracts/src/contracts/current/test/TestAssetProxyDispatcher/TestAssetProxyDispatcher.sol
new file mode 100644
index 000000000..93ec3cef3
--- /dev/null
+++ b/packages/contracts/src/contracts/current/test/TestAssetProxyDispatcher/TestAssetProxyDispatcher.sol
@@ -0,0 +1,34 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../protocol/Exchange/MixinAssetProxyDispatcher.sol";
+
+contract TestAssetProxyDispatcher is MixinAssetProxyDispatcher {
+ function publicDispatchTransferFrom(
+ bytes memory assetMetadata,
+ address from,
+ address to,
+ uint256 amount)
+ public
+ {
+ dispatchTransferFrom(assetMetadata, from, to, amount);
+ }
+}
diff --git a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol
new file mode 100644
index 000000000..1597ff8d5
--- /dev/null
+++ b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol
@@ -0,0 +1,133 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../utils/LibBytes/LibBytes.sol";
+
+contract TestLibBytes is
+ LibBytes
+{
+
+ /// @dev Tests equality of two byte arrays.
+ /// @param lhs First byte array to compare.
+ /// @param rhs Second byte array to compare.
+ /// @return True if arrays are the same. False otherwise.
+ function publicAreBytesEqual(bytes memory lhs, bytes memory rhs)
+ public
+ pure
+ returns (bool equal)
+ {
+ equal = areBytesEqual(lhs, rhs);
+ return equal;
+ }
+
+ /// @dev Reads an address from a position in a byte array.
+ /// @param b Byte array containing an address.
+ /// @param index Index in byte array of address.
+ /// @return address from byte array.
+ function publicReadAddress(
+ bytes memory b,
+ uint256 index)
+ public
+ pure
+ returns (address result)
+ {
+ result = readAddress(b, index);
+ return result;
+ }
+
+ /// @dev Writes an address into a specific position in a byte array.
+ /// @param b Byte array to insert address into.
+ /// @param index Index in byte array of address.
+ /// @param input Address to put into byte array.
+ function publicWriteAddress(
+ bytes memory b,
+ uint256 index,
+ address input)
+ public
+ pure
+ returns (bytes memory)
+ {
+ writeAddress(b, index, input);
+ return b;
+ }
+
+ /// @dev Reads a bytes32 value from a position in a byte array.
+ /// @param b Byte array containing a bytes32 value.
+ /// @param index Index in byte array of bytes32 value.
+ /// @return bytes32 value from byte array.
+ function publicReadBytes32(
+ bytes memory b,
+ uint256 index)
+ public
+ pure
+ returns (bytes32 result)
+ {
+ result = readBytes32(b, index);
+ return result;
+ }
+
+ /// @dev Writes a bytes32 into a specific position in a byte array.
+ /// @param b Byte array to insert <input> into.
+ /// @param index Index in byte array of <input>.
+ /// @param input bytes32 to put into byte array.
+ function publicWriteBytes32(
+ bytes memory b,
+ uint256 index,
+ bytes32 input)
+ public
+ pure
+ returns (bytes memory)
+ {
+ writeBytes32(b, index, input);
+ return b;
+ }
+
+ /// @dev Reads a uint256 value from a position in a byte array.
+ /// @param b Byte array containing a uint256 value.
+ /// @param index Index in byte array of uint256 value.
+ /// @return uint256 value from byte array.
+ function publicReadUint256(
+ bytes memory b,
+ uint256 index)
+ public
+ pure
+ returns (uint256 result)
+ {
+ result = readUint256(b, index);
+ return result;
+ }
+
+ /// @dev Writes a uint256 into a specific position in a byte array.
+ /// @param b Byte array to insert <input> into.
+ /// @param index Index in byte array of <input>.
+ /// @param input uint256 to put into byte array.
+ function publicWriteUint256(
+ bytes memory b,
+ uint256 index,
+ uint256 input)
+ public
+ pure
+ returns (bytes memory)
+ {
+ writeUint256(b, index, input);
+ return b;
+ }
+}
diff --git a/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol
new file mode 100644
index 000000000..0dc7785b2
--- /dev/null
+++ b/packages/contracts/src/contracts/current/test/TestLibs/TestLibs.sol
@@ -0,0 +1,80 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../protocol/Exchange/libs/LibMath.sol";
+import "../../protocol/Exchange/libs/LibOrder.sol";
+import "../../protocol/Exchange/libs/LibFillResults.sol";
+
+contract TestLibs is
+ LibMath,
+ LibOrder,
+ LibFillResults
+{
+ function publicGetPartialAmount(
+ uint256 numerator,
+ uint256 denominator,
+ uint256 target)
+ public
+ pure
+ returns (uint256 partialAmount)
+ {
+ partialAmount = getPartialAmount(
+ numerator,
+ denominator,
+ target
+ );
+ return partialAmount;
+ }
+
+ function publicIsRoundingError(
+ uint256 numerator,
+ uint256 denominator,
+ uint256 target)
+ public
+ pure
+ returns (bool isError)
+ {
+ isError = isRoundingError(
+ numerator,
+ denominator,
+ target
+ );
+ return isError;
+ }
+
+ function publicGetOrderHash(Order memory order)
+ public
+ view
+ returns (bytes32 orderHash)
+ {
+ orderHash = getOrderHash(order);
+ return orderHash;
+ }
+
+ function publicAddFillResults(FillResults memory totalFillResults, FillResults memory singleFillResults)
+ public
+ pure
+ returns (FillResults memory)
+ {
+ addFillResults(totalFillResults, singleFillResults);
+ return totalFillResults;
+ }
+} \ No newline at end of file
diff --git a/packages/contracts/src/contracts/current/test/TestSignatureValidator/TestSignatureValidator.sol b/packages/contracts/src/contracts/current/test/TestSignatureValidator/TestSignatureValidator.sol
new file mode 100644
index 000000000..20202cd7b
--- /dev/null
+++ b/packages/contracts/src/contracts/current/test/TestSignatureValidator/TestSignatureValidator.sol
@@ -0,0 +1,41 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "../../protocol/Exchange/MixinSignatureValidator.sol";
+
+contract TestSignatureValidator is MixinSignatureValidator {
+
+ function publicIsValidSignature(
+ bytes32 hash,
+ address signer,
+ bytes memory signature)
+ public
+ view
+ returns (bool isValid)
+ {
+ isValid = isValidSignature(
+ hash,
+ signer,
+ signature
+ );
+ return isValid;
+ }
+} \ No newline at end of file
diff --git a/packages/contracts/src/contracts/current/tokens/ERC20Token/ERC20Token.sol b/packages/contracts/src/contracts/current/tokens/ERC20Token/ERC20Token.sol
index 0e5b87aa4..6497d3c7a 100644
--- a/packages/contracts/src/contracts/current/tokens/ERC20Token/ERC20Token.sol
+++ b/packages/contracts/src/contracts/current/tokens/ERC20Token/ERC20Token.sol
@@ -1,45 +1,90 @@
-pragma solidity ^0.4.18;
+/*
-import { Token } from "../Token/Token.sol";
+ Copyright 2018 ZeroEx Intl.
-contract ERC20Token is Token {
+ 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
- function transfer(address _to, uint _value)
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+import "./IERC20Token.sol";
+
+contract ERC20Token is IERC20Token {
+
+ string constant INSUFFICIENT_BALANCE = "Insufficient balance to complete transfer.";
+ string constant INSUFFICIENT_ALLOWANCE = "Insufficient allowance to complete transfer.";
+ string constant OVERFLOW = "Transfer would result in an overflow.";
+
+ mapping (address => uint256) balances;
+ mapping (address => mapping (address => uint256)) allowed;
+
+ uint256 public totalSupply;
+
+ function transfer(address _to, uint256 _value)
public
returns (bool)
{
- require(balances[msg.sender] >= _value && balances[_to] + _value >= balances[_to]);
+ require(
+ balances[msg.sender] >= _value,
+ INSUFFICIENT_BALANCE
+ );
+ require(
+ balances[_to] + _value >= balances[_to],
+ OVERFLOW
+ );
balances[msg.sender] -= _value;
balances[_to] += _value;
- Transfer(msg.sender, _to, _value);
+ emit Transfer(msg.sender, _to, _value);
return true;
}
- function transferFrom(address _from, address _to, uint _value)
+ function transferFrom(address _from, address _to, uint256 _value)
public
returns (bool)
{
- require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value >= balances[_to]);
+ require(
+ balances[_from] >= _value,
+ INSUFFICIENT_BALANCE
+ );
+ require(
+ allowed[_from][msg.sender] >= _value,
+ INSUFFICIENT_ALLOWANCE
+ );
+ require(
+ balances[_to] + _value >= balances[_to],
+ OVERFLOW
+ );
balances[_to] += _value;
balances[_from] -= _value;
allowed[_from][msg.sender] -= _value;
- Transfer(_from, _to, _value);
+ emit Transfer(_from, _to, _value);
return true;
}
- function approve(address _spender, uint _value)
+ function approve(address _spender, uint256 _value)
public
returns (bool)
{
allowed[msg.sender][_spender] = _value;
- Approval(msg.sender, _spender, _value);
+ emit Approval(msg.sender, _spender, _value);
return true;
}
function balanceOf(address _owner)
- public
- view
- returns (uint)
+ public view
+ returns (uint256)
{
return balances[_owner];
}
@@ -47,12 +92,9 @@ contract ERC20Token is Token {
function allowance(address _owner, address _spender)
public
view
- returns (uint)
+ returns (uint256)
{
return allowed[_owner][_spender];
}
-
- mapping (address => uint) balances;
- mapping (address => mapping (address => uint)) allowed;
- uint public totalSupply;
}
+
diff --git a/packages/contracts/src/contracts/current/tokens/ERC20Token/IERC20Token.sol b/packages/contracts/src/contracts/current/tokens/ERC20Token/IERC20Token.sol
new file mode 100644
index 000000000..537f5a83d
--- /dev/null
+++ b/packages/contracts/src/contracts/current/tokens/ERC20Token/IERC20Token.sol
@@ -0,0 +1,73 @@
+/*
+
+ 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.23;
+pragma experimental ABIEncoderV2;
+
+contract IERC20Token {
+
+ /// @notice send `value` token to `to` from `msg.sender`
+ /// @param _to The address of the recipient
+ /// @param _value The amount of token to be transferred
+ /// @return Whether the transfer was successful or not
+ function transfer(address _to, uint256 _value)
+ public
+ returns (bool);
+
+ /// @notice send `value` token to `to` from `from` on the condition it is approved by `from`
+ /// @param _from The address of the sender
+ /// @param _to The address of the recipient
+ /// @param _value The amount of token to be transferred
+ /// @return Whether the transfer was successful or not
+ function transferFrom(address _from, address _to, uint256 _value)
+ public
+ returns (bool);
+
+ /// @notice `msg.sender` approves `_spender` to spend `_value` tokens
+ /// @param _spender The address of the account able to transfer the tokens
+ /// @param _value The amount of wei to be approved for transfer
+ /// @return Whether the approval was successful or not
+ function approve(address _spender, uint256 _value)
+ public
+ returns (bool);
+
+ /// @param _owner The address from which the balance will be retrieved
+ /// @return The balance
+ function balanceOf(address _owner)
+ public view
+ returns (uint256);
+
+ /// @param _owner The address of the account owning tokens
+ /// @param _spender The address of the account able to transfer the tokens
+ /// @return Amount of remaining tokens allowed to spent
+ function allowance(address _owner, address _spender)
+ public view
+ returns (uint256);
+
+ event Transfer(
+ address indexed _from,
+ address indexed _to,
+ uint256 _value
+ );
+
+ event Approval(
+ address indexed _owner,
+ address indexed _spender,
+ uint256 _value
+ );
+}
diff --git a/packages/contracts/src/contracts/current/tokens/ERC721Token/ERC721Token.sol b/packages/contracts/src/contracts/current/tokens/ERC721Token/ERC721Token.sol
new file mode 100644
index 000000000..b3493bc99
--- /dev/null
+++ b/packages/contracts/src/contracts/current/tokens/ERC721Token/ERC721Token.sol
@@ -0,0 +1,406 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Smart Contract Solutions, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+pragma solidity ^0.4.23;
+
+import "./IERC721Token.sol";
+import "./IERC721Receiver.sol";
+import "../../utils/SafeMath/SafeMath.sol";
+
+/**
+ * @title ERC721 Non-Fungible Token Standard basic implementation
+ * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
+ * Modified from https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/ERC721/ERC721BasicToken.sol
+ */
+contract ERC721Token is
+ IERC721Token,
+ SafeMath
+{
+ // Equals to `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`
+ // which can be also obtained as `ERC721Receiver(0).onERC721Received.selector`
+ bytes4 constant ERC721_RECEIVED = 0xf0b9e5ba;
+
+ // Mapping from token ID to owner
+ mapping (uint256 => address) internal tokenOwner;
+
+ // Mapping from token ID to approved address
+ mapping (uint256 => address) internal tokenApprovals;
+
+ // Mapping from owner to number of owned token
+ mapping (address => uint256) internal ownedTokensCount;
+
+ // Mapping from owner to operator approvals
+ mapping (address => mapping (address => bool)) internal operatorApprovals;
+
+ /**
+ * @dev Guarantees msg.sender is owner of the given token
+ * @param _tokenId uint256 ID of the token to validate its ownership belongs to msg.sender
+ */
+ modifier onlyOwnerOf(uint256 _tokenId) {
+ require(ownerOf(_tokenId) == msg.sender);
+ _;
+ }
+
+ /**
+ * @dev Checks msg.sender can transfer a token, by being owner, approved, or operator
+ * @param _tokenId uint256 ID of the token to validate
+ */
+ modifier canTransfer(uint256 _tokenId) {
+ require(isApprovedOrOwner(msg.sender, _tokenId));
+ _;
+ }
+
+ function ERC721Token(
+ string _name,
+ string _symbol)
+ public
+ {
+ name_ = _name;
+ symbol_ = _symbol;
+ }
+
+ /**
+ * @dev Gets the token name
+ * @return string representing the token name
+ */
+ function name()
+ public
+ view
+ returns (string)
+ {
+ return name_;
+ }
+
+ /**
+ * @dev Gets the token symbol
+ * @return string representing the token symbol
+ */
+ function symbol()
+ public
+ view
+ returns (string)
+ {
+ return symbol_;
+ }
+
+ /**
+ * @dev Gets the balance of the specified address
+ * @param _owner address to query the balance of
+ * @return uint256 representing the amount owned by the passed address
+ */
+ function balanceOf(address _owner)
+ public
+ view
+ returns (uint256)
+ {
+ require(_owner != address(0));
+ return ownedTokensCount[_owner];
+ }
+
+ /**
+ * @dev Gets the owner of the specified token ID
+ * @param _tokenId uint256 ID of the token to query the owner of
+ * @return owner address currently marked as the owner of the given token ID
+ */
+ function ownerOf(uint256 _tokenId)
+ public
+ view
+ returns (address)
+ {
+ address owner = tokenOwner[_tokenId];
+ require(owner != address(0));
+ return owner;
+ }
+
+ /**
+ * @dev Returns whether the specified token exists
+ * @param _tokenId uint256 ID of the token to query the existance of
+ * @return whether the token exists
+ */
+ function exists(uint256 _tokenId)
+ public
+ view
+ returns (bool)
+ {
+ address owner = tokenOwner[_tokenId];
+ return owner != address(0);
+ }
+
+ /**
+ * @dev Approves another address to transfer the given token ID
+ * @dev The zero address indicates there is no approved address.
+ * @dev There can only be one approved address per token at a given time.
+ * @dev Can only be called by the token owner or an approved operator.
+ * @param _to address to be approved for the given token ID
+ * @param _tokenId uint256 ID of the token to be approved
+ */
+ function approve(address _to, uint256 _tokenId)
+ public
+ {
+ address owner = ownerOf(_tokenId);
+ require(_to != owner);
+ require(msg.sender == owner || isApprovedForAll(owner, msg.sender));
+
+ if (getApproved(_tokenId) != address(0) || _to != address(0)) {
+ tokenApprovals[_tokenId] = _to;
+ emit Approval(owner, _to, _tokenId);
+ }
+ }
+
+ /**
+ * @dev Gets the approved address for a token ID, or zero if no address set
+ * @param _tokenId uint256 ID of the token to query the approval of
+ * @return address currently approved for a the given token ID
+ */
+ function getApproved(uint256 _tokenId)
+ public
+ view
+ returns (address)
+ {
+ return tokenApprovals[_tokenId];
+ }
+
+ /**
+ * @dev Sets or unsets the approval of a given operator
+ * @dev An operator is allowed to transfer all tokens of the sender on their behalf
+ * @param _to operator address to set the approval
+ * @param _approved representing the status of the approval to be set
+ */
+ function setApprovalForAll(address _to, bool _approved)
+ public
+ {
+ require(_to != msg.sender);
+ operatorApprovals[msg.sender][_to] = _approved;
+ emit ApprovalForAll(msg.sender, _to, _approved);
+ }
+
+ /**
+ * @dev Tells whether an operator is approved by a given owner
+ * @param _owner owner address which you want to query the approval of
+ * @param _operator operator address which you want to query the approval of
+ * @return bool whether the given operator is approved by the given owner
+ */
+ function isApprovedForAll(address _owner, address _operator)
+ public
+ view
+ returns (bool)
+ {
+ return operatorApprovals[_owner][_operator];
+ }
+
+ /**
+ * @dev Transfers the ownership of a given token ID to another address
+ * @dev Usage of this method is discouraged, use `safeTransferFrom` whenever possible
+ * @dev Requires the msg sender to be the owner, approved, or operator
+ * @param _from current owner of the token
+ * @param _to address to receive the ownership of the given token ID
+ * @param _tokenId uint256 ID of the token to be transferred
+ */
+ function transferFrom(address _from, address _to, uint256 _tokenId)
+ public
+ canTransfer(_tokenId)
+ {
+ require(_from != address(0));
+ require(_to != address(0));
+
+ clearApproval(_from, _tokenId);
+ removeTokenFrom(_from, _tokenId);
+ addTokenTo(_to, _tokenId);
+
+ emit Transfer(_from, _to, _tokenId);
+ }
+
+ /**
+ * @dev Safely transfers the ownership of a given token ID to another address
+ * @dev If the target address is a contract, it must implement `onERC721Received`,
+ * which is called upon a safe transfer, and return the magic value
+ * `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; otherwise,
+ * the transfer is reverted.
+ * @dev Requires the msg sender to be the owner, approved, or operator
+ * @param _from current owner of the token
+ * @param _to address to receive the ownership of the given token ID
+ * @param _tokenId uint256 ID of the token to be transferred
+ */
+ function safeTransferFrom(
+ address _from,
+ address _to,
+ uint256 _tokenId)
+ public
+ canTransfer(_tokenId)
+ {
+ // solium-disable-next-line arg-overflow
+ safeTransferFrom(_from, _to, _tokenId, "");
+ }
+
+ /**
+ * @dev Safely transfers the ownership of a given token ID to another address
+ * @dev If the target address is a contract, it must implement `onERC721Received`,
+ * which is called upon a safe transfer, and return the magic value
+ * `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; otherwise,
+ * the transfer is reverted.
+ * @dev Requires the msg sender to be the owner, approved, or operator
+ * @param _from current owner of the token
+ * @param _to address to receive the ownership of the given token ID
+ * @param _tokenId uint256 ID of the token to be transferred
+ * @param _data bytes data to send along with a safe transfer check
+ */
+ function safeTransferFrom(
+ address _from,
+ address _to,
+ uint256 _tokenId,
+ bytes _data)
+ public
+ canTransfer(_tokenId)
+ {
+ transferFrom(_from, _to, _tokenId);
+ // solium-disable-next-line arg-overflow
+ require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data));
+ }
+
+ /**
+ * @dev Returns whether the given spender can transfer a given token ID
+ * @param _spender address of the spender to query
+ * @param _tokenId uint256 ID of the token to be transferred
+ * @return bool whether the msg.sender is approved for the given token ID,
+ * is an operator of the owner, or is the owner of the token
+ */
+ function isApprovedOrOwner(address _spender, uint256 _tokenId)
+ internal
+ view
+ returns (bool)
+ {
+ address owner = ownerOf(_tokenId);
+ return _spender == owner || getApproved(_tokenId) == _spender || isApprovedForAll(owner, _spender);
+ }
+
+ /**
+ * @dev Internal function to mint a new token
+ * @dev Reverts if the given token ID already exists
+ * @param _to The address that will own the minted token
+ * @param _tokenId uint256 ID of the token to be minted by the msg.sender
+ */
+ function _mint(address _to, uint256 _tokenId)
+ internal
+ {
+ require(_to != address(0));
+ addTokenTo(_to, _tokenId);
+ emit Transfer(address(0), _to, _tokenId);
+ }
+
+ /**
+ * @dev Internal function to burn a specific token
+ * @dev Reverts if the token does not exist
+ * @param _tokenId uint256 ID of the token being burned by the msg.sender
+ */
+ function _burn(address _owner, uint256 _tokenId)
+ internal
+ {
+ clearApproval(_owner, _tokenId);
+ removeTokenFrom(_owner, _tokenId);
+ emit Transfer(_owner, address(0), _tokenId);
+ }
+
+ /**
+ * @dev Internal function to clear current approval of a given token ID
+ * @dev Reverts if the given address is not indeed the owner of the token
+ * @param _owner owner of the token
+ * @param _tokenId uint256 ID of the token to be transferred
+ */
+ function clearApproval(address _owner, uint256 _tokenId)
+ internal
+ {
+ require(ownerOf(_tokenId) == _owner);
+ if (tokenApprovals[_tokenId] != address(0)) {
+ tokenApprovals[_tokenId] = address(0);
+ emit Approval(_owner, address(0), _tokenId);
+ }
+ }
+
+ /**
+ * @dev Internal function to add a token ID to the list of a given address
+ * @param _to address representing the new owner of the given token ID
+ * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
+ */
+ function addTokenTo(address _to, uint256 _tokenId)
+ internal
+ {
+ require(tokenOwner[_tokenId] == address(0));
+ tokenOwner[_tokenId] = _to;
+ ownedTokensCount[_to] = safeAdd(ownedTokensCount[_to], 1);
+ }
+
+ /**
+ * @dev Internal function to remove a token ID from the list of a given address
+ * @param _from address representing the previous owner of the given token ID
+ * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
+ */
+ function removeTokenFrom(address _from, uint256 _tokenId)
+ internal
+ {
+ require(ownerOf(_tokenId) == _from);
+ ownedTokensCount[_from] = safeSub(ownedTokensCount[_from], 1);
+ tokenOwner[_tokenId] = address(0);
+ }
+
+ /**
+ * @dev Internal function to invoke `onERC721Received` on a target address
+ * @dev The call is not executed if the target address is not a contract
+ * @param _from address representing the previous owner of the given token ID
+ * @param _to target address that will receive the tokens
+ * @param _tokenId uint256 ID of the token to be transferred
+ * @param _data bytes optional data to send along with the call
+ * @return whether the call correctly returned the expected magic value
+ */
+ function checkAndCallSafeTransfer(
+ address _from,
+ address _to,
+ uint256 _tokenId,
+ bytes _data)
+ internal
+ returns (bool)
+ {
+ if (!isContract(_to)) {
+ return true;
+ }
+ bytes4 retval = IERC721Receiver(_to).onERC721Received(_from, _tokenId, _data);
+ return (retval == ERC721_RECEIVED);
+ }
+
+ function isContract(address addr)
+ internal
+ view
+ returns (bool)
+ {
+ uint256 size;
+ // XXX Currently there is no better way to check if there is a contract in an address
+ // than to check the size of the code at that address.
+ // See https://ethereum.stackexchange.com/a/14016/36603
+ // for more details about how this works.
+ // TODO Check this again before the Serenity release, because all addresses will be
+ // contracts then.
+ assembly { size := extcodesize(addr) } // solium-disable-line security/no-inline-assembly
+ return size > 0;
+ }
+} \ No newline at end of file
diff --git a/packages/contracts/src/contracts/current/tokens/ERC721Token/IERC721Receiver.sol b/packages/contracts/src/contracts/current/tokens/ERC721Token/IERC721Receiver.sol
new file mode 100644
index 000000000..3484bf824
--- /dev/null
+++ b/packages/contracts/src/contracts/current/tokens/ERC721Token/IERC721Receiver.sol
@@ -0,0 +1,60 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Smart Contract Solutions, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+pragma solidity ^0.4.23;
+
+/**
+ * @title ERC721 token receiver interface
+ * @dev Interface for any contract that wants to support safeTransfers
+ * rom ERC721 asset contracts.
+ * Modified from https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/ERC721/ERC721Receiver.sol
+ */
+contract IERC721Receiver {
+ /**
+ * @dev Magic value to be returned upon successful reception of an NFT
+ * Equals to `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`,
+ * which can be also obtained as `ERC721Receiver(0).onERC721Received.selector`
+ */
+ bytes4 constant ERC721_RECEIVED = 0xf0b9e5ba;
+
+ /**
+ * @notice Handle the receipt of an NFT
+ * @dev The ERC721 smart contract calls this function on the recipient
+ * after a `safetransfer`. This function MAY throw to revert and reject the
+ * transfer. This function MUST use 50,000 gas or less. Return of other
+ * than the magic value MUST result in the transaction being reverted.
+ * Note: the contract address is always the message sender.
+ * @param _from The sending address
+ * @param _tokenId The NFT identifier which is being transfered
+ * @param _data Additional data with no specified format
+ * @return `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`
+ */
+ function onERC721Received(
+ address _from,
+ uint256 _tokenId,
+ bytes _data)
+ public
+ returns (bytes4);
+} \ No newline at end of file
diff --git a/packages/contracts/src/contracts/current/tokens/ERC721Token/IERC721Token.sol b/packages/contracts/src/contracts/current/tokens/ERC721Token/IERC721Token.sol
new file mode 100644
index 000000000..81e1b97af
--- /dev/null
+++ b/packages/contracts/src/contracts/current/tokens/ERC721Token/IERC721Token.sol
@@ -0,0 +1,105 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 Smart Contract Solutions, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+pragma solidity ^0.4.23;
+
+/**
+ * @title ERC721 Non-Fungible Token Standard basic interface
+ * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
+ * Modified from https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/ERC721/ERC721Basic.sol
+ */
+contract IERC721Token {
+ string internal name_;
+ string internal symbol_;
+
+ event Transfer(
+ address indexed _from,
+ address indexed _to,
+ uint256 _tokenId
+ );
+ event Approval(
+ address indexed _owner,
+ address indexed _approved,
+ uint256 _tokenId
+ );
+ event ApprovalForAll(
+ address indexed _owner,
+ address indexed _operator,
+ bool _approved
+ );
+
+ function name()
+ public
+ view
+ returns (string);
+ function symbol()
+ public
+ view
+ returns (string);
+
+ function balanceOf(address _owner)
+ public
+ view
+ returns (uint256 _balance);
+ function ownerOf(uint256 _tokenId)
+ public
+ view
+ returns (address _owner);
+ function exists(uint256 _tokenId)
+ public
+ view
+ returns (bool _exists);
+
+ function approve(address _to, uint256 _tokenId)
+ public;
+ function getApproved(uint256 _tokenId)
+ public
+ view
+ returns (address _operator);
+
+ function setApprovalForAll(address _operator, bool _approved)
+ public;
+ function isApprovedForAll(address _owner, address _operator)
+ public
+ view
+ returns (bool);
+
+ function transferFrom(
+ address _from,
+ address _to,
+ uint256 _tokenId)
+ public;
+ function safeTransferFrom(
+ address _from,
+ address _to,
+ uint256 _tokenId)
+ public;
+ function safeTransferFrom(
+ address _from,
+ address _to,
+ uint256 _tokenId,
+ bytes _data)
+ public;
+} \ No newline at end of file
diff --git a/packages/contracts/src/contracts/current/tokens/Token/Token.sol b/packages/contracts/src/contracts/current/tokens/Token/Token.sol
deleted file mode 100644
index bf4e71dcd..000000000
--- a/packages/contracts/src/contracts/current/tokens/Token/Token.sol
+++ /dev/null
@@ -1,35 +0,0 @@
-pragma solidity ^0.4.18;
-
-contract Token {
-
- /// @notice send `_value` token to `_to` from `msg.sender`
- /// @param _to The address of the recipient
- /// @param _value The amount of token to be transferred
- /// @return Whether the transfer was successful or not
- function transfer(address _to, uint _value) public returns (bool) {}
-
- /// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
- /// @param _from The address of the sender
- /// @param _to The address of the recipient
- /// @param _value The amount of token to be transferred
- /// @return Whether the transfer was successful or not
- function transferFrom(address _from, address _to, uint _value) public returns (bool) {}
-
- /// @notice `msg.sender` approves `_addr` to spend `_value` tokens
- /// @param _spender The address of the account able to transfer the tokens
- /// @param _value The amount of wei to be approved for transfer
- /// @return Whether the approval was successful or not
- function approve(address _spender, uint _value) public returns (bool) {}
-
- /// @param _owner The address from which the balance will be retrieved
- /// @return The balance
- function balanceOf(address _owner) public view returns (uint) {}
-
- /// @param _owner The address of the account owning tokens
- /// @param _spender The address of the account able to transfer the tokens
- /// @return Amount of remaining tokens allowed to spent
- function allowance(address _owner, address _spender) public view returns (uint) {}
-
- event Transfer(address indexed _from, address indexed _to, uint _value);
- event Approval(address indexed _owner, address indexed _spender, uint _value);
-}
diff --git a/packages/contracts/src/contracts/current/tokens/UnlimitedAllowanceToken/UnlimitedAllowanceToken.sol b/packages/contracts/src/contracts/current/tokens/UnlimitedAllowanceToken/UnlimitedAllowanceToken.sol
index 699f535d2..395e5d356 100644
--- a/packages/contracts/src/contracts/current/tokens/UnlimitedAllowanceToken/UnlimitedAllowanceToken.sol
+++ b/packages/contracts/src/contracts/current/tokens/UnlimitedAllowanceToken/UnlimitedAllowanceToken.sol
@@ -1,6 +1,6 @@
/*
- Copyright 2017 ZeroEx Intl.
+ 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.
@@ -16,31 +16,43 @@
*/
-pragma solidity ^0.4.18;
+pragma solidity ^0.4.23;
+pragma experimental ABIEncoderV2;
-import { ERC20Token } from "../ERC20Token/ERC20Token.sol";
+import "../ERC20Token/ERC20Token.sol";
contract UnlimitedAllowanceToken is ERC20Token {
- uint constant MAX_UINT = 2**256 - 1;
+ uint256 constant MAX_UINT = 2**256 - 1;
/// @dev ERC20 transferFrom, modified such that an allowance of MAX_UINT represents an unlimited allowance. See https://github.com/ethereum/EIPs/issues/717
/// @param _from Address to transfer from.
/// @param _to Address to transfer to.
/// @param _value Amount to transfer.
/// @return Success of transfer.
- function transferFrom(address _from, address _to, uint _value)
+ function transferFrom(address _from, address _to, uint256 _value)
public
returns (bool)
{
- uint allowance = allowed[_from][msg.sender];
- require(balances[_from] >= _value && allowance >= _value && balances[_to] + _value >= balances[_to]);
+ uint256 allowance = allowed[_from][msg.sender];
+ require(
+ balances[_from] >= _value,
+ INSUFFICIENT_BALANCE
+ );
+ require(
+ allowance >= _value,
+ INSUFFICIENT_ALLOWANCE
+ );
+ require(
+ balances[_to] + _value >= balances[_to],
+ OVERFLOW
+ );
balances[_to] += _value;
balances[_from] -= _value;
if (allowance < MAX_UINT) {
allowed[_from][msg.sender] -= _value;
}
- Transfer(_from, _to, _value);
+ emit Transfer(_from, _to, _value);
return true;
}
}
diff --git a/packages/contracts/src/contracts/current/tokens/ZRXToken/ZRXToken.sol b/packages/contracts/src/contracts/current/tokens/ZRXToken/ZRXToken.sol
index 7f5e1f849..2d8e74f2c 100644
--- a/packages/contracts/src/contracts/current/tokens/ZRXToken/ZRXToken.sol
+++ b/packages/contracts/src/contracts/current/tokens/ZRXToken/ZRXToken.sol
@@ -1,6 +1,6 @@
/*
- Copyright 2017 ZeroEx Intl.
+ 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.
diff --git a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol
new file mode 100644
index 000000000..3c5531e35
--- /dev/null
+++ b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol
@@ -0,0 +1,206 @@
+/*
+
+ 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.23;
+
+contract LibBytes {
+
+ // Revert reasons
+ string constant GTE_20_LENGTH_REQUIRED = "Length must be greater than or equal to 20.";
+ string constant GTE_32_LENGTH_REQUIRED = "Length must be greater than or equal to 32.";
+
+ /// @dev Tests equality of two byte arrays.
+ /// @param lhs First byte array to compare.
+ /// @param rhs Second byte array to compare.
+ /// @return True if arrays are the same. False otherwise.
+ function areBytesEqual(bytes memory lhs, bytes memory rhs)
+ internal
+ pure
+ returns (bool equal)
+ {
+ assembly {
+ // Get the number of words occupied by <lhs>
+ let lenFullWords := div(add(mload(lhs), 0x1F), 0x20)
+
+ // Add 1 to the number of words, to account for the length field
+ lenFullWords := add(lenFullWords, 0x1)
+
+ // Test equality word-by-word.
+ // Terminates early if there is a mismatch.
+ for {let i := 0} lt(i, lenFullWords) {i := add(i, 1)} {
+ let lhsWord := mload(add(lhs, mul(i, 0x20)))
+ let rhsWord := mload(add(rhs, mul(i, 0x20)))
+ equal := eq(lhsWord, rhsWord)
+ if eq(equal, 0) {
+ // Break
+ i := lenFullWords
+ }
+ }
+ }
+
+ return equal;
+ }
+
+ /// @dev Reads an address from a position in a byte array.
+ /// @param b Byte array containing an address.
+ /// @param index Index in byte array of address.
+ /// @return address from byte array.
+ function readAddress(
+ bytes memory b,
+ uint256 index)
+ internal
+ pure
+ returns (address result)
+ {
+ require(
+ b.length >= index + 20, // 20 is length of address
+ GTE_20_LENGTH_REQUIRED
+ );
+
+ // Add offset to index:
+ // 1. Arrays are prefixed by 32-byte length parameter (add 32 to index)
+ // 2. Account for size difference between address length and 32-byte storage word (subtract 12 from index)
+ index += 20;
+
+ // Read address from array memory
+ assembly {
+ // 1. Add index to address of bytes array
+ // 2. Load 32-byte word from memory
+ // 3. Apply 20-byte mask to obtain address
+ result := and(mload(add(b, index)), 0xffffffffffffffffffffffffffffffffffffffff)
+ }
+ return result;
+ }
+
+ /// @dev Writes an address into a specific position in a byte array.
+ /// @param b Byte array to insert address into.
+ /// @param index Index in byte array of address.
+ /// @param input Address to put into byte array.
+ function writeAddress(
+ bytes memory b,
+ uint256 index,
+ address input)
+ internal
+ pure
+ {
+ require(
+ b.length >= index + 20, // 20 is length of address
+ GTE_20_LENGTH_REQUIRED
+ );
+
+ // Add offset to index:
+ // 1. Arrays are prefixed by 32-byte length parameter (add 32 to index)
+ // 2. Account for size difference between address length and 32-byte storage word (subtract 12 from index)
+ index += 20;
+
+ // Store address into array memory
+ assembly {
+ // The address occupies 20 bytes and mstore stores 32 bytes.
+ // First fetch the 32-byte word where we'll be storing the address, then
+ // apply a mask so we have only the bytes in the word that the address will not occupy.
+ // Then combine these bytes with the address and store the 32 bytes back to memory with mstore.
+
+ // 1. Add index to address of bytes array
+ // 2. Load 32-byte word from memory
+ // 3. Apply 12-byte mask to obtain extra bytes occupying word of memory where we'll store the address
+ let neighbors := and(mload(add(b, index)), 0xffffffffffffffffffffffff0000000000000000000000000000000000000000)
+
+ // Store the neighbors and address into memory
+ mstore(add(b, index), xor(input, neighbors))
+ }
+ }
+
+ /// @dev Reads a bytes32 value from a position in a byte array.
+ /// @param b Byte array containing a bytes32 value.
+ /// @param index Index in byte array of bytes32 value.
+ /// @return bytes32 value from byte array.
+ function readBytes32(
+ bytes memory b,
+ uint256 index)
+ internal
+ pure
+ returns (bytes32 result)
+ {
+ require(
+ b.length >= index + 32,
+ GTE_32_LENGTH_REQUIRED
+ );
+
+ // Arrays are prefixed by a 256 bit length parameter
+ index += 32;
+
+ // Read the bytes32 from array memory
+ assembly {
+ result := mload(add(b, index))
+ }
+ return result;
+ }
+
+ /// @dev Writes a bytes32 into a specific position in a byte array.
+ /// @param b Byte array to insert <input> into.
+ /// @param index Index in byte array of <input>.
+ /// @param input bytes32 to put into byte array.
+ function writeBytes32(
+ bytes memory b,
+ uint256 index,
+ bytes32 input)
+ internal
+ pure
+ {
+ require(
+ b.length >= index + 32,
+ GTE_32_LENGTH_REQUIRED
+ );
+
+ // Arrays are prefixed by a 256 bit length parameter
+ index += 32;
+
+ // Read the bytes32 from array memory
+ assembly {
+ mstore(add(b, index), input)
+ }
+ }
+
+ /// @dev Reads a uint256 value from a position in a byte array.
+ /// @param b Byte array containing a uint256 value.
+ /// @param index Index in byte array of uint256 value.
+ /// @return uint256 value from byte array.
+ function readUint256(
+ bytes memory b,
+ uint256 index)
+ internal
+ pure
+ returns (uint256 result)
+ {
+ return uint256(readBytes32(b, index));
+ }
+
+ /// @dev Writes a uint256 into a specific position in a byte array.
+ /// @param b Byte array to insert <input> into.
+ /// @param index Index in byte array of <input>.
+ /// @param input uint256 to put into byte array.
+ function writeUint256(
+ bytes memory b,
+ uint256 index,
+ uint256 input)
+ internal
+ pure
+ {
+ writeBytes32(b, index, bytes32(input));
+ }
+}
diff --git a/packages/contracts/src/contracts/current/utils/Ownable/IOwnable.sol b/packages/contracts/src/contracts/current/utils/Ownable/IOwnable.sol
new file mode 100644
index 000000000..63b04945f
--- /dev/null
+++ b/packages/contracts/src/contracts/current/utils/Ownable/IOwnable.sol
@@ -0,0 +1,14 @@
+pragma solidity ^0.4.23;
+pragma experimental ABIEncoderV2;
+
+/*
+ * Ownable
+ *
+ * Base contract with an owner.
+ * Provides onlyOwner modifier, which prevents function from running if it is called by anyone other than the owner.
+ */
+
+contract IOwnable {
+ function transferOwnership(address newOwner)
+ public;
+}
diff --git a/packages/contracts/src/contracts/current/utils/Ownable/Ownable.sol b/packages/contracts/src/contracts/current/utils/Ownable/Ownable.sol
index 9b3d6b9cf..933aa168a 100644
--- a/packages/contracts/src/contracts/current/utils/Ownable/Ownable.sol
+++ b/packages/contracts/src/contracts/current/utils/Ownable/Ownable.sol
@@ -1,4 +1,5 @@
-pragma solidity ^0.4.18;
+pragma solidity ^0.4.23;
+pragma experimental ABIEncoderV2;
/*
* Ownable
@@ -7,17 +8,22 @@ pragma solidity ^0.4.18;
* Provides onlyOwner modifier, which prevents function from running if it is called by anyone other than the owner.
*/
-contract Ownable {
+import "./IOwnable.sol";
+
+contract Ownable is IOwnable {
address public owner;
- function Ownable()
+ constructor ()
public
{
owner = msg.sender;
}
modifier onlyOwner() {
- require(msg.sender == owner);
+ require(
+ msg.sender == owner,
+ "Only contract owner is allowed to call this method."
+ );
_;
}
diff --git a/packages/contracts/src/contracts/current/utils/SafeMath/SafeMath.sol b/packages/contracts/src/contracts/current/utils/SafeMath/SafeMath.sol
index 955a9e379..1ab27eebc 100644
--- a/packages/contracts/src/contracts/current/utils/SafeMath/SafeMath.sol
+++ b/packages/contracts/src/contracts/current/utils/SafeMath/SafeMath.sol
@@ -1,4 +1,5 @@
-pragma solidity ^0.4.18;
+pragma solidity ^0.4.23;
+pragma experimental ABIEncoderV2;
contract SafeMath {
function safeMul(uint a, uint b)
diff --git a/packages/contracts/src/contracts/current/tutorials/Arbitrage/Arbitrage.sol b/packages/contracts/src/contracts/previous/Arbitrage/Arbitrage.sol
index a9f3c22e6..5054afc2f 100644
--- a/packages/contracts/src/contracts/current/tutorials/Arbitrage/Arbitrage.sol
+++ b/packages/contracts/src/contracts/previous/Arbitrage/Arbitrage.sol
@@ -1,9 +1,9 @@
pragma solidity ^0.4.19;
-import { Exchange } from "../../protocol/Exchange/Exchange.sol";
+import { IExchange_v1 as Exchange } from "../Exchange/IExchange_v1.sol";
import { EtherDelta } from "../EtherDelta/EtherDelta.sol";
-import { Ownable } from "../../utils/Ownable/Ownable.sol";
-import { Token } from "../../tokens/Token/Token.sol";
+import { Ownable_v1 as Ownable } from "../Ownable/Ownable_v1.sol";
+import { IToken_v1 as Token } from "../Token/IToken_v1.sol";
/// @title Arbitrage - Facilitates atomic arbitrage of ERC20 tokens between EtherDelta and 0x Exchange contract.
/// @author Leonid Logvinov - <leo@0xProject.com>
diff --git a/packages/contracts/src/contracts/current/tutorials/EtherDelta/AccountLevels.sol b/packages/contracts/src/contracts/previous/EtherDelta/AccountLevels.sol
index 8d7a930d3..8d7a930d3 100644
--- a/packages/contracts/src/contracts/current/tutorials/EtherDelta/AccountLevels.sol
+++ b/packages/contracts/src/contracts/previous/EtherDelta/AccountLevels.sol
diff --git a/packages/contracts/src/contracts/current/tutorials/EtherDelta/EtherDelta.sol b/packages/contracts/src/contracts/previous/EtherDelta/EtherDelta.sol
index 49847ab48..fe599ca0a 100644
--- a/packages/contracts/src/contracts/current/tutorials/EtherDelta/EtherDelta.sol
+++ b/packages/contracts/src/contracts/previous/EtherDelta/EtherDelta.sol
@@ -1,8 +1,8 @@
pragma solidity ^0.4.19;
-import { SafeMath } from "../../utils/SafeMath/SafeMath.sol";
+import { SafeMath } from "../SafeMath/SafeMath_v1.sol";
import { AccountLevels } from "./AccountLevels.sol";
-import { Token } from "../../tokens/Token/Token.sol";
+import { Token } from "../Token/Token_v1.sol";
contract EtherDelta is SafeMath {
address public admin; //the admin address
diff --git a/packages/contracts/src/contracts/previous/Exchange/Exchange_v1.sol b/packages/contracts/src/contracts/previous/Exchange/Exchange_v1.sol
new file mode 100644
index 000000000..3f8e7368d
--- /dev/null
+++ b/packages/contracts/src/contracts/previous/Exchange/Exchange_v1.sol
@@ -0,0 +1,602 @@
+/*
+
+ 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.14;
+
+import { TokenTransferProxy_v1 as TokenTransferProxy } from "../TokenTransferProxy/TokenTransferProxy_v1.sol";
+import { Token_v1 as Token } from "../Token/Token_v1.sol";
+import { SafeMath_v1 as SafeMath } from "../SafeMath/SafeMath_v1.sol";
+
+/// @title Exchange - Facilitates exchange of ERC20 tokens.
+/// @author Amir Bandeali - <amir@0xProject.com>, Will Warren - <will@0xProject.com>
+contract Exchange_v1 is SafeMath {
+
+ // Error Codes
+ enum Errors {
+ ORDER_EXPIRED, // Order has already expired
+ ORDER_FULLY_FILLED_OR_CANCELLED, // Order has already been fully filled or cancelled
+ ROUNDING_ERROR_TOO_LARGE, // Rounding error too large
+ INSUFFICIENT_BALANCE_OR_ALLOWANCE // Insufficient balance or allowance for token transfer
+ }
+
+ string constant public VERSION = "1.0.0";
+ uint16 constant public EXTERNAL_QUERY_GAS_LIMIT = 4999; // Changes to state require at least 5000 gas
+
+ address public ZRX_TOKEN_CONTRACT;
+ address public TOKEN_TRANSFER_PROXY_CONTRACT;
+
+ // Mappings of orderHash => amounts of takerTokenAmount filled or cancelled.
+ mapping (bytes32 => uint) public filled;
+ mapping (bytes32 => uint) public cancelled;
+
+ event LogFill(
+ address indexed maker,
+ address taker,
+ address indexed feeRecipient,
+ address makerToken,
+ address takerToken,
+ uint filledMakerTokenAmount,
+ uint filledTakerTokenAmount,
+ uint paidMakerFee,
+ uint paidTakerFee,
+ bytes32 indexed tokens, // keccak256(makerToken, takerToken), allows subscribing to a token pair
+ bytes32 orderHash
+ );
+
+ event LogCancel(
+ address indexed maker,
+ address indexed feeRecipient,
+ address makerToken,
+ address takerToken,
+ uint cancelledMakerTokenAmount,
+ uint cancelledTakerTokenAmount,
+ bytes32 indexed tokens,
+ bytes32 orderHash
+ );
+
+ event LogError(uint8 indexed errorId, bytes32 indexed orderHash);
+
+ struct Order {
+ address maker;
+ address taker;
+ address makerToken;
+ address takerToken;
+ address feeRecipient;
+ uint makerTokenAmount;
+ uint takerTokenAmount;
+ uint makerFee;
+ uint takerFee;
+ uint expirationTimestampInSec;
+ bytes32 orderHash;
+ }
+
+ function Exchange_v1(address _zrxToken, address _tokenTransferProxy) {
+ ZRX_TOKEN_CONTRACT = _zrxToken;
+ TOKEN_TRANSFER_PROXY_CONTRACT = _tokenTransferProxy;
+ }
+
+ /*
+ * Core exchange functions
+ */
+
+ /// @dev Fills the input order.
+ /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
+ /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
+ /// @param fillTakerTokenAmount Desired amount of takerToken to fill.
+ /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfer will fail before attempting.
+ /// @param v ECDSA signature parameter v.
+ /// @param r ECDSA signature parameters r.
+ /// @param s ECDSA signature parameters s.
+ /// @return Total amount of takerToken filled in trade.
+ function fillOrder(
+ address[5] orderAddresses,
+ uint[6] orderValues,
+ uint fillTakerTokenAmount,
+ bool shouldThrowOnInsufficientBalanceOrAllowance,
+ uint8 v,
+ bytes32 r,
+ bytes32 s)
+ public
+ returns (uint filledTakerTokenAmount)
+ {
+ Order memory order = Order({
+ maker: orderAddresses[0],
+ taker: orderAddresses[1],
+ makerToken: orderAddresses[2],
+ takerToken: orderAddresses[3],
+ feeRecipient: orderAddresses[4],
+ makerTokenAmount: orderValues[0],
+ takerTokenAmount: orderValues[1],
+ makerFee: orderValues[2],
+ takerFee: orderValues[3],
+ expirationTimestampInSec: orderValues[4],
+ orderHash: getOrderHash(orderAddresses, orderValues)
+ });
+
+ require(order.taker == address(0) || order.taker == msg.sender);
+ require(order.makerTokenAmount > 0 && order.takerTokenAmount > 0 && fillTakerTokenAmount > 0);
+ require(isValidSignature(
+ order.maker,
+ order.orderHash,
+ v,
+ r,
+ s
+ ));
+
+ if (block.timestamp >= order.expirationTimestampInSec) {
+ LogError(uint8(Errors.ORDER_EXPIRED), order.orderHash);
+ return 0;
+ }
+
+ uint remainingTakerTokenAmount = safeSub(order.takerTokenAmount, getUnavailableTakerTokenAmount(order.orderHash));
+ filledTakerTokenAmount = min256(fillTakerTokenAmount, remainingTakerTokenAmount);
+ if (filledTakerTokenAmount == 0) {
+ LogError(uint8(Errors.ORDER_FULLY_FILLED_OR_CANCELLED), order.orderHash);
+ return 0;
+ }
+
+ if (isRoundingError(filledTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount)) {
+ LogError(uint8(Errors.ROUNDING_ERROR_TOO_LARGE), order.orderHash);
+ return 0;
+ }
+
+ if (!shouldThrowOnInsufficientBalanceOrAllowance && !isTransferable(order, filledTakerTokenAmount)) {
+ LogError(uint8(Errors.INSUFFICIENT_BALANCE_OR_ALLOWANCE), order.orderHash);
+ return 0;
+ }
+
+ uint filledMakerTokenAmount = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount);
+ uint paidMakerFee;
+ uint paidTakerFee;
+ filled[order.orderHash] = safeAdd(filled[order.orderHash], filledTakerTokenAmount);
+ require(transferViaTokenTransferProxy(
+ order.makerToken,
+ order.maker,
+ msg.sender,
+ filledMakerTokenAmount
+ ));
+ require(transferViaTokenTransferProxy(
+ order.takerToken,
+ msg.sender,
+ order.maker,
+ filledTakerTokenAmount
+ ));
+ if (order.feeRecipient != address(0)) {
+ if (order.makerFee > 0) {
+ paidMakerFee = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.makerFee);
+ require(transferViaTokenTransferProxy(
+ ZRX_TOKEN_CONTRACT,
+ order.maker,
+ order.feeRecipient,
+ paidMakerFee
+ ));
+ }
+ if (order.takerFee > 0) {
+ paidTakerFee = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.takerFee);
+ require(transferViaTokenTransferProxy(
+ ZRX_TOKEN_CONTRACT,
+ msg.sender,
+ order.feeRecipient,
+ paidTakerFee
+ ));
+ }
+ }
+
+ LogFill(
+ order.maker,
+ msg.sender,
+ order.feeRecipient,
+ order.makerToken,
+ order.takerToken,
+ filledMakerTokenAmount,
+ filledTakerTokenAmount,
+ paidMakerFee,
+ paidTakerFee,
+ keccak256(order.makerToken, order.takerToken),
+ order.orderHash
+ );
+ return filledTakerTokenAmount;
+ }
+
+ /// @dev Cancels the input order.
+ /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
+ /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
+ /// @param cancelTakerTokenAmount Desired amount of takerToken to cancel in order.
+ /// @return Amount of takerToken cancelled.
+ function cancelOrder(
+ address[5] orderAddresses,
+ uint[6] orderValues,
+ uint cancelTakerTokenAmount)
+ public
+ returns (uint)
+ {
+ Order memory order = Order({
+ maker: orderAddresses[0],
+ taker: orderAddresses[1],
+ makerToken: orderAddresses[2],
+ takerToken: orderAddresses[3],
+ feeRecipient: orderAddresses[4],
+ makerTokenAmount: orderValues[0],
+ takerTokenAmount: orderValues[1],
+ makerFee: orderValues[2],
+ takerFee: orderValues[3],
+ expirationTimestampInSec: orderValues[4],
+ orderHash: getOrderHash(orderAddresses, orderValues)
+ });
+
+ require(order.maker == msg.sender);
+ require(order.makerTokenAmount > 0 && order.takerTokenAmount > 0 && cancelTakerTokenAmount > 0);
+
+ if (block.timestamp >= order.expirationTimestampInSec) {
+ LogError(uint8(Errors.ORDER_EXPIRED), order.orderHash);
+ return 0;
+ }
+
+ uint remainingTakerTokenAmount = safeSub(order.takerTokenAmount, getUnavailableTakerTokenAmount(order.orderHash));
+ uint cancelledTakerTokenAmount = min256(cancelTakerTokenAmount, remainingTakerTokenAmount);
+ if (cancelledTakerTokenAmount == 0) {
+ LogError(uint8(Errors.ORDER_FULLY_FILLED_OR_CANCELLED), order.orderHash);
+ return 0;
+ }
+
+ cancelled[order.orderHash] = safeAdd(cancelled[order.orderHash], cancelledTakerTokenAmount);
+
+ LogCancel(
+ order.maker,
+ order.feeRecipient,
+ order.makerToken,
+ order.takerToken,
+ getPartialAmount(cancelledTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount),
+ cancelledTakerTokenAmount,
+ keccak256(order.makerToken, order.takerToken),
+ order.orderHash
+ );
+ return cancelledTakerTokenAmount;
+ }
+
+ /*
+ * Wrapper functions
+ */
+
+ /// @dev Fills an order with specified parameters and ECDSA signature, throws if specified amount not filled entirely.
+ /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
+ /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
+ /// @param fillTakerTokenAmount Desired amount of takerToken to fill.
+ /// @param v ECDSA signature parameter v.
+ /// @param r ECDSA signature parameters r.
+ /// @param s ECDSA signature parameters s.
+ function fillOrKillOrder(
+ address[5] orderAddresses,
+ uint[6] orderValues,
+ uint fillTakerTokenAmount,
+ uint8 v,
+ bytes32 r,
+ bytes32 s)
+ public
+ {
+ require(fillOrder(
+ orderAddresses,
+ orderValues,
+ fillTakerTokenAmount,
+ false,
+ v,
+ r,
+ s
+ ) == fillTakerTokenAmount);
+ }
+
+ /// @dev Synchronously executes multiple fill orders in a single transaction.
+ /// @param orderAddresses Array of address arrays containing individual order addresses.
+ /// @param orderValues Array of uint arrays containing individual order values.
+ /// @param fillTakerTokenAmounts Array of desired amounts of takerToken to fill in orders.
+ /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfers will fail before attempting.
+ /// @param v Array ECDSA signature v parameters.
+ /// @param r Array of ECDSA signature r parameters.
+ /// @param s Array of ECDSA signature s parameters.
+ function batchFillOrders(
+ address[5][] orderAddresses,
+ uint[6][] orderValues,
+ uint[] fillTakerTokenAmounts,
+ bool shouldThrowOnInsufficientBalanceOrAllowance,
+ uint8[] v,
+ bytes32[] r,
+ bytes32[] s)
+ public
+ {
+ for (uint i = 0; i < orderAddresses.length; i++) {
+ fillOrder(
+ orderAddresses[i],
+ orderValues[i],
+ fillTakerTokenAmounts[i],
+ shouldThrowOnInsufficientBalanceOrAllowance,
+ v[i],
+ r[i],
+ s[i]
+ );
+ }
+ }
+
+ /// @dev Synchronously executes multiple fillOrKill orders in a single transaction.
+ /// @param orderAddresses Array of address arrays containing individual order addresses.
+ /// @param orderValues Array of uint arrays containing individual order values.
+ /// @param fillTakerTokenAmounts Array of desired amounts of takerToken to fill in orders.
+ /// @param v Array ECDSA signature v parameters.
+ /// @param r Array of ECDSA signature r parameters.
+ /// @param s Array of ECDSA signature s parameters.
+ function batchFillOrKillOrders(
+ address[5][] orderAddresses,
+ uint[6][] orderValues,
+ uint[] fillTakerTokenAmounts,
+ uint8[] v,
+ bytes32[] r,
+ bytes32[] s)
+ public
+ {
+ for (uint i = 0; i < orderAddresses.length; i++) {
+ fillOrKillOrder(
+ orderAddresses[i],
+ orderValues[i],
+ fillTakerTokenAmounts[i],
+ v[i],
+ r[i],
+ s[i]
+ );
+ }
+ }
+
+ /// @dev Synchronously executes multiple fill orders in a single transaction until total fillTakerTokenAmount filled.
+ /// @param orderAddresses Array of address arrays containing individual order addresses.
+ /// @param orderValues Array of uint arrays containing individual order values.
+ /// @param fillTakerTokenAmount Desired total amount of takerToken to fill in orders.
+ /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfers will fail before attempting.
+ /// @param v Array ECDSA signature v parameters.
+ /// @param r Array of ECDSA signature r parameters.
+ /// @param s Array of ECDSA signature s parameters.
+ /// @return Total amount of fillTakerTokenAmount filled in orders.
+ function fillOrdersUpTo(
+ address[5][] orderAddresses,
+ uint[6][] orderValues,
+ uint fillTakerTokenAmount,
+ bool shouldThrowOnInsufficientBalanceOrAllowance,
+ uint8[] v,
+ bytes32[] r,
+ bytes32[] s)
+ public
+ returns (uint)
+ {
+ uint filledTakerTokenAmount = 0;
+ for (uint i = 0; i < orderAddresses.length; i++) {
+ require(orderAddresses[i][3] == orderAddresses[0][3]); // takerToken must be the same for each order
+ filledTakerTokenAmount = safeAdd(filledTakerTokenAmount, fillOrder(
+ orderAddresses[i],
+ orderValues[i],
+ safeSub(fillTakerTokenAmount, filledTakerTokenAmount),
+ shouldThrowOnInsufficientBalanceOrAllowance,
+ v[i],
+ r[i],
+ s[i]
+ ));
+ if (filledTakerTokenAmount == fillTakerTokenAmount) break;
+ }
+ return filledTakerTokenAmount;
+ }
+
+ /// @dev Synchronously cancels multiple orders in a single transaction.
+ /// @param orderAddresses Array of address arrays containing individual order addresses.
+ /// @param orderValues Array of uint arrays containing individual order values.
+ /// @param cancelTakerTokenAmounts Array of desired amounts of takerToken to cancel in orders.
+ function batchCancelOrders(
+ address[5][] orderAddresses,
+ uint[6][] orderValues,
+ uint[] cancelTakerTokenAmounts)
+ public
+ {
+ for (uint i = 0; i < orderAddresses.length; i++) {
+ cancelOrder(
+ orderAddresses[i],
+ orderValues[i],
+ cancelTakerTokenAmounts[i]
+ );
+ }
+ }
+
+ /*
+ * Constant public functions
+ */
+
+ /// @dev Calculates Keccak-256 hash of order with specified parameters.
+ /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
+ /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
+ /// @return Keccak-256 hash of order.
+ function getOrderHash(address[5] orderAddresses, uint[6] orderValues)
+ public
+ constant
+ returns (bytes32)
+ {
+ return keccak256(
+ address(this),
+ orderAddresses[0], // maker
+ orderAddresses[1], // taker
+ orderAddresses[2], // makerToken
+ orderAddresses[3], // takerToken
+ orderAddresses[4], // feeRecipient
+ orderValues[0], // makerTokenAmount
+ orderValues[1], // takerTokenAmount
+ orderValues[2], // makerFee
+ orderValues[3], // takerFee
+ orderValues[4], // expirationTimestampInSec
+ orderValues[5] // salt
+ );
+ }
+
+ /// @dev Verifies that an order signature is valid.
+ /// @param signer address of signer.
+ /// @param hash Signed Keccak-256 hash.
+ /// @param v ECDSA signature parameter v.
+ /// @param r ECDSA signature parameters r.
+ /// @param s ECDSA signature parameters s.
+ /// @return Validity of order signature.
+ function isValidSignature(
+ address signer,
+ bytes32 hash,
+ uint8 v,
+ bytes32 r,
+ bytes32 s)
+ public
+ constant
+ returns (bool)
+ {
+ return signer == ecrecover(
+ keccak256("\x19Ethereum Signed Message:\n32", hash),
+ v,
+ r,
+ s
+ );
+ }
+
+ /// @dev Checks if rounding error > 0.1%.
+ /// @param numerator Numerator.
+ /// @param denominator Denominator.
+ /// @param target Value to multiply with numerator/denominator.
+ /// @return Rounding error is present.
+ function isRoundingError(uint numerator, uint denominator, uint target)
+ public
+ constant
+ returns (bool)
+ {
+ uint remainder = mulmod(target, numerator, denominator);
+ if (remainder == 0) return false; // No rounding error.
+
+ uint errPercentageTimes1000000 = safeDiv(
+ safeMul(remainder, 1000000),
+ safeMul(numerator, target)
+ );
+ return errPercentageTimes1000000 > 1000;
+ }
+
+ /// @dev Calculates partial value given a numerator and denominator.
+ /// @param numerator Numerator.
+ /// @param denominator Denominator.
+ /// @param target Value to calculate partial of.
+ /// @return Partial value of target.
+ function getPartialAmount(uint numerator, uint denominator, uint target)
+ public
+ constant
+ returns (uint)
+ {
+ return safeDiv(safeMul(numerator, target), denominator);
+ }
+
+ /// @dev Calculates the sum of values already filled and cancelled for a given order.
+ /// @param orderHash The Keccak-256 hash of the given order.
+ /// @return Sum of values already filled and cancelled.
+ function getUnavailableTakerTokenAmount(bytes32 orderHash)
+ public
+ constant
+ returns (uint)
+ {
+ return safeAdd(filled[orderHash], cancelled[orderHash]);
+ }
+
+
+ /*
+ * Internal functions
+ */
+
+ /// @dev Transfers a token using TokenTransferProxy transferFrom function.
+ /// @param token Address of token to transferFrom.
+ /// @param from Address transfering token.
+ /// @param to Address receiving token.
+ /// @param value Amount of token to transfer.
+ /// @return Success of token transfer.
+ function transferViaTokenTransferProxy(
+ address token,
+ address from,
+ address to,
+ uint value)
+ internal
+ returns (bool)
+ {
+ return TokenTransferProxy(TOKEN_TRANSFER_PROXY_CONTRACT).transferFrom(token, from, to, value);
+ }
+
+ /// @dev Checks if any order transfers will fail.
+ /// @param order Order struct of params that will be checked.
+ /// @param fillTakerTokenAmount Desired amount of takerToken to fill.
+ /// @return Predicted result of transfers.
+ function isTransferable(Order order, uint fillTakerTokenAmount)
+ internal
+ constant // The called token contracts may attempt to change state, but will not be able to due to gas limits on getBalance and getAllowance.
+ returns (bool)
+ {
+ address taker = msg.sender;
+ uint fillMakerTokenAmount = getPartialAmount(fillTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount);
+
+ if (order.feeRecipient != address(0)) {
+ bool isMakerTokenZRX = order.makerToken == ZRX_TOKEN_CONTRACT;
+ bool isTakerTokenZRX = order.takerToken == ZRX_TOKEN_CONTRACT;
+ uint paidMakerFee = getPartialAmount(fillTakerTokenAmount, order.takerTokenAmount, order.makerFee);
+ uint paidTakerFee = getPartialAmount(fillTakerTokenAmount, order.takerTokenAmount, order.takerFee);
+ uint requiredMakerZRX = isMakerTokenZRX ? safeAdd(fillMakerTokenAmount, paidMakerFee) : paidMakerFee;
+ uint requiredTakerZRX = isTakerTokenZRX ? safeAdd(fillTakerTokenAmount, paidTakerFee) : paidTakerFee;
+
+ if ( getBalance(ZRX_TOKEN_CONTRACT, order.maker) < requiredMakerZRX
+ || getAllowance(ZRX_TOKEN_CONTRACT, order.maker) < requiredMakerZRX
+ || getBalance(ZRX_TOKEN_CONTRACT, taker) < requiredTakerZRX
+ || getAllowance(ZRX_TOKEN_CONTRACT, taker) < requiredTakerZRX
+ ) return false;
+
+ if (!isMakerTokenZRX && ( getBalance(order.makerToken, order.maker) < fillMakerTokenAmount // Don't double check makerToken if ZRX
+ || getAllowance(order.makerToken, order.maker) < fillMakerTokenAmount)
+ ) return false;
+ if (!isTakerTokenZRX && ( getBalance(order.takerToken, taker) < fillTakerTokenAmount // Don't double check takerToken if ZRX
+ || getAllowance(order.takerToken, taker) < fillTakerTokenAmount)
+ ) return false;
+ } else if ( getBalance(order.makerToken, order.maker) < fillMakerTokenAmount
+ || getAllowance(order.makerToken, order.maker) < fillMakerTokenAmount
+ || getBalance(order.takerToken, taker) < fillTakerTokenAmount
+ || getAllowance(order.takerToken, taker) < fillTakerTokenAmount
+ ) return false;
+
+ return true;
+ }
+
+ /// @dev Get token balance of an address.
+ /// @param token Address of token.
+ /// @param owner Address of owner.
+ /// @return Token balance of owner.
+ function getBalance(address token, address owner)
+ internal
+ constant // The called token contract may attempt to change state, but will not be able to due to an added gas limit.
+ returns (uint)
+ {
+ return Token(token).balanceOf.gas(EXTERNAL_QUERY_GAS_LIMIT)(owner); // Limit gas to prevent reentrancy
+ }
+
+ /// @dev Get allowance of token given to TokenTransferProxy by an address.
+ /// @param token Address of token.
+ /// @param owner Address of owner.
+ /// @return Allowance of token given to TokenTransferProxy by owner.
+ function getAllowance(address token, address owner)
+ internal
+ constant // The called token contract may attempt to change state, but will not be able to due to an added gas limit.
+ returns (uint)
+ {
+ return Token(token).allowance.gas(EXTERNAL_QUERY_GAS_LIMIT)(owner, TOKEN_TRANSFER_PROXY_CONTRACT); // Limit gas to prevent reentrancy
+ }
+}
diff --git a/packages/contracts/src/contracts/previous/Exchange/IExchange_v1.sol b/packages/contracts/src/contracts/previous/Exchange/IExchange_v1.sol
new file mode 100644
index 000000000..ec4bce7b0
--- /dev/null
+++ b/packages/contracts/src/contracts/previous/Exchange/IExchange_v1.sol
@@ -0,0 +1,226 @@
+pragma solidity ^0.4.19;
+
+contract IExchange_v1 {
+
+ // Error Codes
+ enum Errors {
+ ORDER_EXPIRED, // Order has already expired
+ ORDER_FULLY_FILLED_OR_CANCELLED, // Order has already been fully filled or cancelled
+ ROUNDING_ERROR_TOO_LARGE, // Rounding error too large
+ INSUFFICIENT_BALANCE_OR_ALLOWANCE // Insufficient balance or allowance for token transfer
+ }
+
+ event LogError(uint8 indexed errorId, bytes32 indexed orderHash);
+
+ event LogFill(
+ address indexed maker,
+ address taker,
+ address indexed feeRecipient,
+ address makerToken,
+ address takerToken,
+ uint filledMakerTokenAmount,
+ uint filledTakerTokenAmount,
+ uint paidMakerFee,
+ uint paidTakerFee,
+ bytes32 indexed tokens, // keccak256(makerToken, takerToken), allows subscribing to a token pair
+ bytes32 orderHash
+ );
+
+ event LogCancel(
+ address indexed maker,
+ address indexed feeRecipient,
+ address makerToken,
+ address takerToken,
+ uint cancelledMakerTokenAmount,
+ uint cancelledTakerTokenAmount,
+ bytes32 indexed tokens,
+ bytes32 orderHash
+ );
+
+ function ZRX_TOKEN_CONTRACT()
+ public view
+ returns (address);
+
+ function TOKEN_TRANSFER_PROXY_CONTRACT()
+ public view
+ returns (address);
+
+ function EXTERNAL_QUERY_GAS_LIMIT()
+ public view
+ returns (uint16);
+
+ function VERSION()
+ public view
+ returns (string);
+
+ function filled(bytes32)
+ public view
+ returns (uint256);
+
+ function cancelled(bytes32)
+ public view
+ returns (uint256);
+
+ /// @dev Calculates the sum of values already filled and cancelled for a given order.
+ /// @param orderHash The Keccak-256 hash of the given order.
+ /// @return Sum of values already filled and cancelled.
+ function getUnavailableTakerTokenAmount(bytes32 orderHash)
+ public constant
+ returns (uint);
+
+ /// @dev Calculates partial value given a numerator and denominator.
+ /// @param numerator Numerator.
+ /// @param denominator Denominator.
+ /// @param target Value to calculate partial of.
+ /// @return Partial value of target.
+ function getPartialAmount(uint numerator, uint denominator, uint target)
+ public constant
+ returns (uint);
+
+ /// @dev Checks if rounding error > 0.1%.
+ /// @param numerator Numerator.
+ /// @param denominator Denominator.
+ /// @param target Value to multiply with numerator/denominator.
+ /// @return Rounding error is present.
+ function isRoundingError(uint numerator, uint denominator, uint target)
+ public constant
+ returns (bool);
+
+ /// @dev Calculates Keccak-256 hash of order with specified parameters.
+ /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
+ /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
+ /// @return Keccak-256 hash of order.
+ function getOrderHash(address[5] orderAddresses, uint[6] orderValues)
+ public
+ constant
+ returns (bytes32);
+
+ /// @dev Verifies that an order signature is valid.
+ /// @param signer address of signer.
+ /// @param hash Signed Keccak-256 hash.
+ /// @param v ECDSA signature parameter v.
+ /// @param r ECDSA signature parameters r.
+ /// @param s ECDSA signature parameters s.
+ /// @return Validity of order signature.
+ function isValidSignature(
+ address signer,
+ bytes32 hash,
+ uint8 v,
+ bytes32 r,
+ bytes32 s)
+ public constant
+ returns (bool);
+
+ /// @dev Fills the input order.
+ /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
+ /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
+ /// @param fillTakerTokenAmount Desired amount of takerToken to fill.
+ /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfer will fail before attempting.
+ /// @param v ECDSA signature parameter v.
+ /// @param r ECDSA signature parameters r.
+ /// @param s ECDSA signature parameters s.
+ /// @return Total amount of takerToken filled in trade.
+ function fillOrder(
+ address[5] orderAddresses,
+ uint[6] orderValues,
+ uint fillTakerTokenAmount,
+ bool shouldThrowOnInsufficientBalanceOrAllowance,
+ uint8 v,
+ bytes32 r,
+ bytes32 s)
+ public
+ returns (uint filledTakerTokenAmount);
+
+ /// @dev Cancels the input order.
+ /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
+ /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
+ /// @param cancelTakerTokenAmount Desired amount of takerToken to cancel in order.
+ /// @return Amount of takerToken cancelled.
+ function cancelOrder(
+ address[5] orderAddresses,
+ uint[6] orderValues,
+ uint cancelTakerTokenAmount)
+ public
+ returns (uint);
+
+
+ /// @dev Fills an order with specified parameters and ECDSA signature, throws if specified amount not filled entirely.
+ /// @param orderAddresses Array of order's maker, taker, makerToken, takerToken, and feeRecipient.
+ /// @param orderValues Array of order's makerTokenAmount, takerTokenAmount, makerFee, takerFee, expirationTimestampInSec, and salt.
+ /// @param fillTakerTokenAmount Desired amount of takerToken to fill.
+ /// @param v ECDSA signature parameter v.
+ /// @param r ECDSA signature parameters r.
+ /// @param s ECDSA signature parameters s.
+ function fillOrKillOrder(
+ address[5] orderAddresses,
+ uint[6] orderValues,
+ uint fillTakerTokenAmount,
+ uint8 v,
+ bytes32 r,
+ bytes32 s)
+ public;
+
+ /// @dev Synchronously executes multiple fill orders in a single transaction.
+ /// @param orderAddresses Array of address arrays containing individual order addresses.
+ /// @param orderValues Array of uint arrays containing individual order values.
+ /// @param fillTakerTokenAmounts Array of desired amounts of takerToken to fill in orders.
+ /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfers will fail before attempting.
+ /// @param v Array ECDSA signature v parameters.
+ /// @param r Array of ECDSA signature r parameters.
+ /// @param s Array of ECDSA signature s parameters.
+ function batchFillOrders(
+ address[5][] orderAddresses,
+ uint[6][] orderValues,
+ uint[] fillTakerTokenAmounts,
+ bool shouldThrowOnInsufficientBalanceOrAllowance,
+ uint8[] v,
+ bytes32[] r,
+ bytes32[] s)
+ public;
+
+ /// @dev Synchronously executes multiple fillOrKill orders in a single transaction.
+ /// @param orderAddresses Array of address arrays containing individual order addresses.
+ /// @param orderValues Array of uint arrays containing individual order values.
+ /// @param fillTakerTokenAmounts Array of desired amounts of takerToken to fill in orders.
+ /// @param v Array ECDSA signature v parameters.
+ /// @param r Array of ECDSA signature r parameters.
+ /// @param s Array of ECDSA signature s parameters.
+ function batchFillOrKillOrders(
+ address[5][] orderAddresses,
+ uint[6][] orderValues,
+ uint[] fillTakerTokenAmounts,
+ uint8[] v,
+ bytes32[] r,
+ bytes32[] s)
+ public;
+
+ /// @dev Synchronously executes multiple fill orders in a single transaction until total fillTakerTokenAmount filled.
+ /// @param orderAddresses Array of address arrays containing individual order addresses.
+ /// @param orderValues Array of uint arrays containing individual order values.
+ /// @param fillTakerTokenAmount Desired total amount of takerToken to fill in orders.
+ /// @param shouldThrowOnInsufficientBalanceOrAllowance Test if transfers will fail before attempting.
+ /// @param v Array ECDSA signature v parameters.
+ /// @param r Array of ECDSA signature r parameters.
+ /// @param s Array of ECDSA signature s parameters.
+ /// @return Total amount of fillTakerTokenAmount filled in orders.
+ function fillOrdersUpTo(
+ address[5][] orderAddresses,
+ uint[6][] orderValues,
+ uint fillTakerTokenAmount,
+ bool shouldThrowOnInsufficientBalanceOrAllowance,
+ uint8[] v,
+ bytes32[] r,
+ bytes32[] s)
+ public
+ returns (uint);
+
+ /// @dev Synchronously cancels multiple orders in a single transaction.
+ /// @param orderAddresses Array of address arrays containing individual order addresses.
+ /// @param orderValues Array of uint arrays containing individual order values.
+ /// @param cancelTakerTokenAmounts Array of desired amounts of takerToken to cancel in orders.
+ function batchCancelOrders(
+ address[5][] orderAddresses,
+ uint[6][] orderValues,
+ uint[] cancelTakerTokenAmounts)
+ public;
+}
diff --git a/packages/contracts/src/contracts/previous/Ownable/IOwnable_v1.sol b/packages/contracts/src/contracts/previous/Ownable/IOwnable_v1.sol
new file mode 100644
index 000000000..7e22d544d
--- /dev/null
+++ b/packages/contracts/src/contracts/previous/Ownable/IOwnable_v1.sol
@@ -0,0 +1,18 @@
+pragma solidity ^0.4.19;
+
+/*
+ * Ownable
+ *
+ * Base contract with an owner.
+ * Provides onlyOwner modifier, which prevents function from running if it is called by anyone other than the owner.
+ */
+
+contract IOwnable_v1 {
+
+ function owner()
+ public view
+ returns (address);
+
+ function transferOwnership(address newOwner)
+ public;
+}
diff --git a/packages/contracts/src/contracts/previous/TokenRegistry/ITokenRegistery.sol b/packages/contracts/src/contracts/previous/TokenRegistry/ITokenRegistery.sol
new file mode 100644
index 000000000..b8bdaf3b9
--- /dev/null
+++ b/packages/contracts/src/contracts/previous/TokenRegistry/ITokenRegistery.sol
@@ -0,0 +1,195 @@
+/*
+
+ 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 { IOwnable_v1 as IOwnable } from "../Ownable/IOwnable_v1.sol";
+
+/// @title Token Registry - Stores metadata associated with ERC20 tokens. See ERC22 https://github.com/ethereum/EIPs/issues/22
+/// @author Amir Bandeali - <amir@0xProject.com>, Will Warren - <will@0xProject.com>
+contract ITokenRegistery is IOwnable {
+
+ event LogAddToken(
+ address indexed token,
+ string name,
+ string symbol,
+ uint8 decimals,
+ bytes ipfsHash,
+ bytes swarmHash
+ );
+
+ event LogRemoveToken(
+ address indexed token,
+ string name,
+ string symbol,
+ uint8 decimals,
+ bytes ipfsHash,
+ bytes swarmHash
+ );
+
+ event LogTokenNameChange(
+ address indexed token,
+ string oldName,
+ string newName
+ );
+
+ event LogTokenSymbolChange(
+ address indexed token,
+ string oldSymbol,
+ string newSymbol
+ );
+
+ event LogTokenIpfsHashChange(
+ address indexed token,
+ bytes oldIpfsHash,
+ bytes newIpfsHash
+ );
+
+ event LogTokenSwarmHashChange(
+ address indexed token,
+ bytes oldSwarmHash,
+ bytes newSwarmHash
+ );
+
+ function tokens(address tokenAddress)
+ public view
+ returns (
+ address token,
+ string name,
+ string symbol,
+ uint8 decimals,
+ bytes ipfsHash,
+ bytes swarmHash
+ );
+
+ function tokenAddresses(uint256 index)
+ public view
+ returns (address);
+
+
+ /// @dev Allows owner to add a new token to the registry.
+ /// @param _token Address of new token.
+ /// @param _name Name of new token.
+ /// @param _symbol Symbol for new token.
+ /// @param _decimals Number of decimals, divisibility of new token.
+ /// @param _ipfsHash IPFS hash of token icon.
+ /// @param _swarmHash Swarm hash of token icon.
+ function addToken(
+ address _token,
+ string _name,
+ string _symbol,
+ uint8 _decimals,
+ bytes _ipfsHash,
+ bytes _swarmHash)
+ public;
+
+ /// @dev Allows owner to remove an existing token from the registry.
+ /// @param _token Address of existing token.
+ function removeToken(address _token, uint _index)
+ public;
+
+ /// @dev Allows owner to modify an existing token's name.
+ /// @param _token Address of existing token.
+ /// @param _name New name.
+ function setTokenName(address _token, string _name)
+ public;
+
+ /// @dev Allows owner to modify an existing token's symbol.
+ /// @param _token Address of existing token.
+ /// @param _symbol New symbol.
+ function setTokenSymbol(address _token, string _symbol)
+ public;
+
+ /// @dev Allows owner to modify an existing token's IPFS hash.
+ /// @param _token Address of existing token.
+ /// @param _ipfsHash New IPFS hash.
+ function setTokenIpfsHash(address _token, bytes _ipfsHash)
+ public;
+
+ /// @dev Allows owner to modify an existing token's Swarm hash.
+ /// @param _token Address of existing token.
+ /// @param _swarmHash New Swarm hash.
+ function setTokenSwarmHash(address _token, bytes _swarmHash)
+ public;
+
+ /*
+ * Web3 call functions
+ */
+
+ /// @dev Provides a registered token's address when given the token symbol.
+ /// @param _symbol Symbol of registered token.
+ /// @return Token's address.
+ function getTokenAddressBySymbol(string _symbol)
+ public constant
+ returns (address);
+
+ /// @dev Provides a registered token's address when given the token name.
+ /// @param _name Name of registered token.
+ /// @return Token's address.
+ function getTokenAddressByName(string _name)
+ public constant
+ returns (address);
+
+ /// @dev Provides a registered token's metadata, looked up by address.
+ /// @param _token Address of registered token.
+ /// @return Token metadata.
+ function getTokenMetaData(address _token)
+ public constant
+ returns (
+ address, //tokenAddress
+ string, //name
+ string, //symbol
+ uint8, //decimals
+ bytes, //ipfsHash
+ bytes //swarmHash
+ );
+
+ /// @dev Provides a registered token's metadata, looked up by name.
+ /// @param _name Name of registered token.
+ /// @return Token metadata.
+ function getTokenByName(string _name)
+ public constant
+ returns (
+ address, //tokenAddress
+ string, //name
+ string, //symbol
+ uint8, //decimals
+ bytes, //ipfsHash
+ bytes //swarmHash
+ );
+
+ /// @dev Provides a registered token's metadata, looked up by symbol.
+ /// @param _symbol Symbol of registered token.
+ /// @return Token metadata.
+ function getTokenBySymbol(string _symbol)
+ public constant
+ returns (
+ address, //tokenAddress
+ string, //name
+ string, //symbol
+ uint8, //decimals
+ bytes, //ipfsHash
+ bytes //swarmHash
+ );
+
+ /// @dev Returns an array containing all token addresses.
+ /// @return Array of token addresses.
+ function getTokenAddresses()
+ public constant
+ returns (address[]);
+}
diff --git a/packages/contracts/src/contracts/current/protocol/TokenRegistry/TokenRegistry.sol b/packages/contracts/src/contracts/previous/TokenRegistry/TokenRegistry.sol
index 3bd2fbfaf..7417a10a3 100644
--- a/packages/contracts/src/contracts/current/protocol/TokenRegistry/TokenRegistry.sol
+++ b/packages/contracts/src/contracts/previous/TokenRegistry/TokenRegistry.sol
@@ -1,6 +1,6 @@
/*
- Copyright 2017 ZeroEx Intl.
+ 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.
@@ -18,7 +18,7 @@
pragma solidity ^0.4.11;
-import { Ownable_v1 as Ownable } from "../../../previous/Ownable/Ownable_v1.sol";
+import { Ownable_v1 as Ownable } from "../Ownable/Ownable_v1.sol";
/// @title Token Registry - Stores metadata associated with ERC20 tokens. See ERC22 https://github.com/ethereum/EIPs/issues/22
/// @author Amir Bandeali - <amir@0xProject.com>, Will Warren - <will@0xProject.com>
diff --git a/packages/contracts/src/contracts/current/protocol/TokenTransferProxy/TokenTransferProxy.sol b/packages/contracts/src/contracts/previous/TokenTransferProxy/TokenTransferProxy_v1.sol
index 1ce949fa6..e3659d8ba 100644
--- a/packages/contracts/src/contracts/current/protocol/TokenTransferProxy/TokenTransferProxy.sol
+++ b/packages/contracts/src/contracts/previous/TokenTransferProxy/TokenTransferProxy_v1.sol
@@ -1,6 +1,6 @@
/*
- Copyright 2017 ZeroEx Intl.
+ 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.
@@ -18,12 +18,12 @@
pragma solidity ^0.4.11;
-import { Token_v1 as Token } from "../../../previous/Token/Token_v1.sol";
-import { Ownable_v1 as Ownable } from "../../../previous/Ownable/Ownable_v1.sol";
+import { Token_v1 as Token } from "../Token/Token_v1.sol";
+import { Ownable_v1 as Ownable } from "../Ownable/Ownable_v1.sol";
/// @title TokenTransferProxy - Transfers tokens on behalf of contracts that have been approved via decentralized governance.
/// @author Amir Bandeali - <amir@0xProject.com>, Will Warren - <will@0xProject.com>
-contract TokenTransferProxy is Ownable {
+contract TokenTransferProxy_v1 is Ownable {
/// @dev Only authorized addresses can invoke functions with this modifier.
modifier onlyAuthorized {
diff --git a/packages/contracts/src/contracts/previous/UnlimitedAllowanceToken/UnlimitedAllowanceToken_v1.sol b/packages/contracts/src/contracts/previous/UnlimitedAllowanceToken/UnlimitedAllowanceToken_v1.sol
index 6376f3f2c..46379c43d 100644
--- a/packages/contracts/src/contracts/previous/UnlimitedAllowanceToken/UnlimitedAllowanceToken_v1.sol
+++ b/packages/contracts/src/contracts/previous/UnlimitedAllowanceToken/UnlimitedAllowanceToken_v1.sol
@@ -1,6 +1,6 @@
/*
- Copyright 2017 ZeroEx Intl.
+ 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.
diff --git a/packages/contracts/src/utils/address_utils.ts b/packages/contracts/src/utils/address_utils.ts
new file mode 100644
index 000000000..01a7a6fd4
--- /dev/null
+++ b/packages/contracts/src/utils/address_utils.ts
@@ -0,0 +1,12 @@
+import { ZeroEx } from '0x.js';
+
+import { crypto } from './crypto';
+
+export const addressUtils = {
+ generatePseudoRandomAddress(): string {
+ const randomBigNum = ZeroEx.generatePseudoRandomSalt();
+ const randomBuff = crypto.solSHA3([randomBigNum]);
+ const randomAddress = `0x${randomBuff.slice(0, 20).toString('hex')}`;
+ return randomAddress;
+ },
+};
diff --git a/packages/contracts/src/utils/artifacts.ts b/packages/contracts/src/utils/artifacts.ts
new file mode 100644
index 000000000..caf5b94fd
--- /dev/null
+++ b/packages/contracts/src/utils/artifacts.ts
@@ -0,0 +1,37 @@
+import { ContractArtifact } from '@0xproject/sol-compiler';
+
+import * as DummyERC20Token from '../artifacts/DummyERC20Token.json';
+import * as DummyERC721Token from '../artifacts/DummyERC721Token.json';
+import * as ERC20Proxy from '../artifacts/ERC20Proxy.json';
+import * as ERC721Proxy from '../artifacts/ERC721Proxy.json';
+import * as Exchange from '../artifacts/Exchange.json';
+import * as MixinAuthorizable from '../artifacts/MixinAuthorizable.json';
+import * as MultiSigWallet from '../artifacts/MultiSigWallet.json';
+import * as MultiSigWalletWithTimeLock from '../artifacts/MultiSigWalletWithTimeLock.json';
+import * as MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress from '../artifacts/MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.json';
+import * as TestAssetProxyDispatcher from '../artifacts/TestAssetProxyDispatcher.json';
+import * as TestLibBytes from '../artifacts/TestLibBytes.json';
+import * as TestLibs from '../artifacts/TestLibs.json';
+import * as TestSignatureValidator from '../artifacts/TestSignatureValidator.json';
+import * as TokenRegistry from '../artifacts/TokenRegistry.json';
+import * as EtherToken from '../artifacts/WETH9.json';
+import * as ZRX from '../artifacts/ZRXToken.json';
+
+export const artifacts = {
+ DummyERC20Token: (DummyERC20Token as any) as ContractArtifact,
+ DummyERC721Token: (DummyERC721Token as any) as ContractArtifact,
+ ERC20Proxy: (ERC20Proxy as any) as ContractArtifact,
+ ERC721Proxy: (ERC721Proxy as any) as ContractArtifact,
+ Exchange: (Exchange as any) as ContractArtifact,
+ EtherToken: (EtherToken as any) as ContractArtifact,
+ MixinAuthorizable: (MixinAuthorizable as any) as ContractArtifact,
+ MultiSigWallet: (MultiSigWallet as any) as ContractArtifact,
+ MultiSigWalletWithTimeLock: (MultiSigWalletWithTimeLock as any) as ContractArtifact,
+ MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress: (MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress as any) as ContractArtifact,
+ TestAssetProxyDispatcher: (TestAssetProxyDispatcher as any) as ContractArtifact,
+ TestLibBytes: (TestLibBytes as any) as ContractArtifact,
+ TestLibs: (TestLibs as any) as ContractArtifact,
+ TestSignatureValidator: (TestSignatureValidator as any) as ContractArtifact,
+ TokenRegistry: (TokenRegistry as any) as ContractArtifact,
+ ZRX: (ZRX as any) as ContractArtifact,
+};
diff --git a/packages/contracts/src/utils/asset_proxy_utils.ts b/packages/contracts/src/utils/asset_proxy_utils.ts
new file mode 100644
index 000000000..1a7a312dd
--- /dev/null
+++ b/packages/contracts/src/utils/asset_proxy_utils.ts
@@ -0,0 +1,38 @@
+import { BigNumber } from '@0xproject/utils';
+import BN = require('bn.js');
+import ethUtil = require('ethereumjs-util');
+
+import { AssetProxyId } from './types';
+
+export const assetProxyUtils = {
+ encodeAssetProxyId(assetProxyId: AssetProxyId): Buffer {
+ return ethUtil.toBuffer(assetProxyId);
+ },
+ encodeAddress(address: string): Buffer {
+ if (!ethUtil.isValidAddress(address)) {
+ throw new Error(`Invalid Address: ${address}`);
+ }
+ const encodedAddress = ethUtil.toBuffer(address);
+ return encodedAddress;
+ },
+ encodeUint256(value: BigNumber): Buffer {
+ const formattedValue = new BN(value.toString(10));
+ const encodedValue = ethUtil.toBuffer(formattedValue);
+ return encodedValue;
+ },
+ encodeERC20ProxyData(tokenAddress: string): string {
+ const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC20);
+ const encodedAddress = assetProxyUtils.encodeAddress(tokenAddress);
+ const encodedMetadata = Buffer.concat([encodedAssetProxyId, encodedAddress]);
+ const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata);
+ return encodedMetadataHex;
+ },
+ encodeERC721ProxyData(tokenAddress: string, tokenId: BigNumber): string {
+ const encodedAssetProxyId = assetProxyUtils.encodeAssetProxyId(AssetProxyId.ERC721);
+ const encodedAddress = assetProxyUtils.encodeAddress(tokenAddress);
+ const encodedTokenId = assetProxyUtils.encodeUint256(tokenId);
+ const encodedMetadata = Buffer.concat([encodedAssetProxyId, encodedAddress, encodedTokenId]);
+ const encodedMetadataHex = ethUtil.bufferToHex(encodedMetadata);
+ return encodedMetadataHex;
+ },
+};
diff --git a/packages/contracts/src/utils/chai_setup.ts b/packages/contracts/src/utils/chai_setup.ts
new file mode 100644
index 000000000..078edd309
--- /dev/null
+++ b/packages/contracts/src/utils/chai_setup.ts
@@ -0,0 +1,13 @@
+import * as chai from 'chai';
+import chaiAsPromised = require('chai-as-promised');
+import ChaiBigNumber = require('chai-bignumber');
+import * as dirtyChai from 'dirty-chai';
+
+export const chaiSetup = {
+ configure() {
+ chai.config.includeStack = true;
+ chai.use(ChaiBigNumber());
+ chai.use(dirtyChai);
+ chai.use(chaiAsPromised);
+ },
+};
diff --git a/packages/contracts/src/utils/constants.ts b/packages/contracts/src/utils/constants.ts
new file mode 100644
index 000000000..3fd2a0a3b
--- /dev/null
+++ b/packages/contracts/src/utils/constants.ts
@@ -0,0 +1,42 @@
+import { ZeroEx } from '0x.js';
+import { BigNumber } from '@0xproject/utils';
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
+
+const TESTRPC_PRIVATE_KEYS_STRINGS = [
+ '0xf2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257d',
+ '0x5d862464fe9303452126c8bc94274b8c5f9874cbd219789b3eb2128075a76f72',
+ '0xdf02719c4df8b9b8ac7f551fcb5d9ef48fa27eef7a66453879f4d8fdc6e78fb1',
+ '0xff12e391b79415e941a94de3bf3a9aee577aed0731e297d5cfa0b8a1e02fa1d0',
+ '0x752dd9cf65e68cfaba7d60225cbdbc1f4729dd5e5507def72815ed0d8abc6249',
+ '0xefb595a0178eb79a8df953f87c5148402a224cdf725e88c0146727c6aceadccd',
+ '0x83c6d2cc5ddcf9711a6d59b417dc20eb48afd58d45290099e5987e3d768f328f',
+ '0xbb2d3f7c9583780a7d3904a2f55d792707c345f21de1bacb2d389934d82796b2',
+ '0xb2fd4d29c1390b71b8795ae81196bfd60293adf99f9d32a0aff06288fcdac55f',
+ '0x23cb7121166b9a2f93ae0b7c05bde02eae50d64449b2cbb42bc84e9d38d6cc89',
+];
+
+export const constants = {
+ INVALID_OPCODE: 'invalid opcode',
+ REVERT: 'revert',
+ TESTRPC_NETWORK_ID: 50,
+ MAX_ETHERTOKEN_WITHDRAW_GAS: 43000,
+ MAX_TOKEN_TRANSFERFROM_GAS: 80000,
+ MAX_TOKEN_APPROVE_GAS: 60000,
+ DUMMY_TOKEN_NAME: '',
+ DUMMY_TOKEN_SYMBOL: '',
+ DUMMY_TOKEN_DECIMALS: new BigNumber(18),
+ DUMMY_TOKEN_TOTAL_SUPPLY: new BigNumber(0),
+ NUM_DUMMY_ERC20_TO_DEPLOY: 3,
+ NUM_DUMMY_ERC721_TO_DEPLOY: 1,
+ NUM_ERC721_TOKENS_TO_MINT: 2,
+ TESTRPC_PRIVATE_KEYS: _.map(TESTRPC_PRIVATE_KEYS_STRINGS, privateKeyString => ethUtil.toBuffer(privateKeyString)),
+ INITIAL_ERC20_BALANCE: ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18),
+ INITIAL_ERC20_ALLOWANCE: ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18),
+ STATIC_ORDER_PARAMS: {
+ makerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
+ takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
+ makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18),
+ takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18),
+ },
+};
diff --git a/packages/contracts/src/utils/crypto.ts b/packages/contracts/src/utils/crypto.ts
new file mode 100644
index 000000000..222cb7cca
--- /dev/null
+++ b/packages/contracts/src/utils/crypto.ts
@@ -0,0 +1,45 @@
+import BN = require('bn.js');
+import ABI = require('ethereumjs-abi');
+import ethUtil = require('ethereumjs-util');
+import * as _ from 'lodash';
+
+export const crypto = {
+ /**
+ * We convert types from JS to Solidity as follows:
+ * BigNumber -> uint256
+ * number -> uint8
+ * string -> string
+ * boolean -> bool
+ * valid Ethereum address -> address
+ */
+ solSHA3(args: any[]): Buffer {
+ return crypto._solHash(args, ABI.soliditySHA3);
+ },
+ solSHA256(args: any[]): Buffer {
+ return crypto._solHash(args, ABI.soliditySHA256);
+ },
+ _solHash(args: any[], hashFunction: (types: string[], values: any[]) => Buffer) {
+ const argTypes: string[] = [];
+ _.each(args, (arg, i) => {
+ const isNumber = _.isFinite(arg);
+ if (isNumber) {
+ argTypes.push('uint8');
+ } else if (arg.isBigNumber) {
+ argTypes.push('uint256');
+ args[i] = new BN(arg.toString(10), 10);
+ } else if (ethUtil.isValidAddress(arg)) {
+ argTypes.push('address');
+ } else if (_.isString(arg)) {
+ argTypes.push('string');
+ } else if (_.isBuffer(arg)) {
+ argTypes.push('bytes');
+ } else if (_.isBoolean(arg)) {
+ argTypes.push('bool');
+ } else {
+ throw new Error(`Unable to guess arg type: ${arg}`);
+ }
+ });
+ const hash = hashFunction(argTypes, args);
+ return hash;
+ },
+};
diff --git a/packages/contracts/src/utils/erc20_wrapper.ts b/packages/contracts/src/utils/erc20_wrapper.ts
new file mode 100644
index 000000000..f12571b48
--- /dev/null
+++ b/packages/contracts/src/utils/erc20_wrapper.ts
@@ -0,0 +1,116 @@
+import { Provider } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
+
+import { DummyERC20TokenContract } from '../contract_wrappers/generated/dummy_e_r_c20_token';
+import { ERC20ProxyContract } from '../contract_wrappers/generated/e_r_c20_proxy';
+
+import { artifacts } from './artifacts';
+import { constants } from './constants';
+import { ERC20BalancesByOwner } from './types';
+import { txDefaults } from './web3_wrapper';
+
+export class ERC20Wrapper {
+ private _tokenOwnerAddresses: string[];
+ private _contractOwnerAddress: string;
+ private _provider: Provider;
+ private _dummyTokenContracts?: DummyERC20TokenContract[];
+ private _proxyContract?: ERC20ProxyContract;
+ constructor(provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) {
+ this._provider = provider;
+ this._tokenOwnerAddresses = tokenOwnerAddresses;
+ this._contractOwnerAddress = contractOwnerAddress;
+ }
+ public async deployDummyTokensAsync(): Promise<DummyERC20TokenContract[]> {
+ this._dummyTokenContracts = await Promise.all(
+ _.times(constants.NUM_DUMMY_ERC20_TO_DEPLOY, () =>
+ DummyERC20TokenContract.deployFrom0xArtifactAsync(
+ artifacts.DummyERC20Token,
+ this._provider,
+ txDefaults,
+ constants.DUMMY_TOKEN_NAME,
+ constants.DUMMY_TOKEN_SYMBOL,
+ constants.DUMMY_TOKEN_DECIMALS,
+ constants.DUMMY_TOKEN_TOTAL_SUPPLY,
+ ),
+ ),
+ );
+ return this._dummyTokenContracts;
+ }
+ public async deployProxyAsync(): Promise<ERC20ProxyContract> {
+ this._proxyContract = await ERC20ProxyContract.deployFrom0xArtifactAsync(
+ artifacts.ERC20Proxy,
+ this._provider,
+ txDefaults,
+ );
+ return this._proxyContract;
+ }
+ public async setBalancesAndAllowancesAsync() {
+ this._validateDummyTokenContractsExistOrThrow();
+ this._validateProxyContractExistsOrThrow();
+ const setBalancePromises: Array<Promise<string>> = [];
+ const setAllowancePromises: Array<Promise<string>> = [];
+ _.forEach(this._dummyTokenContracts, dummyTokenContract => {
+ _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => {
+ setBalancePromises.push(
+ dummyTokenContract.setBalance.sendTransactionAsync(
+ tokenOwnerAddress,
+ constants.INITIAL_ERC20_BALANCE,
+ { from: this._contractOwnerAddress },
+ ),
+ );
+ setAllowancePromises.push(
+ dummyTokenContract.approve.sendTransactionAsync(
+ (this._proxyContract as ERC20ProxyContract).address,
+ constants.INITIAL_ERC20_ALLOWANCE,
+ { from: tokenOwnerAddress },
+ ),
+ );
+ });
+ });
+ await Promise.all([...setBalancePromises, ...setAllowancePromises]);
+ }
+ public async getBalancesAsync(): Promise<ERC20BalancesByOwner> {
+ this._validateDummyTokenContractsExistOrThrow();
+ const balancesByOwner: ERC20BalancesByOwner = {};
+ const balancePromises: Array<Promise<BigNumber>> = [];
+ const balanceInfo: Array<{ tokenOwnerAddress: string; tokenAddress: string }> = [];
+ _.forEach(this._dummyTokenContracts, dummyTokenContract => {
+ _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => {
+ balancePromises.push(dummyTokenContract.balanceOf.callAsync(tokenOwnerAddress));
+ balanceInfo.push({
+ tokenOwnerAddress,
+ tokenAddress: dummyTokenContract.address,
+ });
+ });
+ });
+ const balances = await Promise.all(balancePromises);
+ _.forEach(balances, (balance, balanceIndex) => {
+ const tokenAddress = balanceInfo[balanceIndex].tokenAddress;
+ const tokenOwnerAddress = balanceInfo[balanceIndex].tokenOwnerAddress;
+ if (_.isUndefined(balancesByOwner[tokenOwnerAddress])) {
+ balancesByOwner[tokenOwnerAddress] = {};
+ }
+ const wrappedBalance = new BigNumber(balance);
+ balancesByOwner[tokenOwnerAddress][tokenAddress] = wrappedBalance;
+ });
+ return balancesByOwner;
+ }
+ public getTokenOwnerAddresses(): string[] {
+ return this._tokenOwnerAddresses;
+ }
+ public getTokenAddresses(): string[] {
+ const tokenAddresses = _.map(this._dummyTokenContracts, dummyTokenContract => dummyTokenContract.address);
+ return tokenAddresses;
+ }
+ private _validateDummyTokenContractsExistOrThrow() {
+ if (_.isUndefined(this._dummyTokenContracts)) {
+ throw new Error('Dummy ERC20 tokens not yet deployed, please call "deployDummyTokensAsync"');
+ }
+ }
+ private _validateProxyContractExistsOrThrow() {
+ if (_.isUndefined(this._proxyContract)) {
+ throw new Error('ERC20 proxy contract not yet deployed, please call "deployProxyAsync"');
+ }
+ }
+}
diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts
new file mode 100644
index 000000000..5b5de262f
--- /dev/null
+++ b/packages/contracts/src/utils/erc721_wrapper.ts
@@ -0,0 +1,145 @@
+import { ZeroEx } from '0x.js';
+import { Provider } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
+
+import { DummyERC721TokenContract } from '../contract_wrappers/generated/dummy_e_r_c721_token';
+import { ERC721ProxyContract } from '../contract_wrappers/generated/e_r_c721_proxy';
+
+import { artifacts } from './artifacts';
+import { constants } from './constants';
+import { ERC721TokenIdsByOwner } from './types';
+import { txDefaults } from './web3_wrapper';
+
+export class ERC721Wrapper {
+ private _tokenOwnerAddresses: string[];
+ private _contractOwnerAddress: string;
+ private _provider: Provider;
+ private _dummyTokenContracts?: DummyERC721TokenContract[];
+ private _proxyContract?: ERC721ProxyContract;
+ private _initialTokenIdsByOwner: ERC721TokenIdsByOwner = {};
+ constructor(provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) {
+ this._provider = provider;
+ this._tokenOwnerAddresses = tokenOwnerAddresses;
+ this._contractOwnerAddress = contractOwnerAddress;
+ }
+ public async deployDummyTokensAsync(): Promise<DummyERC721TokenContract[]> {
+ this._dummyTokenContracts = await Promise.all(
+ _.times(constants.NUM_DUMMY_ERC721_TO_DEPLOY, () =>
+ DummyERC721TokenContract.deployFrom0xArtifactAsync(
+ artifacts.DummyERC721Token,
+ this._provider,
+ txDefaults,
+ constants.DUMMY_TOKEN_NAME,
+ constants.DUMMY_TOKEN_SYMBOL,
+ ),
+ ),
+ );
+ return this._dummyTokenContracts;
+ }
+ public async deployProxyAsync(): Promise<ERC721ProxyContract> {
+ this._proxyContract = await ERC721ProxyContract.deployFrom0xArtifactAsync(
+ artifacts.ERC721Proxy,
+ this._provider,
+ txDefaults,
+ );
+ return this._proxyContract;
+ }
+ public async setBalancesAndAllowancesAsync() {
+ this._validateDummyTokenContractsExistOrThrow();
+ this._validateProxyContractExistsOrThrow();
+ const setBalancePromises: Array<Promise<string>> = [];
+ const setAllowancePromises: Array<Promise<string>> = [];
+ this._initialTokenIdsByOwner = {};
+ _.forEach(this._dummyTokenContracts, dummyTokenContract => {
+ _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => {
+ _.forEach(_.range(constants.NUM_ERC721_TOKENS_TO_MINT), () => {
+ const tokenId = ZeroEx.generatePseudoRandomSalt();
+ setBalancePromises.push(
+ dummyTokenContract.mint.sendTransactionAsync(tokenOwnerAddress, tokenId, {
+ from: this._contractOwnerAddress,
+ }),
+ );
+ if (_.isUndefined(this._initialTokenIdsByOwner[tokenOwnerAddress])) {
+ this._initialTokenIdsByOwner[tokenOwnerAddress] = {
+ [dummyTokenContract.address]: [],
+ };
+ }
+ if (_.isUndefined(this._initialTokenIdsByOwner[tokenOwnerAddress][dummyTokenContract.address])) {
+ this._initialTokenIdsByOwner[tokenOwnerAddress][dummyTokenContract.address] = [];
+ }
+ this._initialTokenIdsByOwner[tokenOwnerAddress][dummyTokenContract.address].push(tokenId);
+ });
+ const shouldApprove = true;
+ setAllowancePromises.push(
+ dummyTokenContract.setApprovalForAll.sendTransactionAsync(
+ (this._proxyContract as ERC721ProxyContract).address,
+ shouldApprove,
+ { from: tokenOwnerAddress },
+ ),
+ );
+ });
+ });
+ await Promise.all([...setBalancePromises, ...setAllowancePromises]);
+ }
+ public async getBalancesAsync(): Promise<ERC721TokenIdsByOwner> {
+ this._validateDummyTokenContractsExistOrThrow();
+ this._validateBalancesAndAllowancesSetOrThrow();
+ const tokenIdsByOwner: ERC721TokenIdsByOwner = {};
+ const tokenOwnerPromises: Array<Promise<string>> = [];
+ const tokenInfo: Array<{ tokenId: BigNumber; tokenAddress: string }> = [];
+ _.forEach(this._dummyTokenContracts, dummyTokenContract => {
+ _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => {
+ const initialTokenOwnerIds = this._initialTokenIdsByOwner[tokenOwnerAddress][
+ dummyTokenContract.address
+ ];
+ _.forEach(initialTokenOwnerIds, tokenId => {
+ tokenOwnerPromises.push(dummyTokenContract.ownerOf.callAsync(tokenId));
+ tokenInfo.push({
+ tokenId,
+ tokenAddress: dummyTokenContract.address,
+ });
+ });
+ });
+ });
+ const tokenOwnerAddresses = await Promise.all(tokenOwnerPromises);
+ _.forEach(tokenOwnerAddresses, (tokenOwnerAddress, ownerIndex) => {
+ const tokenAddress = tokenInfo[ownerIndex].tokenAddress;
+ const tokenId = tokenInfo[ownerIndex].tokenId;
+ if (_.isUndefined(tokenIdsByOwner[tokenOwnerAddress])) {
+ tokenIdsByOwner[tokenOwnerAddress] = {
+ [tokenAddress]: [],
+ };
+ }
+ if (_.isUndefined(tokenIdsByOwner[tokenOwnerAddress][tokenAddress])) {
+ tokenIdsByOwner[tokenOwnerAddress][tokenAddress] = [];
+ }
+ tokenIdsByOwner[tokenOwnerAddress][tokenAddress].push(tokenId);
+ });
+ return tokenIdsByOwner;
+ }
+ public getTokenOwnerAddresses(): string[] {
+ return this._tokenOwnerAddresses;
+ }
+ public getTokenAddresses(): string[] {
+ const tokenAddresses = _.map(this._dummyTokenContracts, dummyTokenContract => dummyTokenContract.address);
+ return tokenAddresses;
+ }
+ private _validateDummyTokenContractsExistOrThrow() {
+ if (_.isUndefined(this._dummyTokenContracts)) {
+ throw new Error('Dummy ERC721 tokens not yet deployed, please call "deployDummyTokensAsync"');
+ }
+ }
+ private _validateProxyContractExistsOrThrow() {
+ if (_.isUndefined(this._proxyContract)) {
+ throw new Error('ERC721 proxy contract not yet deployed, please call "deployProxyAsync"');
+ }
+ }
+ private _validateBalancesAndAllowancesSetOrThrow() {
+ if (_.keys(this._initialTokenIdsByOwner).length === 0) {
+ throw new Error(
+ 'Dummy ERC721 balances and allowances not yet set, please call "setBalancesAndAllowancesAsync"',
+ );
+ }
+ }
+}
diff --git a/packages/contracts/src/utils/exchange_wrapper.ts b/packages/contracts/src/utils/exchange_wrapper.ts
new file mode 100644
index 000000000..27fdd698f
--- /dev/null
+++ b/packages/contracts/src/utils/exchange_wrapper.ts
@@ -0,0 +1,234 @@
+import { TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js';
+import { BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
+import * as Web3 from 'web3';
+
+import { ExchangeContract } from '../contract_wrappers/generated/exchange';
+
+import { constants } from './constants';
+import { formatters } from './formatters';
+import { LogDecoder } from './log_decoder';
+import { orderUtils } from './order_utils';
+import { AssetProxyId, SignedOrder, SignedTransaction } from './types';
+
+export class ExchangeWrapper {
+ private _exchange: ExchangeContract;
+ private _logDecoder: LogDecoder = new LogDecoder(constants.TESTRPC_NETWORK_ID);
+ private _zeroEx: ZeroEx;
+ constructor(exchangeContract: ExchangeContract, zeroEx: ZeroEx) {
+ this._exchange = exchangeContract;
+ this._zeroEx = zeroEx;
+ }
+ public async fillOrderAsync(
+ signedOrder: SignedOrder,
+ from: string,
+ opts: { takerAssetFillAmount?: BigNumber } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = orderUtils.createFill(signedOrder, opts.takerAssetFillAmount);
+ const txHash = await this._exchange.fillOrder.sendTransactionAsync(
+ params.order,
+ params.takerAssetFillAmount,
+ params.signature,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async cancelOrderAsync(signedOrder: SignedOrder, from: string): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = orderUtils.createCancel(signedOrder);
+ const txHash = await this._exchange.cancelOrder.sendTransactionAsync(params.order, { from });
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async fillOrKillOrderAsync(
+ signedOrder: SignedOrder,
+ from: string,
+ opts: { takerAssetFillAmount?: BigNumber } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = orderUtils.createFill(signedOrder, opts.takerAssetFillAmount);
+ const txHash = await this._exchange.fillOrKillOrder.sendTransactionAsync(
+ params.order,
+ params.takerAssetFillAmount,
+ params.signature,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async fillOrderNoThrowAsync(
+ signedOrder: SignedOrder,
+ from: string,
+ opts: { takerAssetFillAmount?: BigNumber } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = orderUtils.createFill(signedOrder, opts.takerAssetFillAmount);
+ const txHash = await this._exchange.fillOrderNoThrow.sendTransactionAsync(
+ params.order,
+ params.takerAssetFillAmount,
+ params.signature,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async batchFillOrdersAsync(
+ orders: SignedOrder[],
+ from: string,
+ opts: { takerAssetFillAmounts?: BigNumber[] } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = formatters.createBatchFill(orders, opts.takerAssetFillAmounts);
+ const txHash = await this._exchange.batchFillOrders.sendTransactionAsync(
+ params.orders,
+ params.takerAssetFillAmounts,
+ params.signatures,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async batchFillOrKillOrdersAsync(
+ orders: SignedOrder[],
+ from: string,
+ opts: { takerAssetFillAmounts?: BigNumber[] } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = formatters.createBatchFill(orders, opts.takerAssetFillAmounts);
+ const txHash = await this._exchange.batchFillOrKillOrders.sendTransactionAsync(
+ params.orders,
+ params.takerAssetFillAmounts,
+ params.signatures,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async batchFillOrdersNoThrowAsync(
+ orders: SignedOrder[],
+ from: string,
+ opts: { takerAssetFillAmounts?: BigNumber[] } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = formatters.createBatchFill(orders, opts.takerAssetFillAmounts);
+ const txHash = await this._exchange.batchFillOrdersNoThrow.sendTransactionAsync(
+ params.orders,
+ params.takerAssetFillAmounts,
+ params.signatures,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async marketSellOrdersAsync(
+ orders: SignedOrder[],
+ from: string,
+ opts: { takerAssetFillAmount: BigNumber },
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = formatters.createMarketSellOrders(orders, opts.takerAssetFillAmount);
+ const txHash = await this._exchange.marketSellOrders.sendTransactionAsync(
+ params.orders,
+ params.takerAssetFillAmount,
+ params.signatures,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async marketSellOrdersNoThrowAsync(
+ orders: SignedOrder[],
+ from: string,
+ opts: { takerAssetFillAmount: BigNumber },
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = formatters.createMarketSellOrders(orders, opts.takerAssetFillAmount);
+ const txHash = await this._exchange.marketSellOrdersNoThrow.sendTransactionAsync(
+ params.orders,
+ params.takerAssetFillAmount,
+ params.signatures,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async marketBuyOrdersAsync(
+ orders: SignedOrder[],
+ from: string,
+ opts: { makerAssetFillAmount: BigNumber },
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = formatters.createMarketBuyOrders(orders, opts.makerAssetFillAmount);
+ const txHash = await this._exchange.marketBuyOrders.sendTransactionAsync(
+ params.orders,
+ params.makerAssetFillAmount,
+ params.signatures,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async marketBuyOrdersNoThrowAsync(
+ orders: SignedOrder[],
+ from: string,
+ opts: { makerAssetFillAmount: BigNumber },
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = formatters.createMarketBuyOrders(orders, opts.makerAssetFillAmount);
+ const txHash = await this._exchange.marketBuyOrdersNoThrow.sendTransactionAsync(
+ params.orders,
+ params.makerAssetFillAmount,
+ params.signatures,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async batchCancelOrdersAsync(
+ orders: SignedOrder[],
+ from: string,
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const params = formatters.createBatchCancel(orders);
+ const txHash = await this._exchange.batchCancelOrders.sendTransactionAsync(params.orders, { from });
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async cancelOrdersUpToAsync(salt: BigNumber, from: string): Promise<TransactionReceiptWithDecodedLogs> {
+ const txHash = await this._exchange.cancelOrdersUpTo.sendTransactionAsync(salt, { from });
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async registerAssetProxyAsync(
+ assetProxyId: AssetProxyId,
+ assetProxyAddress: string,
+ from: string,
+ opts: { oldAssetProxyAddressIfExists?: string } = {},
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const oldAssetProxyAddress = _.isUndefined(opts.oldAssetProxyAddressIfExists)
+ ? ZeroEx.NULL_ADDRESS
+ : opts.oldAssetProxyAddressIfExists;
+ const txHash = await this._exchange.registerAssetProxy.sendTransactionAsync(
+ assetProxyId,
+ assetProxyAddress,
+ oldAssetProxyAddress,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async executeTransactionAsync(
+ signedTx: SignedTransaction,
+ from: string,
+ ): Promise<TransactionReceiptWithDecodedLogs> {
+ const txHash = await this._exchange.executeTransaction.sendTransactionAsync(
+ signedTx.salt,
+ signedTx.signer,
+ signedTx.data,
+ signedTx.signature,
+ { from },
+ );
+ const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash);
+ return tx;
+ }
+ public async getTakerAssetFilledAmountAsync(orderHashHex: string): Promise<BigNumber> {
+ const filledAmount = new BigNumber(await this._exchange.filled.callAsync(orderHashHex));
+ return filledAmount;
+ }
+ private async _getTxWithDecodedExchangeLogsAsync(txHash: string) {
+ const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
+ tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address);
+ tx.logs = _.map(tx.logs, log => this._logDecoder.decodeLogOrThrow(log));
+ return tx;
+ }
+}
diff --git a/packages/contracts/src/utils/formatters.ts b/packages/contracts/src/utils/formatters.ts
new file mode 100644
index 000000000..e706c15b5
--- /dev/null
+++ b/packages/contracts/src/utils/formatters.ts
@@ -0,0 +1,60 @@
+import { BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
+
+import { orderUtils } from './order_utils';
+import { BatchCancelOrders, BatchFillOrders, MarketBuyOrders, MarketSellOrders, SignedOrder } from './types';
+
+export const formatters = {
+ createBatchFill(signedOrders: SignedOrder[], takerAssetFillAmounts: BigNumber[] = []) {
+ const batchFill: BatchFillOrders = {
+ orders: [],
+ signatures: [],
+ takerAssetFillAmounts,
+ };
+ _.forEach(signedOrders, signedOrder => {
+ const orderStruct = orderUtils.getOrderStruct(signedOrder);
+ batchFill.orders.push(orderStruct);
+ batchFill.signatures.push(signedOrder.signature);
+ if (takerAssetFillAmounts.length < signedOrders.length) {
+ batchFill.takerAssetFillAmounts.push(signedOrder.takerAssetAmount);
+ }
+ });
+ return batchFill;
+ },
+ createMarketSellOrders(signedOrders: SignedOrder[], takerAssetFillAmount: BigNumber) {
+ const marketSellOrders: MarketSellOrders = {
+ orders: [],
+ signatures: [],
+ takerAssetFillAmount,
+ };
+ _.forEach(signedOrders, signedOrder => {
+ const orderStruct = orderUtils.getOrderStruct(signedOrder);
+ marketSellOrders.orders.push(orderStruct);
+ marketSellOrders.signatures.push(signedOrder.signature);
+ });
+ return marketSellOrders;
+ },
+ createMarketBuyOrders(signedOrders: SignedOrder[], makerAssetFillAmount: BigNumber) {
+ const marketBuyOrders: MarketBuyOrders = {
+ orders: [],
+ signatures: [],
+ makerAssetFillAmount,
+ };
+ _.forEach(signedOrders, signedOrder => {
+ const orderStruct = orderUtils.getOrderStruct(signedOrder);
+ marketBuyOrders.orders.push(orderStruct);
+ marketBuyOrders.signatures.push(signedOrder.signature);
+ });
+ return marketBuyOrders;
+ },
+ createBatchCancel(signedOrders: SignedOrder[]) {
+ const batchCancel: BatchCancelOrders = {
+ orders: [],
+ };
+ _.forEach(signedOrders, signedOrder => {
+ const orderStruct = orderUtils.getOrderStruct(signedOrder);
+ batchCancel.orders.push(orderStruct);
+ });
+ return batchCancel;
+ },
+};
diff --git a/packages/contracts/src/utils/log_decoder.ts b/packages/contracts/src/utils/log_decoder.ts
new file mode 100644
index 000000000..747c7644d
--- /dev/null
+++ b/packages/contracts/src/utils/log_decoder.ts
@@ -0,0 +1,39 @@
+import { ContractArtifact } from '@0xproject/sol-compiler';
+import { AbiDefinition, LogEntry, LogWithDecodedArgs, RawLog } from '@0xproject/types';
+import { AbiDecoder, BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
+
+import { artifacts } from './artifacts';
+
+export class LogDecoder {
+ private _abiDecoder: AbiDecoder;
+ constructor(networkIdIfExists?: number) {
+ if (_.isUndefined(networkIdIfExists)) {
+ throw new Error('networkId not specified');
+ }
+ const abiArrays: AbiDefinition[][] = [];
+ _.forEach(artifacts, (artifact: ContractArtifact) => {
+ const compilerOutput = artifact.compilerOutput;
+ abiArrays.push(compilerOutput.abi);
+ });
+ this._abiDecoder = new AbiDecoder(abiArrays);
+ }
+ public decodeLogOrThrow<ArgsType>(log: LogEntry): LogWithDecodedArgs<ArgsType> | RawLog {
+ const logWithDecodedArgsOrLog = this._abiDecoder.tryToDecodeLogOrNoop(log);
+ if (_.isUndefined((logWithDecodedArgsOrLog as LogWithDecodedArgs<ArgsType>).args)) {
+ throw new Error(`Unable to decode log: ${JSON.stringify(log)}`);
+ }
+ wrapLogBigNumbers(logWithDecodedArgsOrLog);
+ return logWithDecodedArgsOrLog;
+ }
+}
+
+function wrapLogBigNumbers(log: any): any {
+ const argNames = _.keys(log.args);
+ for (const argName of argNames) {
+ const isWeb3BigNumber = _.startsWith(log.args[argName].constructor.toString(), 'function BigNumber(');
+ if (isWeb3BigNumber) {
+ log.args[argName] = new BigNumber(log.args[argName]);
+ }
+ }
+}
diff --git a/packages/contracts/src/utils/multi_sig_wrapper.ts b/packages/contracts/src/utils/multi_sig_wrapper.ts
new file mode 100644
index 000000000..84fed7944
--- /dev/null
+++ b/packages/contracts/src/utils/multi_sig_wrapper.ts
@@ -0,0 +1,43 @@
+import { AbiDefinition, MethodAbi } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import ABI = require('ethereumjs-abi');
+import ethUtil = require('ethereumjs-util');
+import * as _ from 'lodash';
+import * as Web3 from 'web3';
+
+import { MultiSigWalletContract } from '../contract_wrappers/generated/multi_sig_wallet';
+
+import { TransactionDataParams } from './types';
+
+export class MultiSigWrapper {
+ private _multiSig: MultiSigWalletContract;
+ public static encodeFnArgs(name: string, abi: AbiDefinition[], args: any[]) {
+ const abiEntity = _.find(abi, { name }) as MethodAbi;
+ if (_.isUndefined(abiEntity)) {
+ throw new Error(`Did not find abi entry for name: ${name}`);
+ }
+ const types = _.map(abiEntity.inputs, input => input.type);
+ const funcSig = ethUtil.bufferToHex(ABI.methodID(name, types));
+ const argsData = _.map(args, arg => {
+ const target = _.isBoolean(arg) ? +arg : arg;
+ const targetBuff = ethUtil.toBuffer(target);
+ return ethUtil.setLengthLeft(targetBuff, 32).toString('hex');
+ });
+ return funcSig + argsData.join('');
+ }
+ constructor(multiSigContract: MultiSigWalletContract) {
+ this._multiSig = multiSigContract;
+ }
+ public async submitTransactionAsync(
+ destination: string,
+ from: string,
+ dataParams: TransactionDataParams,
+ value: BigNumber = new BigNumber(0),
+ ) {
+ const { name, abi, args = [] } = dataParams;
+ const encoded = MultiSigWrapper.encodeFnArgs(name, abi, args);
+ return this._multiSig.submitTransaction.sendTransactionAsync(destination, value, encoded, {
+ from,
+ });
+ }
+}
diff --git a/packages/contracts/src/utils/order_factory.ts b/packages/contracts/src/utils/order_factory.ts
new file mode 100644
index 000000000..044e9b865
--- /dev/null
+++ b/packages/contracts/src/utils/order_factory.ts
@@ -0,0 +1,37 @@
+import { ZeroEx } from '0x.js';
+import { BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
+
+import { orderUtils } from './order_utils';
+import { signingUtils } from './signing_utils';
+import { SignatureType, SignedOrder, UnsignedOrder } from './types';
+
+export class OrderFactory {
+ private _defaultOrderParams: Partial<UnsignedOrder>;
+ private _privateKey: Buffer;
+ constructor(privateKey: Buffer, defaultOrderParams: Partial<UnsignedOrder>) {
+ this._defaultOrderParams = defaultOrderParams;
+ this._privateKey = privateKey;
+ }
+ public newSignedOrder(
+ customOrderParams: Partial<UnsignedOrder> = {},
+ signatureType: SignatureType = SignatureType.Ecrecover,
+ ): SignedOrder {
+ const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000));
+ const order = ({
+ senderAddress: ZeroEx.NULL_ADDRESS,
+ expirationTimeSeconds: randomExpiration,
+ salt: ZeroEx.generatePseudoRandomSalt(),
+ takerAddress: ZeroEx.NULL_ADDRESS,
+ ...this._defaultOrderParams,
+ ...customOrderParams,
+ } as any) as UnsignedOrder;
+ const orderHashBuff = orderUtils.getOrderHashBuff(order);
+ const signature = signingUtils.signMessage(orderHashBuff, this._privateKey, signatureType);
+ const signedOrder = {
+ ...order,
+ signature: `0x${signature.toString('hex')}`,
+ };
+ return signedOrder;
+ }
+}
diff --git a/packages/contracts/src/utils/order_utils.ts b/packages/contracts/src/utils/order_utils.ts
new file mode 100644
index 000000000..10bbf4f7c
--- /dev/null
+++ b/packages/contracts/src/utils/order_utils.ts
@@ -0,0 +1,83 @@
+import { BigNumber } from '@0xproject/utils';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
+import ethUtil = require('ethereumjs-util');
+import * as _ from 'lodash';
+
+import { crypto } from './crypto';
+import { OrderStruct, SignatureType, SignedOrder, UnsignedOrder } from './types';
+
+export const orderUtils = {
+ createFill: (signedOrder: SignedOrder, takerAssetFillAmount?: BigNumber) => {
+ const fill = {
+ order: orderUtils.getOrderStruct(signedOrder),
+ takerAssetFillAmount: takerAssetFillAmount || signedOrder.takerAssetAmount,
+ signature: signedOrder.signature,
+ };
+ return fill;
+ },
+ createCancel(signedOrder: SignedOrder, takerAssetCancelAmount?: BigNumber) {
+ const cancel = {
+ order: orderUtils.getOrderStruct(signedOrder),
+ takerAssetCancelAmount: takerAssetCancelAmount || signedOrder.takerAssetAmount,
+ };
+ return cancel;
+ },
+ getOrderStruct(signedOrder: SignedOrder): OrderStruct {
+ const orderStruct = {
+ senderAddress: signedOrder.senderAddress,
+ makerAddress: signedOrder.makerAddress,
+ takerAddress: signedOrder.takerAddress,
+ feeRecipientAddress: signedOrder.feeRecipientAddress,
+ makerAssetAmount: signedOrder.makerAssetAmount,
+ takerAssetAmount: signedOrder.takerAssetAmount,
+ makerFee: signedOrder.makerFee,
+ takerFee: signedOrder.takerFee,
+ expirationTimeSeconds: signedOrder.expirationTimeSeconds,
+ salt: signedOrder.salt,
+ makerAssetData: signedOrder.makerAssetData,
+ takerAssetData: signedOrder.takerAssetData,
+ };
+ return orderStruct;
+ },
+ getOrderHashBuff(order: SignedOrder | UnsignedOrder): Buffer {
+ const orderSchemaHashBuff = crypto.solSHA3([
+ 'address exchangeAddress',
+ 'address makerAddress',
+ 'address takerAddress',
+ 'address feeRecipientAddress',
+ 'address senderAddress',
+ 'uint256 makerAssetAmount',
+ 'uint256 takerAssetAmount',
+ 'uint256 makerFee',
+ 'uint256 takerFee',
+ 'uint256 expirationTimeSeconds',
+ 'uint256 salt',
+ 'bytes makerAssetData',
+ 'bytes takerAssetData',
+ ]);
+ const orderParamsHashBuff = crypto.solSHA3([
+ order.exchangeAddress,
+ order.makerAddress,
+ order.takerAddress,
+ order.feeRecipientAddress,
+ order.senderAddress,
+ order.makerAssetAmount,
+ order.takerAssetAmount,
+ order.makerFee,
+ order.takerFee,
+ order.expirationTimeSeconds,
+ order.salt,
+ ethUtil.toBuffer(order.makerAssetData),
+ ethUtil.toBuffer(order.takerAssetData),
+ ]);
+ const orderSchemaHashHex = `0x${orderSchemaHashBuff.toString('hex')}`;
+ const orderParamsHashHex = `0x${orderParamsHashBuff.toString('hex')}`;
+ const orderHashBuff = crypto.solSHA3([new BigNumber(orderSchemaHashHex), new BigNumber(orderParamsHashHex)]);
+ return orderHashBuff;
+ },
+ getOrderHashHex(order: SignedOrder | UnsignedOrder): string {
+ const orderHashBuff = orderUtils.getOrderHashBuff(order);
+ const orderHashHex = `0x${orderHashBuff.toString('hex')}`;
+ return orderHashHex;
+ },
+};
diff --git a/packages/contracts/src/utils/signing_utils.ts b/packages/contracts/src/utils/signing_utils.ts
new file mode 100644
index 000000000..61ab1f138
--- /dev/null
+++ b/packages/contracts/src/utils/signing_utils.ts
@@ -0,0 +1,30 @@
+import * as ethUtil from 'ethereumjs-util';
+
+import { SignatureType } from './types';
+
+export const signingUtils = {
+ signMessage(message: Buffer, privateKey: Buffer, signatureType: SignatureType): Buffer {
+ if (signatureType === SignatureType.Ecrecover) {
+ const prefixedMessage = ethUtil.hashPersonalMessage(message);
+ const ecSignature = ethUtil.ecsign(prefixedMessage, privateKey);
+ const signature = Buffer.concat([
+ ethUtil.toBuffer(signatureType),
+ ethUtil.toBuffer(ecSignature.v),
+ ecSignature.r,
+ ecSignature.s,
+ ]);
+ return signature;
+ } else if (signatureType === SignatureType.EIP712) {
+ const ecSignature = ethUtil.ecsign(message, privateKey);
+ const signature = Buffer.concat([
+ ethUtil.toBuffer(signatureType),
+ ethUtil.toBuffer(ecSignature.v),
+ ecSignature.r,
+ ecSignature.s,
+ ]);
+ return signature;
+ } else {
+ throw new Error(`${signatureType} is not a valid signature type`);
+ }
+ },
+};
diff --git a/packages/contracts/src/utils/token_registry_wrapper.ts b/packages/contracts/src/utils/token_registry_wrapper.ts
new file mode 100644
index 000000000..91bd5503d
--- /dev/null
+++ b/packages/contracts/src/utils/token_registry_wrapper.ts
@@ -0,0 +1,60 @@
+import * as Web3 from 'web3';
+
+import { TokenRegistryContract } from '../contract_wrappers/generated/token_registry';
+
+import { Token } from './types';
+
+export class TokenRegWrapper {
+ private _tokenReg: TokenRegistryContract;
+ constructor(tokenRegContract: TokenRegistryContract) {
+ this._tokenReg = tokenRegContract;
+ }
+ public async addTokenAsync(token: Token, from: string) {
+ const tx = this._tokenReg.addToken.sendTransactionAsync(
+ token.address as string,
+ token.name,
+ token.symbol,
+ token.decimals,
+ token.ipfsHash,
+ token.swarmHash,
+ { from },
+ );
+ return tx;
+ }
+ public async getTokenMetaDataAsync(tokenAddress: string) {
+ const data = await this._tokenReg.getTokenMetaData.callAsync(tokenAddress);
+ const token: Token = {
+ address: data[0],
+ name: data[1],
+ symbol: data[2],
+ decimals: data[3],
+ ipfsHash: data[4],
+ swarmHash: data[5],
+ };
+ return token;
+ }
+ public async getTokenByNameAsync(tokenName: string) {
+ const data = await this._tokenReg.getTokenByName.callAsync(tokenName);
+ const token: Token = {
+ address: data[0],
+ name: data[1],
+ symbol: data[2],
+ decimals: data[3],
+ ipfsHash: data[4],
+ swarmHash: data[5],
+ };
+ return token;
+ }
+ public async getTokenBySymbolAsync(tokenSymbol: string) {
+ const data = await this._tokenReg.getTokenBySymbol.callAsync(tokenSymbol);
+ const token: Token = {
+ address: data[0],
+ name: data[1],
+ symbol: data[2],
+ decimals: data[3],
+ ipfsHash: data[4],
+ swarmHash: data[5],
+ };
+ return token;
+ }
+}
diff --git a/packages/contracts/src/utils/transaction_factory.ts b/packages/contracts/src/utils/transaction_factory.ts
new file mode 100644
index 000000000..3a4f48330
--- /dev/null
+++ b/packages/contracts/src/utils/transaction_factory.ts
@@ -0,0 +1,35 @@
+import { ZeroEx } from '0x.js';
+import { BigNumber } from '@0xproject/utils';
+import * as ethUtil from 'ethereumjs-util';
+
+import { crypto } from './crypto';
+import { signingUtils } from './signing_utils';
+import { SignatureType, SignedTransaction } from './types';
+
+export class TransactionFactory {
+ private _signer: string;
+ private _exchangeAddress: string;
+ private _privateKey: Buffer;
+ constructor(privateKey: Buffer, exchangeAddress: string) {
+ this._privateKey = privateKey;
+ this._exchangeAddress = exchangeAddress;
+ const signerBuff = ethUtil.privateToAddress(this._privateKey);
+ this._signer = `0x${signerBuff.toString('hex')}`;
+ }
+ public newSignedTransaction(
+ data: string,
+ signatureType: SignatureType = SignatureType.Ecrecover,
+ ): SignedTransaction {
+ const salt = ZeroEx.generatePseudoRandomSalt();
+ const txHash = crypto.solSHA3([this._exchangeAddress, salt, ethUtil.toBuffer(data)]);
+ const signature = signingUtils.signMessage(txHash, this._privateKey, signatureType);
+ const signedTx = {
+ exchangeAddress: this._exchangeAddress,
+ salt,
+ signer: this._signer,
+ data,
+ signature: `0x${signature.toString('hex')}`,
+ };
+ return signedTx;
+ }
+}
diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts
new file mode 100644
index 000000000..234b14ef9
--- /dev/null
+++ b/packages/contracts/src/utils/types.ts
@@ -0,0 +1,145 @@
+import { AbiDefinition, ContractAbi } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+
+export interface ERC20BalancesByOwner {
+ [ownerAddress: string]: {
+ [tokenAddress: string]: BigNumber;
+ };
+}
+
+export interface ERC721TokenIdsByOwner {
+ [ownerAddress: string]: {
+ [tokenAddress: string]: BigNumber[];
+ };
+}
+
+export interface SubmissionContractEventArgs {
+ transactionId: BigNumber;
+}
+
+export interface BatchFillOrders {
+ orders: OrderStruct[];
+ signatures: string[];
+ takerAssetFillAmounts: BigNumber[];
+}
+
+export interface MarketSellOrders {
+ orders: OrderStruct[];
+ signatures: string[];
+ takerAssetFillAmount: BigNumber;
+}
+
+export interface MarketBuyOrders {
+ orders: OrderStruct[];
+ signatures: string[];
+ makerAssetFillAmount: BigNumber;
+}
+
+export interface BatchCancelOrders {
+ orders: OrderStruct[];
+}
+
+export interface CancelOrdersBefore {
+ salt: BigNumber;
+}
+
+export enum AssetProxyId {
+ INVALID,
+ ERC20,
+ ERC721,
+}
+
+export interface TransactionDataParams {
+ name: string;
+ abi: AbiDefinition[];
+ args: any[];
+}
+
+export interface MultiSigConfig {
+ owners: string[];
+ confirmationsRequired: number;
+ secondsRequired: number;
+}
+
+export interface MultiSigConfigByNetwork {
+ [networkName: string]: MultiSigConfig;
+}
+
+export interface Token {
+ address?: string;
+ name: string;
+ symbol: string;
+ decimals: number;
+ ipfsHash: string;
+ swarmHash: string;
+}
+
+export enum ExchangeContractErrs {
+ ERROR_ORDER_EXPIRED,
+ ERROR_ORDER_FULLY_FILLED,
+ ERROR_ORDER_CANCELLED,
+ ERROR_ROUNDING_ERROR_TOO_LARGE,
+ ERROR_INSUFFICIENT_BALANCE_OR_ALLOWANCE,
+}
+
+export enum ContractName {
+ TokenRegistry = 'TokenRegistry',
+ MultiSigWalletWithTimeLock = 'MultiSigWalletWithTimeLock',
+ Exchange = 'Exchange',
+ ZRXToken = 'ZRXToken',
+ DummyERC20Token = 'DummyERC20Token',
+ EtherToken = 'WETH9',
+ MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress = 'MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress',
+ AccountLevels = 'AccountLevels',
+ EtherDelta = 'EtherDelta',
+ Arbitrage = 'Arbitrage',
+ TestAssetProxyDispatcher = 'TestAssetProxyDispatcher',
+ TestLibs = 'TestLibs',
+ TestSignatureValidator = 'TestSignatureValidator',
+ ERC20Proxy = 'ERC20Proxy',
+ ERC721Proxy = 'ERC721Proxy',
+ DummyERC721Token = 'DummyERC721Token',
+ TestLibBytes = 'TestLibBytes',
+ Authorizable = 'Authorizable',
+}
+
+export interface SignedOrder extends UnsignedOrder {
+ signature: string;
+}
+
+export interface OrderStruct {
+ senderAddress: string;
+ makerAddress: string;
+ takerAddress: string;
+ feeRecipientAddress: string;
+ makerAssetAmount: BigNumber;
+ takerAssetAmount: BigNumber;
+ makerFee: BigNumber;
+ takerFee: BigNumber;
+ expirationTimeSeconds: BigNumber;
+ salt: BigNumber;
+ makerAssetData: string;
+ takerAssetData: string;
+}
+
+export interface UnsignedOrder extends OrderStruct {
+ exchangeAddress: string;
+}
+
+export enum SignatureType {
+ Illegal,
+ Invalid,
+ Caller,
+ Ecrecover,
+ EIP712,
+ Trezor,
+ Contract,
+}
+
+export interface SignedTransaction {
+ exchangeAddress: string;
+ salt: BigNumber;
+ signer: string;
+ data: string;
+ signature: string;
+}
diff --git a/packages/contracts/src/utils/web3_wrapper.ts b/packages/contracts/src/utils/web3_wrapper.ts
new file mode 100644
index 000000000..ed1c488a2
--- /dev/null
+++ b/packages/contracts/src/utils/web3_wrapper.ts
@@ -0,0 +1,12 @@
+import { devConstants, web3Factory } from '@0xproject/dev-utils';
+import { Provider } from '@0xproject/types';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
+
+export const txDefaults = {
+ from: devConstants.TESTRPC_FIRST_ADDRESS,
+ gas: devConstants.GAS_ESTIMATE,
+};
+const providerConfigs = { shouldUseInProcessGanache: true };
+export const web3 = web3Factory.create(providerConfigs);
+export const provider = web3.currentProvider;
+export const web3Wrapper = new Web3Wrapper(provider);