Skip to content

Commit

Permalink
Merge pull request #1191 from thesandboxgame/feat/marketplace-fee-side
Browse files Browse the repository at this point in the history
feat: take fees only on ERC20
  • Loading branch information
adjisb authored Oct 4, 2023
2 parents 96e65b8 + 8e299fa commit 7a1fe68
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 144 deletions.
8 changes: 3 additions & 5 deletions packages/marketplace/contracts/exchange/ExchangeCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Ini
import {LibFill} from "./libraries/LibFill.sol";
import {IAssetMatcher} from "../interfaces/IAssetMatcher.sol";
import {TransferExecutor} from "../transfer-manager/TransferExecutor.sol";
import {LibFeeSide} from "../transfer-manager/lib/LibFeeSide.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";
Expand Down Expand Up @@ -161,17 +159,17 @@ abstract contract ExchangeCore is Initializable, TransferExecutor, ITransferMana

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

Expand Down
41 changes: 31 additions & 10 deletions packages/marketplace/contracts/lib-asset/LibAsset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ library LibAsset {
ERC721_ASSET_CLASS,
ERC1155_ASSET_CLASS
}
bytes32 internal constant ASSET_TYPE_TYPEHASH = keccak256("AssetType(uint256 assetClass,bytes data)");

bytes32 internal constant ASSET_TYPEHASH =
keccak256("Asset(AssetType assetType,uint256 value)AssetType(uint256 assetClass,bytes data)");
enum FeeSide {
NONE,
LEFT,
RIGHT
}

struct AssetType {
LibAsset.AssetClassType assetClass;
AssetClassType assetClass;
bytes data;
}

Expand All @@ -28,16 +30,35 @@ library LibAsset {
uint256 value;
}

/// @notice calculate hash of asset type
/// @param assetType to be hashed
/// @return hash of assetType
bytes32 internal constant ASSET_TYPE_TYPEHASH = keccak256("AssetType(uint256 assetClass,bytes data)");

bytes32 internal constant ASSET_TYPEHASH =
keccak256("Asset(AssetType assetType,uint256 value)AssetType(uint256 assetClass,bytes data)");

/// @notice decides if the fees will be taken and from which side
/// @param leftClass left side asset class type
/// @param rightClass right side asset class type
/// @return side from which the fees will be taken or none
function getFeeSide(AssetClassType leftClass, AssetClassType rightClass) internal pure returns (FeeSide) {
if (leftClass == AssetClassType.ERC20_ASSET_CLASS && rightClass != AssetClassType.ERC20_ASSET_CLASS) {
return FeeSide.LEFT;
}
if (rightClass == AssetClassType.ERC20_ASSET_CLASS && leftClass != AssetClassType.ERC20_ASSET_CLASS) {
return FeeSide.RIGHT;
}
return FeeSide.NONE;
}

/// @notice calculate hash of asset type
/// @param assetType to be hashed
/// @return hash of assetType
function hash(AssetType memory assetType) internal pure returns (bytes32) {
return keccak256(abi.encode(ASSET_TYPE_TYPEHASH, assetType.assetClass, keccak256(assetType.data)));
}

/// @notice calculate hash of asset
/// @param asset to be hashed
/// @return hash of asset
/// @notice calculate hash of asset
/// @param asset to be hashed
/// @return hash of asset
function hash(Asset memory asset) internal pure returns (bytes32) {
return keccak256(abi.encode(ASSET_TYPEHASH, hash(asset.assetType), asset.value));
}
Expand Down
5 changes: 2 additions & 3 deletions packages/marketplace/contracts/mocks/LibFeeSideTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
pragma solidity 0.8.21;

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

// TODO: MAKE THE TESTS!!!
contract LibFeeSideTest {
function getFeeSideTest(
LibAsset.AssetClassType maker,
LibAsset.AssetClassType taker
) external pure returns (LibFeeSide.FeeSide) {
return LibFeeSide.getFeeSide(maker, taker);
) external pure returns (LibAsset.FeeSide) {
return LibAsset.getFeeSide(maker, taker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

pragma solidity 0.8.21;

import {ITransferManager, LibDeal, LibFeeSide} from "../transfer-manager/interfaces/ITransferManager.sol";
import {ITransferManager} from "../transfer-manager/interfaces/ITransferManager.sol";
import {LibAsset} from "../lib-asset/LibAsset.sol";

abstract contract SimpleTransferManager is ITransferManager {
function doTransfers(
LibDeal.DealSide memory left,
LibDeal.DealSide memory right,
LibFeeSide.FeeSide /* feeSide */
DealSide memory left,
DealSide memory right,
LibAsset.FeeSide /* feeSide */
) internal override {
transfer(left.asset, left.from, right.from);
transfer(right.asset, right.from, left.from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ERC165Upgradeable, IERC165Upgradeable} from "@openzeppelin/contracts-upg
import {IRoyaltiesProvider} from "../interfaces/IRoyaltiesProvider.sol";
import {BpLibrary} from "../lib-bp/BpLibrary.sol";
import {IRoyaltyUGC} from "./interfaces/IRoyaltyUGC.sol";
import {ITransferManager, LibDeal, LibFeeSide} from "./interfaces/ITransferManager.sol";
import {ITransferManager} from "./interfaces/ITransferManager.sol";
import {LibAsset} from "../lib-asset/LibAsset.sol";
import {LibPart} from "../lib-part/LibPart.sol";

Expand Down Expand Up @@ -99,15 +99,11 @@ abstract contract TransferManager is ERC165Upgradeable, ITransferManager {
/// @notice executes transfers for 2 matched orders
/// @param left DealSide from the left order (see LibDeal.sol)
/// @param right DealSide from the right order (see LibDeal.sol)
function doTransfers(
LibDeal.DealSide memory left,
LibDeal.DealSide memory right,
LibFeeSide.FeeSide feeSide
) internal override {
if (feeSide == LibFeeSide.FeeSide.LEFT) {
function doTransfers(DealSide memory left, DealSide memory right, LibAsset.FeeSide feeSide) internal override {
if (feeSide == LibAsset.FeeSide.LEFT) {
doTransfersWithFees(left, right);
transferPayouts(right.asset.assetType, right.asset.value, right.from, left.payouts);
} else if (feeSide == LibFeeSide.FeeSide.RIGHT) {
} else if (feeSide == LibAsset.FeeSide.RIGHT) {
doTransfersWithFees(right, left);
transferPayouts(left.asset.assetType, left.asset.value, left.from, right.payouts);
} else {
Expand All @@ -119,7 +115,7 @@ abstract contract TransferManager is ERC165Upgradeable, ITransferManager {
/// @notice executes the fee-side transfers (payment + fees)
/// @param paymentSide DealSide of the fee-side order
/// @param nftSide DealSide of the nft-side order
function doTransfersWithFees(LibDeal.DealSide memory paymentSide, LibDeal.DealSide memory nftSide) internal {
function doTransfersWithFees(DealSide memory paymentSide, DealSide memory nftSide) internal {
uint256 rest = paymentSide.asset.value;

if (_applyFees(paymentSide.from)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

pragma solidity 0.8.21;

import {LibDeal} from "../lib/LibDeal.sol";
import {LibFeeSide} from "../lib/LibFeeSide.sol";
import {ITransferExecutor} from "./ITransferExecutor.sol";
import {LibAsset} from "../../lib-asset/LibAsset.sol";
import {LibPart} from "../../lib-part/LibPart.sol";

abstract contract ITransferManager is ITransferExecutor {
function doTransfers(
LibDeal.DealSide memory left,
LibDeal.DealSide memory right,
LibFeeSide.FeeSide feeSide
) internal virtual;
struct DealSide {
LibAsset.Asset asset;
LibPart.Part[] payouts;
address from;
}

function doTransfers(DealSide memory left, DealSide memory right, LibAsset.FeeSide feeSide) internal virtual;
}
14 changes: 0 additions & 14 deletions packages/marketplace/contracts/transfer-manager/lib/LibDeal.sol

This file was deleted.

This file was deleted.

32 changes: 0 additions & 32 deletions packages/marketplace/contracts/transfer-manager/lib/LibFeeSide.sol

This file was deleted.

Loading

1 comment on commit 7a1fe68

@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

86.59%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts/exchange
   AssetMatcher.sol100%100%100%100%
   Exchange.sol80.82%71.88%88.24%87.50%101, 180, 66, 82, 92, 92, 92, 92–93, 93, 93–94
   ExchangeCore.sol86.17%65.63%100%96.08%114, 138–139, 139, 139, 142, 203, 205, 209, 230–231, 248, 74
   OrderValidator.sol71.70%58.33%100%79.17%40, 69–70, 70, 70, 74, 85–86, 91, 93, 93, 93, 93–94, 96
   WhiteList.sol97.67%93.75%100%100%63
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.sol90.40%85.29%100%90.79%166–167, 170–171, 212, 216, 247, 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.