Skip to content

Commit

Permalink
feat: remove asset matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
adjisb committed Oct 3, 2023
1 parent 8bc3d5e commit b9229db
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 160 deletions.
18 changes: 0 additions & 18 deletions packages/deploy/deploy/marketplace/03_deploy_asset_matcher.ts

This file was deleted.

8 changes: 1 addition & 7 deletions packages/deploy/deploy/marketplace/10_deploy_exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
}
const orderValidator = await deployments.get('OrderValidator');
const royaltiesRegistry = await deployments.get('RoyaltiesRegistry');
const assetMatcher = await deployments.get('AssetMatcher');

const newProtocolFeePrimary = 0;
const newProtocolFeeSecondary = 250;
Expand All @@ -38,7 +37,6 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
exchangeFeeRecipient,
royaltiesRegistry.address,
orderValidator.address,
assetMatcher.address,
],
},
upgradeIndex: 0,
Expand All @@ -49,8 +47,4 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
};
export default func;
func.tags = ['Exchange', 'Exchange_deploy'];
func.dependencies = [
'RoyaltiesRegistry_deploy',
'OrderValidator_deploy',
'AssetMatcher_deploy',
];
func.dependencies = ['RoyaltiesRegistry_deploy', 'OrderValidator_deploy'];
1 change: 0 additions & 1 deletion packages/deploy/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const importedPackages = {
'contracts/royalties-registry/RoyaltiesRegistry.sol',
'contracts/exchange/OrderValidator.sol',
'contracts/exchange/Exchange.sol',
'contracts/exchange/AssetMatcher.sol',
],
};

Expand Down
14 changes: 0 additions & 14 deletions packages/marketplace/contracts/exchange/AssetMatcher.md

This file was deleted.

41 changes: 0 additions & 41 deletions packages/marketplace/contracts/exchange/AssetMatcher.sol

This file was deleted.

13 changes: 2 additions & 11 deletions packages/marketplace/contracts/exchange/Exchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {ContextUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/Cont
import {ERC165Upgradeable} from "@openzeppelin/contracts-upgradeable/utils/introspection/ERC165Upgradeable.sol";
import {ERC2771HandlerUpgradeable} from "@sandbox-smart-contracts/dependency-metatx/contracts/ERC2771HandlerUpgradeable.sol";
import {IOrderValidator} from "../interfaces/IOrderValidator.sol";
import {IAssetMatcher} from "../interfaces/IAssetMatcher.sol";
import {TransferManager, IRoyaltiesProvider} from "../transfer-manager/TransferManager.sol";
import {LibOrder} from "../lib-order/LibOrder.sol";
import {ExchangeCore} from "./ExchangeCore.sol";
Expand Down Expand Up @@ -52,7 +51,6 @@ contract Exchange is
/// @param newDefaultFeeReceiver market fee receiver
/// @param newRoyaltiesProvider registry for the different types of royalties
/// @param orderValidatorAddress new OrderValidator contract address
/// @param newAssetMatcher new AssetMatcher contract address
// solhint-disable-next-line func-name-mixedcase
function __Exchange_init(
address admin,
Expand All @@ -61,8 +59,7 @@ contract Exchange is
uint256 newProtocolFeeSecondary,
address newDefaultFeeReceiver,
IRoyaltiesProvider newRoyaltiesProvider,
IOrderValidator orderValidatorAddress,
IAssetMatcher newAssetMatcher
IOrderValidator orderValidatorAddress
) external initializer {
__ERC2771Handler_init_unchained(newTrustedForwarder);
__AccessControl_init_unchained();
Expand All @@ -73,7 +70,7 @@ contract Exchange is
newDefaultFeeReceiver,
newRoyaltiesProvider
);
__ExchangeCoreInitialize(orderValidatorAddress, newAssetMatcher);
__ExchangeCoreInitialize(orderValidatorAddress);
_grantRole(DEFAULT_ADMIN_ROLE, admin);
}

Expand Down Expand Up @@ -109,12 +106,6 @@ contract Exchange is
_setRoyaltiesRegistry(newRoyaltiesRegistry);
}

/// @notice set AssetMatcher address
/// @param contractAddress new AssetMatcher contract address
function setAssetMatcherContract(IAssetMatcher contractAddress) external onlyRole(DEFAULT_ADMIN_ROLE) {
_setAssetMatcherContract(contractAddress);
}

/// @notice set OrderValidator address
/// @param contractAddress new OrderValidator contract address
function setOrderValidatorContract(IOrderValidator contractAddress) external onlyRole(DEFAULT_ADMIN_ROLE) {
Expand Down
47 changes: 9 additions & 38 deletions packages/marketplace/contracts/exchange/ExchangeCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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} from "../transfer-manager/TransferExecutor.sol";
import {LibAsset} from "../lib-asset/LibAsset.sol";
import {LibOrder} from "../lib-order/LibOrder.sol";
Expand All @@ -24,10 +23,6 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
bytes signatureRight; // signature for the right order
}

/// @notice AssetMatcher contract
/// @return AssetMatcher address
IAssetMatcher public assetMatcher;

/// @notice OrderValidator contract
/// @return OrderValidator address
IOrderValidator public orderValidator;
Expand Down Expand Up @@ -60,29 +55,14 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
uint256 valueLeft,
uint256 valueRight
);
event AssetMatcherSet(IAssetMatcher indexed contractAddress);
event OrderValidatorSet(IOrderValidator indexed contractAddress);

/// @notice initializer for ExchangeCore
/// @param newOrderValidatorAddress new OrderValidator contract address
/// @param newAssetMatcher new AssetMatcher contract address
/// @dev initialize permissions for native token exchange
// solhint-disable-next-line func-name-mixedcase
function __ExchangeCoreInitialize(
IOrderValidator newOrderValidatorAddress,
IAssetMatcher newAssetMatcher
) internal onlyInitializing {
function __ExchangeCoreInitialize(IOrderValidator newOrderValidatorAddress) internal onlyInitializing {
_setOrderValidatorContract(newOrderValidatorAddress);
_setAssetMatcherContract(newAssetMatcher);
}

/// @notice set AssetMatcher address
/// @param contractAddress new AssetMatcher contract address
/// @dev matches assets between left and right order
function _setAssetMatcherContract(IAssetMatcher contractAddress) internal {
require(address(contractAddress) != address(0), "invalid asset matcher");
assetMatcher = contractAddress;
emit AssetMatcherSet(contractAddress);
}

/// @notice set OrderValidator address
Expand Down Expand Up @@ -152,12 +132,17 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
LibOrder.Order calldata orderLeft,
LibOrder.Order calldata orderRight
) internal {
(LibAsset.AssetType memory makeMatch, LibAsset.AssetType memory takeMatch) = _matchAssets(
orderLeft,
orderRight
LibAsset.AssetType memory makeMatch = LibAsset.matchAssets(
orderLeft.makeAsset.assetType,
orderRight.takeAsset.assetType
);
LibAsset.AssetType memory takeMatch = LibAsset.matchAssets(
orderLeft.takeAsset.assetType,
orderRight.makeAsset.assetType
);

LibFill.FillResult memory newFill = _parseOrdersSetFillEmitMatch(sender, orderLeft, orderRight);

doTransfers(
ITransferManager.DealSide({
asset: LibAsset.Asset({assetType: makeMatch, value: newFill.leftValue}),
Expand Down Expand Up @@ -234,19 +219,5 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana
}
}

/// @notice match assets from orders
/// @param orderLeft left order
/// @param orderRight right order
/// @dev each make asset must correrspond to the other take asset and be different from 0
function _matchAssets(
LibOrder.Order memory orderLeft,
LibOrder.Order memory orderRight
) internal view returns (LibAsset.AssetType memory makeMatch, LibAsset.AssetType memory takeMatch) {
makeMatch = assetMatcher.matchAssets(orderLeft.makeAsset.assetType, orderRight.takeAsset.assetType);
require(makeMatch.assetClass != LibAsset.AssetClassType.INVALID_ASSET_CLASS, "assets don't match");
takeMatch = assetMatcher.matchAssets(orderLeft.takeAsset.assetType, orderRight.makeAsset.assetType);
require(takeMatch.assetClass != LibAsset.AssetClassType.INVALID_ASSET_CLASS, "assets don't match");
}

uint256[49] private __gap;
}
22 changes: 22 additions & 0 deletions packages/marketplace/contracts/lib-asset/LibAsset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ library LibAsset {
return FeeSide.NONE;
}

/// @notice calculate if Asset types match with each other
/// @param leftType to be matched with rightAssetType
/// @param rightType to be matched with leftAssetType
/// @return AssetType of the match
function matchAssets(
AssetType calldata leftType,
AssetType calldata rightType
) internal pure returns (AssetType memory) {
AssetClassType classLeft = leftType.assetClass;
AssetClassType classRight = rightType.assetClass;

require(classLeft != AssetClassType.INVALID_ASSET_CLASS, "not found IAssetMatcher");
require(classRight != AssetClassType.INVALID_ASSET_CLASS, "not found IAssetMatcher");
require(classLeft == classRight, "assets don't match");

bytes32 leftHash = keccak256(leftType.data);
bytes32 rightHash = keccak256(rightType.data);
require(leftHash == rightHash, "assets don't match");

return leftType;
}

/// @notice calculate hash of asset type
/// @param assetType to be hashed
/// @return hash of assetType
Expand Down
39 changes: 39 additions & 0 deletions packages/marketplace/contracts/mocks/LibAssetTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.21;

import {LibAsset} from "../lib-asset/LibAsset.sol";

contract LibAssetTest {
function getFeeSide(
LibAsset.AssetClassType leftClass,
LibAsset.AssetClassType rightClass
) external pure returns (LibAsset.FeeSide) {
return LibAsset.getFeeSide(leftClass, rightClass);
}

/// @notice calculate if Asset types match with each other
/// @param leftType to be matched with rightAssetType
/// @param rightType to be matched with leftAssetType
/// @return AssetType of the match
function matchAssets(
LibAsset.AssetType calldata leftType,
LibAsset.AssetType calldata rightType
) external pure returns (LibAsset.AssetType memory) {
return LibAsset.matchAssets(leftType, rightType);
}

/// @notice calculate hash of asset type
/// @param assetType to be hashed
/// @return hash of assetType
function hash(LibAsset.AssetType memory assetType) external pure returns (bytes32) {
return LibAsset.hash(assetType);
}

/// @notice calculate hash of asset
/// @param asset to be hashed
/// @return hash of asset
function hash(LibAsset.Asset memory asset) external pure returns (bytes32) {
return LibAsset.hash(asset);
}
}
Loading

2 comments on commit b9229db

@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

82.76%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts/exchange
   Exchange.sol79.71%70%87.50%86.96%171, 63, 79, 89, 89, 89, 89–90, 90, 90–91, 98
   ExchangeCore.sol84.81%61.54%100%95.45%118–119, 119, 119, 122, 188, 190, 194, 215–216, 64, 94
   OrderValidator.sol71.70%58.33%100%79.17%38, 67–68, 68, 68, 72, 83–84, 89, 91, 91, 91, 91–92, 94
   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.sol90.48%75%100%100%24, 30
   TransferManager.sol81.65%68.97%100%87.50%137, 190, 199, 206–207, 207, 207–208, 212, 220, 227–228, 252, 269, 273–275, 275, 275–277, 282–283, 306, 310–311, 62, 82–83
packages/marketplace/contracts/transfer-manager/interfaces
   IRoyaltyUGC.sol100%100%100%100%
   ITransferExecutor.sol100%100%100%100%
   ITransferManager.sol100%100%100%100%

@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

82.76%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts/exchange
   Exchange.sol79.71%70%87.50%86.96%171, 63, 79, 89, 89, 89, 89–90, 90, 90–91, 98
   ExchangeCore.sol84.81%61.54%100%95.45%118–119, 119, 119, 122, 188, 190, 194, 215–216, 64, 94
   OrderValidator.sol71.70%58.33%100%79.17%38, 67–68, 68, 68, 72, 83–84, 89, 91, 91, 91, 91–92, 94
   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.sol90.48%75%100%100%24, 30
   TransferManager.sol81.65%68.97%100%87.50%137, 190, 199, 206–207, 207, 207–208, 212, 220, 227–228, 252, 269, 273–275, 275, 275–277, 282–283, 306, 310–311, 62, 82–83
packages/marketplace/contracts/transfer-manager/interfaces
   IRoyaltyUGC.sol100%100%100%100%
   ITransferExecutor.sol100%100%100%100%
   ITransferManager.sol100%100%100%100%

Please sign in to comment.