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

Commit 434d027

Browse files
authored
Merge pull request #2043 from jalextowle/feature/contracts/3.0/order-matching-unit-tests
MatchOrders Unit Tests
2 parents f66212c + 6b4e632 commit 434d027

File tree

6 files changed

+2152
-125
lines changed

6 files changed

+2152
-125
lines changed

contracts/exchange/contracts/src/MixinMatchOrders.sol

Lines changed: 110 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,116 @@ contract MixinMatchOrders is
473473
);
474474
}
475475

476+
/// @dev Settles matched order by transferring appropriate funds between order makers, taker, and fee recipient.
477+
/// @param leftOrderHash First matched order hash.
478+
/// @param rightOrderHash Second matched order hash.
479+
/// @param leftOrder First matched order.
480+
/// @param rightOrder Second matched order.
481+
/// @param takerAddress Address that matched the orders. The taker receives the spread between orders as profit.
482+
/// @param matchedFillResults Struct holding amounts to transfer between makers, taker, and fee recipients.
483+
function _settleMatchedOrders(
484+
bytes32 leftOrderHash,
485+
bytes32 rightOrderHash,
486+
LibOrder.Order memory leftOrder,
487+
LibOrder.Order memory rightOrder,
488+
address takerAddress,
489+
LibFillResults.MatchedFillResults memory matchedFillResults
490+
)
491+
internal
492+
{
493+
address leftFeeRecipientAddress = leftOrder.feeRecipientAddress;
494+
address rightFeeRecipientAddress = rightOrder.feeRecipientAddress;
495+
496+
// Right maker asset -> left maker
497+
_dispatchTransferFrom(
498+
rightOrderHash,
499+
rightOrder.makerAssetData,
500+
rightOrder.makerAddress,
501+
leftOrder.makerAddress,
502+
matchedFillResults.left.takerAssetFilledAmount
503+
);
504+
505+
// Left maker asset -> right maker
506+
_dispatchTransferFrom(
507+
leftOrderHash,
508+
leftOrder.makerAssetData,
509+
leftOrder.makerAddress,
510+
rightOrder.makerAddress,
511+
matchedFillResults.right.takerAssetFilledAmount
512+
);
513+
514+
// Right maker fee -> right fee recipient
515+
_dispatchTransferFrom(
516+
rightOrderHash,
517+
rightOrder.makerFeeAssetData,
518+
rightOrder.makerAddress,
519+
rightFeeRecipientAddress,
520+
matchedFillResults.right.makerFeePaid
521+
);
522+
523+
// Left maker fee -> left fee recipient
524+
_dispatchTransferFrom(
525+
leftOrderHash,
526+
leftOrder.makerFeeAssetData,
527+
leftOrder.makerAddress,
528+
leftFeeRecipientAddress,
529+
matchedFillResults.left.makerFeePaid
530+
);
531+
532+
// Settle taker profits.
533+
_dispatchTransferFrom(
534+
leftOrderHash,
535+
leftOrder.makerAssetData,
536+
leftOrder.makerAddress,
537+
takerAddress,
538+
matchedFillResults.profitInLeftMakerAsset
539+
);
540+
_dispatchTransferFrom(
541+
rightOrderHash,
542+
rightOrder.makerAssetData,
543+
rightOrder.makerAddress,
544+
takerAddress,
545+
matchedFillResults.profitInRightMakerAsset
546+
);
547+
548+
// Settle taker fees.
549+
if (
550+
leftFeeRecipientAddress == rightFeeRecipientAddress &&
551+
leftOrder.takerFeeAssetData.equals(rightOrder.takerFeeAssetData)
552+
) {
553+
// Fee recipients and taker fee assets are identical, so we can
554+
// transfer them in one go.
555+
_dispatchTransferFrom(
556+
leftOrderHash,
557+
leftOrder.takerFeeAssetData,
558+
takerAddress,
559+
leftFeeRecipientAddress,
560+
_safeAdd(
561+
matchedFillResults.left.takerFeePaid,
562+
matchedFillResults.right.takerFeePaid
563+
)
564+
);
565+
} else {
566+
// Right taker fee -> right fee recipient
567+
_dispatchTransferFrom(
568+
rightOrderHash,
569+
rightOrder.takerFeeAssetData,
570+
takerAddress,
571+
rightFeeRecipientAddress,
572+
matchedFillResults.right.takerFeePaid
573+
);
574+
575+
// Left taker fee -> left fee recipient
576+
_dispatchTransferFrom(
577+
leftOrderHash,
578+
leftOrder.takerFeeAssetData,
579+
takerAddress,
580+
leftFeeRecipientAddress,
581+
matchedFillResults.left.takerFeePaid
582+
);
583+
}
584+
}
585+
476586
/// @dev Match complementary orders that have a profitable spread.
477587
/// Each order is filled at their respective price point, and
478588
/// the matcher receives a profit denominated in the left maker asset.
@@ -708,115 +818,4 @@ contract MixinMatchOrders is
708818

709819
return matchedFillResults;
710820
}
711-
712-
/// @dev Settles matched order by transferring appropriate funds between order makers, taker, and fee recipient.
713-
/// @param leftOrderHash First matched order hash.
714-
/// @param rightOrderHash Second matched order hash.
715-
/// @param leftOrder First matched order.
716-
/// @param rightOrder Second matched order.
717-
/// @param takerAddress Address that matched the orders. The taker receives the spread between orders as profit.
718-
/// @param matchedFillResults Struct holding amounts to transfer between makers, taker, and fee recipients.
719-
function _settleMatchedOrders(
720-
bytes32 leftOrderHash,
721-
bytes32 rightOrderHash,
722-
LibOrder.Order memory leftOrder,
723-
LibOrder.Order memory rightOrder,
724-
address takerAddress,
725-
LibFillResults.MatchedFillResults memory matchedFillResults
726-
)
727-
private
728-
{
729-
address leftFeeRecipientAddress = leftOrder.feeRecipientAddress;
730-
address rightFeeRecipientAddress = rightOrder.feeRecipientAddress;
731-
732-
// Right maker asset -> left maker
733-
_dispatchTransferFrom(
734-
rightOrderHash,
735-
rightOrder.makerAssetData,
736-
rightOrder.makerAddress,
737-
leftOrder.makerAddress,
738-
matchedFillResults.left.takerAssetFilledAmount
739-
);
740-
741-
// Left maker asset -> right maker
742-
_dispatchTransferFrom(
743-
leftOrderHash,
744-
leftOrder.makerAssetData,
745-
leftOrder.makerAddress,
746-
rightOrder.makerAddress,
747-
matchedFillResults.right.takerAssetFilledAmount
748-
);
749-
750-
// Right maker fee -> right fee recipient
751-
_dispatchTransferFrom(
752-
rightOrderHash,
753-
rightOrder.makerFeeAssetData,
754-
rightOrder.makerAddress,
755-
rightFeeRecipientAddress,
756-
matchedFillResults.right.makerFeePaid
757-
);
758-
759-
// Left maker fee -> left fee recipient
760-
_dispatchTransferFrom(
761-
leftOrderHash,
762-
leftOrder.makerFeeAssetData,
763-
leftOrder.makerAddress,
764-
leftFeeRecipientAddress,
765-
matchedFillResults.left.makerFeePaid
766-
);
767-
768-
// Settle taker profits.
769-
_dispatchTransferFrom(
770-
leftOrderHash,
771-
leftOrder.makerAssetData,
772-
leftOrder.makerAddress,
773-
takerAddress,
774-
matchedFillResults.profitInLeftMakerAsset
775-
);
776-
777-
_dispatchTransferFrom(
778-
rightOrderHash,
779-
rightOrder.makerAssetData,
780-
rightOrder.makerAddress,
781-
takerAddress,
782-
matchedFillResults.profitInRightMakerAsset
783-
);
784-
785-
// Settle taker fees.
786-
if (
787-
leftFeeRecipientAddress == rightFeeRecipientAddress &&
788-
leftOrder.takerFeeAssetData.equals(rightOrder.takerFeeAssetData)
789-
) {
790-
// Fee recipients and taker fee assets are identical, so we can
791-
// transfer them in one go.
792-
_dispatchTransferFrom(
793-
leftOrderHash,
794-
leftOrder.takerFeeAssetData,
795-
takerAddress,
796-
leftFeeRecipientAddress,
797-
_safeAdd(
798-
matchedFillResults.left.takerFeePaid,
799-
matchedFillResults.right.takerFeePaid
800-
)
801-
);
802-
} else {
803-
// Right taker fee -> right fee recipient
804-
_dispatchTransferFrom(
805-
rightOrderHash,
806-
rightOrder.takerFeeAssetData,
807-
takerAddress,
808-
rightFeeRecipientAddress,
809-
matchedFillResults.right.takerFeePaid
810-
);
811-
812-
// Left taker fee -> left fee recipient
813-
_dispatchTransferFrom(
814-
leftOrderHash,
815-
leftOrder.takerFeeAssetData,
816-
takerAddress,
817-
leftFeeRecipientAddress,
818-
matchedFillResults.left.takerFeePaid
819-
);
820-
}
821-
}
822821
}

contracts/exchange/contracts/test/TestExchangeInternals.sol

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ contract TestExchangeInternals is
3939
Exchange(chainId)
4040
{}
4141

42+
function assertValidMatch(
43+
LibOrder.Order memory leftOrder,
44+
LibOrder.Order memory rightOrder
45+
)
46+
public
47+
view
48+
{
49+
_assertValidMatch(leftOrder, rightOrder);
50+
}
51+
4252
function calculateFillResults(
4353
Order memory order,
4454
uint256 takerAssetFilledAmount
@@ -50,6 +60,43 @@ contract TestExchangeInternals is
5060
return _calculateFillResults(order, takerAssetFilledAmount);
5161
}
5262

63+
function calculateCompleteFillBoth(
64+
uint256 leftMakerAssetAmountRemaining,
65+
uint256 leftTakerAssetAmountRemaining,
66+
uint256 rightMakerAssetAmountRemaining,
67+
uint256 rightTakerAssetAmountRemaining
68+
)
69+
public
70+
pure
71+
returns (MatchedFillResults memory fillResults)
72+
{
73+
_calculateCompleteFillBoth(
74+
fillResults,
75+
leftMakerAssetAmountRemaining,
76+
leftTakerAssetAmountRemaining,
77+
rightMakerAssetAmountRemaining,
78+
rightTakerAssetAmountRemaining
79+
);
80+
return fillResults;
81+
}
82+
83+
function calculateCompleteRightFill(
84+
LibOrder.Order memory leftOrder,
85+
uint256 rightMakerAssetAmountRemaining,
86+
uint256 rightTakerAssetAmountRemaining
87+
)
88+
public
89+
pure
90+
returns (MatchedFillResults memory fillResults)
91+
{
92+
_calculateCompleteRightFill(
93+
fillResults,
94+
leftOrder,
95+
rightMakerAssetAmountRemaining,
96+
rightTakerAssetAmountRemaining
97+
);
98+
}
99+
53100
/// @dev Call `_updateFilledState()` but first set `filled[order]` to
54101
/// `orderTakerAssetFilledAmount`.
55102
function testUpdateFilledState(
@@ -82,6 +129,26 @@ contract TestExchangeInternals is
82129
_settleOrder(orderHash, order, takerAddress, fillResults);
83130
}
84131

132+
function settleMatchOrders(
133+
bytes32 leftOrderHash,
134+
bytes32 rightOrderHash,
135+
LibOrder.Order memory leftOrder,
136+
LibOrder.Order memory rightOrder,
137+
address takerAddress,
138+
LibFillResults.MatchedFillResults memory matchedFillResults
139+
)
140+
public
141+
{
142+
_settleMatchedOrders(
143+
leftOrderHash,
144+
rightOrderHash,
145+
leftOrder,
146+
rightOrder,
147+
takerAddress,
148+
matchedFillResults
149+
);
150+
}
151+
85152
/// @dev Overidden to only log arguments so we can test `_settleOrder()`.
86153
function _dispatchTransferFrom(
87154
bytes32 orderHash,

contracts/exchange/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"lint-contracts": "solhint -c ../.solhint.json contracts/**/**/**/**/*.sol"
3535
},
3636
"config": {
37-
"abis": "./generated-artifacts/@(Exchange|ExchangeWrapper|IAssetProxyDispatcher|IEIP1271Wallet|IExchange|IExchangeCore|IMatchOrders|ISignatureValidator|ITransactions|IWallet|IWrapperFunctions|IsolatedExchange|ReentrancyTester|TestAssetProxyDispatcher|TestExchangeInternals|TestLibExchangeRichErrorDecoder|TestSignatureValidator|TestValidatorWallet|TestWrapperFunctions|Whitelist).json",
38-
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually."
37+
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
38+
"abis": "./generated-artifacts/@(Exchange|ExchangeWrapper|IAssetProxyDispatcher|IEIP1271Wallet|IExchange|IExchangeCore|IMatchOrders|ISignatureValidator|ITransactions|IWallet|IWrapperFunctions|IsolatedExchange|ReentrancyTester|TestAssetProxyDispatcher|TestExchangeInternals|TestLibExchangeRichErrorDecoder|TestSignatureValidator|TestValidatorWallet|TestWrapperFunctions|Whitelist).json"
3939
},
4040
"repository": {
4141
"type": "git",

contracts/exchange/test/dispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ describe('AssetProxyDispatcher', () => {
271271
return expect(tx).to.revertWith(expectedError);
272272
});
273273

274-
it('should should revert with the correct error when assetData length < 4 bytes', async () => {
274+
it('should revert with the correct error when assetData length < 4 bytes', async () => {
275275
await assetProxyDispatcher.registerAssetProxy.awaitTransactionSuccessAsync(erc20Proxy.address, {
276276
from: owner,
277277
});

0 commit comments

Comments
 (0)