Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

New Order Matching Strategy #1913

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a5f2f3b
Implemented batchMatchOrders
jalextowle Jun 24, 2019
94049b1
Created tests for batchMatchOrders
jalextowle Jun 24, 2019
a53a97a
Removed batchMatchOrdersWithMaximumFill
jalextowle Jun 27, 2019
31a6205
Added a test for partial matching
jalextowle Jun 28, 2019
619cce4
Added getMatchOrdersResultsAsync and getBatchMatchOrdersResultsAsync
jalextowle Jul 1, 2019
cbe1d70
Added RichErrors to batchMatchOrders
jalextowle Jul 1, 2019
af74057
Improved the efficiency of batchMatchOrders
jalextowle Jul 2, 2019
27d6dd1
Added a new test
jalextowle Jul 2, 2019
6eafc4c
Implemented batchMatchOrdersWithMaximalFill
jalextowle Jun 27, 2019
e13fba1
Implemented matchOrdersWithMaximalFill
jalextowle Jun 30, 2019
787b0ab
Modified the matchOrders tests to test matchOrdersWithMaximalFill
jalextowle Jul 1, 2019
af9dd7f
Polished MixinMatchOrders and removed unimplemented test
jalextowle Jul 1, 2019
006e666
Split up TestExchangeInternals into two contracts
jalextowle Jul 2, 2019
fb8479b
Rebased onto PR #1900
jalextowle Jul 2, 2019
09cff3b
Added testing for the BatchMatchOrdersError Rich Error
jalextowle Jul 2, 2019
5842293
Added a reentrancy test for matchOrdersWithMaximalFill
jalextowle Jul 3, 2019
e8687c9
Ran prettier
jalextowle Jul 3, 2019
f0d5cdc
Improved test coverage for batchMatchOrdersWithMaximalFill
jalextowle Jul 3, 2019
5c49ec3
Created a test for case 3 of matchOrdersWithMaximalFill
jalextowle Jul 3, 2019
06d8610
Improved the testing for matchOrders and batchMatchOrders
jalextowle Jul 4, 2019
2e90abb
Addessed some review comments
jalextowle Jul 6, 2019
5aac941
Updated batchMatchOrders to fix an edge case and added tests
jalextowle Jul 7, 2019
eda3904
Adapted tests from matchOrders to test matchOrdersWithMaximalFill
jalextowle Jul 7, 2019
0ed1478
Addressed lingering review comments
jalextowle Jul 9, 2019
d41c139
Changed the returndata for batchMatchOrders
jalextowle Jul 9, 2019
eb1e117
Fixed lingering issues
jalextowle Jul 10, 2019
4462631
Rebased onto 3.0
jalextowle Jul 17, 2019
fb9e402
Addressed review comments
jalextowle Jul 17, 2019
9b653d5
Added ERC1155 Aggregation
jalextowle Jul 17, 2019
349d66a
Addressed lingering review comments
jalextowle Jul 21, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/dev-utils/compiler.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
},
"contracts": [
"src/DevUtils.sol",
"src/EthBalanceChecker.sol",
"src/LibAssetData.sol",
"src/LibTransactionDecoder.sol",
"src/EthBalanceChecker.sol",
"src/OrderTransferSimulationUtils.sol"
]
}
2 changes: 1 addition & 1 deletion contracts/dev-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"lint-contracts": "solhint -c ../.solhint.json contracts/**/**/**/**/*.sol"
},
"config": {
"abis": "./generated-artifacts/@(DevUtils|LibAssetData|LibTransactionDecoder|EthBalanceChecker|OrderTransferSimulationUtils).json",
"abis": "./generated-artifacts/@(DevUtils|EthBalanceChecker|LibAssetData|LibTransactionDecoder|OrderTransferSimulationUtils).json",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually."
},
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions contracts/dev-utils/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import { ContractArtifact } from 'ethereum-types';

import * as DevUtils from '../generated-artifacts/DevUtils.json';
import * as EthBalanceChecker from '../generated-artifacts/EthBalanceChecker.json';
import * as LibAssetData from '../generated-artifacts/LibAssetData.json';
import * as LibTransactionDecoder from '../generated-artifacts/LibTransactionDecoder.json';
import * as OrderTransferSimulationUtils from '../generated-artifacts/OrderTransferSimulationUtils.json';
export const artifacts = {
DevUtils: DevUtils as ContractArtifact,
LibAssetData: LibAssetData as ContractArtifact,
LibTransactionDecoder: LibTransactionDecoder as ContractArtifact,
EthBalanceChecker: EthBalanceChecker as ContractArtifact,
OrderTransferSimulationUtils: OrderTransferSimulationUtils as ContractArtifact,
};
1 change: 1 addition & 0 deletions contracts/dev-utils/src/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* -----------------------------------------------------------------------------
*/
export * from '../generated-wrappers/dev_utils';
export * from '../generated-wrappers/eth_balance_checker';
export * from '../generated-wrappers/lib_asset_data';
export * from '../generated-wrappers/lib_transaction_decoder';
export * from '../generated-wrappers/order_transfer_simulation_utils';
2 changes: 1 addition & 1 deletion contracts/dev-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"include": ["./src/**/*", "./test/**/*", "./generated-wrappers/**/*"],
"files": [
"generated-artifacts/DevUtils.json",
"generated-artifacts/EthBalanceChecker.json",
"generated-artifacts/LibAssetData.json",
"generated-artifacts/LibTransactionDecoder.json",
"generated-artifacts/EthBalanceChecker.json",
"generated-artifacts/OrderTransferSimulationUtils.json"
],
"exclude": ["./deploy/solc/solc_bin"]
Expand Down
16 changes: 12 additions & 4 deletions contracts/exchange-libs/contracts/src/LibFillResults.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*

Copyright 2018 ZeroEx Intl.
Copyright 2019 ZeroEx Intl.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,13 @@ import "@0x/contracts-utils/contracts/src/SafeMath.sol";
contract LibFillResults is
SafeMath
{
struct BatchMatchedFillResults {
FillResults[] left; // Fill results for left orders
FillResults[] right; // Fill results for right orders
uint256 profitInLeftMakerAsset; // Profit taken from left makers
uint256 profitInRightMakerAsset; // Profit taken from right makers
}

struct FillResults {
uint256 makerAssetFilledAmount; // Total amount of makerAsset(s) filled.
uint256 takerAssetFilledAmount; // Total amount of takerAsset(s) filled.
Expand All @@ -32,9 +39,10 @@ contract LibFillResults is
}

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.
FillResults left; // Amounts filled and fees paid of left order.
FillResults right; // Amounts filled and fees paid of right order.
uint256 profitInLeftMakerAsset; // Profit taken from the left maker
uint256 profitInRightMakerAsset; // Profit taken from the right maker
}

/// @dev Adds properties of both FillResults instances.
Expand Down
1 change: 1 addition & 0 deletions contracts/exchange/compiler.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"test/ReentrantERC20Token.sol",
"test/TestAssetProxyDispatcher.sol",
"test/TestExchangeInternals.sol",
"test/TestExchangeMath.sol",
"test/TestLibExchangeRichErrorDecoder.sol",
"test/TestSignatureValidator.sol",
"test/TestValidatorWallet.sol"
Expand Down
25 changes: 25 additions & 0 deletions contracts/exchange/contracts/src/LibExchangeRichErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ library LibExchangeRichErrors {
bytes4 internal constant INCOMPLETE_FILL_ERROR_SELECTOR =
0x152aa60e;

// bytes4(keccak256("BatchMatchOrdersError(uint8)"))
bytes4 internal constant BATCH_MATCH_ORDERS_ERROR_SELECTOR =
0xd4092f4f;

// solhint-disable func-name-mixedcase
function SignatureErrorSelector()
internal
Expand Down Expand Up @@ -242,6 +246,27 @@ library LibExchangeRichErrors {
return INCOMPLETE_FILL_ERROR_SELECTOR;
}

function BatchMatchOrdersErrorSelector()
internal
pure
returns (bytes4)
{
return BATCH_MATCH_ORDERS_ERROR_SELECTOR;
}

function BatchMatchOrdersError(
IExchangeRichErrors.BatchMatchOrdersErrorCodes errorCode
)
internal
pure
returns (bytes memory)
{
return abi.encodeWithSelector(
BATCH_MATCH_ORDERS_ERROR_SELECTOR,
errorCode
);
}

function SignatureError(
IExchangeRichErrors.SignatureErrorCodes errorCode,
bytes32 hash,
Expand Down
25 changes: 17 additions & 8 deletions contracts/exchange/contracts/src/MixinExchangeCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import "./MixinSignatureValidator.sol";

contract MixinExchangeCore is
IExchangeCore,
IExchangeRichErrors,
LibExchangeSelectors,
LibMath,
LibFillResults,
Expand Down Expand Up @@ -69,7 +70,11 @@ contract MixinExchangeCore is

// Ensure orderEpoch is monotonically increasing
if (newOrderEpoch <= oldOrderEpoch) {
LibRichErrors._rrevert(LibExchangeRichErrors.OrderEpochError(makerAddress, orderSenderAddress, oldOrderEpoch));
LibRichErrors._rrevert(LibExchangeRichErrors.OrderEpochError(
makerAddress,
orderSenderAddress,
oldOrderEpoch
));
}

// Update orderEpoch
Expand Down Expand Up @@ -340,14 +345,18 @@ contract MixinExchangeCore is
// Validate sender is allowed to fill this order
if (order.senderAddress != address(0)) {
if (order.senderAddress != msg.sender) {
LibRichErrors._rrevert(LibExchangeRichErrors.InvalidSenderError(orderInfo.orderHash, msg.sender));
LibRichErrors._rrevert(LibExchangeRichErrors.InvalidSenderError(
orderInfo.orderHash, msg.sender
));
}
}

// Validate taker is allowed to fill this order
if (order.takerAddress != address(0)) {
if (order.takerAddress != takerAddress) {
LibRichErrors._rrevert(LibExchangeRichErrors.InvalidTakerError(orderInfo.orderHash, takerAddress));
LibRichErrors._rrevert(LibExchangeRichErrors.InvalidTakerError(
orderInfo.orderHash, takerAddress
));
}
}

Expand All @@ -366,7 +375,7 @@ contract MixinExchangeCore is
makerAddress,
signature)) {
LibRichErrors._rrevert(LibExchangeRichErrors.SignatureError(
IExchangeRichErrors.SignatureErrorCodes.BAD_SIGNATURE,
SignatureErrorCodes.BAD_SIGNATURE,
orderInfo.orderHash,
makerAddress,
signature
Expand Down Expand Up @@ -395,7 +404,7 @@ contract MixinExchangeCore is
// TODO: reconsider necessity for v2.1
if (takerAssetFillAmount == 0) {
LibRichErrors._rrevert(LibExchangeRichErrors.FillError(
IExchangeRichErrors.FillErrorCodes.INVALID_TAKER_AMOUNT,
FillErrorCodes.INVALID_TAKER_AMOUNT,
orderInfo.orderHash
));
}
Expand All @@ -405,7 +414,7 @@ contract MixinExchangeCore is
// as an extra defence against potential bugs.
if (takerAssetFilledAmount > takerAssetFillAmount) {
LibRichErrors._rrevert(LibExchangeRichErrors.FillError(
IExchangeRichErrors.FillErrorCodes.TAKER_OVERPAY,
FillErrorCodes.TAKER_OVERPAY,
orderInfo.orderHash
));
}
Expand All @@ -416,7 +425,7 @@ contract MixinExchangeCore is
if (_safeAdd(orderInfo.orderTakerAssetFilledAmount, takerAssetFilledAmount)
> order.takerAssetAmount) {
LibRichErrors._rrevert(LibExchangeRichErrors.FillError(
IExchangeRichErrors.FillErrorCodes.OVERFILL,
FillErrorCodes.OVERFILL,
orderInfo.orderHash
));
}
Expand All @@ -441,7 +450,7 @@ contract MixinExchangeCore is
if (_safeMul(makerAssetFilledAmount, order.takerAssetAmount)
> _safeMul(order.makerAssetAmount, takerAssetFilledAmount)) {
LibRichErrors._rrevert(LibExchangeRichErrors.FillError(
IExchangeRichErrors.FillErrorCodes.INVALID_FILL_PRICE,
FillErrorCodes.INVALID_FILL_PRICE,
orderInfo.orderHash
));
}
Expand Down
Loading