From 9e01f4c9a30a0fd06c105222abfa1ab09e8fec3a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 4 Dec 2018 15:40:25 +0100 Subject: Refactor out libs into @0x/contracts-libs --- contracts/core/compiler.json | 1 - .../examples/ExchangeWrapper/ExchangeWrapper.sol | 2 +- .../contracts/examples/Whitelist/Whitelist.sol | 2 +- .../extensions/DutchAuction/DutchAuction.sol | 2 +- .../extensions/Forwarder/MixinExchangeWrapper.sol | 8 +- .../extensions/Forwarder/MixinForwarderCore.sol | 6 +- .../contracts/extensions/Forwarder/MixinWeth.sol | 2 +- .../Forwarder/interfaces/IForwarderCore.sol | 4 +- .../Forwarder/mixins/MExchangeWrapper.sol | 4 +- .../extensions/OrderValidator/OrderValidator.sol | 2 +- .../AssetProxy/libs/LibAssetProxyErrors.sol | 38 ---- .../core/contracts/protocol/Exchange/Exchange.sol | 2 +- .../protocol/Exchange/MixinExchangeCore.sol | 8 +- .../protocol/Exchange/MixinMatchOrders.sol | 8 +- .../protocol/Exchange/MixinTransactions.sol | 4 +- .../protocol/Exchange/MixinWrapperFunctions.sol | 8 +- .../protocol/Exchange/interfaces/IExchangeCore.sol | 4 +- .../protocol/Exchange/interfaces/IMatchOrders.sol | 4 +- .../Exchange/interfaces/IWrapperFunctions.sol | 4 +- .../protocol/Exchange/libs/LibAbiEncoder.sol | 215 ----------------- .../protocol/Exchange/libs/LibConstants.sol | 49 ---- .../contracts/protocol/Exchange/libs/LibEIP712.sol | 87 ------- .../protocol/Exchange/libs/LibExchangeErrors.sol | 70 ------ .../protocol/Exchange/libs/LibFillResults.sol | 53 ----- .../contracts/protocol/Exchange/libs/LibMath.sol | 253 --------------------- .../contracts/protocol/Exchange/libs/LibOrder.sol | 145 ------------ .../protocol/Exchange/mixins/MExchangeCore.sol | 4 +- .../protocol/Exchange/mixins/MMatchOrders.sol | 4 +- .../protocol/Exchange/mixins/MWrapperFunctions.sol | 4 +- .../ReentrantERC20Token/ReentrantERC20Token.sol | 2 +- .../core/contracts/test/TestLibs/TestLibs.sol | 152 ------------- contracts/core/package.json | 3 +- contracts/core/src/artifacts/index.ts | 2 - contracts/core/src/wrappers/index.ts | 1 - contracts/core/test/exchange/libs.ts | 125 ---------- .../test/utils/fill_order_combinatorial_utils.ts | 7 +- contracts/core/tsconfig.json | 1 - contracts/libs/.solhint.json | 20 ++ contracts/libs/README.md | 70 ++++++ contracts/libs/compiler.json | 22 ++ .../AssetProxy/libs/LibAssetProxyErrors.sol | 38 ++++ .../protocol/Exchange/libs/LibAbiEncoder.sol | 215 +++++++++++++++++ .../protocol/Exchange/libs/LibConstants.sol | 49 ++++ .../contracts/protocol/Exchange/libs/LibEIP712.sol | 87 +++++++ .../protocol/Exchange/libs/LibExchangeErrors.sol | 70 ++++++ .../protocol/Exchange/libs/LibFillResults.sol | 53 +++++ .../contracts/protocol/Exchange/libs/LibMath.sol | 253 +++++++++++++++++++++ .../contracts/protocol/Exchange/libs/LibOrder.sol | 145 ++++++++++++ .../libs/contracts/test/TestLibs/TestLibs.sol | 152 +++++++++++++ contracts/libs/package.json | 92 ++++++++ contracts/libs/src/artifacts/index.ts | 17 ++ contracts/libs/src/index.ts | 2 + contracts/libs/src/wrappers/index.ts | 6 + contracts/libs/test/exchange/libs.ts | 125 ++++++++++ contracts/libs/test/global_hooks.ts | 17 ++ contracts/libs/tsconfig.json | 18 ++ contracts/libs/tslint.json | 6 + 57 files changed, 1509 insertions(+), 1238 deletions(-) delete mode 100644 contracts/core/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol delete mode 100644 contracts/core/contracts/protocol/Exchange/libs/LibAbiEncoder.sol delete mode 100644 contracts/core/contracts/protocol/Exchange/libs/LibConstants.sol delete mode 100644 contracts/core/contracts/protocol/Exchange/libs/LibEIP712.sol delete mode 100644 contracts/core/contracts/protocol/Exchange/libs/LibExchangeErrors.sol delete mode 100644 contracts/core/contracts/protocol/Exchange/libs/LibFillResults.sol delete mode 100644 contracts/core/contracts/protocol/Exchange/libs/LibMath.sol delete mode 100644 contracts/core/contracts/protocol/Exchange/libs/LibOrder.sol delete mode 100644 contracts/core/contracts/test/TestLibs/TestLibs.sol delete mode 100644 contracts/core/test/exchange/libs.ts create mode 100644 contracts/libs/.solhint.json create mode 100644 contracts/libs/README.md create mode 100644 contracts/libs/compiler.json create mode 100644 contracts/libs/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol create mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol create mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibConstants.sol create mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibEIP712.sol create mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol create mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibFillResults.sol create mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibMath.sol create mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibOrder.sol create mode 100644 contracts/libs/contracts/test/TestLibs/TestLibs.sol create mode 100644 contracts/libs/package.json create mode 100644 contracts/libs/src/artifacts/index.ts create mode 100644 contracts/libs/src/index.ts create mode 100644 contracts/libs/src/wrappers/index.ts create mode 100644 contracts/libs/test/exchange/libs.ts create mode 100644 contracts/libs/test/global_hooks.ts create mode 100644 contracts/libs/tsconfig.json create mode 100644 contracts/libs/tslint.json (limited to 'contracts') diff --git a/contracts/core/compiler.json b/contracts/core/compiler.json index 8d9732794..7e527130a 100644 --- a/contracts/core/compiler.json +++ b/contracts/core/compiler.json @@ -44,7 +44,6 @@ "ReentrantERC20Token", "TestAssetProxyOwner", "TestAssetProxyDispatcher", - "TestLibs", "TestExchangeInternals", "TestSignatureValidator", "TestStaticCallReceiver", diff --git a/contracts/core/contracts/examples/ExchangeWrapper/ExchangeWrapper.sol b/contracts/core/contracts/examples/ExchangeWrapper/ExchangeWrapper.sol index 2fa0e3c5e..0b85f5864 100644 --- a/contracts/core/contracts/examples/ExchangeWrapper/ExchangeWrapper.sol +++ b/contracts/core/contracts/examples/ExchangeWrapper/ExchangeWrapper.sol @@ -20,7 +20,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; contract ExchangeWrapper { diff --git a/contracts/core/contracts/examples/Whitelist/Whitelist.sol b/contracts/core/contracts/examples/Whitelist/Whitelist.sol index 61c21c095..96ff47488 100644 --- a/contracts/core/contracts/examples/Whitelist/Whitelist.sol +++ b/contracts/core/contracts/examples/Whitelist/Whitelist.sol @@ -20,7 +20,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; import "@0x/contracts-utils/contracts/utils/Ownable/Ownable.sol"; diff --git a/contracts/core/contracts/extensions/DutchAuction/DutchAuction.sol b/contracts/core/contracts/extensions/DutchAuction/DutchAuction.sol index dfd4c46dc..02aafe3c9 100644 --- a/contracts/core/contracts/extensions/DutchAuction/DutchAuction.sol +++ b/contracts/core/contracts/extensions/DutchAuction/DutchAuction.sol @@ -20,7 +20,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; import "../../tokens/ERC20Token/IERC20Token.sol"; import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; diff --git a/contracts/core/contracts/extensions/Forwarder/MixinExchangeWrapper.sol b/contracts/core/contracts/extensions/Forwarder/MixinExchangeWrapper.sol index 4991c0ea5..e5f0894e7 100644 --- a/contracts/core/contracts/extensions/Forwarder/MixinExchangeWrapper.sol +++ b/contracts/core/contracts/extensions/Forwarder/MixinExchangeWrapper.sol @@ -21,10 +21,10 @@ pragma experimental ABIEncoderV2; import "./libs/LibConstants.sol"; import "./mixins/MExchangeWrapper.sol"; -import "../../protocol/Exchange/libs/LibAbiEncoder.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; -import "../../protocol/Exchange/libs/LibFillResults.sol"; -import "../../protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; contract MixinExchangeWrapper is diff --git a/contracts/core/contracts/extensions/Forwarder/MixinForwarderCore.sol b/contracts/core/contracts/extensions/Forwarder/MixinForwarderCore.sol index a5dd99225..d7b50903a 100644 --- a/contracts/core/contracts/extensions/Forwarder/MixinForwarderCore.sol +++ b/contracts/core/contracts/extensions/Forwarder/MixinForwarderCore.sol @@ -25,9 +25,9 @@ import "./mixins/MAssets.sol"; import "./mixins/MExchangeWrapper.sol"; import "./interfaces/IForwarderCore.sol"; import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; -import "../../protocol/Exchange/libs/LibFillResults.sol"; -import "../../protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; contract MixinForwarderCore is diff --git a/contracts/core/contracts/extensions/Forwarder/MixinWeth.sol b/contracts/core/contracts/extensions/Forwarder/MixinWeth.sol index d2814a49b..2b2277004 100644 --- a/contracts/core/contracts/extensions/Forwarder/MixinWeth.sol +++ b/contracts/core/contracts/extensions/Forwarder/MixinWeth.sol @@ -18,7 +18,7 @@ pragma solidity 0.4.24; -import "../../protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; import "./libs/LibConstants.sol"; import "./mixins/MWeth.sol"; diff --git a/contracts/core/contracts/extensions/Forwarder/interfaces/IForwarderCore.sol b/contracts/core/contracts/extensions/Forwarder/interfaces/IForwarderCore.sol index 74c7da01d..223a3d217 100644 --- a/contracts/core/contracts/extensions/Forwarder/interfaces/IForwarderCore.sol +++ b/contracts/core/contracts/extensions/Forwarder/interfaces/IForwarderCore.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../../../protocol/Exchange/libs/LibOrder.sol"; -import "../../../protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; contract IForwarderCore { diff --git a/contracts/core/contracts/extensions/Forwarder/mixins/MExchangeWrapper.sol b/contracts/core/contracts/extensions/Forwarder/mixins/MExchangeWrapper.sol index 13c26b03a..75287a494 100644 --- a/contracts/core/contracts/extensions/Forwarder/mixins/MExchangeWrapper.sol +++ b/contracts/core/contracts/extensions/Forwarder/mixins/MExchangeWrapper.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../../../protocol/Exchange/libs/LibOrder.sol"; -import "../../../protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; contract MExchangeWrapper { diff --git a/contracts/core/contracts/extensions/OrderValidator/OrderValidator.sol b/contracts/core/contracts/extensions/OrderValidator/OrderValidator.sol index e3f4f6832..b4ead1776 100644 --- a/contracts/core/contracts/extensions/OrderValidator/OrderValidator.sol +++ b/contracts/core/contracts/extensions/OrderValidator/OrderValidator.sol @@ -20,7 +20,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; import "../../tokens/ERC20Token/IERC20Token.sol"; import "../../tokens/ERC721Token/IERC721Token.sol"; import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; diff --git a/contracts/core/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol b/contracts/core/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol deleted file mode 100644 index 1d9a70cc1..000000000 --- a/contracts/core/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol +++ /dev/null @@ -1,38 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -// solhint-disable -pragma solidity 0.4.24; - - -/// @dev This contract documents the revert reasons used in the AssetProxy contracts. -/// This contract is intended to serve as a reference, but is not actually used for efficiency reasons. -contract LibAssetProxyErrors { - - /// Authorizable errors /// - string constant SENDER_NOT_AUTHORIZED = "SENDER_NOT_AUTHORIZED"; // Sender not authorized to call this method. - string constant TARGET_NOT_AUTHORIZED = "TARGET_NOT_AUTHORIZED"; // Target address not authorized to call this method. - string constant TARGET_ALREADY_AUTHORIZED = "TARGET_ALREADY_AUTHORIZED"; // Target address must not already be authorized. - string constant INDEX_OUT_OF_BOUNDS = "INDEX_OUT_OF_BOUNDS"; // Specified array index is out of bounds. - string constant AUTHORIZED_ADDRESS_MISMATCH = "AUTHORIZED_ADDRESS_MISMATCH"; // Address at index does not match given target address. - - /// Transfer errors /// - string constant INVALID_AMOUNT = "INVALID_AMOUNT"; // Transfer amount must equal 1. - string constant TRANSFER_FAILED = "TRANSFER_FAILED"; // Transfer failed. - string constant LENGTH_GREATER_THAN_131_REQUIRED = "LENGTH_GREATER_THAN_131_REQUIRED"; // Byte array must have a length greater than 0. -} diff --git a/contracts/core/contracts/protocol/Exchange/Exchange.sol b/contracts/core/contracts/protocol/Exchange/Exchange.sol index ead36009f..c199d3c39 100644 --- a/contracts/core/contracts/protocol/Exchange/Exchange.sol +++ b/contracts/core/contracts/protocol/Exchange/Exchange.sol @@ -19,7 +19,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "./libs/LibConstants.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibConstants.sol"; import "./MixinExchangeCore.sol"; import "./MixinSignatureValidator.sol"; import "./MixinWrapperFunctions.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/MixinExchangeCore.sol b/contracts/core/contracts/protocol/Exchange/MixinExchangeCore.sol index da721f78f..befd17659 100644 --- a/contracts/core/contracts/protocol/Exchange/MixinExchangeCore.sol +++ b/contracts/core/contracts/protocol/Exchange/MixinExchangeCore.sol @@ -20,10 +20,10 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "@0x/contracts-utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol"; -import "./libs/LibConstants.sol"; -import "./libs/LibFillResults.sol"; -import "./libs/LibOrder.sol"; -import "./libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibConstants.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; import "./mixins/MExchangeCore.sol"; import "./mixins/MSignatureValidator.sol"; import "./mixins/MTransactions.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/MixinMatchOrders.sol b/contracts/core/contracts/protocol/Exchange/MixinMatchOrders.sol index da1e9d270..fee2a1a1c 100644 --- a/contracts/core/contracts/protocol/Exchange/MixinMatchOrders.sol +++ b/contracts/core/contracts/protocol/Exchange/MixinMatchOrders.sol @@ -15,10 +15,10 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "@0x/contracts-utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol"; -import "./libs/LibConstants.sol"; -import "./libs/LibMath.sol"; -import "./libs/LibOrder.sol"; -import "./libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibConstants.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; import "./mixins/MExchangeCore.sol"; import "./mixins/MMatchOrders.sol"; import "./mixins/MTransactions.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/MixinTransactions.sol b/contracts/core/contracts/protocol/Exchange/MixinTransactions.sol index 3a76ca202..2f9eaca4f 100644 --- a/contracts/core/contracts/protocol/Exchange/MixinTransactions.sol +++ b/contracts/core/contracts/protocol/Exchange/MixinTransactions.sol @@ -17,10 +17,10 @@ */ pragma solidity 0.4.24; -import "./libs/LibExchangeErrors.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol"; import "./mixins/MSignatureValidator.sol"; import "./mixins/MTransactions.sol"; -import "./libs/LibEIP712.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibEIP712.sol"; contract MixinTransactions is diff --git a/contracts/core/contracts/protocol/Exchange/MixinWrapperFunctions.sol b/contracts/core/contracts/protocol/Exchange/MixinWrapperFunctions.sol index 415246358..6a5c775fa 100644 --- a/contracts/core/contracts/protocol/Exchange/MixinWrapperFunctions.sol +++ b/contracts/core/contracts/protocol/Exchange/MixinWrapperFunctions.sol @@ -20,10 +20,10 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "@0x/contracts-utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol"; -import "./libs/LibMath.sol"; -import "./libs/LibOrder.sol"; -import "./libs/LibFillResults.sol"; -import "./libs/LibAbiEncoder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol"; import "./mixins/MExchangeCore.sol"; import "./mixins/MWrapperFunctions.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/interfaces/IExchangeCore.sol b/contracts/core/contracts/protocol/Exchange/interfaces/IExchangeCore.sol index 9995e0385..36e1d181e 100644 --- a/contracts/core/contracts/protocol/Exchange/interfaces/IExchangeCore.sol +++ b/contracts/core/contracts/protocol/Exchange/interfaces/IExchangeCore.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../libs/LibOrder.sol"; -import "../libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; contract IExchangeCore { diff --git a/contracts/core/contracts/protocol/Exchange/interfaces/IMatchOrders.sol b/contracts/core/contracts/protocol/Exchange/interfaces/IMatchOrders.sol index 73447f3ae..114c57503 100644 --- a/contracts/core/contracts/protocol/Exchange/interfaces/IMatchOrders.sol +++ b/contracts/core/contracts/protocol/Exchange/interfaces/IMatchOrders.sol @@ -18,8 +18,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../libs/LibOrder.sol"; -import "../libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; contract IMatchOrders { diff --git a/contracts/core/contracts/protocol/Exchange/interfaces/IWrapperFunctions.sol b/contracts/core/contracts/protocol/Exchange/interfaces/IWrapperFunctions.sol index 56a533646..01deef4ae 100644 --- a/contracts/core/contracts/protocol/Exchange/interfaces/IWrapperFunctions.sol +++ b/contracts/core/contracts/protocol/Exchange/interfaces/IWrapperFunctions.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../libs/LibOrder.sol"; -import "../libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; contract IWrapperFunctions { diff --git a/contracts/core/contracts/protocol/Exchange/libs/LibAbiEncoder.sol b/contracts/core/contracts/protocol/Exchange/libs/LibAbiEncoder.sol deleted file mode 100644 index 4aad37709..000000000 --- a/contracts/core/contracts/protocol/Exchange/libs/LibAbiEncoder.sol +++ /dev/null @@ -1,215 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; -pragma experimental ABIEncoderV2; - -import "./LibOrder.sol"; - - -contract LibAbiEncoder { - - /// @dev ABI encodes calldata for `fillOrder`. - /// @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 ABI encoded calldata for `fillOrder`. - function abiEncodeFillOrder( - LibOrder.Order memory order, - uint256 takerAssetFillAmount, - bytes memory signature - ) - internal - pure - returns (bytes memory fillOrderCalldata) - { - // 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 makerAssetData (*) | - // | | 0x160 | | 12. Offset to takerAssetData (*) | - // | | 0x180 | 32 | makerAssetData Length | - // | | 0x1A0 | ** | makerAssetData Contents | - // | | 0x1C0 | 32 | takerAssetData Length | - // | | 0x1E0 | ** | takerAssetData 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 - - assembly { - - // Areas below may use the following variables: - // 1. Start -- Start of this area in memory - // 2. 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. 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 - fillOrderCalldata := mload(0x40) - // bytes4(keccak256("fillOrder((address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes),uint256,bytes)")) - // = 0xb4be83d5 - // Leave 0x20 bytes to store the length - mstore(add(fillOrderCalldata, 0x20), 0xb4be83d500000000000000000000000000000000000000000000000000000000) - let headerAreaEnd := add(fillOrderCalldata, 0x24) - - /////// 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 makerAssetData - mstore(add(dataAreaEnd, 0x160), mload(add(sourceOffset, 0x160))) // Offset to takerAssetData - dataAreaEnd := add(dataAreaEnd, 0x180) - sourceOffset := add(sourceOffset, 0x180) - - // Write offset to - mstore(add(dataAreaStart, mul(10, 0x20)), sub(dataAreaEnd, dataAreaStart)) - - // Calculate length of - sourceOffset := mload(add(order, 0x140)) // makerAssetData - arrayLenBytes := mload(sourceOffset) - sourceOffset := add(sourceOffset, 0x20) - arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20) - - // Write length of - mstore(dataAreaEnd, arrayLenBytes) - dataAreaEnd := add(dataAreaEnd, 0x20) - - // Write contents of - 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 - mstore(add(dataAreaStart, mul(11, 0x20)), sub(dataAreaEnd, dataAreaStart)) - - // Calculate length of - sourceOffset := mload(add(order, 0x160)) // takerAssetData - arrayLenBytes := mload(sourceOffset) - sourceOffset := add(sourceOffset, 0x20) - arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20) - - // Write length of - mstore(dataAreaEnd, arrayLenBytes) - dataAreaEnd := add(dataAreaEnd, 0x20) - - // Write contents of - 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) - } - - // Set length of calldata - mstore(fillOrderCalldata, sub(dataAreaEnd, add(fillOrderCalldata, 0x20))) - - // Increment free memory pointer - mstore(0x40, dataAreaEnd) - } - - return fillOrderCalldata; - } -} diff --git a/contracts/core/contracts/protocol/Exchange/libs/LibConstants.sol b/contracts/core/contracts/protocol/Exchange/libs/LibConstants.sol deleted file mode 100644 index 8d2732cd3..000000000 --- a/contracts/core/contracts/protocol/Exchange/libs/LibConstants.sol +++ /dev/null @@ -1,49 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - - -// solhint-disable max-line-length -contract LibConstants { - - // Asset data for ZRX token. Used for fee transfers. - // @TODO: Hardcode constant when we deploy. Currently - // not constant to make testing easier. - - // The proxyId for ZRX_ASSET_DATA is bytes4(keccak256("ERC20Token(address)")) = 0xf47261b0 - - // Kovan ZRX address is 0x6ff6c0ff1d68b964901f986d4c9fa3ac68346570. - // The ABI encoded proxyId and address is 0xf47261b00000000000000000000000006ff6c0ff1d68b964901f986d4c9fa3ac68346570 - // bytes constant public ZRX_ASSET_DATA = "\xf4\x72\x61\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x6f\xf6\xc0\xff\x1d\x68\xb9\x64\x90\x1f\x98\x6d\x4c\x9f\xa3\xac\x68\x34\x65\x70"; - - // Mainnet ZRX address is 0xe41d2489571d322189246dafa5ebde1f4699f498. - // The ABI encoded proxyId and address is 0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498 - // bytes constant public ZRX_ASSET_DATA = "\xf4\x72\x61\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe4\x1d\x24\x89\x57\x1d\x32\x21\x89\x24\x6d\xaf\xa5\xeb\xde\x1f\x46\x99\xf4\x98"; - - // solhint-disable-next-line var-name-mixedcase - bytes public ZRX_ASSET_DATA; - - // @TODO: Remove when we deploy. - constructor (bytes memory zrxAssetData) - public - { - ZRX_ASSET_DATA = zrxAssetData; - } -} -// solhint-enable max-line-length diff --git a/contracts/core/contracts/protocol/Exchange/libs/LibEIP712.sol b/contracts/core/contracts/protocol/Exchange/libs/LibEIP712.sol deleted file mode 100644 index 203edc1fd..000000000 --- a/contracts/core/contracts/protocol/Exchange/libs/LibEIP712.sol +++ /dev/null @@ -1,87 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - - -contract LibEIP712 { - - // EIP191 header for EIP712 prefix - string constant internal EIP191_HEADER = "\x19\x01"; - - // EIP712 Domain Name value - string constant internal EIP712_DOMAIN_NAME = "0x Protocol"; - - // EIP712 Domain Version value - string constant internal EIP712_DOMAIN_VERSION = "2"; - - // Hash of the EIP712 Domain Separator Schema - bytes32 constant internal EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256(abi.encodePacked( - "EIP712Domain(", - "string name,", - "string version,", - "address verifyingContract", - ")" - )); - - // Hash of the EIP712 Domain Separator data - // solhint-disable-next-line var-name-mixedcase - bytes32 public EIP712_DOMAIN_HASH; - - constructor () - public - { - EIP712_DOMAIN_HASH = keccak256(abi.encodePacked( - EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH, - keccak256(bytes(EIP712_DOMAIN_NAME)), - keccak256(bytes(EIP712_DOMAIN_VERSION)), - bytes32(address(this)) - )); - } - - /// @dev Calculates EIP712 encoding for a hash struct in this EIP712 Domain. - /// @param hashStruct The EIP712 hash struct. - /// @return EIP712 hash applied to this EIP712 Domain. - function hashEIP712Message(bytes32 hashStruct) - internal - view - returns (bytes32 result) - { - bytes32 eip712DomainHash = EIP712_DOMAIN_HASH; - - // Assembly for more efficient computing: - // keccak256(abi.encodePacked( - // EIP191_HEADER, - // EIP712_DOMAIN_HASH, - // hashStruct - // )); - - assembly { - // Load free memory pointer - let memPtr := mload(64) - - mstore(memPtr, 0x1901000000000000000000000000000000000000000000000000000000000000) // EIP191 header - mstore(add(memPtr, 2), eip712DomainHash) // EIP712 domain hash - mstore(add(memPtr, 34), hashStruct) // Hash of struct - - // Compute hash - result := keccak256(memPtr, 66) - } - return result; - } -} diff --git a/contracts/core/contracts/protocol/Exchange/libs/LibExchangeErrors.sol b/contracts/core/contracts/protocol/Exchange/libs/LibExchangeErrors.sol deleted file mode 100644 index a0f75bc06..000000000 --- a/contracts/core/contracts/protocol/Exchange/libs/LibExchangeErrors.sol +++ /dev/null @@ -1,70 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -// solhint-disable -pragma solidity 0.4.24; - - -/// @dev This contract documents the revert reasons used in the Exchange contract. -/// This contract is intended to serve as a reference, but is not actually used for efficiency reasons. -contract LibExchangeErrors { - - /// Order validation errors /// - string constant ORDER_UNFILLABLE = "ORDER_UNFILLABLE"; // Order cannot be filled. - string constant INVALID_MAKER = "INVALID_MAKER"; // Invalid makerAddress. - string constant INVALID_TAKER = "INVALID_TAKER"; // Invalid takerAddress. - string constant INVALID_SENDER = "INVALID_SENDER"; // Invalid `msg.sender`. - string constant INVALID_ORDER_SIGNATURE = "INVALID_ORDER_SIGNATURE"; // Signature validation failed. - - /// fillOrder validation errors /// - string constant INVALID_TAKER_AMOUNT = "INVALID_TAKER_AMOUNT"; // takerAssetFillAmount cannot equal 0. - string constant ROUNDING_ERROR = "ROUNDING_ERROR"; // Rounding error greater than 0.1% of takerAssetFillAmount. - - /// Signature validation errors /// - string constant INVALID_SIGNATURE = "INVALID_SIGNATURE"; // Signature validation failed. - string constant SIGNATURE_ILLEGAL = "SIGNATURE_ILLEGAL"; // Signature type is illegal. - string constant SIGNATURE_UNSUPPORTED = "SIGNATURE_UNSUPPORTED"; // Signature type unsupported. - - /// cancelOrdersUptTo errors /// - string constant INVALID_NEW_ORDER_EPOCH = "INVALID_NEW_ORDER_EPOCH"; // Specified salt must be greater than or equal to existing orderEpoch. - - /// fillOrKillOrder errors /// - string constant COMPLETE_FILL_FAILED = "COMPLETE_FILL_FAILED"; // Desired takerAssetFillAmount could not be completely filled. - - /// matchOrders errors /// - string constant NEGATIVE_SPREAD_REQUIRED = "NEGATIVE_SPREAD_REQUIRED"; // Matched orders must have a negative spread. - - /// Transaction errors /// - string constant REENTRANCY_ILLEGAL = "REENTRANCY_ILLEGAL"; // Recursive reentrancy is not allowed. - string constant INVALID_TX_HASH = "INVALID_TX_HASH"; // Transaction has already been executed. - string constant INVALID_TX_SIGNATURE = "INVALID_TX_SIGNATURE"; // Signature validation failed. - string constant FAILED_EXECUTION = "FAILED_EXECUTION"; // Transaction execution failed. - - /// registerAssetProxy errors /// - string constant ASSET_PROXY_ALREADY_EXISTS = "ASSET_PROXY_ALREADY_EXISTS"; // AssetProxy with same id already exists. - - /// dispatchTransferFrom errors /// - string constant ASSET_PROXY_DOES_NOT_EXIST = "ASSET_PROXY_DOES_NOT_EXIST"; // No assetProxy registered at given id. - string constant TRANSFER_FAILED = "TRANSFER_FAILED"; // Asset transfer unsuccesful. - - /// Length validation errors /// - string constant LENGTH_GREATER_THAN_0_REQUIRED = "LENGTH_GREATER_THAN_0_REQUIRED"; // Byte array must have a length greater than 0. - string constant LENGTH_GREATER_THAN_3_REQUIRED = "LENGTH_GREATER_THAN_3_REQUIRED"; // Byte array must have a length greater than 3. - string constant LENGTH_0_REQUIRED = "LENGTH_0_REQUIRED"; // Byte array must have a length of 0. - string constant LENGTH_65_REQUIRED = "LENGTH_65_REQUIRED"; // Byte array must have a length of 65. -} diff --git a/contracts/core/contracts/protocol/Exchange/libs/LibFillResults.sol b/contracts/core/contracts/protocol/Exchange/libs/LibFillResults.sol deleted file mode 100644 index fbd9950bf..000000000 --- a/contracts/core/contracts/protocol/Exchange/libs/LibFillResults.sol +++ /dev/null @@ -1,53 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - -import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; - - -contract LibFillResults is - SafeMath -{ - struct FillResults { - uint256 makerAssetFilledAmount; // Total amount of makerAsset(s) filled. - uint256 takerAssetFilledAmount; // Total amount of takerAsset(s) filled. - uint256 makerFeePaid; // Total amount of ZRX paid by maker(s) to feeRecipient(s). - uint256 takerFeePaid; // Total amount of ZRX paid by taker to feeRecipients(s). - } - - struct MatchedFillResults { - FillResults left; // Amounts filled and fees paid of left order. - FillResults right; // Amounts filled and fees paid of right order. - uint256 leftMakerAssetSpreadAmount; // Spread between price of left and right order, denominated in the left order's makerAsset, paid to taker. - } - - /// @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); - } -} diff --git a/contracts/core/contracts/protocol/Exchange/libs/LibMath.sol b/contracts/core/contracts/protocol/Exchange/libs/LibMath.sol deleted file mode 100644 index b24876a9c..000000000 --- a/contracts/core/contracts/protocol/Exchange/libs/LibMath.sol +++ /dev/null @@ -1,253 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - -import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; - - -contract LibMath is - SafeMath -{ - /// @dev Calculates partial value given a numerator and denominator rounded down. - /// Reverts if rounding error is >= 0.1% - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to calculate partial of. - /// @return Partial value of target rounded down. - function safeGetPartialAmountFloor( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (uint256 partialAmount) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - require( - !isRoundingErrorFloor( - numerator, - denominator, - target - ), - "ROUNDING_ERROR" - ); - - partialAmount = safeDiv( - safeMul(numerator, target), - denominator - ); - return partialAmount; - } - - /// @dev Calculates partial value given a numerator and denominator rounded down. - /// Reverts if rounding error is >= 0.1% - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to calculate partial of. - /// @return Partial value of target rounded up. - function safeGetPartialAmountCeil( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (uint256 partialAmount) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - require( - !isRoundingErrorCeil( - numerator, - denominator, - target - ), - "ROUNDING_ERROR" - ); - - // safeDiv computes `floor(a / b)`. We use the identity (a, b integer): - // ceil(a / b) = floor((a + b - 1) / b) - // To implement `ceil(a / b)` using safeDiv. - partialAmount = safeDiv( - safeAdd( - safeMul(numerator, target), - safeSub(denominator, 1) - ), - denominator - ); - return partialAmount; - } - - /// @dev Calculates partial value given a numerator and denominator rounded down. - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to calculate partial of. - /// @return Partial value of target rounded down. - function getPartialAmountFloor( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (uint256 partialAmount) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - partialAmount = safeDiv( - safeMul(numerator, target), - denominator - ); - return partialAmount; - } - - /// @dev Calculates partial value given a numerator and denominator rounded down. - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to calculate partial of. - /// @return Partial value of target rounded up. - function getPartialAmountCeil( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (uint256 partialAmount) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - // safeDiv computes `floor(a / b)`. We use the identity (a, b integer): - // ceil(a / b) = floor((a + b - 1) / b) - // To implement `ceil(a / b)` using safeDiv. - partialAmount = safeDiv( - safeAdd( - safeMul(numerator, target), - safeSub(denominator, 1) - ), - denominator - ); - return partialAmount; - } - - /// @dev Checks if rounding error >= 0.1% when rounding down. - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to multiply with numerator/denominator. - /// @return Rounding error is present. - function isRoundingErrorFloor( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (bool isError) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - // The absolute rounding error is the difference between the rounded - // value and the ideal value. The relative rounding error is the - // absolute rounding error divided by the absolute value of the - // ideal value. This is undefined when the ideal value is zero. - // - // The ideal value is `numerator * target / denominator`. - // Let's call `numerator * target % denominator` the remainder. - // The absolute error is `remainder / denominator`. - // - // When the ideal value is zero, we require the absolute error to - // be zero. Fortunately, this is always the case. The ideal value is - // zero iff `numerator == 0` and/or `target == 0`. In this case the - // remainder and absolute error are also zero. - if (target == 0 || numerator == 0) { - return false; - } - - // Otherwise, we want the relative rounding error to be strictly - // less than 0.1%. - // The relative error is `remainder / (numerator * target)`. - // We want the relative error less than 1 / 1000: - // remainder / (numerator * denominator) < 1 / 1000 - // or equivalently: - // 1000 * remainder < numerator * target - // so we have a rounding error iff: - // 1000 * remainder >= numerator * target - uint256 remainder = mulmod( - target, - numerator, - denominator - ); - isError = safeMul(1000, remainder) >= safeMul(numerator, target); - return isError; - } - - /// @dev Checks if rounding error >= 0.1% when rounding up. - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to multiply with numerator/denominator. - /// @return Rounding error is present. - function isRoundingErrorCeil( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (bool isError) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - // See the comments in `isRoundingError`. - if (target == 0 || numerator == 0) { - // When either is zero, the ideal value and rounded value are zero - // and there is no rounding error. (Although the relative error - // is undefined.) - return false; - } - // Compute remainder as before - uint256 remainder = mulmod( - target, - numerator, - denominator - ); - remainder = safeSub(denominator, remainder) % denominator; - isError = safeMul(1000, remainder) >= safeMul(numerator, target); - return isError; - } -} diff --git a/contracts/core/contracts/protocol/Exchange/libs/LibOrder.sol b/contracts/core/contracts/protocol/Exchange/libs/LibOrder.sol deleted file mode 100644 index 0fe7c2161..000000000 --- a/contracts/core/contracts/protocol/Exchange/libs/LibOrder.sol +++ /dev/null @@ -1,145 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - -import "./LibEIP712.sol"; - - -contract LibOrder is - LibEIP712 -{ - // Hash for the EIP712 Order Schema - bytes32 constant internal EIP712_ORDER_SCHEMA_HASH = keccak256(abi.encodePacked( - "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", - ")" - )); - - // A valid order remains fillable until it is expired, fully filled, or cancelled. - // An order's state is unaffected by external factors, like account balances. - enum OrderStatus { - INVALID, // Default value - INVALID_MAKER_ASSET_AMOUNT, // Order does not have a valid maker asset amount - INVALID_TAKER_ASSET_AMOUNT, // Order does not have a valid taker asset amount - FILLABLE, // Order is fillable - EXPIRED, // Order has already expired - FULLY_FILLED, // Order is fully filled - CANCELLED // Order has been cancelled - } - - // solhint-disable max-line-length - struct Order { - address makerAddress; // Address that created the order. - address takerAddress; // Address that is allowed to fill the order. If set to 0, any address is allowed to fill the order. - address feeRecipientAddress; // Address that will recieve fees when order is filled. - address senderAddress; // Address that is allowed to call Exchange contract methods that affect this order. If set to 0, any address is allowed to call these methods. - uint256 makerAssetAmount; // Amount of makerAsset being offered by maker. Must be greater than 0. - uint256 takerAssetAmount; // Amount of takerAsset being bid on by maker. Must be greater than 0. - uint256 makerFee; // Amount of ZRX paid to feeRecipient by maker when order is filled. If set to 0, no transfer of ZRX from maker to feeRecipient will be attempted. - uint256 takerFee; // Amount of ZRX paid to feeRecipient by taker when order is filled. If set to 0, no transfer of ZRX from taker to feeRecipient will be attempted. - uint256 expirationTimeSeconds; // Timestamp in seconds at which order expires. - uint256 salt; // Arbitrary number to facilitate uniqueness of the order's hash. - bytes makerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring makerAsset. The last byte references the id of this proxy. - bytes takerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring takerAsset. The last byte references the id of this proxy. - } - // solhint-enable max-line-length - - struct OrderInfo { - uint8 orderStatus; // Status that describes order's validity and fillability. - bytes32 orderHash; // EIP712 hash of the order (see LibOrder.getOrderHash). - uint256 orderTakerAssetFilledAmount; // Amount of order that has already been filled. - } - - /// @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) - { - orderHash = hashEIP712Message(hashOrder(order)); - return orderHash; - } - - /// @dev Calculates EIP712 hash of the order. - /// @param order The order structure. - /// @return EIP712 hash of the order. - function hashOrder(Order memory order) - internal - pure - returns (bytes32 result) - { - bytes32 schemaHash = EIP712_ORDER_SCHEMA_HASH; - bytes32 makerAssetDataHash = keccak256(order.makerAssetData); - bytes32 takerAssetDataHash = keccak256(order.takerAssetData); - - // Assembly for more efficiently computing: - // keccak256(abi.encodePacked( - // EIP712_ORDER_SCHEMA_HASH, - // bytes32(order.makerAddress), - // bytes32(order.takerAddress), - // bytes32(order.feeRecipientAddress), - // bytes32(order.senderAddress), - // order.makerAssetAmount, - // order.takerAssetAmount, - // order.makerFee, - // order.takerFee, - // order.expirationTimeSeconds, - // order.salt, - // keccak256(order.makerAssetData), - // keccak256(order.takerAssetData) - // )); - - assembly { - // Calculate memory addresses that will be swapped out before hashing - let pos1 := sub(order, 32) - let pos2 := add(order, 320) - let pos3 := add(order, 352) - - // Backup - let temp1 := mload(pos1) - let temp2 := mload(pos2) - let temp3 := mload(pos3) - - // Hash in place - mstore(pos1, schemaHash) - mstore(pos2, makerAssetDataHash) - mstore(pos3, takerAssetDataHash) - result := keccak256(pos1, 416) - - // Restore - mstore(pos1, temp1) - mstore(pos2, temp2) - mstore(pos3, temp3) - } - return result; - } -} diff --git a/contracts/core/contracts/protocol/Exchange/mixins/MExchangeCore.sol b/contracts/core/contracts/protocol/Exchange/mixins/MExchangeCore.sol index 742499568..ab3e9d6e2 100644 --- a/contracts/core/contracts/protocol/Exchange/mixins/MExchangeCore.sol +++ b/contracts/core/contracts/protocol/Exchange/mixins/MExchangeCore.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../libs/LibOrder.sol"; -import "../libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; import "../interfaces/IExchangeCore.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/mixins/MMatchOrders.sol b/contracts/core/contracts/protocol/Exchange/mixins/MMatchOrders.sol index 96fa34bc0..719e193e7 100644 --- a/contracts/core/contracts/protocol/Exchange/mixins/MMatchOrders.sol +++ b/contracts/core/contracts/protocol/Exchange/mixins/MMatchOrders.sol @@ -18,8 +18,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../libs/LibOrder.sol"; -import "../libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; import "../interfaces/IMatchOrders.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/mixins/MWrapperFunctions.sol b/contracts/core/contracts/protocol/Exchange/mixins/MWrapperFunctions.sol index 4adfbde01..763af8885 100644 --- a/contracts/core/contracts/protocol/Exchange/mixins/MWrapperFunctions.sol +++ b/contracts/core/contracts/protocol/Exchange/mixins/MWrapperFunctions.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../libs/LibOrder.sol"; -import "../libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; import "../interfaces/IWrapperFunctions.sol"; diff --git a/contracts/core/contracts/test/ReentrantERC20Token/ReentrantERC20Token.sol b/contracts/core/contracts/test/ReentrantERC20Token/ReentrantERC20Token.sol index 3e9091931..2336e552f 100644 --- a/contracts/core/contracts/test/ReentrantERC20Token/ReentrantERC20Token.sol +++ b/contracts/core/contracts/test/ReentrantERC20Token/ReentrantERC20Token.sol @@ -22,7 +22,7 @@ pragma experimental ABIEncoderV2; import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; import "../../tokens/ERC20Token/ERC20Token.sol"; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; // solhint-disable no-unused-vars diff --git a/contracts/core/contracts/test/TestLibs/TestLibs.sol b/contracts/core/contracts/test/TestLibs/TestLibs.sol deleted file mode 100644 index a10f981fc..000000000 --- a/contracts/core/contracts/test/TestLibs/TestLibs.sol +++ /dev/null @@ -1,152 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; -pragma experimental ABIEncoderV2; - -import "../../protocol/Exchange/libs/LibMath.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; -import "../../protocol/Exchange/libs/LibFillResults.sol"; -import "../../protocol/Exchange/libs/LibAbiEncoder.sol"; - - -contract TestLibs is - LibMath, - LibOrder, - LibFillResults, - LibAbiEncoder -{ - function publicAbiEncodeFillOrder( - Order memory order, - uint256 takerAssetFillAmount, - bytes memory signature - ) - public - pure - returns (bytes memory fillOrderCalldata) - { - fillOrderCalldata = abiEncodeFillOrder( - order, - takerAssetFillAmount, - signature - ); - return fillOrderCalldata; - } - - function publicGetPartialAmountFloor( - uint256 numerator, - uint256 denominator, - uint256 target - ) - public - pure - returns (uint256 partialAmount) - { - partialAmount = getPartialAmountFloor( - numerator, - denominator, - target - ); - return partialAmount; - } - - function publicGetPartialAmountCeil( - uint256 numerator, - uint256 denominator, - uint256 target - ) - public - pure - returns (uint256 partialAmount) - { - partialAmount = getPartialAmountCeil( - numerator, - denominator, - target - ); - return partialAmount; - } - - function publicIsRoundingErrorFloor( - uint256 numerator, - uint256 denominator, - uint256 target - ) - public - pure - returns (bool isError) - { - isError = isRoundingErrorFloor( - numerator, - denominator, - target - ); - return isError; - } - - function publicIsRoundingErrorCeil( - uint256 numerator, - uint256 denominator, - uint256 target - ) - public - pure - returns (bool isError) - { - isError = isRoundingErrorCeil( - numerator, - denominator, - target - ); - return isError; - } - - function publicGetOrderHash(Order memory order) - public - view - returns (bytes32 orderHash) - { - orderHash = getOrderHash(order); - return orderHash; - } - - function getOrderSchemaHash() - public - pure - returns (bytes32) - { - return EIP712_ORDER_SCHEMA_HASH; - } - - function getDomainSeparatorSchemaHash() - public - pure - returns (bytes32) - { - return EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH; - } - - function publicAddFillResults(FillResults memory totalFillResults, FillResults memory singleFillResults) - public - pure - returns (FillResults memory) - { - addFillResults(totalFillResults, singleFillResults); - return totalFillResults; - } -} diff --git a/contracts/core/package.json b/contracts/core/package.json index 701e1866e..43fa9370e 100644 --- a/contracts/core/package.json +++ b/contracts/core/package.json @@ -33,7 +33,7 @@ "lint-contracts": "solhint contracts/**/**/**/**/*.sol" }, "config": { - "abis": "generated-artifacts/@(AssetProxyOwner|DummyERC20Token|DummyERC721Receiver|DummyERC721Token|DummyMultipleReturnERC20Token|DummyNoReturnERC20Token|DutchAuction|ERC20Token|ERC20Proxy|ERC721Token|ERC721Proxy|Forwarder|Exchange|ExchangeWrapper|IAssetData|IAssetProxy|InvalidERC721Receiver|MixinAuthorizable|MultiAssetProxy|OrderValidator|ReentrantERC20Token|TestAssetProxyOwner|TestAssetProxyDispatcher|TestConstants|TestExchangeInternals|TestLibBytes|TestLibs|TestSignatureValidator|TestStaticCallReceiver|Validator|Wallet|Whitelist|WETH9|ZRXToken).json" + "abis": "generated-artifacts/@(AssetProxyOwner|DummyERC20Token|DummyERC721Receiver|DummyERC721Token|DummyMultipleReturnERC20Token|DummyNoReturnERC20Token|DutchAuction|ERC20Token|ERC20Proxy|ERC721Token|ERC721Proxy|Forwarder|Exchange|ExchangeWrapper|IAssetData|IAssetProxy|InvalidERC721Receiver|MixinAuthorizable|MultiAssetProxy|OrderValidator|ReentrantERC20Token|TestAssetProxyOwner|TestAssetProxyDispatcher|TestConstants|TestExchangeInternals|TestLibBytes|TestSignatureValidator|TestStaticCallReceiver|Validator|Wallet|Whitelist|WETH9|ZRXToken).json" }, "repository": { "type": "git", @@ -76,6 +76,7 @@ "@0x/order-utils": "^3.0.4", "@0x/contracts-multisig": "^1.0.0", "@0x/contracts-utils": "^1.0.0", + "@0x/contracts-libs": "^1.0.0", "@0x/types": "^1.3.0", "@0x/typescript-typings": "^3.0.4", "@0x/utils": "^2.0.6", diff --git a/contracts/core/src/artifacts/index.ts b/contracts/core/src/artifacts/index.ts index b925ec87f..d578c36fe 100644 --- a/contracts/core/src/artifacts/index.ts +++ b/contracts/core/src/artifacts/index.ts @@ -26,7 +26,6 @@ import * as ReentrantERC20Token from '../../generated-artifacts/ReentrantERC20To import * as TestAssetProxyDispatcher from '../../generated-artifacts/TestAssetProxyDispatcher.json'; import * as TestAssetProxyOwner from '../../generated-artifacts/TestAssetProxyOwner.json'; import * as TestExchangeInternals from '../../generated-artifacts/TestExchangeInternals.json'; -import * as TestLibs from '../../generated-artifacts/TestLibs.json'; import * as TestSignatureValidator from '../../generated-artifacts/TestSignatureValidator.json'; import * as TestStaticCallReceiver from '../../generated-artifacts/TestStaticCallReceiver.json'; import * as Validator from '../../generated-artifacts/Validator.json'; @@ -62,7 +61,6 @@ export const artifacts = { TestAssetProxyDispatcher: TestAssetProxyDispatcher as ContractArtifact, TestAssetProxyOwner: TestAssetProxyOwner as ContractArtifact, TestExchangeInternals: TestExchangeInternals as ContractArtifact, - TestLibs: TestLibs as ContractArtifact, TestSignatureValidator: TestSignatureValidator as ContractArtifact, TestStaticCallReceiver: TestStaticCallReceiver as ContractArtifact, Validator: Validator as ContractArtifact, diff --git a/contracts/core/src/wrappers/index.ts b/contracts/core/src/wrappers/index.ts index 0a64a4db0..ed9d8ef47 100644 --- a/contracts/core/src/wrappers/index.ts +++ b/contracts/core/src/wrappers/index.ts @@ -21,7 +21,6 @@ export * from '../../generated-wrappers/reentrant_erc20_token'; export * from '../../generated-wrappers/test_asset_proxy_dispatcher'; export * from '../../generated-wrappers/test_asset_proxy_owner'; export * from '../../generated-wrappers/test_exchange_internals'; -export * from '../../generated-wrappers/test_libs'; export * from '../../generated-wrappers/test_signature_validator'; export * from '../../generated-wrappers/test_static_call_receiver'; export * from '../../generated-wrappers/validator'; diff --git a/contracts/core/test/exchange/libs.ts b/contracts/core/test/exchange/libs.ts deleted file mode 100644 index 44ff6a844..000000000 --- a/contracts/core/test/exchange/libs.ts +++ /dev/null @@ -1,125 +0,0 @@ -import { - addressUtils, - chaiSetup, - constants, - OrderFactory, - provider, - txDefaults, - web3Wrapper, -} from '@0x/contracts-test-utils'; -import { BlockchainLifecycle } from '@0x/dev-utils'; -import { assetDataUtils, orderHashUtils } from '@0x/order-utils'; -import { SignedOrder } from '@0x/types'; -import { BigNumber } from '@0x/utils'; -import * as chai from 'chai'; - -import { TestLibsContract } from '../../generated-wrappers/test_libs'; -import { artifacts } from '../../src/artifacts'; - -chaiSetup.configure(); -const expect = chai.expect; - -const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); - -describe('Exchange libs', () => { - let signedOrder: SignedOrder; - let orderFactory: OrderFactory; - let libs: TestLibsContract; - - before(async () => { - await blockchainLifecycle.startAsync(); - }); - after(async () => { - await blockchainLifecycle.revertAsync(); - }); - before(async () => { - const accounts = await web3Wrapper.getAvailableAddressesAsync(); - const makerAddress = accounts[0]; - libs = await TestLibsContract.deployFrom0xArtifactAsync(artifacts.TestLibs, provider, txDefaults); - - const defaultOrderParams = { - ...constants.STATIC_ORDER_PARAMS, - exchangeAddress: libs.address, - makerAddress, - feeRecipientAddress: addressUtils.generatePseudoRandomAddress(), - makerAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()), - takerAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()), - }; - const privateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(makerAddress)]; - orderFactory = new OrderFactory(privateKey, defaultOrderParams); - }); - - beforeEach(async () => { - await blockchainLifecycle.startAsync(); - }); - afterEach(async () => { - await blockchainLifecycle.revertAsync(); - }); - // Note(albrow): These tests are designed to be supplemental to the - // combinatorial tests in test/exchange/internal. They test specific edge - // cases that are not covered by the combinatorial tests. - describe('LibMath', () => { - describe('isRoundingError', () => { - it('should return true if there is a rounding error of 0.1%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(999); - const target = new BigNumber(50); - // rounding error = ((20*50/999) - floor(20*50/999)) / (20*50/999) = 0.1% - const isRoundingError = await libs.publicIsRoundingErrorFloor.callAsync(numerator, denominator, target); - expect(isRoundingError).to.be.true(); - }); - it('should return false if there is a rounding of 0.09%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(9991); - const target = new BigNumber(500); - // rounding error = ((20*500/9991) - floor(20*500/9991)) / (20*500/9991) = 0.09% - const isRoundingError = await libs.publicIsRoundingErrorFloor.callAsync(numerator, denominator, target); - expect(isRoundingError).to.be.false(); - }); - it('should return true if there is a rounding error of 0.11%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(9989); - const target = new BigNumber(500); - // rounding error = ((20*500/9989) - floor(20*500/9989)) / (20*500/9989) = 0.011% - const isRoundingError = await libs.publicIsRoundingErrorFloor.callAsync(numerator, denominator, target); - expect(isRoundingError).to.be.true(); - }); - }); - describe('isRoundingErrorCeil', () => { - it('should return true if there is a rounding error of 0.1%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(1001); - const target = new BigNumber(50); - // rounding error = (ceil(20*50/1001) - (20*50/1001)) / (20*50/1001) = 0.1% - const isRoundingError = await libs.publicIsRoundingErrorCeil.callAsync(numerator, denominator, target); - expect(isRoundingError).to.be.true(); - }); - it('should return false if there is a rounding of 0.09%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(10009); - const target = new BigNumber(500); - // rounding error = (ceil(20*500/10009) - (20*500/10009)) / (20*500/10009) = 0.09% - const isRoundingError = await libs.publicIsRoundingErrorCeil.callAsync(numerator, denominator, target); - expect(isRoundingError).to.be.false(); - }); - it('should return true if there is a rounding error of 0.11%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(10011); - const target = new BigNumber(500); - // rounding error = (ceil(20*500/10011) - (20*500/10011)) / (20*500/10011) = 0.11% - const isRoundingError = await libs.publicIsRoundingErrorCeil.callAsync(numerator, denominator, target); - expect(isRoundingError).to.be.true(); - }); - }); - }); - - describe('LibOrder', () => { - describe('getOrderHash', () => { - it('should output the correct orderHash', async () => { - signedOrder = await orderFactory.newSignedOrderAsync(); - const orderHashHex = await libs.publicGetOrderHash.callAsync(signedOrder); - expect(orderHashUtils.getOrderHashHex(signedOrder)).to.be.equal(orderHashHex); - }); - }); - }); -}); diff --git a/contracts/core/test/utils/fill_order_combinatorial_utils.ts b/contracts/core/test/utils/fill_order_combinatorial_utils.ts index 6372ad29a..497ed4d59 100644 --- a/contracts/core/test/utils/fill_order_combinatorial_utils.ts +++ b/contracts/core/test/utils/fill_order_combinatorial_utils.ts @@ -1,3 +1,4 @@ +import { artifacts as libsArtifacts } from '@0x/contracts-libs'; import { AllowanceAmountScenario, AssetDataScenario, @@ -131,7 +132,11 @@ export async function fillOrderCombinatorialUtilsFactoryAsync( exchangeContract.address, ); - const testLibsContract = await TestLibsContract.deployFrom0xArtifactAsync(artifacts.TestLibs, provider, txDefaults); + const testLibsContract = await TestLibsContract.deployFrom0xArtifactAsync( + libsArtifacts.TestLibs, + provider, + txDefaults, + ); const fillOrderCombinatorialUtils = new FillOrderCombinatorialUtils( orderFactory, diff --git a/contracts/core/tsconfig.json b/contracts/core/tsconfig.json index 82d37b51e..f2f3c4e97 100644 --- a/contracts/core/tsconfig.json +++ b/contracts/core/tsconfig.json @@ -33,7 +33,6 @@ "./generated-artifacts/TestAssetProxyDispatcher.json", "./generated-artifacts/TestAssetProxyOwner.json", "./generated-artifacts/TestExchangeInternals.json", - "./generated-artifacts/TestLibs.json", "./generated-artifacts/TestSignatureValidator.json", "./generated-artifacts/TestStaticCallReceiver.json", "./generated-artifacts/Validator.json", diff --git a/contracts/libs/.solhint.json b/contracts/libs/.solhint.json new file mode 100644 index 000000000..076afe9f3 --- /dev/null +++ b/contracts/libs/.solhint.json @@ -0,0 +1,20 @@ +{ + "extends": "default", + "rules": { + "avoid-low-level-calls": false, + "avoid-tx-origin": "warn", + "bracket-align": false, + "code-complexity": false, + "const-name-snakecase": "error", + "expression-indent": "error", + "function-max-lines": false, + "func-order": "error", + "indent": ["error", 4], + "max-line-length": ["warn", 160], + "no-inline-assembly": false, + "quotes": ["error", "double"], + "separate-by-one-line-in-contract": "error", + "space-after-comma": "error", + "statement-indent": "error" + } +} diff --git a/contracts/libs/README.md b/contracts/libs/README.md new file mode 100644 index 000000000..42548f7c3 --- /dev/null +++ b/contracts/libs/README.md @@ -0,0 +1,70 @@ +## Contracts libs + +Smart contracts libs used in the 0x protocol. + +## Usage + +Contracts can be found in the [contracts](./contracts) directory. The contents of this directory are broken down into the following subdirectories: + +* [protocol](./contracts/protocol) + * This directory contains the libs used by protocol contracts. +* [test](./contracts/test) + * This directory contains mocks and other contracts that are used solely for testing contracts within the other directories. + +## Contributing + +We strongly recommend that the community help us make improvements and determine the future direction of the protocol. To report bugs within this package, please create an issue in this repository. + +For proposals regarding the 0x protocol's smart contract architecture, message format, or additional functionality, go to the [0x Improvement Proposals (ZEIPs)](https://github.com/0xProject/ZEIPs) repository and follow the contribution guidelines provided therein. + +Please read our [contribution guidelines](../../CONTRIBUTING.md) before getting started. + +### Install Dependencies + +If you don't have yarn workspaces enabled (Yarn < v1.0) - enable them: + +```bash +yarn config set workspaces-experimental true +``` + +Then install dependencies + +```bash +yarn install +``` + +### Build + +To build this package and all other monorepo packages that it depends on, run the following from the monorepo root directory: + +```bash +PKG=@0x/contracts-libs yarn build +``` + +Or continuously rebuild on change: + +```bash +PKG=@0x/contracts-libs yarn watch +``` + +### Clean + +```bash +yarn clean +``` + +### Lint + +```bash +yarn lint +``` + +### Run Tests + +```bash +yarn test +``` + +#### Testing options + +Contracts testing options like coverage, profiling, revert traces or backing node choosing - are described [here](../TESTING.md). diff --git a/contracts/libs/compiler.json b/contracts/libs/compiler.json new file mode 100644 index 000000000..cf7c52a73 --- /dev/null +++ b/contracts/libs/compiler.json @@ -0,0 +1,22 @@ +{ + "artifactsDir": "./generated-artifacts", + "contractsDir": "./contracts", + "compilerSettings": { + "optimizer": { + "enabled": true, + "runs": 1000000 + }, + "outputSelection": { + "*": { + "*": [ + "abi", + "evm.bytecode.object", + "evm.bytecode.sourceMap", + "evm.deployedBytecode.object", + "evm.deployedBytecode.sourceMap" + ] + } + } + }, + "contracts": ["TestLibs", "LibOrder", "LibMath", "LibFillResults", "LibAbiEncoder", "LibEIP712"] +} diff --git a/contracts/libs/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol b/contracts/libs/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol new file mode 100644 index 000000000..1d9a70cc1 --- /dev/null +++ b/contracts/libs/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol @@ -0,0 +1,38 @@ +/* + + 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. + +*/ + +// solhint-disable +pragma solidity 0.4.24; + + +/// @dev This contract documents the revert reasons used in the AssetProxy contracts. +/// This contract is intended to serve as a reference, but is not actually used for efficiency reasons. +contract LibAssetProxyErrors { + + /// Authorizable errors /// + string constant SENDER_NOT_AUTHORIZED = "SENDER_NOT_AUTHORIZED"; // Sender not authorized to call this method. + string constant TARGET_NOT_AUTHORIZED = "TARGET_NOT_AUTHORIZED"; // Target address not authorized to call this method. + string constant TARGET_ALREADY_AUTHORIZED = "TARGET_ALREADY_AUTHORIZED"; // Target address must not already be authorized. + string constant INDEX_OUT_OF_BOUNDS = "INDEX_OUT_OF_BOUNDS"; // Specified array index is out of bounds. + string constant AUTHORIZED_ADDRESS_MISMATCH = "AUTHORIZED_ADDRESS_MISMATCH"; // Address at index does not match given target address. + + /// Transfer errors /// + string constant INVALID_AMOUNT = "INVALID_AMOUNT"; // Transfer amount must equal 1. + string constant TRANSFER_FAILED = "TRANSFER_FAILED"; // Transfer failed. + string constant LENGTH_GREATER_THAN_131_REQUIRED = "LENGTH_GREATER_THAN_131_REQUIRED"; // Byte array must have a length greater than 0. +} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol new file mode 100644 index 000000000..4aad37709 --- /dev/null +++ b/contracts/libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol @@ -0,0 +1,215 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "./LibOrder.sol"; + + +contract LibAbiEncoder { + + /// @dev ABI encodes calldata for `fillOrder`. + /// @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 ABI encoded calldata for `fillOrder`. + function abiEncodeFillOrder( + LibOrder.Order memory order, + uint256 takerAssetFillAmount, + bytes memory signature + ) + internal + pure + returns (bytes memory fillOrderCalldata) + { + // 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 makerAssetData (*) | + // | | 0x160 | | 12. Offset to takerAssetData (*) | + // | | 0x180 | 32 | makerAssetData Length | + // | | 0x1A0 | ** | makerAssetData Contents | + // | | 0x1C0 | 32 | takerAssetData Length | + // | | 0x1E0 | ** | takerAssetData 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 + + assembly { + + // Areas below may use the following variables: + // 1. Start -- Start of this area in memory + // 2. 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. 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 + fillOrderCalldata := mload(0x40) + // bytes4(keccak256("fillOrder((address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes),uint256,bytes)")) + // = 0xb4be83d5 + // Leave 0x20 bytes to store the length + mstore(add(fillOrderCalldata, 0x20), 0xb4be83d500000000000000000000000000000000000000000000000000000000) + let headerAreaEnd := add(fillOrderCalldata, 0x24) + + /////// 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 makerAssetData + mstore(add(dataAreaEnd, 0x160), mload(add(sourceOffset, 0x160))) // Offset to takerAssetData + dataAreaEnd := add(dataAreaEnd, 0x180) + sourceOffset := add(sourceOffset, 0x180) + + // Write offset to + mstore(add(dataAreaStart, mul(10, 0x20)), sub(dataAreaEnd, dataAreaStart)) + + // Calculate length of + sourceOffset := mload(add(order, 0x140)) // makerAssetData + arrayLenBytes := mload(sourceOffset) + sourceOffset := add(sourceOffset, 0x20) + arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20) + + // Write length of + mstore(dataAreaEnd, arrayLenBytes) + dataAreaEnd := add(dataAreaEnd, 0x20) + + // Write contents of + 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 + mstore(add(dataAreaStart, mul(11, 0x20)), sub(dataAreaEnd, dataAreaStart)) + + // Calculate length of + sourceOffset := mload(add(order, 0x160)) // takerAssetData + arrayLenBytes := mload(sourceOffset) + sourceOffset := add(sourceOffset, 0x20) + arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20) + + // Write length of + mstore(dataAreaEnd, arrayLenBytes) + dataAreaEnd := add(dataAreaEnd, 0x20) + + // Write contents of + 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) + } + + // Set length of calldata + mstore(fillOrderCalldata, sub(dataAreaEnd, add(fillOrderCalldata, 0x20))) + + // Increment free memory pointer + mstore(0x40, dataAreaEnd) + } + + return fillOrderCalldata; + } +} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibConstants.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibConstants.sol new file mode 100644 index 000000000..8d2732cd3 --- /dev/null +++ b/contracts/libs/contracts/protocol/Exchange/libs/LibConstants.sol @@ -0,0 +1,49 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + + +// solhint-disable max-line-length +contract LibConstants { + + // Asset data for ZRX token. Used for fee transfers. + // @TODO: Hardcode constant when we deploy. Currently + // not constant to make testing easier. + + // The proxyId for ZRX_ASSET_DATA is bytes4(keccak256("ERC20Token(address)")) = 0xf47261b0 + + // Kovan ZRX address is 0x6ff6c0ff1d68b964901f986d4c9fa3ac68346570. + // The ABI encoded proxyId and address is 0xf47261b00000000000000000000000006ff6c0ff1d68b964901f986d4c9fa3ac68346570 + // bytes constant public ZRX_ASSET_DATA = "\xf4\x72\x61\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x6f\xf6\xc0\xff\x1d\x68\xb9\x64\x90\x1f\x98\x6d\x4c\x9f\xa3\xac\x68\x34\x65\x70"; + + // Mainnet ZRX address is 0xe41d2489571d322189246dafa5ebde1f4699f498. + // The ABI encoded proxyId and address is 0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498 + // bytes constant public ZRX_ASSET_DATA = "\xf4\x72\x61\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe4\x1d\x24\x89\x57\x1d\x32\x21\x89\x24\x6d\xaf\xa5\xeb\xde\x1f\x46\x99\xf4\x98"; + + // solhint-disable-next-line var-name-mixedcase + bytes public ZRX_ASSET_DATA; + + // @TODO: Remove when we deploy. + constructor (bytes memory zrxAssetData) + public + { + ZRX_ASSET_DATA = zrxAssetData; + } +} +// solhint-enable max-line-length diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibEIP712.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibEIP712.sol new file mode 100644 index 000000000..203edc1fd --- /dev/null +++ b/contracts/libs/contracts/protocol/Exchange/libs/LibEIP712.sol @@ -0,0 +1,87 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + + +contract LibEIP712 { + + // EIP191 header for EIP712 prefix + string constant internal EIP191_HEADER = "\x19\x01"; + + // EIP712 Domain Name value + string constant internal EIP712_DOMAIN_NAME = "0x Protocol"; + + // EIP712 Domain Version value + string constant internal EIP712_DOMAIN_VERSION = "2"; + + // Hash of the EIP712 Domain Separator Schema + bytes32 constant internal EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256(abi.encodePacked( + "EIP712Domain(", + "string name,", + "string version,", + "address verifyingContract", + ")" + )); + + // Hash of the EIP712 Domain Separator data + // solhint-disable-next-line var-name-mixedcase + bytes32 public EIP712_DOMAIN_HASH; + + constructor () + public + { + EIP712_DOMAIN_HASH = keccak256(abi.encodePacked( + EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH, + keccak256(bytes(EIP712_DOMAIN_NAME)), + keccak256(bytes(EIP712_DOMAIN_VERSION)), + bytes32(address(this)) + )); + } + + /// @dev Calculates EIP712 encoding for a hash struct in this EIP712 Domain. + /// @param hashStruct The EIP712 hash struct. + /// @return EIP712 hash applied to this EIP712 Domain. + function hashEIP712Message(bytes32 hashStruct) + internal + view + returns (bytes32 result) + { + bytes32 eip712DomainHash = EIP712_DOMAIN_HASH; + + // Assembly for more efficient computing: + // keccak256(abi.encodePacked( + // EIP191_HEADER, + // EIP712_DOMAIN_HASH, + // hashStruct + // )); + + assembly { + // Load free memory pointer + let memPtr := mload(64) + + mstore(memPtr, 0x1901000000000000000000000000000000000000000000000000000000000000) // EIP191 header + mstore(add(memPtr, 2), eip712DomainHash) // EIP712 domain hash + mstore(add(memPtr, 34), hashStruct) // Hash of struct + + // Compute hash + result := keccak256(memPtr, 66) + } + return result; + } +} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol new file mode 100644 index 000000000..a0f75bc06 --- /dev/null +++ b/contracts/libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol @@ -0,0 +1,70 @@ +/* + + 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. + +*/ + +// solhint-disable +pragma solidity 0.4.24; + + +/// @dev This contract documents the revert reasons used in the Exchange contract. +/// This contract is intended to serve as a reference, but is not actually used for efficiency reasons. +contract LibExchangeErrors { + + /// Order validation errors /// + string constant ORDER_UNFILLABLE = "ORDER_UNFILLABLE"; // Order cannot be filled. + string constant INVALID_MAKER = "INVALID_MAKER"; // Invalid makerAddress. + string constant INVALID_TAKER = "INVALID_TAKER"; // Invalid takerAddress. + string constant INVALID_SENDER = "INVALID_SENDER"; // Invalid `msg.sender`. + string constant INVALID_ORDER_SIGNATURE = "INVALID_ORDER_SIGNATURE"; // Signature validation failed. + + /// fillOrder validation errors /// + string constant INVALID_TAKER_AMOUNT = "INVALID_TAKER_AMOUNT"; // takerAssetFillAmount cannot equal 0. + string constant ROUNDING_ERROR = "ROUNDING_ERROR"; // Rounding error greater than 0.1% of takerAssetFillAmount. + + /// Signature validation errors /// + string constant INVALID_SIGNATURE = "INVALID_SIGNATURE"; // Signature validation failed. + string constant SIGNATURE_ILLEGAL = "SIGNATURE_ILLEGAL"; // Signature type is illegal. + string constant SIGNATURE_UNSUPPORTED = "SIGNATURE_UNSUPPORTED"; // Signature type unsupported. + + /// cancelOrdersUptTo errors /// + string constant INVALID_NEW_ORDER_EPOCH = "INVALID_NEW_ORDER_EPOCH"; // Specified salt must be greater than or equal to existing orderEpoch. + + /// fillOrKillOrder errors /// + string constant COMPLETE_FILL_FAILED = "COMPLETE_FILL_FAILED"; // Desired takerAssetFillAmount could not be completely filled. + + /// matchOrders errors /// + string constant NEGATIVE_SPREAD_REQUIRED = "NEGATIVE_SPREAD_REQUIRED"; // Matched orders must have a negative spread. + + /// Transaction errors /// + string constant REENTRANCY_ILLEGAL = "REENTRANCY_ILLEGAL"; // Recursive reentrancy is not allowed. + string constant INVALID_TX_HASH = "INVALID_TX_HASH"; // Transaction has already been executed. + string constant INVALID_TX_SIGNATURE = "INVALID_TX_SIGNATURE"; // Signature validation failed. + string constant FAILED_EXECUTION = "FAILED_EXECUTION"; // Transaction execution failed. + + /// registerAssetProxy errors /// + string constant ASSET_PROXY_ALREADY_EXISTS = "ASSET_PROXY_ALREADY_EXISTS"; // AssetProxy with same id already exists. + + /// dispatchTransferFrom errors /// + string constant ASSET_PROXY_DOES_NOT_EXIST = "ASSET_PROXY_DOES_NOT_EXIST"; // No assetProxy registered at given id. + string constant TRANSFER_FAILED = "TRANSFER_FAILED"; // Asset transfer unsuccesful. + + /// Length validation errors /// + string constant LENGTH_GREATER_THAN_0_REQUIRED = "LENGTH_GREATER_THAN_0_REQUIRED"; // Byte array must have a length greater than 0. + string constant LENGTH_GREATER_THAN_3_REQUIRED = "LENGTH_GREATER_THAN_3_REQUIRED"; // Byte array must have a length greater than 3. + string constant LENGTH_0_REQUIRED = "LENGTH_0_REQUIRED"; // Byte array must have a length of 0. + string constant LENGTH_65_REQUIRED = "LENGTH_65_REQUIRED"; // Byte array must have a length of 65. +} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibFillResults.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibFillResults.sol new file mode 100644 index 000000000..fbd9950bf --- /dev/null +++ b/contracts/libs/contracts/protocol/Exchange/libs/LibFillResults.sol @@ -0,0 +1,53 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; + + +contract LibFillResults is + SafeMath +{ + struct FillResults { + uint256 makerAssetFilledAmount; // Total amount of makerAsset(s) filled. + uint256 takerAssetFilledAmount; // Total amount of takerAsset(s) filled. + uint256 makerFeePaid; // Total amount of ZRX paid by maker(s) to feeRecipient(s). + uint256 takerFeePaid; // Total amount of ZRX paid by taker to feeRecipients(s). + } + + struct MatchedFillResults { + FillResults left; // Amounts filled and fees paid of left order. + FillResults right; // Amounts filled and fees paid of right order. + uint256 leftMakerAssetSpreadAmount; // Spread between price of left and right order, denominated in the left order's makerAsset, paid to taker. + } + + /// @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); + } +} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibMath.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibMath.sol new file mode 100644 index 000000000..b24876a9c --- /dev/null +++ b/contracts/libs/contracts/protocol/Exchange/libs/LibMath.sol @@ -0,0 +1,253 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; + + +contract LibMath is + SafeMath +{ + /// @dev Calculates partial value given a numerator and denominator rounded down. + /// Reverts if rounding error is >= 0.1% + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to calculate partial of. + /// @return Partial value of target rounded down. + function safeGetPartialAmountFloor( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (uint256 partialAmount) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + require( + !isRoundingErrorFloor( + numerator, + denominator, + target + ), + "ROUNDING_ERROR" + ); + + partialAmount = safeDiv( + safeMul(numerator, target), + denominator + ); + return partialAmount; + } + + /// @dev Calculates partial value given a numerator and denominator rounded down. + /// Reverts if rounding error is >= 0.1% + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to calculate partial of. + /// @return Partial value of target rounded up. + function safeGetPartialAmountCeil( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (uint256 partialAmount) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + require( + !isRoundingErrorCeil( + numerator, + denominator, + target + ), + "ROUNDING_ERROR" + ); + + // safeDiv computes `floor(a / b)`. We use the identity (a, b integer): + // ceil(a / b) = floor((a + b - 1) / b) + // To implement `ceil(a / b)` using safeDiv. + partialAmount = safeDiv( + safeAdd( + safeMul(numerator, target), + safeSub(denominator, 1) + ), + denominator + ); + return partialAmount; + } + + /// @dev Calculates partial value given a numerator and denominator rounded down. + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to calculate partial of. + /// @return Partial value of target rounded down. + function getPartialAmountFloor( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (uint256 partialAmount) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + partialAmount = safeDiv( + safeMul(numerator, target), + denominator + ); + return partialAmount; + } + + /// @dev Calculates partial value given a numerator and denominator rounded down. + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to calculate partial of. + /// @return Partial value of target rounded up. + function getPartialAmountCeil( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (uint256 partialAmount) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + // safeDiv computes `floor(a / b)`. We use the identity (a, b integer): + // ceil(a / b) = floor((a + b - 1) / b) + // To implement `ceil(a / b)` using safeDiv. + partialAmount = safeDiv( + safeAdd( + safeMul(numerator, target), + safeSub(denominator, 1) + ), + denominator + ); + return partialAmount; + } + + /// @dev Checks if rounding error >= 0.1% when rounding down. + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to multiply with numerator/denominator. + /// @return Rounding error is present. + function isRoundingErrorFloor( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (bool isError) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + // The absolute rounding error is the difference between the rounded + // value and the ideal value. The relative rounding error is the + // absolute rounding error divided by the absolute value of the + // ideal value. This is undefined when the ideal value is zero. + // + // The ideal value is `numerator * target / denominator`. + // Let's call `numerator * target % denominator` the remainder. + // The absolute error is `remainder / denominator`. + // + // When the ideal value is zero, we require the absolute error to + // be zero. Fortunately, this is always the case. The ideal value is + // zero iff `numerator == 0` and/or `target == 0`. In this case the + // remainder and absolute error are also zero. + if (target == 0 || numerator == 0) { + return false; + } + + // Otherwise, we want the relative rounding error to be strictly + // less than 0.1%. + // The relative error is `remainder / (numerator * target)`. + // We want the relative error less than 1 / 1000: + // remainder / (numerator * denominator) < 1 / 1000 + // or equivalently: + // 1000 * remainder < numerator * target + // so we have a rounding error iff: + // 1000 * remainder >= numerator * target + uint256 remainder = mulmod( + target, + numerator, + denominator + ); + isError = safeMul(1000, remainder) >= safeMul(numerator, target); + return isError; + } + + /// @dev Checks if rounding error >= 0.1% when rounding up. + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to multiply with numerator/denominator. + /// @return Rounding error is present. + function isRoundingErrorCeil( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (bool isError) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + // See the comments in `isRoundingError`. + if (target == 0 || numerator == 0) { + // When either is zero, the ideal value and rounded value are zero + // and there is no rounding error. (Although the relative error + // is undefined.) + return false; + } + // Compute remainder as before + uint256 remainder = mulmod( + target, + numerator, + denominator + ); + remainder = safeSub(denominator, remainder) % denominator; + isError = safeMul(1000, remainder) >= safeMul(numerator, target); + return isError; + } +} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibOrder.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibOrder.sol new file mode 100644 index 000000000..0fe7c2161 --- /dev/null +++ b/contracts/libs/contracts/protocol/Exchange/libs/LibOrder.sol @@ -0,0 +1,145 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "./LibEIP712.sol"; + + +contract LibOrder is + LibEIP712 +{ + // Hash for the EIP712 Order Schema + bytes32 constant internal EIP712_ORDER_SCHEMA_HASH = keccak256(abi.encodePacked( + "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", + ")" + )); + + // A valid order remains fillable until it is expired, fully filled, or cancelled. + // An order's state is unaffected by external factors, like account balances. + enum OrderStatus { + INVALID, // Default value + INVALID_MAKER_ASSET_AMOUNT, // Order does not have a valid maker asset amount + INVALID_TAKER_ASSET_AMOUNT, // Order does not have a valid taker asset amount + FILLABLE, // Order is fillable + EXPIRED, // Order has already expired + FULLY_FILLED, // Order is fully filled + CANCELLED // Order has been cancelled + } + + // solhint-disable max-line-length + struct Order { + address makerAddress; // Address that created the order. + address takerAddress; // Address that is allowed to fill the order. If set to 0, any address is allowed to fill the order. + address feeRecipientAddress; // Address that will recieve fees when order is filled. + address senderAddress; // Address that is allowed to call Exchange contract methods that affect this order. If set to 0, any address is allowed to call these methods. + uint256 makerAssetAmount; // Amount of makerAsset being offered by maker. Must be greater than 0. + uint256 takerAssetAmount; // Amount of takerAsset being bid on by maker. Must be greater than 0. + uint256 makerFee; // Amount of ZRX paid to feeRecipient by maker when order is filled. If set to 0, no transfer of ZRX from maker to feeRecipient will be attempted. + uint256 takerFee; // Amount of ZRX paid to feeRecipient by taker when order is filled. If set to 0, no transfer of ZRX from taker to feeRecipient will be attempted. + uint256 expirationTimeSeconds; // Timestamp in seconds at which order expires. + uint256 salt; // Arbitrary number to facilitate uniqueness of the order's hash. + bytes makerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring makerAsset. The last byte references the id of this proxy. + bytes takerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring takerAsset. The last byte references the id of this proxy. + } + // solhint-enable max-line-length + + struct OrderInfo { + uint8 orderStatus; // Status that describes order's validity and fillability. + bytes32 orderHash; // EIP712 hash of the order (see LibOrder.getOrderHash). + uint256 orderTakerAssetFilledAmount; // Amount of order that has already been filled. + } + + /// @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) + { + orderHash = hashEIP712Message(hashOrder(order)); + return orderHash; + } + + /// @dev Calculates EIP712 hash of the order. + /// @param order The order structure. + /// @return EIP712 hash of the order. + function hashOrder(Order memory order) + internal + pure + returns (bytes32 result) + { + bytes32 schemaHash = EIP712_ORDER_SCHEMA_HASH; + bytes32 makerAssetDataHash = keccak256(order.makerAssetData); + bytes32 takerAssetDataHash = keccak256(order.takerAssetData); + + // Assembly for more efficiently computing: + // keccak256(abi.encodePacked( + // EIP712_ORDER_SCHEMA_HASH, + // bytes32(order.makerAddress), + // bytes32(order.takerAddress), + // bytes32(order.feeRecipientAddress), + // bytes32(order.senderAddress), + // order.makerAssetAmount, + // order.takerAssetAmount, + // order.makerFee, + // order.takerFee, + // order.expirationTimeSeconds, + // order.salt, + // keccak256(order.makerAssetData), + // keccak256(order.takerAssetData) + // )); + + assembly { + // Calculate memory addresses that will be swapped out before hashing + let pos1 := sub(order, 32) + let pos2 := add(order, 320) + let pos3 := add(order, 352) + + // Backup + let temp1 := mload(pos1) + let temp2 := mload(pos2) + let temp3 := mload(pos3) + + // Hash in place + mstore(pos1, schemaHash) + mstore(pos2, makerAssetDataHash) + mstore(pos3, takerAssetDataHash) + result := keccak256(pos1, 416) + + // Restore + mstore(pos1, temp1) + mstore(pos2, temp2) + mstore(pos3, temp3) + } + return result; + } +} diff --git a/contracts/libs/contracts/test/TestLibs/TestLibs.sol b/contracts/libs/contracts/test/TestLibs/TestLibs.sol new file mode 100644 index 000000000..a10f981fc --- /dev/null +++ b/contracts/libs/contracts/test/TestLibs/TestLibs.sol @@ -0,0 +1,152 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "../../protocol/Exchange/libs/LibMath.sol"; +import "../../protocol/Exchange/libs/LibOrder.sol"; +import "../../protocol/Exchange/libs/LibFillResults.sol"; +import "../../protocol/Exchange/libs/LibAbiEncoder.sol"; + + +contract TestLibs is + LibMath, + LibOrder, + LibFillResults, + LibAbiEncoder +{ + function publicAbiEncodeFillOrder( + Order memory order, + uint256 takerAssetFillAmount, + bytes memory signature + ) + public + pure + returns (bytes memory fillOrderCalldata) + { + fillOrderCalldata = abiEncodeFillOrder( + order, + takerAssetFillAmount, + signature + ); + return fillOrderCalldata; + } + + function publicGetPartialAmountFloor( + uint256 numerator, + uint256 denominator, + uint256 target + ) + public + pure + returns (uint256 partialAmount) + { + partialAmount = getPartialAmountFloor( + numerator, + denominator, + target + ); + return partialAmount; + } + + function publicGetPartialAmountCeil( + uint256 numerator, + uint256 denominator, + uint256 target + ) + public + pure + returns (uint256 partialAmount) + { + partialAmount = getPartialAmountCeil( + numerator, + denominator, + target + ); + return partialAmount; + } + + function publicIsRoundingErrorFloor( + uint256 numerator, + uint256 denominator, + uint256 target + ) + public + pure + returns (bool isError) + { + isError = isRoundingErrorFloor( + numerator, + denominator, + target + ); + return isError; + } + + function publicIsRoundingErrorCeil( + uint256 numerator, + uint256 denominator, + uint256 target + ) + public + pure + returns (bool isError) + { + isError = isRoundingErrorCeil( + numerator, + denominator, + target + ); + return isError; + } + + function publicGetOrderHash(Order memory order) + public + view + returns (bytes32 orderHash) + { + orderHash = getOrderHash(order); + return orderHash; + } + + function getOrderSchemaHash() + public + pure + returns (bytes32) + { + return EIP712_ORDER_SCHEMA_HASH; + } + + function getDomainSeparatorSchemaHash() + public + pure + returns (bytes32) + { + return EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH; + } + + function publicAddFillResults(FillResults memory totalFillResults, FillResults memory singleFillResults) + public + pure + returns (FillResults memory) + { + addFillResults(totalFillResults, singleFillResults); + return totalFillResults; + } +} diff --git a/contracts/libs/package.json b/contracts/libs/package.json new file mode 100644 index 000000000..74288be76 --- /dev/null +++ b/contracts/libs/package.json @@ -0,0 +1,92 @@ +{ + "private": true, + "name": "@0x/contracts-libs", + "version": "1.0.0", + "engines": { + "node": ">=6.12" + }, + "description": "Smart contract libs of 0x protocol", + "main": "lib/src/index.js", + "directories": { + "test": "test" + }, + "scripts": { + "build": "yarn pre_build && tsc -b", + "build:ci": "yarn build", + "pre_build": "run-s compile generate_contract_wrappers", + "test": "yarn run_mocha", + "rebuild_and_test": "run-s build test", + "test:coverage": "SOLIDITY_COVERAGE=true run-s build run_mocha coverage:report:text coverage:report:lcov", + "test:profiler": "SOLIDITY_PROFILER=true run-s build run_mocha profiler:report:html", + "test:trace": "SOLIDITY_REVERT_TRACE=true run-s build run_mocha", + "run_mocha": + "mocha --require source-map-support/register --require make-promises-safe 'lib/test/**/*.js' --timeout 100000 --bail --exit", + "compile": "sol-compiler --contracts-dir contracts", + "clean": "shx rm -rf lib generated-artifacts generated-wrappers", + "generate_contract_wrappers": "abi-gen --abis ${npm_package_config_abis} --template ../../node_modules/@0x/abi-gen-templates/contract.handlebars --partials '../../node_modules/@0x/abi-gen-templates/partials/**/*.handlebars' --output generated-wrappers --backend ethers", + "lint": "tslint --format stylish --project . --exclude ./generated-wrappers/**/* --exclude ./generated-artifacts/**/* --exclude **/lib/**/* && yarn lint-contracts", + "coverage:report:text": "istanbul report text", + "coverage:report:html": "istanbul report html && open coverage/index.html", + "profiler:report:html": "istanbul report html && open coverage/index.html", + "coverage:report:lcov": "istanbul report lcov", + "test:circleci": "yarn test", + "lint-contracts": "solhint contracts/**/**/**/**/*.sol" + }, + "config": { + "abis": "generated-artifacts/@(LibMath|LibOrder|LibFillResults|LibAbiEncoder|TestLibs|LibEIP712).json" + }, + "repository": { + "type": "git", + "url": "https://github.com/0xProject/0x-monorepo.git" + }, + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/0xProject/0x-monorepo/issues" + }, + "homepage": "https://github.com/0xProject/0x-monorepo/contracts/libs/README.md", + "devDependencies": { + "@0x/contracts-test-utils": "^1.0.0", + "@0x/abi-gen": "^1.0.17", + "@0x/dev-utils": "^1.0.19", + "@0x/sol-compiler": "^1.1.14", + "@0x/sol-cov": "^2.1.14", + "@0x/subproviders": "^2.1.6", + "@0x/tslint-config": "^1.0.10", + "@types/bn.js": "^4.11.0", + "@types/lodash": "4.14.104", + "@types/node": "*", + "@types/yargs": "^10.0.0", + "chai": "^4.0.1", + "chai-as-promised": "^7.1.0", + "chai-bignumber": "^2.0.1", + "dirty-chai": "^2.0.1", + "make-promises-safe": "^1.1.0", + "ethereumjs-abi": "0.6.5", + "mocha": "^4.1.0", + "npm-run-all": "^4.1.2", + "shx": "^0.2.2", + "solc": "^0.4.24", + "solhint": "^1.2.1", + "tslint": "5.11.0", + "typescript": "3.0.1", + "yargs": "^10.0.3" + }, + "dependencies": { + "@0x/base-contract": "^3.0.8", + "@0x/order-utils": "^3.0.4", + "@0x/contracts-multisig": "^1.0.0", + "@0x/contracts-utils": "^1.0.0", + "@0x/types": "^1.3.0", + "@0x/typescript-typings": "^3.0.4", + "@0x/utils": "^2.0.6", + "@0x/web3-wrapper": "^3.1.6", + "@types/js-combinatorics": "^0.5.29", + "bn.js": "^4.11.8", + "ethereum-types": "^1.1.2", + "ethereumjs-util": "^5.1.1", + "lodash": "^4.17.5" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/contracts/libs/src/artifacts/index.ts b/contracts/libs/src/artifacts/index.ts new file mode 100644 index 000000000..3955bbe2b --- /dev/null +++ b/contracts/libs/src/artifacts/index.ts @@ -0,0 +1,17 @@ +import { ContractArtifact } from 'ethereum-types'; + +import * as LibAbiEncoder from '../../generated-artifacts/LibAbiEncoder.json'; +import * as LibEIP721 from '../../generated-artifacts/LibEIP712.json'; +import * as LibFillResults from '../../generated-artifacts/LibFillResults.json'; +import * as LibMath from '../../generated-artifacts/LibMath.json'; +import * as LibOrder from '../../generated-artifacts/LibOrder.json'; +import * as TestLibs from '../../generated-artifacts/TestLibs.json'; + +export const artifacts = { + TestLibs: TestLibs as ContractArtifact, + LibAbiEncoder: LibAbiEncoder as ContractArtifact, + LibFillResults: LibFillResults as ContractArtifact, + LibMath: LibMath as ContractArtifact, + LibOrder: LibOrder as ContractArtifact, + LibEIP721: LibEIP721 as ContractArtifact, +}; diff --git a/contracts/libs/src/index.ts b/contracts/libs/src/index.ts new file mode 100644 index 000000000..d55f08ea2 --- /dev/null +++ b/contracts/libs/src/index.ts @@ -0,0 +1,2 @@ +export * from './artifacts'; +export * from './wrappers'; diff --git a/contracts/libs/src/wrappers/index.ts b/contracts/libs/src/wrappers/index.ts new file mode 100644 index 000000000..baaae6e34 --- /dev/null +++ b/contracts/libs/src/wrappers/index.ts @@ -0,0 +1,6 @@ +export * from '../../generated-wrappers/test_libs'; +export * from '../../generated-wrappers/lib_abi_encoder'; +export * from '../../generated-wrappers/lib_fill_results'; +export * from '../../generated-wrappers/lib_math'; +export * from '../../generated-wrappers/lib_order'; +export * from '../../generated-wrappers/lib_e_i_p712'; diff --git a/contracts/libs/test/exchange/libs.ts b/contracts/libs/test/exchange/libs.ts new file mode 100644 index 000000000..44ff6a844 --- /dev/null +++ b/contracts/libs/test/exchange/libs.ts @@ -0,0 +1,125 @@ +import { + addressUtils, + chaiSetup, + constants, + OrderFactory, + provider, + txDefaults, + web3Wrapper, +} from '@0x/contracts-test-utils'; +import { BlockchainLifecycle } from '@0x/dev-utils'; +import { assetDataUtils, orderHashUtils } from '@0x/order-utils'; +import { SignedOrder } from '@0x/types'; +import { BigNumber } from '@0x/utils'; +import * as chai from 'chai'; + +import { TestLibsContract } from '../../generated-wrappers/test_libs'; +import { artifacts } from '../../src/artifacts'; + +chaiSetup.configure(); +const expect = chai.expect; + +const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); + +describe('Exchange libs', () => { + let signedOrder: SignedOrder; + let orderFactory: OrderFactory; + let libs: TestLibsContract; + + before(async () => { + await blockchainLifecycle.startAsync(); + }); + after(async () => { + await blockchainLifecycle.revertAsync(); + }); + before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + const makerAddress = accounts[0]; + libs = await TestLibsContract.deployFrom0xArtifactAsync(artifacts.TestLibs, provider, txDefaults); + + const defaultOrderParams = { + ...constants.STATIC_ORDER_PARAMS, + exchangeAddress: libs.address, + makerAddress, + feeRecipientAddress: addressUtils.generatePseudoRandomAddress(), + makerAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()), + takerAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()), + }; + const privateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(makerAddress)]; + orderFactory = new OrderFactory(privateKey, defaultOrderParams); + }); + + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + // Note(albrow): These tests are designed to be supplemental to the + // combinatorial tests in test/exchange/internal. They test specific edge + // cases that are not covered by the combinatorial tests. + describe('LibMath', () => { + describe('isRoundingError', () => { + it('should return true if there is a rounding error of 0.1%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(999); + const target = new BigNumber(50); + // rounding error = ((20*50/999) - floor(20*50/999)) / (20*50/999) = 0.1% + const isRoundingError = await libs.publicIsRoundingErrorFloor.callAsync(numerator, denominator, target); + expect(isRoundingError).to.be.true(); + }); + it('should return false if there is a rounding of 0.09%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(9991); + const target = new BigNumber(500); + // rounding error = ((20*500/9991) - floor(20*500/9991)) / (20*500/9991) = 0.09% + const isRoundingError = await libs.publicIsRoundingErrorFloor.callAsync(numerator, denominator, target); + expect(isRoundingError).to.be.false(); + }); + it('should return true if there is a rounding error of 0.11%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(9989); + const target = new BigNumber(500); + // rounding error = ((20*500/9989) - floor(20*500/9989)) / (20*500/9989) = 0.011% + const isRoundingError = await libs.publicIsRoundingErrorFloor.callAsync(numerator, denominator, target); + expect(isRoundingError).to.be.true(); + }); + }); + describe('isRoundingErrorCeil', () => { + it('should return true if there is a rounding error of 0.1%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(1001); + const target = new BigNumber(50); + // rounding error = (ceil(20*50/1001) - (20*50/1001)) / (20*50/1001) = 0.1% + const isRoundingError = await libs.publicIsRoundingErrorCeil.callAsync(numerator, denominator, target); + expect(isRoundingError).to.be.true(); + }); + it('should return false if there is a rounding of 0.09%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(10009); + const target = new BigNumber(500); + // rounding error = (ceil(20*500/10009) - (20*500/10009)) / (20*500/10009) = 0.09% + const isRoundingError = await libs.publicIsRoundingErrorCeil.callAsync(numerator, denominator, target); + expect(isRoundingError).to.be.false(); + }); + it('should return true if there is a rounding error of 0.11%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(10011); + const target = new BigNumber(500); + // rounding error = (ceil(20*500/10011) - (20*500/10011)) / (20*500/10011) = 0.11% + const isRoundingError = await libs.publicIsRoundingErrorCeil.callAsync(numerator, denominator, target); + expect(isRoundingError).to.be.true(); + }); + }); + }); + + describe('LibOrder', () => { + describe('getOrderHash', () => { + it('should output the correct orderHash', async () => { + signedOrder = await orderFactory.newSignedOrderAsync(); + const orderHashHex = await libs.publicGetOrderHash.callAsync(signedOrder); + expect(orderHashUtils.getOrderHashHex(signedOrder)).to.be.equal(orderHashHex); + }); + }); + }); +}); diff --git a/contracts/libs/test/global_hooks.ts b/contracts/libs/test/global_hooks.ts new file mode 100644 index 000000000..f8ace376a --- /dev/null +++ b/contracts/libs/test/global_hooks.ts @@ -0,0 +1,17 @@ +import { env, EnvVars } from '@0x/dev-utils'; + +import { coverage, profiler, provider } from '@0x/contracts-test-utils'; +before('start web3 provider', () => { + provider.start(); +}); +after('generate coverage report', async () => { + if (env.parseBoolean(EnvVars.SolidityCoverage)) { + const coverageSubprovider = coverage.getCoverageSubproviderSingleton(); + await coverageSubprovider.writeCoverageAsync(); + } + if (env.parseBoolean(EnvVars.SolidityProfiler)) { + const profilerSubprovider = profiler.getProfilerSubproviderSingleton(); + await profilerSubprovider.writeProfilerOutputAsync(); + } + provider.stop(); +}); diff --git a/contracts/libs/tsconfig.json b/contracts/libs/tsconfig.json new file mode 100644 index 000000000..27ca35085 --- /dev/null +++ b/contracts/libs/tsconfig.json @@ -0,0 +1,18 @@ +{ + "extends": "../../tsconfig", + "compilerOptions": { + "outDir": "lib", + "rootDir": ".", + "resolveJsonModule": true + }, + "include": ["./src/**/*", "./test/**/*", "./generated-wrappers/**/*"], + "files": [ + "./generated-artifacts/TestLibs.json", + "./generated-artifacts/LibOrder.json", + "./generated-artifacts/LibFillResults.json", + "./generated-artifacts/LibAbiEncoder.json", + "./generated-artifacts/LibEIP712.json", + "./generated-artifacts/LibMath.json" + ], + "exclude": ["./deploy/solc/solc_bin"] +} diff --git a/contracts/libs/tslint.json b/contracts/libs/tslint.json new file mode 100644 index 000000000..1bb3ac2a2 --- /dev/null +++ b/contracts/libs/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": ["@0x/tslint-config"], + "rules": { + "custom-no-magic-numbers": false + } +} -- cgit v1.2.3 From ac5a3fae9b5a21ef59dcc204cedc282ee12bc895 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 4 Dec 2018 15:59:34 +0100 Subject: Import TestLibs contract from @0x/contracts-libs --- contracts/core/test/utils/fill_order_combinatorial_utils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'contracts') diff --git a/contracts/core/test/utils/fill_order_combinatorial_utils.ts b/contracts/core/test/utils/fill_order_combinatorial_utils.ts index 497ed4d59..5d0ea07a8 100644 --- a/contracts/core/test/utils/fill_order_combinatorial_utils.ts +++ b/contracts/core/test/utils/fill_order_combinatorial_utils.ts @@ -1,4 +1,4 @@ -import { artifacts as libsArtifacts } from '@0x/contracts-libs'; +import { artifacts as libsArtifacts, TestLibsContract } from '@0x/contracts-libs'; import { AllowanceAmountScenario, AssetDataScenario, @@ -33,7 +33,6 @@ import * as _ from 'lodash'; import 'make-promises-safe'; import { ExchangeContract, ExchangeFillEventArgs } from '../../generated-wrappers/exchange'; -import { TestLibsContract } from '../../generated-wrappers/test_libs'; import { artifacts } from '../../src/artifacts'; import { AssetWrapper } from './asset_wrapper'; -- cgit v1.2.3 From 323195f4ad099a0f028e3043950d241ad8d15b37 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 4 Dec 2018 17:12:38 +0100 Subject: De-nest libs contracts --- .../examples/ExchangeWrapper/ExchangeWrapper.sol | 2 +- .../contracts/examples/Whitelist/Whitelist.sol | 2 +- .../extensions/DutchAuction/DutchAuction.sol | 2 +- .../extensions/Forwarder/MixinExchangeWrapper.sol | 8 +- .../extensions/Forwarder/MixinForwarderCore.sol | 6 +- .../contracts/extensions/Forwarder/MixinWeth.sol | 2 +- .../Forwarder/interfaces/IForwarderCore.sol | 4 +- .../Forwarder/mixins/MExchangeWrapper.sol | 4 +- .../extensions/OrderValidator/OrderValidator.sol | 2 +- .../core/contracts/protocol/Exchange/Exchange.sol | 2 +- .../protocol/Exchange/MixinExchangeCore.sol | 8 +- .../protocol/Exchange/MixinMatchOrders.sol | 8 +- .../protocol/Exchange/MixinTransactions.sol | 4 +- .../protocol/Exchange/MixinWrapperFunctions.sol | 8 +- .../protocol/Exchange/interfaces/IExchangeCore.sol | 4 +- .../protocol/Exchange/interfaces/IMatchOrders.sol | 4 +- .../Exchange/interfaces/IWrapperFunctions.sol | 4 +- .../protocol/Exchange/mixins/MExchangeCore.sol | 4 +- .../protocol/Exchange/mixins/MMatchOrders.sol | 4 +- .../protocol/Exchange/mixins/MWrapperFunctions.sol | 4 +- .../ReentrantERC20Token/ReentrantERC20Token.sol | 2 +- contracts/libs/README.md | 4 +- contracts/libs/contracts/libs/LibAbiEncoder.sol | 215 +++++++++++++++++ .../libs/contracts/libs/LibAssetProxyErrors.sol | 38 ++++ contracts/libs/contracts/libs/LibConstants.sol | 49 ++++ contracts/libs/contracts/libs/LibEIP712.sol | 87 +++++++ .../libs/contracts/libs/LibExchangeErrors.sol | 70 ++++++ contracts/libs/contracts/libs/LibFillResults.sol | 53 +++++ contracts/libs/contracts/libs/LibMath.sol | 253 +++++++++++++++++++++ contracts/libs/contracts/libs/LibOrder.sol | 145 ++++++++++++ .../AssetProxy/libs/LibAssetProxyErrors.sol | 38 ---- .../protocol/Exchange/libs/LibAbiEncoder.sol | 215 ----------------- .../protocol/Exchange/libs/LibConstants.sol | 49 ---- .../contracts/protocol/Exchange/libs/LibEIP712.sol | 87 ------- .../protocol/Exchange/libs/LibExchangeErrors.sol | 70 ------ .../protocol/Exchange/libs/LibFillResults.sol | 53 ----- .../contracts/protocol/Exchange/libs/LibMath.sol | 253 --------------------- .../contracts/protocol/Exchange/libs/LibOrder.sol | 145 ------------ .../libs/contracts/test/TestLibs/TestLibs.sol | 8 +- 39 files changed, 960 insertions(+), 960 deletions(-) create mode 100644 contracts/libs/contracts/libs/LibAbiEncoder.sol create mode 100644 contracts/libs/contracts/libs/LibAssetProxyErrors.sol create mode 100644 contracts/libs/contracts/libs/LibConstants.sol create mode 100644 contracts/libs/contracts/libs/LibEIP712.sol create mode 100644 contracts/libs/contracts/libs/LibExchangeErrors.sol create mode 100644 contracts/libs/contracts/libs/LibFillResults.sol create mode 100644 contracts/libs/contracts/libs/LibMath.sol create mode 100644 contracts/libs/contracts/libs/LibOrder.sol delete mode 100644 contracts/libs/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol delete mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol delete mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibConstants.sol delete mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibEIP712.sol delete mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol delete mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibFillResults.sol delete mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibMath.sol delete mode 100644 contracts/libs/contracts/protocol/Exchange/libs/LibOrder.sol (limited to 'contracts') diff --git a/contracts/core/contracts/examples/ExchangeWrapper/ExchangeWrapper.sol b/contracts/core/contracts/examples/ExchangeWrapper/ExchangeWrapper.sol index 0b85f5864..ca5a64a26 100644 --- a/contracts/core/contracts/examples/ExchangeWrapper/ExchangeWrapper.sol +++ b/contracts/core/contracts/examples/ExchangeWrapper/ExchangeWrapper.sol @@ -20,7 +20,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; contract ExchangeWrapper { diff --git a/contracts/core/contracts/examples/Whitelist/Whitelist.sol b/contracts/core/contracts/examples/Whitelist/Whitelist.sol index 96ff47488..cfcddddd3 100644 --- a/contracts/core/contracts/examples/Whitelist/Whitelist.sol +++ b/contracts/core/contracts/examples/Whitelist/Whitelist.sol @@ -20,7 +20,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; import "@0x/contracts-utils/contracts/utils/Ownable/Ownable.sol"; diff --git a/contracts/core/contracts/extensions/DutchAuction/DutchAuction.sol b/contracts/core/contracts/extensions/DutchAuction/DutchAuction.sol index 02aafe3c9..a40991ae7 100644 --- a/contracts/core/contracts/extensions/DutchAuction/DutchAuction.sol +++ b/contracts/core/contracts/extensions/DutchAuction/DutchAuction.sol @@ -20,7 +20,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; import "../../tokens/ERC20Token/IERC20Token.sol"; import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; diff --git a/contracts/core/contracts/extensions/Forwarder/MixinExchangeWrapper.sol b/contracts/core/contracts/extensions/Forwarder/MixinExchangeWrapper.sol index e5f0894e7..210eb14c2 100644 --- a/contracts/core/contracts/extensions/Forwarder/MixinExchangeWrapper.sol +++ b/contracts/core/contracts/extensions/Forwarder/MixinExchangeWrapper.sol @@ -21,10 +21,10 @@ pragma experimental ABIEncoderV2; import "./libs/LibConstants.sol"; import "./mixins/MExchangeWrapper.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/libs/LibAbiEncoder.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibMath.sol"; contract MixinExchangeWrapper is diff --git a/contracts/core/contracts/extensions/Forwarder/MixinForwarderCore.sol b/contracts/core/contracts/extensions/Forwarder/MixinForwarderCore.sol index d7b50903a..bab78d79b 100644 --- a/contracts/core/contracts/extensions/Forwarder/MixinForwarderCore.sol +++ b/contracts/core/contracts/extensions/Forwarder/MixinForwarderCore.sol @@ -25,9 +25,9 @@ import "./mixins/MAssets.sol"; import "./mixins/MExchangeWrapper.sol"; import "./interfaces/IForwarderCore.sol"; import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibMath.sol"; contract MixinForwarderCore is diff --git a/contracts/core/contracts/extensions/Forwarder/MixinWeth.sol b/contracts/core/contracts/extensions/Forwarder/MixinWeth.sol index 2b2277004..2a281f3ae 100644 --- a/contracts/core/contracts/extensions/Forwarder/MixinWeth.sol +++ b/contracts/core/contracts/extensions/Forwarder/MixinWeth.sol @@ -18,7 +18,7 @@ pragma solidity 0.4.24; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/libs/LibMath.sol"; import "./libs/LibConstants.sol"; import "./mixins/MWeth.sol"; diff --git a/contracts/core/contracts/extensions/Forwarder/interfaces/IForwarderCore.sol b/contracts/core/contracts/extensions/Forwarder/interfaces/IForwarderCore.sol index 223a3d217..eede20bb8 100644 --- a/contracts/core/contracts/extensions/Forwarder/interfaces/IForwarderCore.sol +++ b/contracts/core/contracts/extensions/Forwarder/interfaces/IForwarderCore.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; contract IForwarderCore { diff --git a/contracts/core/contracts/extensions/Forwarder/mixins/MExchangeWrapper.sol b/contracts/core/contracts/extensions/Forwarder/mixins/MExchangeWrapper.sol index 75287a494..d9e71786a 100644 --- a/contracts/core/contracts/extensions/Forwarder/mixins/MExchangeWrapper.sol +++ b/contracts/core/contracts/extensions/Forwarder/mixins/MExchangeWrapper.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; contract MExchangeWrapper { diff --git a/contracts/core/contracts/extensions/OrderValidator/OrderValidator.sol b/contracts/core/contracts/extensions/OrderValidator/OrderValidator.sol index b4ead1776..9e9e63e9b 100644 --- a/contracts/core/contracts/extensions/OrderValidator/OrderValidator.sol +++ b/contracts/core/contracts/extensions/OrderValidator/OrderValidator.sol @@ -20,7 +20,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; import "../../tokens/ERC20Token/IERC20Token.sol"; import "../../tokens/ERC721Token/IERC721Token.sol"; import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/Exchange.sol b/contracts/core/contracts/protocol/Exchange/Exchange.sol index c199d3c39..65ca742ea 100644 --- a/contracts/core/contracts/protocol/Exchange/Exchange.sol +++ b/contracts/core/contracts/protocol/Exchange/Exchange.sol @@ -19,7 +19,7 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibConstants.sol"; +import "@0x/contracts-libs/contracts/libs/LibConstants.sol"; import "./MixinExchangeCore.sol"; import "./MixinSignatureValidator.sol"; import "./MixinWrapperFunctions.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/MixinExchangeCore.sol b/contracts/core/contracts/protocol/Exchange/MixinExchangeCore.sol index befd17659..68d6a3897 100644 --- a/contracts/core/contracts/protocol/Exchange/MixinExchangeCore.sol +++ b/contracts/core/contracts/protocol/Exchange/MixinExchangeCore.sol @@ -20,10 +20,10 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "@0x/contracts-utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibConstants.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/libs/LibConstants.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibMath.sol"; import "./mixins/MExchangeCore.sol"; import "./mixins/MSignatureValidator.sol"; import "./mixins/MTransactions.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/MixinMatchOrders.sol b/contracts/core/contracts/protocol/Exchange/MixinMatchOrders.sol index fee2a1a1c..fc6d73482 100644 --- a/contracts/core/contracts/protocol/Exchange/MixinMatchOrders.sol +++ b/contracts/core/contracts/protocol/Exchange/MixinMatchOrders.sol @@ -15,10 +15,10 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "@0x/contracts-utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibConstants.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibConstants.sol"; +import "@0x/contracts-libs/contracts/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; import "./mixins/MExchangeCore.sol"; import "./mixins/MMatchOrders.sol"; import "./mixins/MTransactions.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/MixinTransactions.sol b/contracts/core/contracts/protocol/Exchange/MixinTransactions.sol index 2f9eaca4f..87c614382 100644 --- a/contracts/core/contracts/protocol/Exchange/MixinTransactions.sol +++ b/contracts/core/contracts/protocol/Exchange/MixinTransactions.sol @@ -17,10 +17,10 @@ */ pragma solidity 0.4.24; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol"; +import "@0x/contracts-libs/contracts/libs/LibExchangeErrors.sol"; import "./mixins/MSignatureValidator.sol"; import "./mixins/MTransactions.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibEIP712.sol"; +import "@0x/contracts-libs/contracts/libs/LibEIP712.sol"; contract MixinTransactions is diff --git a/contracts/core/contracts/protocol/Exchange/MixinWrapperFunctions.sol b/contracts/core/contracts/protocol/Exchange/MixinWrapperFunctions.sol index 6a5c775fa..2d43432ff 100644 --- a/contracts/core/contracts/protocol/Exchange/MixinWrapperFunctions.sol +++ b/contracts/core/contracts/protocol/Exchange/MixinWrapperFunctions.sol @@ -20,10 +20,10 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; import "@0x/contracts-utils/contracts/utils/ReentrancyGuard/ReentrancyGuard.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibMath.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol"; +import "@0x/contracts-libs/contracts/libs/LibMath.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibAbiEncoder.sol"; import "./mixins/MExchangeCore.sol"; import "./mixins/MWrapperFunctions.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/interfaces/IExchangeCore.sol b/contracts/core/contracts/protocol/Exchange/interfaces/IExchangeCore.sol index 36e1d181e..0da73529c 100644 --- a/contracts/core/contracts/protocol/Exchange/interfaces/IExchangeCore.sol +++ b/contracts/core/contracts/protocol/Exchange/interfaces/IExchangeCore.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; contract IExchangeCore { diff --git a/contracts/core/contracts/protocol/Exchange/interfaces/IMatchOrders.sol b/contracts/core/contracts/protocol/Exchange/interfaces/IMatchOrders.sol index 114c57503..b88e158c3 100644 --- a/contracts/core/contracts/protocol/Exchange/interfaces/IMatchOrders.sol +++ b/contracts/core/contracts/protocol/Exchange/interfaces/IMatchOrders.sol @@ -18,8 +18,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; contract IMatchOrders { diff --git a/contracts/core/contracts/protocol/Exchange/interfaces/IWrapperFunctions.sol b/contracts/core/contracts/protocol/Exchange/interfaces/IWrapperFunctions.sol index 01deef4ae..833bb7e88 100644 --- a/contracts/core/contracts/protocol/Exchange/interfaces/IWrapperFunctions.sol +++ b/contracts/core/contracts/protocol/Exchange/interfaces/IWrapperFunctions.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; contract IWrapperFunctions { diff --git a/contracts/core/contracts/protocol/Exchange/mixins/MExchangeCore.sol b/contracts/core/contracts/protocol/Exchange/mixins/MExchangeCore.sol index ab3e9d6e2..099bdcc33 100644 --- a/contracts/core/contracts/protocol/Exchange/mixins/MExchangeCore.sol +++ b/contracts/core/contracts/protocol/Exchange/mixins/MExchangeCore.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; import "../interfaces/IExchangeCore.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/mixins/MMatchOrders.sol b/contracts/core/contracts/protocol/Exchange/mixins/MMatchOrders.sol index 719e193e7..bb285de03 100644 --- a/contracts/core/contracts/protocol/Exchange/mixins/MMatchOrders.sol +++ b/contracts/core/contracts/protocol/Exchange/mixins/MMatchOrders.sol @@ -18,8 +18,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; import "../interfaces/IMatchOrders.sol"; diff --git a/contracts/core/contracts/protocol/Exchange/mixins/MWrapperFunctions.sol b/contracts/core/contracts/protocol/Exchange/mixins/MWrapperFunctions.sol index 763af8885..2d21bf057 100644 --- a/contracts/core/contracts/protocol/Exchange/mixins/MWrapperFunctions.sol +++ b/contracts/core/contracts/protocol/Exchange/mixins/MWrapperFunctions.sol @@ -19,8 +19,8 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibFillResults.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibFillResults.sol"; import "../interfaces/IWrapperFunctions.sol"; diff --git a/contracts/core/contracts/test/ReentrantERC20Token/ReentrantERC20Token.sol b/contracts/core/contracts/test/ReentrantERC20Token/ReentrantERC20Token.sol index 2336e552f..8e077e3e8 100644 --- a/contracts/core/contracts/test/ReentrantERC20Token/ReentrantERC20Token.sol +++ b/contracts/core/contracts/test/ReentrantERC20Token/ReentrantERC20Token.sol @@ -22,7 +22,7 @@ pragma experimental ABIEncoderV2; import "@0x/contracts-utils/contracts/utils/LibBytes/LibBytes.sol"; import "../../tokens/ERC20Token/ERC20Token.sol"; import "../../protocol/Exchange/interfaces/IExchange.sol"; -import "@0x/contracts-libs/contracts/protocol/Exchange/libs/LibOrder.sol"; +import "@0x/contracts-libs/contracts/libs/LibOrder.sol"; // solhint-disable no-unused-vars diff --git a/contracts/libs/README.md b/contracts/libs/README.md index 42548f7c3..66eedf6be 100644 --- a/contracts/libs/README.md +++ b/contracts/libs/README.md @@ -6,8 +6,8 @@ Smart contracts libs used in the 0x protocol. Contracts can be found in the [contracts](./contracts) directory. The contents of this directory are broken down into the following subdirectories: -* [protocol](./contracts/protocol) - * This directory contains the libs used by protocol contracts. +* [libs](./contracts/protocol) + * This directory contains the libs. * [test](./contracts/test) * This directory contains mocks and other contracts that are used solely for testing contracts within the other directories. diff --git a/contracts/libs/contracts/libs/LibAbiEncoder.sol b/contracts/libs/contracts/libs/LibAbiEncoder.sol new file mode 100644 index 000000000..4aad37709 --- /dev/null +++ b/contracts/libs/contracts/libs/LibAbiEncoder.sol @@ -0,0 +1,215 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; +pragma experimental ABIEncoderV2; + +import "./LibOrder.sol"; + + +contract LibAbiEncoder { + + /// @dev ABI encodes calldata for `fillOrder`. + /// @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 ABI encoded calldata for `fillOrder`. + function abiEncodeFillOrder( + LibOrder.Order memory order, + uint256 takerAssetFillAmount, + bytes memory signature + ) + internal + pure + returns (bytes memory fillOrderCalldata) + { + // 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 makerAssetData (*) | + // | | 0x160 | | 12. Offset to takerAssetData (*) | + // | | 0x180 | 32 | makerAssetData Length | + // | | 0x1A0 | ** | makerAssetData Contents | + // | | 0x1C0 | 32 | takerAssetData Length | + // | | 0x1E0 | ** | takerAssetData 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 + + assembly { + + // Areas below may use the following variables: + // 1. Start -- Start of this area in memory + // 2. 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. 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 + fillOrderCalldata := mload(0x40) + // bytes4(keccak256("fillOrder((address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes),uint256,bytes)")) + // = 0xb4be83d5 + // Leave 0x20 bytes to store the length + mstore(add(fillOrderCalldata, 0x20), 0xb4be83d500000000000000000000000000000000000000000000000000000000) + let headerAreaEnd := add(fillOrderCalldata, 0x24) + + /////// 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 makerAssetData + mstore(add(dataAreaEnd, 0x160), mload(add(sourceOffset, 0x160))) // Offset to takerAssetData + dataAreaEnd := add(dataAreaEnd, 0x180) + sourceOffset := add(sourceOffset, 0x180) + + // Write offset to + mstore(add(dataAreaStart, mul(10, 0x20)), sub(dataAreaEnd, dataAreaStart)) + + // Calculate length of + sourceOffset := mload(add(order, 0x140)) // makerAssetData + arrayLenBytes := mload(sourceOffset) + sourceOffset := add(sourceOffset, 0x20) + arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20) + + // Write length of + mstore(dataAreaEnd, arrayLenBytes) + dataAreaEnd := add(dataAreaEnd, 0x20) + + // Write contents of + 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 + mstore(add(dataAreaStart, mul(11, 0x20)), sub(dataAreaEnd, dataAreaStart)) + + // Calculate length of + sourceOffset := mload(add(order, 0x160)) // takerAssetData + arrayLenBytes := mload(sourceOffset) + sourceOffset := add(sourceOffset, 0x20) + arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20) + + // Write length of + mstore(dataAreaEnd, arrayLenBytes) + dataAreaEnd := add(dataAreaEnd, 0x20) + + // Write contents of + 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) + } + + // Set length of calldata + mstore(fillOrderCalldata, sub(dataAreaEnd, add(fillOrderCalldata, 0x20))) + + // Increment free memory pointer + mstore(0x40, dataAreaEnd) + } + + return fillOrderCalldata; + } +} diff --git a/contracts/libs/contracts/libs/LibAssetProxyErrors.sol b/contracts/libs/contracts/libs/LibAssetProxyErrors.sol new file mode 100644 index 000000000..1d9a70cc1 --- /dev/null +++ b/contracts/libs/contracts/libs/LibAssetProxyErrors.sol @@ -0,0 +1,38 @@ +/* + + 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. + +*/ + +// solhint-disable +pragma solidity 0.4.24; + + +/// @dev This contract documents the revert reasons used in the AssetProxy contracts. +/// This contract is intended to serve as a reference, but is not actually used for efficiency reasons. +contract LibAssetProxyErrors { + + /// Authorizable errors /// + string constant SENDER_NOT_AUTHORIZED = "SENDER_NOT_AUTHORIZED"; // Sender not authorized to call this method. + string constant TARGET_NOT_AUTHORIZED = "TARGET_NOT_AUTHORIZED"; // Target address not authorized to call this method. + string constant TARGET_ALREADY_AUTHORIZED = "TARGET_ALREADY_AUTHORIZED"; // Target address must not already be authorized. + string constant INDEX_OUT_OF_BOUNDS = "INDEX_OUT_OF_BOUNDS"; // Specified array index is out of bounds. + string constant AUTHORIZED_ADDRESS_MISMATCH = "AUTHORIZED_ADDRESS_MISMATCH"; // Address at index does not match given target address. + + /// Transfer errors /// + string constant INVALID_AMOUNT = "INVALID_AMOUNT"; // Transfer amount must equal 1. + string constant TRANSFER_FAILED = "TRANSFER_FAILED"; // Transfer failed. + string constant LENGTH_GREATER_THAN_131_REQUIRED = "LENGTH_GREATER_THAN_131_REQUIRED"; // Byte array must have a length greater than 0. +} diff --git a/contracts/libs/contracts/libs/LibConstants.sol b/contracts/libs/contracts/libs/LibConstants.sol new file mode 100644 index 000000000..8d2732cd3 --- /dev/null +++ b/contracts/libs/contracts/libs/LibConstants.sol @@ -0,0 +1,49 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + + +// solhint-disable max-line-length +contract LibConstants { + + // Asset data for ZRX token. Used for fee transfers. + // @TODO: Hardcode constant when we deploy. Currently + // not constant to make testing easier. + + // The proxyId for ZRX_ASSET_DATA is bytes4(keccak256("ERC20Token(address)")) = 0xf47261b0 + + // Kovan ZRX address is 0x6ff6c0ff1d68b964901f986d4c9fa3ac68346570. + // The ABI encoded proxyId and address is 0xf47261b00000000000000000000000006ff6c0ff1d68b964901f986d4c9fa3ac68346570 + // bytes constant public ZRX_ASSET_DATA = "\xf4\x72\x61\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x6f\xf6\xc0\xff\x1d\x68\xb9\x64\x90\x1f\x98\x6d\x4c\x9f\xa3\xac\x68\x34\x65\x70"; + + // Mainnet ZRX address is 0xe41d2489571d322189246dafa5ebde1f4699f498. + // The ABI encoded proxyId and address is 0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498 + // bytes constant public ZRX_ASSET_DATA = "\xf4\x72\x61\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe4\x1d\x24\x89\x57\x1d\x32\x21\x89\x24\x6d\xaf\xa5\xeb\xde\x1f\x46\x99\xf4\x98"; + + // solhint-disable-next-line var-name-mixedcase + bytes public ZRX_ASSET_DATA; + + // @TODO: Remove when we deploy. + constructor (bytes memory zrxAssetData) + public + { + ZRX_ASSET_DATA = zrxAssetData; + } +} +// solhint-enable max-line-length diff --git a/contracts/libs/contracts/libs/LibEIP712.sol b/contracts/libs/contracts/libs/LibEIP712.sol new file mode 100644 index 000000000..203edc1fd --- /dev/null +++ b/contracts/libs/contracts/libs/LibEIP712.sol @@ -0,0 +1,87 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + + +contract LibEIP712 { + + // EIP191 header for EIP712 prefix + string constant internal EIP191_HEADER = "\x19\x01"; + + // EIP712 Domain Name value + string constant internal EIP712_DOMAIN_NAME = "0x Protocol"; + + // EIP712 Domain Version value + string constant internal EIP712_DOMAIN_VERSION = "2"; + + // Hash of the EIP712 Domain Separator Schema + bytes32 constant internal EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256(abi.encodePacked( + "EIP712Domain(", + "string name,", + "string version,", + "address verifyingContract", + ")" + )); + + // Hash of the EIP712 Domain Separator data + // solhint-disable-next-line var-name-mixedcase + bytes32 public EIP712_DOMAIN_HASH; + + constructor () + public + { + EIP712_DOMAIN_HASH = keccak256(abi.encodePacked( + EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH, + keccak256(bytes(EIP712_DOMAIN_NAME)), + keccak256(bytes(EIP712_DOMAIN_VERSION)), + bytes32(address(this)) + )); + } + + /// @dev Calculates EIP712 encoding for a hash struct in this EIP712 Domain. + /// @param hashStruct The EIP712 hash struct. + /// @return EIP712 hash applied to this EIP712 Domain. + function hashEIP712Message(bytes32 hashStruct) + internal + view + returns (bytes32 result) + { + bytes32 eip712DomainHash = EIP712_DOMAIN_HASH; + + // Assembly for more efficient computing: + // keccak256(abi.encodePacked( + // EIP191_HEADER, + // EIP712_DOMAIN_HASH, + // hashStruct + // )); + + assembly { + // Load free memory pointer + let memPtr := mload(64) + + mstore(memPtr, 0x1901000000000000000000000000000000000000000000000000000000000000) // EIP191 header + mstore(add(memPtr, 2), eip712DomainHash) // EIP712 domain hash + mstore(add(memPtr, 34), hashStruct) // Hash of struct + + // Compute hash + result := keccak256(memPtr, 66) + } + return result; + } +} diff --git a/contracts/libs/contracts/libs/LibExchangeErrors.sol b/contracts/libs/contracts/libs/LibExchangeErrors.sol new file mode 100644 index 000000000..a0f75bc06 --- /dev/null +++ b/contracts/libs/contracts/libs/LibExchangeErrors.sol @@ -0,0 +1,70 @@ +/* + + 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. + +*/ + +// solhint-disable +pragma solidity 0.4.24; + + +/// @dev This contract documents the revert reasons used in the Exchange contract. +/// This contract is intended to serve as a reference, but is not actually used for efficiency reasons. +contract LibExchangeErrors { + + /// Order validation errors /// + string constant ORDER_UNFILLABLE = "ORDER_UNFILLABLE"; // Order cannot be filled. + string constant INVALID_MAKER = "INVALID_MAKER"; // Invalid makerAddress. + string constant INVALID_TAKER = "INVALID_TAKER"; // Invalid takerAddress. + string constant INVALID_SENDER = "INVALID_SENDER"; // Invalid `msg.sender`. + string constant INVALID_ORDER_SIGNATURE = "INVALID_ORDER_SIGNATURE"; // Signature validation failed. + + /// fillOrder validation errors /// + string constant INVALID_TAKER_AMOUNT = "INVALID_TAKER_AMOUNT"; // takerAssetFillAmount cannot equal 0. + string constant ROUNDING_ERROR = "ROUNDING_ERROR"; // Rounding error greater than 0.1% of takerAssetFillAmount. + + /// Signature validation errors /// + string constant INVALID_SIGNATURE = "INVALID_SIGNATURE"; // Signature validation failed. + string constant SIGNATURE_ILLEGAL = "SIGNATURE_ILLEGAL"; // Signature type is illegal. + string constant SIGNATURE_UNSUPPORTED = "SIGNATURE_UNSUPPORTED"; // Signature type unsupported. + + /// cancelOrdersUptTo errors /// + string constant INVALID_NEW_ORDER_EPOCH = "INVALID_NEW_ORDER_EPOCH"; // Specified salt must be greater than or equal to existing orderEpoch. + + /// fillOrKillOrder errors /// + string constant COMPLETE_FILL_FAILED = "COMPLETE_FILL_FAILED"; // Desired takerAssetFillAmount could not be completely filled. + + /// matchOrders errors /// + string constant NEGATIVE_SPREAD_REQUIRED = "NEGATIVE_SPREAD_REQUIRED"; // Matched orders must have a negative spread. + + /// Transaction errors /// + string constant REENTRANCY_ILLEGAL = "REENTRANCY_ILLEGAL"; // Recursive reentrancy is not allowed. + string constant INVALID_TX_HASH = "INVALID_TX_HASH"; // Transaction has already been executed. + string constant INVALID_TX_SIGNATURE = "INVALID_TX_SIGNATURE"; // Signature validation failed. + string constant FAILED_EXECUTION = "FAILED_EXECUTION"; // Transaction execution failed. + + /// registerAssetProxy errors /// + string constant ASSET_PROXY_ALREADY_EXISTS = "ASSET_PROXY_ALREADY_EXISTS"; // AssetProxy with same id already exists. + + /// dispatchTransferFrom errors /// + string constant ASSET_PROXY_DOES_NOT_EXIST = "ASSET_PROXY_DOES_NOT_EXIST"; // No assetProxy registered at given id. + string constant TRANSFER_FAILED = "TRANSFER_FAILED"; // Asset transfer unsuccesful. + + /// Length validation errors /// + string constant LENGTH_GREATER_THAN_0_REQUIRED = "LENGTH_GREATER_THAN_0_REQUIRED"; // Byte array must have a length greater than 0. + string constant LENGTH_GREATER_THAN_3_REQUIRED = "LENGTH_GREATER_THAN_3_REQUIRED"; // Byte array must have a length greater than 3. + string constant LENGTH_0_REQUIRED = "LENGTH_0_REQUIRED"; // Byte array must have a length of 0. + string constant LENGTH_65_REQUIRED = "LENGTH_65_REQUIRED"; // Byte array must have a length of 65. +} diff --git a/contracts/libs/contracts/libs/LibFillResults.sol b/contracts/libs/contracts/libs/LibFillResults.sol new file mode 100644 index 000000000..fbd9950bf --- /dev/null +++ b/contracts/libs/contracts/libs/LibFillResults.sol @@ -0,0 +1,53 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; + + +contract LibFillResults is + SafeMath +{ + struct FillResults { + uint256 makerAssetFilledAmount; // Total amount of makerAsset(s) filled. + uint256 takerAssetFilledAmount; // Total amount of takerAsset(s) filled. + uint256 makerFeePaid; // Total amount of ZRX paid by maker(s) to feeRecipient(s). + uint256 takerFeePaid; // Total amount of ZRX paid by taker to feeRecipients(s). + } + + struct MatchedFillResults { + FillResults left; // Amounts filled and fees paid of left order. + FillResults right; // Amounts filled and fees paid of right order. + uint256 leftMakerAssetSpreadAmount; // Spread between price of left and right order, denominated in the left order's makerAsset, paid to taker. + } + + /// @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); + } +} diff --git a/contracts/libs/contracts/libs/LibMath.sol b/contracts/libs/contracts/libs/LibMath.sol new file mode 100644 index 000000000..b24876a9c --- /dev/null +++ b/contracts/libs/contracts/libs/LibMath.sol @@ -0,0 +1,253 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; + + +contract LibMath is + SafeMath +{ + /// @dev Calculates partial value given a numerator and denominator rounded down. + /// Reverts if rounding error is >= 0.1% + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to calculate partial of. + /// @return Partial value of target rounded down. + function safeGetPartialAmountFloor( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (uint256 partialAmount) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + require( + !isRoundingErrorFloor( + numerator, + denominator, + target + ), + "ROUNDING_ERROR" + ); + + partialAmount = safeDiv( + safeMul(numerator, target), + denominator + ); + return partialAmount; + } + + /// @dev Calculates partial value given a numerator and denominator rounded down. + /// Reverts if rounding error is >= 0.1% + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to calculate partial of. + /// @return Partial value of target rounded up. + function safeGetPartialAmountCeil( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (uint256 partialAmount) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + require( + !isRoundingErrorCeil( + numerator, + denominator, + target + ), + "ROUNDING_ERROR" + ); + + // safeDiv computes `floor(a / b)`. We use the identity (a, b integer): + // ceil(a / b) = floor((a + b - 1) / b) + // To implement `ceil(a / b)` using safeDiv. + partialAmount = safeDiv( + safeAdd( + safeMul(numerator, target), + safeSub(denominator, 1) + ), + denominator + ); + return partialAmount; + } + + /// @dev Calculates partial value given a numerator and denominator rounded down. + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to calculate partial of. + /// @return Partial value of target rounded down. + function getPartialAmountFloor( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (uint256 partialAmount) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + partialAmount = safeDiv( + safeMul(numerator, target), + denominator + ); + return partialAmount; + } + + /// @dev Calculates partial value given a numerator and denominator rounded down. + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to calculate partial of. + /// @return Partial value of target rounded up. + function getPartialAmountCeil( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (uint256 partialAmount) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + // safeDiv computes `floor(a / b)`. We use the identity (a, b integer): + // ceil(a / b) = floor((a + b - 1) / b) + // To implement `ceil(a / b)` using safeDiv. + partialAmount = safeDiv( + safeAdd( + safeMul(numerator, target), + safeSub(denominator, 1) + ), + denominator + ); + return partialAmount; + } + + /// @dev Checks if rounding error >= 0.1% when rounding down. + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to multiply with numerator/denominator. + /// @return Rounding error is present. + function isRoundingErrorFloor( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (bool isError) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + // The absolute rounding error is the difference between the rounded + // value and the ideal value. The relative rounding error is the + // absolute rounding error divided by the absolute value of the + // ideal value. This is undefined when the ideal value is zero. + // + // The ideal value is `numerator * target / denominator`. + // Let's call `numerator * target % denominator` the remainder. + // The absolute error is `remainder / denominator`. + // + // When the ideal value is zero, we require the absolute error to + // be zero. Fortunately, this is always the case. The ideal value is + // zero iff `numerator == 0` and/or `target == 0`. In this case the + // remainder and absolute error are also zero. + if (target == 0 || numerator == 0) { + return false; + } + + // Otherwise, we want the relative rounding error to be strictly + // less than 0.1%. + // The relative error is `remainder / (numerator * target)`. + // We want the relative error less than 1 / 1000: + // remainder / (numerator * denominator) < 1 / 1000 + // or equivalently: + // 1000 * remainder < numerator * target + // so we have a rounding error iff: + // 1000 * remainder >= numerator * target + uint256 remainder = mulmod( + target, + numerator, + denominator + ); + isError = safeMul(1000, remainder) >= safeMul(numerator, target); + return isError; + } + + /// @dev Checks if rounding error >= 0.1% when rounding up. + /// @param numerator Numerator. + /// @param denominator Denominator. + /// @param target Value to multiply with numerator/denominator. + /// @return Rounding error is present. + function isRoundingErrorCeil( + uint256 numerator, + uint256 denominator, + uint256 target + ) + internal + pure + returns (bool isError) + { + require( + denominator > 0, + "DIVISION_BY_ZERO" + ); + + // See the comments in `isRoundingError`. + if (target == 0 || numerator == 0) { + // When either is zero, the ideal value and rounded value are zero + // and there is no rounding error. (Although the relative error + // is undefined.) + return false; + } + // Compute remainder as before + uint256 remainder = mulmod( + target, + numerator, + denominator + ); + remainder = safeSub(denominator, remainder) % denominator; + isError = safeMul(1000, remainder) >= safeMul(numerator, target); + return isError; + } +} diff --git a/contracts/libs/contracts/libs/LibOrder.sol b/contracts/libs/contracts/libs/LibOrder.sol new file mode 100644 index 000000000..0fe7c2161 --- /dev/null +++ b/contracts/libs/contracts/libs/LibOrder.sol @@ -0,0 +1,145 @@ +/* + + Copyright 2018 ZeroEx Intl. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +pragma solidity 0.4.24; + +import "./LibEIP712.sol"; + + +contract LibOrder is + LibEIP712 +{ + // Hash for the EIP712 Order Schema + bytes32 constant internal EIP712_ORDER_SCHEMA_HASH = keccak256(abi.encodePacked( + "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", + ")" + )); + + // A valid order remains fillable until it is expired, fully filled, or cancelled. + // An order's state is unaffected by external factors, like account balances. + enum OrderStatus { + INVALID, // Default value + INVALID_MAKER_ASSET_AMOUNT, // Order does not have a valid maker asset amount + INVALID_TAKER_ASSET_AMOUNT, // Order does not have a valid taker asset amount + FILLABLE, // Order is fillable + EXPIRED, // Order has already expired + FULLY_FILLED, // Order is fully filled + CANCELLED // Order has been cancelled + } + + // solhint-disable max-line-length + struct Order { + address makerAddress; // Address that created the order. + address takerAddress; // Address that is allowed to fill the order. If set to 0, any address is allowed to fill the order. + address feeRecipientAddress; // Address that will recieve fees when order is filled. + address senderAddress; // Address that is allowed to call Exchange contract methods that affect this order. If set to 0, any address is allowed to call these methods. + uint256 makerAssetAmount; // Amount of makerAsset being offered by maker. Must be greater than 0. + uint256 takerAssetAmount; // Amount of takerAsset being bid on by maker. Must be greater than 0. + uint256 makerFee; // Amount of ZRX paid to feeRecipient by maker when order is filled. If set to 0, no transfer of ZRX from maker to feeRecipient will be attempted. + uint256 takerFee; // Amount of ZRX paid to feeRecipient by taker when order is filled. If set to 0, no transfer of ZRX from taker to feeRecipient will be attempted. + uint256 expirationTimeSeconds; // Timestamp in seconds at which order expires. + uint256 salt; // Arbitrary number to facilitate uniqueness of the order's hash. + bytes makerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring makerAsset. The last byte references the id of this proxy. + bytes takerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring takerAsset. The last byte references the id of this proxy. + } + // solhint-enable max-line-length + + struct OrderInfo { + uint8 orderStatus; // Status that describes order's validity and fillability. + bytes32 orderHash; // EIP712 hash of the order (see LibOrder.getOrderHash). + uint256 orderTakerAssetFilledAmount; // Amount of order that has already been filled. + } + + /// @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) + { + orderHash = hashEIP712Message(hashOrder(order)); + return orderHash; + } + + /// @dev Calculates EIP712 hash of the order. + /// @param order The order structure. + /// @return EIP712 hash of the order. + function hashOrder(Order memory order) + internal + pure + returns (bytes32 result) + { + bytes32 schemaHash = EIP712_ORDER_SCHEMA_HASH; + bytes32 makerAssetDataHash = keccak256(order.makerAssetData); + bytes32 takerAssetDataHash = keccak256(order.takerAssetData); + + // Assembly for more efficiently computing: + // keccak256(abi.encodePacked( + // EIP712_ORDER_SCHEMA_HASH, + // bytes32(order.makerAddress), + // bytes32(order.takerAddress), + // bytes32(order.feeRecipientAddress), + // bytes32(order.senderAddress), + // order.makerAssetAmount, + // order.takerAssetAmount, + // order.makerFee, + // order.takerFee, + // order.expirationTimeSeconds, + // order.salt, + // keccak256(order.makerAssetData), + // keccak256(order.takerAssetData) + // )); + + assembly { + // Calculate memory addresses that will be swapped out before hashing + let pos1 := sub(order, 32) + let pos2 := add(order, 320) + let pos3 := add(order, 352) + + // Backup + let temp1 := mload(pos1) + let temp2 := mload(pos2) + let temp3 := mload(pos3) + + // Hash in place + mstore(pos1, schemaHash) + mstore(pos2, makerAssetDataHash) + mstore(pos3, takerAssetDataHash) + result := keccak256(pos1, 416) + + // Restore + mstore(pos1, temp1) + mstore(pos2, temp2) + mstore(pos3, temp3) + } + return result; + } +} diff --git a/contracts/libs/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol b/contracts/libs/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol deleted file mode 100644 index 1d9a70cc1..000000000 --- a/contracts/libs/contracts/protocol/AssetProxy/libs/LibAssetProxyErrors.sol +++ /dev/null @@ -1,38 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -// solhint-disable -pragma solidity 0.4.24; - - -/// @dev This contract documents the revert reasons used in the AssetProxy contracts. -/// This contract is intended to serve as a reference, but is not actually used for efficiency reasons. -contract LibAssetProxyErrors { - - /// Authorizable errors /// - string constant SENDER_NOT_AUTHORIZED = "SENDER_NOT_AUTHORIZED"; // Sender not authorized to call this method. - string constant TARGET_NOT_AUTHORIZED = "TARGET_NOT_AUTHORIZED"; // Target address not authorized to call this method. - string constant TARGET_ALREADY_AUTHORIZED = "TARGET_ALREADY_AUTHORIZED"; // Target address must not already be authorized. - string constant INDEX_OUT_OF_BOUNDS = "INDEX_OUT_OF_BOUNDS"; // Specified array index is out of bounds. - string constant AUTHORIZED_ADDRESS_MISMATCH = "AUTHORIZED_ADDRESS_MISMATCH"; // Address at index does not match given target address. - - /// Transfer errors /// - string constant INVALID_AMOUNT = "INVALID_AMOUNT"; // Transfer amount must equal 1. - string constant TRANSFER_FAILED = "TRANSFER_FAILED"; // Transfer failed. - string constant LENGTH_GREATER_THAN_131_REQUIRED = "LENGTH_GREATER_THAN_131_REQUIRED"; // Byte array must have a length greater than 0. -} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol deleted file mode 100644 index 4aad37709..000000000 --- a/contracts/libs/contracts/protocol/Exchange/libs/LibAbiEncoder.sol +++ /dev/null @@ -1,215 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; -pragma experimental ABIEncoderV2; - -import "./LibOrder.sol"; - - -contract LibAbiEncoder { - - /// @dev ABI encodes calldata for `fillOrder`. - /// @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 ABI encoded calldata for `fillOrder`. - function abiEncodeFillOrder( - LibOrder.Order memory order, - uint256 takerAssetFillAmount, - bytes memory signature - ) - internal - pure - returns (bytes memory fillOrderCalldata) - { - // 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 makerAssetData (*) | - // | | 0x160 | | 12. Offset to takerAssetData (*) | - // | | 0x180 | 32 | makerAssetData Length | - // | | 0x1A0 | ** | makerAssetData Contents | - // | | 0x1C0 | 32 | takerAssetData Length | - // | | 0x1E0 | ** | takerAssetData 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 - - assembly { - - // Areas below may use the following variables: - // 1. Start -- Start of this area in memory - // 2. 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. 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 - fillOrderCalldata := mload(0x40) - // bytes4(keccak256("fillOrder((address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes),uint256,bytes)")) - // = 0xb4be83d5 - // Leave 0x20 bytes to store the length - mstore(add(fillOrderCalldata, 0x20), 0xb4be83d500000000000000000000000000000000000000000000000000000000) - let headerAreaEnd := add(fillOrderCalldata, 0x24) - - /////// 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 makerAssetData - mstore(add(dataAreaEnd, 0x160), mload(add(sourceOffset, 0x160))) // Offset to takerAssetData - dataAreaEnd := add(dataAreaEnd, 0x180) - sourceOffset := add(sourceOffset, 0x180) - - // Write offset to - mstore(add(dataAreaStart, mul(10, 0x20)), sub(dataAreaEnd, dataAreaStart)) - - // Calculate length of - sourceOffset := mload(add(order, 0x140)) // makerAssetData - arrayLenBytes := mload(sourceOffset) - sourceOffset := add(sourceOffset, 0x20) - arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20) - - // Write length of - mstore(dataAreaEnd, arrayLenBytes) - dataAreaEnd := add(dataAreaEnd, 0x20) - - // Write contents of - 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 - mstore(add(dataAreaStart, mul(11, 0x20)), sub(dataAreaEnd, dataAreaStart)) - - // Calculate length of - sourceOffset := mload(add(order, 0x160)) // takerAssetData - arrayLenBytes := mload(sourceOffset) - sourceOffset := add(sourceOffset, 0x20) - arrayLenWords := div(add(arrayLenBytes, 0x1F), 0x20) - - // Write length of - mstore(dataAreaEnd, arrayLenBytes) - dataAreaEnd := add(dataAreaEnd, 0x20) - - // Write contents of - 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) - } - - // Set length of calldata - mstore(fillOrderCalldata, sub(dataAreaEnd, add(fillOrderCalldata, 0x20))) - - // Increment free memory pointer - mstore(0x40, dataAreaEnd) - } - - return fillOrderCalldata; - } -} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibConstants.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibConstants.sol deleted file mode 100644 index 8d2732cd3..000000000 --- a/contracts/libs/contracts/protocol/Exchange/libs/LibConstants.sol +++ /dev/null @@ -1,49 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - - -// solhint-disable max-line-length -contract LibConstants { - - // Asset data for ZRX token. Used for fee transfers. - // @TODO: Hardcode constant when we deploy. Currently - // not constant to make testing easier. - - // The proxyId for ZRX_ASSET_DATA is bytes4(keccak256("ERC20Token(address)")) = 0xf47261b0 - - // Kovan ZRX address is 0x6ff6c0ff1d68b964901f986d4c9fa3ac68346570. - // The ABI encoded proxyId and address is 0xf47261b00000000000000000000000006ff6c0ff1d68b964901f986d4c9fa3ac68346570 - // bytes constant public ZRX_ASSET_DATA = "\xf4\x72\x61\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x6f\xf6\xc0\xff\x1d\x68\xb9\x64\x90\x1f\x98\x6d\x4c\x9f\xa3\xac\x68\x34\x65\x70"; - - // Mainnet ZRX address is 0xe41d2489571d322189246dafa5ebde1f4699f498. - // The ABI encoded proxyId and address is 0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498 - // bytes constant public ZRX_ASSET_DATA = "\xf4\x72\x61\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe4\x1d\x24\x89\x57\x1d\x32\x21\x89\x24\x6d\xaf\xa5\xeb\xde\x1f\x46\x99\xf4\x98"; - - // solhint-disable-next-line var-name-mixedcase - bytes public ZRX_ASSET_DATA; - - // @TODO: Remove when we deploy. - constructor (bytes memory zrxAssetData) - public - { - ZRX_ASSET_DATA = zrxAssetData; - } -} -// solhint-enable max-line-length diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibEIP712.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibEIP712.sol deleted file mode 100644 index 203edc1fd..000000000 --- a/contracts/libs/contracts/protocol/Exchange/libs/LibEIP712.sol +++ /dev/null @@ -1,87 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - - -contract LibEIP712 { - - // EIP191 header for EIP712 prefix - string constant internal EIP191_HEADER = "\x19\x01"; - - // EIP712 Domain Name value - string constant internal EIP712_DOMAIN_NAME = "0x Protocol"; - - // EIP712 Domain Version value - string constant internal EIP712_DOMAIN_VERSION = "2"; - - // Hash of the EIP712 Domain Separator Schema - bytes32 constant internal EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH = keccak256(abi.encodePacked( - "EIP712Domain(", - "string name,", - "string version,", - "address verifyingContract", - ")" - )); - - // Hash of the EIP712 Domain Separator data - // solhint-disable-next-line var-name-mixedcase - bytes32 public EIP712_DOMAIN_HASH; - - constructor () - public - { - EIP712_DOMAIN_HASH = keccak256(abi.encodePacked( - EIP712_DOMAIN_SEPARATOR_SCHEMA_HASH, - keccak256(bytes(EIP712_DOMAIN_NAME)), - keccak256(bytes(EIP712_DOMAIN_VERSION)), - bytes32(address(this)) - )); - } - - /// @dev Calculates EIP712 encoding for a hash struct in this EIP712 Domain. - /// @param hashStruct The EIP712 hash struct. - /// @return EIP712 hash applied to this EIP712 Domain. - function hashEIP712Message(bytes32 hashStruct) - internal - view - returns (bytes32 result) - { - bytes32 eip712DomainHash = EIP712_DOMAIN_HASH; - - // Assembly for more efficient computing: - // keccak256(abi.encodePacked( - // EIP191_HEADER, - // EIP712_DOMAIN_HASH, - // hashStruct - // )); - - assembly { - // Load free memory pointer - let memPtr := mload(64) - - mstore(memPtr, 0x1901000000000000000000000000000000000000000000000000000000000000) // EIP191 header - mstore(add(memPtr, 2), eip712DomainHash) // EIP712 domain hash - mstore(add(memPtr, 34), hashStruct) // Hash of struct - - // Compute hash - result := keccak256(memPtr, 66) - } - return result; - } -} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol deleted file mode 100644 index a0f75bc06..000000000 --- a/contracts/libs/contracts/protocol/Exchange/libs/LibExchangeErrors.sol +++ /dev/null @@ -1,70 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -// solhint-disable -pragma solidity 0.4.24; - - -/// @dev This contract documents the revert reasons used in the Exchange contract. -/// This contract is intended to serve as a reference, but is not actually used for efficiency reasons. -contract LibExchangeErrors { - - /// Order validation errors /// - string constant ORDER_UNFILLABLE = "ORDER_UNFILLABLE"; // Order cannot be filled. - string constant INVALID_MAKER = "INVALID_MAKER"; // Invalid makerAddress. - string constant INVALID_TAKER = "INVALID_TAKER"; // Invalid takerAddress. - string constant INVALID_SENDER = "INVALID_SENDER"; // Invalid `msg.sender`. - string constant INVALID_ORDER_SIGNATURE = "INVALID_ORDER_SIGNATURE"; // Signature validation failed. - - /// fillOrder validation errors /// - string constant INVALID_TAKER_AMOUNT = "INVALID_TAKER_AMOUNT"; // takerAssetFillAmount cannot equal 0. - string constant ROUNDING_ERROR = "ROUNDING_ERROR"; // Rounding error greater than 0.1% of takerAssetFillAmount. - - /// Signature validation errors /// - string constant INVALID_SIGNATURE = "INVALID_SIGNATURE"; // Signature validation failed. - string constant SIGNATURE_ILLEGAL = "SIGNATURE_ILLEGAL"; // Signature type is illegal. - string constant SIGNATURE_UNSUPPORTED = "SIGNATURE_UNSUPPORTED"; // Signature type unsupported. - - /// cancelOrdersUptTo errors /// - string constant INVALID_NEW_ORDER_EPOCH = "INVALID_NEW_ORDER_EPOCH"; // Specified salt must be greater than or equal to existing orderEpoch. - - /// fillOrKillOrder errors /// - string constant COMPLETE_FILL_FAILED = "COMPLETE_FILL_FAILED"; // Desired takerAssetFillAmount could not be completely filled. - - /// matchOrders errors /// - string constant NEGATIVE_SPREAD_REQUIRED = "NEGATIVE_SPREAD_REQUIRED"; // Matched orders must have a negative spread. - - /// Transaction errors /// - string constant REENTRANCY_ILLEGAL = "REENTRANCY_ILLEGAL"; // Recursive reentrancy is not allowed. - string constant INVALID_TX_HASH = "INVALID_TX_HASH"; // Transaction has already been executed. - string constant INVALID_TX_SIGNATURE = "INVALID_TX_SIGNATURE"; // Signature validation failed. - string constant FAILED_EXECUTION = "FAILED_EXECUTION"; // Transaction execution failed. - - /// registerAssetProxy errors /// - string constant ASSET_PROXY_ALREADY_EXISTS = "ASSET_PROXY_ALREADY_EXISTS"; // AssetProxy with same id already exists. - - /// dispatchTransferFrom errors /// - string constant ASSET_PROXY_DOES_NOT_EXIST = "ASSET_PROXY_DOES_NOT_EXIST"; // No assetProxy registered at given id. - string constant TRANSFER_FAILED = "TRANSFER_FAILED"; // Asset transfer unsuccesful. - - /// Length validation errors /// - string constant LENGTH_GREATER_THAN_0_REQUIRED = "LENGTH_GREATER_THAN_0_REQUIRED"; // Byte array must have a length greater than 0. - string constant LENGTH_GREATER_THAN_3_REQUIRED = "LENGTH_GREATER_THAN_3_REQUIRED"; // Byte array must have a length greater than 3. - string constant LENGTH_0_REQUIRED = "LENGTH_0_REQUIRED"; // Byte array must have a length of 0. - string constant LENGTH_65_REQUIRED = "LENGTH_65_REQUIRED"; // Byte array must have a length of 65. -} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibFillResults.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibFillResults.sol deleted file mode 100644 index fbd9950bf..000000000 --- a/contracts/libs/contracts/protocol/Exchange/libs/LibFillResults.sol +++ /dev/null @@ -1,53 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - -import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; - - -contract LibFillResults is - SafeMath -{ - struct FillResults { - uint256 makerAssetFilledAmount; // Total amount of makerAsset(s) filled. - uint256 takerAssetFilledAmount; // Total amount of takerAsset(s) filled. - uint256 makerFeePaid; // Total amount of ZRX paid by maker(s) to feeRecipient(s). - uint256 takerFeePaid; // Total amount of ZRX paid by taker to feeRecipients(s). - } - - struct MatchedFillResults { - FillResults left; // Amounts filled and fees paid of left order. - FillResults right; // Amounts filled and fees paid of right order. - uint256 leftMakerAssetSpreadAmount; // Spread between price of left and right order, denominated in the left order's makerAsset, paid to taker. - } - - /// @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); - } -} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibMath.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibMath.sol deleted file mode 100644 index b24876a9c..000000000 --- a/contracts/libs/contracts/protocol/Exchange/libs/LibMath.sol +++ /dev/null @@ -1,253 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - -import "@0x/contracts-utils/contracts/utils/SafeMath/SafeMath.sol"; - - -contract LibMath is - SafeMath -{ - /// @dev Calculates partial value given a numerator and denominator rounded down. - /// Reverts if rounding error is >= 0.1% - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to calculate partial of. - /// @return Partial value of target rounded down. - function safeGetPartialAmountFloor( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (uint256 partialAmount) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - require( - !isRoundingErrorFloor( - numerator, - denominator, - target - ), - "ROUNDING_ERROR" - ); - - partialAmount = safeDiv( - safeMul(numerator, target), - denominator - ); - return partialAmount; - } - - /// @dev Calculates partial value given a numerator and denominator rounded down. - /// Reverts if rounding error is >= 0.1% - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to calculate partial of. - /// @return Partial value of target rounded up. - function safeGetPartialAmountCeil( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (uint256 partialAmount) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - require( - !isRoundingErrorCeil( - numerator, - denominator, - target - ), - "ROUNDING_ERROR" - ); - - // safeDiv computes `floor(a / b)`. We use the identity (a, b integer): - // ceil(a / b) = floor((a + b - 1) / b) - // To implement `ceil(a / b)` using safeDiv. - partialAmount = safeDiv( - safeAdd( - safeMul(numerator, target), - safeSub(denominator, 1) - ), - denominator - ); - return partialAmount; - } - - /// @dev Calculates partial value given a numerator and denominator rounded down. - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to calculate partial of. - /// @return Partial value of target rounded down. - function getPartialAmountFloor( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (uint256 partialAmount) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - partialAmount = safeDiv( - safeMul(numerator, target), - denominator - ); - return partialAmount; - } - - /// @dev Calculates partial value given a numerator and denominator rounded down. - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to calculate partial of. - /// @return Partial value of target rounded up. - function getPartialAmountCeil( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (uint256 partialAmount) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - // safeDiv computes `floor(a / b)`. We use the identity (a, b integer): - // ceil(a / b) = floor((a + b - 1) / b) - // To implement `ceil(a / b)` using safeDiv. - partialAmount = safeDiv( - safeAdd( - safeMul(numerator, target), - safeSub(denominator, 1) - ), - denominator - ); - return partialAmount; - } - - /// @dev Checks if rounding error >= 0.1% when rounding down. - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to multiply with numerator/denominator. - /// @return Rounding error is present. - function isRoundingErrorFloor( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (bool isError) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - // The absolute rounding error is the difference between the rounded - // value and the ideal value. The relative rounding error is the - // absolute rounding error divided by the absolute value of the - // ideal value. This is undefined when the ideal value is zero. - // - // The ideal value is `numerator * target / denominator`. - // Let's call `numerator * target % denominator` the remainder. - // The absolute error is `remainder / denominator`. - // - // When the ideal value is zero, we require the absolute error to - // be zero. Fortunately, this is always the case. The ideal value is - // zero iff `numerator == 0` and/or `target == 0`. In this case the - // remainder and absolute error are also zero. - if (target == 0 || numerator == 0) { - return false; - } - - // Otherwise, we want the relative rounding error to be strictly - // less than 0.1%. - // The relative error is `remainder / (numerator * target)`. - // We want the relative error less than 1 / 1000: - // remainder / (numerator * denominator) < 1 / 1000 - // or equivalently: - // 1000 * remainder < numerator * target - // so we have a rounding error iff: - // 1000 * remainder >= numerator * target - uint256 remainder = mulmod( - target, - numerator, - denominator - ); - isError = safeMul(1000, remainder) >= safeMul(numerator, target); - return isError; - } - - /// @dev Checks if rounding error >= 0.1% when rounding up. - /// @param numerator Numerator. - /// @param denominator Denominator. - /// @param target Value to multiply with numerator/denominator. - /// @return Rounding error is present. - function isRoundingErrorCeil( - uint256 numerator, - uint256 denominator, - uint256 target - ) - internal - pure - returns (bool isError) - { - require( - denominator > 0, - "DIVISION_BY_ZERO" - ); - - // See the comments in `isRoundingError`. - if (target == 0 || numerator == 0) { - // When either is zero, the ideal value and rounded value are zero - // and there is no rounding error. (Although the relative error - // is undefined.) - return false; - } - // Compute remainder as before - uint256 remainder = mulmod( - target, - numerator, - denominator - ); - remainder = safeSub(denominator, remainder) % denominator; - isError = safeMul(1000, remainder) >= safeMul(numerator, target); - return isError; - } -} diff --git a/contracts/libs/contracts/protocol/Exchange/libs/LibOrder.sol b/contracts/libs/contracts/protocol/Exchange/libs/LibOrder.sol deleted file mode 100644 index 0fe7c2161..000000000 --- a/contracts/libs/contracts/protocol/Exchange/libs/LibOrder.sol +++ /dev/null @@ -1,145 +0,0 @@ -/* - - Copyright 2018 ZeroEx Intl. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -pragma solidity 0.4.24; - -import "./LibEIP712.sol"; - - -contract LibOrder is - LibEIP712 -{ - // Hash for the EIP712 Order Schema - bytes32 constant internal EIP712_ORDER_SCHEMA_HASH = keccak256(abi.encodePacked( - "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", - ")" - )); - - // A valid order remains fillable until it is expired, fully filled, or cancelled. - // An order's state is unaffected by external factors, like account balances. - enum OrderStatus { - INVALID, // Default value - INVALID_MAKER_ASSET_AMOUNT, // Order does not have a valid maker asset amount - INVALID_TAKER_ASSET_AMOUNT, // Order does not have a valid taker asset amount - FILLABLE, // Order is fillable - EXPIRED, // Order has already expired - FULLY_FILLED, // Order is fully filled - CANCELLED // Order has been cancelled - } - - // solhint-disable max-line-length - struct Order { - address makerAddress; // Address that created the order. - address takerAddress; // Address that is allowed to fill the order. If set to 0, any address is allowed to fill the order. - address feeRecipientAddress; // Address that will recieve fees when order is filled. - address senderAddress; // Address that is allowed to call Exchange contract methods that affect this order. If set to 0, any address is allowed to call these methods. - uint256 makerAssetAmount; // Amount of makerAsset being offered by maker. Must be greater than 0. - uint256 takerAssetAmount; // Amount of takerAsset being bid on by maker. Must be greater than 0. - uint256 makerFee; // Amount of ZRX paid to feeRecipient by maker when order is filled. If set to 0, no transfer of ZRX from maker to feeRecipient will be attempted. - uint256 takerFee; // Amount of ZRX paid to feeRecipient by taker when order is filled. If set to 0, no transfer of ZRX from taker to feeRecipient will be attempted. - uint256 expirationTimeSeconds; // Timestamp in seconds at which order expires. - uint256 salt; // Arbitrary number to facilitate uniqueness of the order's hash. - bytes makerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring makerAsset. The last byte references the id of this proxy. - bytes takerAssetData; // Encoded data that can be decoded by a specified proxy contract when transferring takerAsset. The last byte references the id of this proxy. - } - // solhint-enable max-line-length - - struct OrderInfo { - uint8 orderStatus; // Status that describes order's validity and fillability. - bytes32 orderHash; // EIP712 hash of the order (see LibOrder.getOrderHash). - uint256 orderTakerAssetFilledAmount; // Amount of order that has already been filled. - } - - /// @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) - { - orderHash = hashEIP712Message(hashOrder(order)); - return orderHash; - } - - /// @dev Calculates EIP712 hash of the order. - /// @param order The order structure. - /// @return EIP712 hash of the order. - function hashOrder(Order memory order) - internal - pure - returns (bytes32 result) - { - bytes32 schemaHash = EIP712_ORDER_SCHEMA_HASH; - bytes32 makerAssetDataHash = keccak256(order.makerAssetData); - bytes32 takerAssetDataHash = keccak256(order.takerAssetData); - - // Assembly for more efficiently computing: - // keccak256(abi.encodePacked( - // EIP712_ORDER_SCHEMA_HASH, - // bytes32(order.makerAddress), - // bytes32(order.takerAddress), - // bytes32(order.feeRecipientAddress), - // bytes32(order.senderAddress), - // order.makerAssetAmount, - // order.takerAssetAmount, - // order.makerFee, - // order.takerFee, - // order.expirationTimeSeconds, - // order.salt, - // keccak256(order.makerAssetData), - // keccak256(order.takerAssetData) - // )); - - assembly { - // Calculate memory addresses that will be swapped out before hashing - let pos1 := sub(order, 32) - let pos2 := add(order, 320) - let pos3 := add(order, 352) - - // Backup - let temp1 := mload(pos1) - let temp2 := mload(pos2) - let temp3 := mload(pos3) - - // Hash in place - mstore(pos1, schemaHash) - mstore(pos2, makerAssetDataHash) - mstore(pos3, takerAssetDataHash) - result := keccak256(pos1, 416) - - // Restore - mstore(pos1, temp1) - mstore(pos2, temp2) - mstore(pos3, temp3) - } - return result; - } -} diff --git a/contracts/libs/contracts/test/TestLibs/TestLibs.sol b/contracts/libs/contracts/test/TestLibs/TestLibs.sol index a10f981fc..bd5f9f9da 100644 --- a/contracts/libs/contracts/test/TestLibs/TestLibs.sol +++ b/contracts/libs/contracts/test/TestLibs/TestLibs.sol @@ -19,10 +19,10 @@ pragma solidity 0.4.24; pragma experimental ABIEncoderV2; -import "../../protocol/Exchange/libs/LibMath.sol"; -import "../../protocol/Exchange/libs/LibOrder.sol"; -import "../../protocol/Exchange/libs/LibFillResults.sol"; -import "../../protocol/Exchange/libs/LibAbiEncoder.sol"; +import "../../libs/LibMath.sol"; +import "../../libs/LibOrder.sol"; +import "../../libs/LibFillResults.sol"; +import "../../libs/LibAbiEncoder.sol"; contract TestLibs is -- cgit v1.2.3