Skip to content

Commit

Permalink
feat: remove BUY and SELL orders
Browse files Browse the repository at this point in the history
  • Loading branch information
adjisb committed Oct 2, 2023
1 parent 37e6c63 commit 7a7e951
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 630 deletions.
86 changes: 22 additions & 64 deletions packages/marketplace/contracts/exchange/ExchangeCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ pragma solidity 0.8.21;
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {LibFill} from "./libraries/LibFill.sol";
import {IAssetMatcher} from "../interfaces/IAssetMatcher.sol";
import {TransferExecutor, LibTransfer} from "../transfer-manager/TransferExecutor.sol";
import {LibDeal, LibAsset} from "../transfer-manager/lib/LibDeal.sol";
import {TransferExecutor} from "../transfer-manager/TransferExecutor.sol";
import {LibFeeSide} from "../transfer-manager/lib/LibFeeSide.sol";
import {LibOrderDataGeneric, LibOrder} from "./libraries/LibOrderDataGeneric.sol";
import {LibDeal} from "../transfer-manager/lib/LibDeal.sol";
import {LibAsset} from "../lib-asset/LibAsset.sol";
import {LibOrder} from "../lib-order/LibOrder.sol";
import {LibPart} from "../lib-part/LibPart.sol";
import {ITransferManager} from "../transfer-manager/interfaces/ITransferManager.sol";
import {IOrderValidator} from "../interfaces/IOrderValidator.sol";

/// @notice ExchangeCore contract
/// @dev contains the main functions for the marketplace
abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferManager {
using LibTransfer for address payable;

// a list of left/right orders that match each other
// left and right are symmetrical except for fees that are taken from left side first.
struct ExchangeMatch {
Expand Down Expand Up @@ -159,99 +159,57 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
orderRight
);

(
LibOrderDataGeneric.GenericOrderData memory leftOrderData,
LibOrderDataGeneric.GenericOrderData memory rightOrderData,
LibFill.FillResult memory newFill
) = _parseOrdersSetFillEmitMatch(sender, orderLeft, orderRight);

LibFill.FillResult memory newFill = _parseOrdersSetFillEmitMatch(sender, orderLeft, orderRight);
doTransfers(
LibDeal.DealSide({
asset: LibAsset.Asset({assetType: makeMatch, value: newFill.leftValue}),
payouts: leftOrderData.payouts,
payouts: _payToMaker(orderLeft),
from: orderLeft.maker
}),
LibDeal.DealSide({
asset: LibAsset.Asset(takeMatch, newFill.rightValue),
payouts: rightOrderData.payouts,
payouts: _payToMaker(orderRight),
from: orderRight.maker
}),
LibFeeSide.getFeeSide(makeMatch.assetClass, takeMatch.assetClass)
);
}

/// @notice create a payout array that pays to maker 100%
/// @param order the order from which the maker is taken
/// @return an array with just one entry that pays to order.maker
function _payToMaker(LibOrder.Order memory order) internal pure returns (LibPart.Part[] memory) {
LibPart.Part[] memory payout = new LibPart.Part[](1);
payout[0].account = order.maker;
payout[0].value = 10000;
return payout;
}

/// @notice parse orders with LibOrderDataGeneric parse() to get the order data, then create a new fill with setFillEmitMatch()
/// @param sender the message sender
/// @param orderLeft left order
/// @param orderRight right order
/// @return leftOrderData generic order data from left order
/// @return rightOrderData generic order data from right order
/// @return newFill fill result
function _parseOrdersSetFillEmitMatch(
address sender,
LibOrder.Order calldata orderLeft,
LibOrder.Order calldata orderRight
)
internal
returns (
LibOrderDataGeneric.GenericOrderData memory leftOrderData,
LibOrderDataGeneric.GenericOrderData memory rightOrderData,
LibFill.FillResult memory newFill
)
{
) internal returns (LibFill.FillResult memory newFill) {
bytes32 leftOrderKeyHash = LibOrder.hashKey(orderLeft);
bytes32 rightOrderKeyHash = LibOrder.hashKey(orderRight);

leftOrderData = LibOrderDataGeneric.parse(orderLeft);
rightOrderData = LibOrderDataGeneric.parse(orderRight);

newFill = _setFillEmitMatch(
sender,
orderLeft,
orderRight,
leftOrderKeyHash,
rightOrderKeyHash,
leftOrderData.isMakeFill,
rightOrderData.isMakeFill
);
}

/// @notice calculates fills for the matched orders and set them in "fills" mapping
/// @param sender the message sender
/// @param orderLeft left order of the match
/// @param orderRight right order of the match
/// @param leftMakeFill true if the left orders uses make-side fills, false otherwise
/// @param rightMakeFill true if the right orders uses make-side fills, false otherwise
/// @return newFill returns change in orders' fills by the match
function _setFillEmitMatch(
address sender,
LibOrder.Order calldata orderLeft,
LibOrder.Order calldata orderRight,
bytes32 leftOrderKeyHash,
bytes32 rightOrderKeyHash,
bool leftMakeFill,
bool rightMakeFill
) internal returns (LibFill.FillResult memory newFill) {
uint256 leftOrderFill = _getOrderFill(orderLeft.salt, leftOrderKeyHash);
uint256 rightOrderFill = _getOrderFill(orderRight.salt, rightOrderKeyHash);
newFill = LibFill.fillOrder(orderLeft, orderRight, leftOrderFill, rightOrderFill, leftMakeFill, rightMakeFill);
newFill = LibFill.fillOrder(orderLeft, orderRight, leftOrderFill, rightOrderFill);

require(newFill.rightValue > 0 && newFill.leftValue > 0, "nothing to fill");

if (orderLeft.salt != 0) {
if (leftMakeFill) {
fills[leftOrderKeyHash] = leftOrderFill + newFill.leftValue;
} else {
fills[leftOrderKeyHash] = leftOrderFill + newFill.rightValue;
}
fills[leftOrderKeyHash] = leftOrderFill + newFill.rightValue;
}

if (orderRight.salt != 0) {
if (rightMakeFill) {
fills[rightOrderKeyHash] = rightOrderFill + newFill.rightValue;
} else {
fills[rightOrderKeyHash] = rightOrderFill + newFill.leftValue;
}
fills[rightOrderKeyHash] = rightOrderFill + newFill.leftValue;
}

emit Match({
Expand Down
39 changes: 18 additions & 21 deletions packages/marketplace/contracts/exchange/libraries/LibFill.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {LibOrder, LibMath} from "../../lib-order/LibOrder.sol";
import {LibOrder} from "../../lib-order/LibOrder.sol";
import {LibMath} from "./LibMath.sol";

/// @title This library provides `fillOrder` function.
/// @notice It calculates fill of both orders (part of the Order that can be filled).
Expand All @@ -11,46 +12,42 @@ library LibFill {
uint256 rightValue;
}

struct IsMakeFill {
bool leftMake;
bool rightMake;
}

/// @notice Should return filled values
/// @param leftOrder left order
/// @param rightOrder right order
/// @param leftOrderFill current fill of the left order (0 if order is unfilled)
/// @param rightOrderFill current fill of the right order (0 if order is unfilled)
/// @param leftIsMakeFill true if left orders fill is calculated from the make side, false if from the take side
/// @param rightIsMakeFill true if right orders fill is calculated from the make side, false if from the take side
/// @dev We have 3 cases, 1st: left order should be fully filled
/// @dev 2nd: right order should be fully filled or 3d: both should be fully filled if required values are the same
/// @return the fill result of both orders
function fillOrder(
LibOrder.Order calldata leftOrder,
LibOrder.Order calldata rightOrder,
uint256 leftOrderFill,
uint256 rightOrderFill,
bool leftIsMakeFill,
bool rightIsMakeFill
uint256 rightOrderFill
) internal pure returns (FillResult memory) {
(uint256 leftMakeValue, uint256 leftTakeValue) = LibOrder.calculateRemaining(
leftOrder,
leftOrderFill,
leftIsMakeFill
);
(uint256 rightMakeValue, uint256 rightTakeValue) = LibOrder.calculateRemaining(
rightOrder,
rightOrderFill,
rightIsMakeFill
);
(uint256 leftMakeValue, uint256 leftTakeValue) = calculateRemaining(leftOrder, leftOrderFill);
(uint256 rightMakeValue, uint256 rightTakeValue) = calculateRemaining(rightOrder, rightOrderFill);

if (rightTakeValue > leftMakeValue) {
return fillLeft(leftMakeValue, leftTakeValue, rightOrder.makeAsset.value, rightOrder.takeAsset.value);
}
return fillRight(leftOrder.makeAsset.value, leftOrder.takeAsset.value, rightMakeValue, rightTakeValue);
}

/// @notice calculate the remaining fill from orders
/// @param order order that we will calculate the remaining fill
/// @param fill to be subtracted
/// @return makeValue remaining fill from make side
/// @return takeValue remaining fill from take side
function calculateRemaining(
LibOrder.Order calldata order,
uint256 fill
) internal pure returns (uint256 makeValue, uint256 takeValue) {
takeValue = order.takeAsset.value - fill;
makeValue = LibMath.safeGetPartialAmountFloor(order.makeAsset.value, order.takeAsset.value, takeValue);
}

function fillRight(
uint256 leftMakeValue,
uint256 leftTakeValue,
Expand Down
75 changes: 0 additions & 75 deletions packages/marketplace/contracts/exchange/libraries/LibOrderData.md

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions packages/marketplace/contracts/lib-order/LibOrder.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ This library contains struct `Order` with some functions for this struct:
- `uint` salt - random number to distinguish different maker's Orders
- `uint` start - Order can't be matched before this date (optional)
- `uint` end - Order can't be matched after this date (optional)
- `bytes4` dataType - type of data, usually hash of some string, e.g.: "v1", "v2" (see more [here](LibOrderData.md))
- `bytes` data - generic data, can be anything, extendable part of the order (see more [here](LibOrderData.md))

#### Types for EIP-712 signature:
```javascript
Expand All @@ -36,8 +34,6 @@ const Types = {
{name: 'salt', type: 'uint256'},
{name: 'start', type: 'uint256'},
{name: 'end', type: 'uint256'},
{name: 'dataType', type: 'bytes4'},
{name: 'data', type: 'bytes'},
]
};
```
Loading

1 comment on commit 7a7e951

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

81.47%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts/exchange
   AssetMatcher.sol100%100%100%100%
   Exchange.sol83.58%78.57%87.50%86.96%176, 66, 82, 92, 92–93, 93, 93–94
   ExchangeCore.sol86.17%65.63%100%96.08%116, 140–141, 141, 141, 144, 205, 207, 211, 232–233, 250, 76
   OrderValidator.sol71.70%58.33%100%79.17%38, 67–68, 68, 68, 72, 84–85, 90, 92, 92, 92, 92–93, 95
   WhiteList.sol97.73%93.75%100%100%57
packages/marketplace/contracts/exchange/libraries
   LibFill.sol60.87%33.33%75%69.23%32–33, 58, 68–69, 69, 69–70
   LibMath.sol27.50%18.75%50%30%100–103, 17–18, 33–34, 50, 50, 50–51, 72, 72, 72–73, 75, 88, 88, 88–89, 93, 93, 93, 93, 93, 97
packages/marketplace/contracts/interfaces
   IAssetMatcher.sol100%100%100%100%
   IOrderValidator.sol100%100%100%100%
   IRoyaltiesProvider.sol100%100%100%100%
   IWhiteList.sol100%100%100%100%
packages/marketplace/contracts/lib-asset
   LibAsset.sol100%100%100%100%
packages/marketplace/contracts/lib-bp
   BpLibrary.sol100%100%100%100%
packages/marketplace/contracts/lib-order
   LibOrder.sol73.33%50%100%100%64, 64, 66, 66
packages/marketplace/contracts/lib-part
   LibPart.sol0%100%0%0%21
packages/marketplace/contracts/royalties
   IERC2981.sol100%100%100%100%
   LibRoyalties2981.sol78.57%50%100%88.89%19–20, 23
packages/marketplace/contracts/royalties-registry
   IMultiRoyaltyRecipients.sol100%100%100%100%
   RoyaltiesRegistry.sol79.20%79.41%100%75%166–167, 170–171, 212, 216, 232–233, 236–244, 247, 247, 247–248, 250, 256, 259, 276, 60
packages/marketplace/contracts/transfer-manager
   TransferExecutor.sol75.76%64.29%100%81.25%46–48, 55–56, 60, 63–64
   TransferManager.sol82.58%71.43%100%87.36%110, 114–115, 195, 202–203, 203, 203–204, 208, 248, 265, 269–271, 271, 271–273, 278–279, 302, 306–307, 62, 82–83
packages/marketplace/contracts/transfer-manager/interfaces
   IRoyaltyUGC.sol100%100%100%100%
   ITransferExecutor.sol100%100%100%100%
   ITransferManager.sol100%100%100%100%
packages/marketplace/contracts/transfer-manager/lib
   LibDeal.sol100%100%100%100%
   LibFeeSide.sol44.44%37.50%100%44.44%21, 24, 24, 24–25, 27, 27, 27–28, 30

Please sign in to comment.