Skip to content

Commit

Permalink
Lean out some of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-turek committed Sep 16, 2024
1 parent c2c2266 commit 2cd712d
Showing 1 changed file with 165 additions and 99 deletions.
264 changes: 165 additions & 99 deletions packages/marketplace/contracts/TransferManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -202,52 +202,61 @@ abstract contract TransferManager is Initializable, ITransferManager {
/// @param paymentSide DealSide of the fee-side order
/// @param nftSide DealSide of the nft-side order
function _doTransfersWithFeesAndRoyalties(DealSide memory paymentSide, DealSide memory nftSide) internal {
uint256 fees;
uint256 remainder = paymentSide.asset.value;
(, address nftSideRecipient) = _getRecipients(paymentSide, nftSide);

if (nftSide.asset.assetType.assetClass == LibAsset.AssetClass.BUNDLE) {
if (_isTSBSeller(nftSide.account)) {
fees = protocolFeePrimary;
// No royalties
if (fees > 0 && remainder > 0) {
remainder = _transferPercentage(
remainder,
paymentSide,
paymentSide.asset.value,
defaultFeeReceiver,
fees,
PROTOCOL_FEE_MULTIPLIER
);
}
} else {
(uint256 fees, bool shouldTransferRoyalties) = _calculateFeesAndRoyalties(nftSide);
bool isBundle = nftSide.asset.assetType.assetClass == LibAsset.AssetClass.BUNDLE;

if (isBundle) {
if (!_isTSBSeller(nftSide.account)) {
remainder = _doTransfersWithFeesAndRoyaltiesForBundle(paymentSide, nftSide, nftSideRecipient);
}
} else {
if (_isTSBSeller(nftSide.account) || _isPrimaryMarket(nftSide)) {
fees = protocolFeePrimary;
// No royalties
} else {
fees = protocolFeeSecondary;
remainder = _transferRoyalties(remainder, paymentSide, nftSide);
}

if (fees > 0 && remainder > 0) {
remainder = _transferPercentage(
remainder,
paymentSide,
paymentSide.asset.value,
defaultFeeReceiver,
fees,
PROTOCOL_FEE_MULTIPLIER
);
// No royalties but primary fee should be paid on the total value of the bundle
remainder = _transferProtocolFees(remainder, paymentSide, fees);
}
} else if (shouldTransferRoyalties) {
remainder = _transferRoyalties(remainder, paymentSide, nftSide);
}
if (!isBundle) {
remainder = _transferProtocolFees(remainder, paymentSide, fees);
}

if (remainder > 0) {
_transfer(LibAsset.Asset(paymentSide.asset.assetType, remainder), paymentSide.account, nftSideRecipient);
}
}

function _calculateFeesAndRoyalties(
DealSide memory nftSide
) internal returns (uint256 fees, bool shouldTransferRoyalties) {
if (_isTSBSeller(nftSide.account) || _isPrimaryMarket(nftSide)) {
fees = protocolFeePrimary;
shouldTransferRoyalties = false;
} else {
fees = protocolFeeSecondary;
shouldTransferRoyalties = true;
}
}

function _transferProtocolFees(
uint256 remainder,
DealSide memory paymentSide,
uint256 fees
) internal returns (uint256) {
if (fees > 0 && remainder > 0) {
remainder = _transferPercentage(
remainder,
paymentSide,
paymentSide.asset.value,
defaultFeeReceiver,
fees,
PROTOCOL_FEE_MULTIPLIER
);
}
return remainder;
}

function _doTransfersWithFeesAndRoyaltiesForBundle(
DealSide memory paymentSide,
DealSide memory nftSide,
Expand All @@ -258,93 +267,150 @@ abstract contract TransferManager is Initializable, ITransferManager {
uint256 feeSecondary = protocolFeeSecondary;
LibAsset.Bundle memory bundle = LibAsset.decodeBundle(nftSide.asset.assetType);

remainder = _processERC721Bundles(
paymentSide,
nftSide,
nftSideRecipient,
remainder,
feePrimary,
feeSecondary,
bundle
);
remainder = _processERC1155Bundles(
paymentSide,
nftSide,
nftSideRecipient,
remainder,
feePrimary,
feeSecondary,
bundle
);
remainder = _processQuadBundles(paymentSide, nftSideRecipient, remainder, feeSecondary, bundle);
return remainder;
}

function _processERC721Bundles(
DealSide memory paymentSide,
DealSide memory nftSide,
address nftSideRecipient,
uint256 remainder,
uint256 feePrimary,
uint256 feeSecondary,
LibAsset.Bundle memory bundle
) internal returns (uint256) {
for (uint256 i; i < bundle.bundledERC721.length; i++) {
address token = bundle.bundledERC721[i].erc721Address;
uint256 idLength = bundle.bundledERC721[i].ids.length;
for (uint256 j; j < idLength; j++) {
if (_isPrimaryMarketForBundledAsset(nftSide.account, token, bundle.bundledERC721[i].ids[j])) {
// No royalties

if (feePrimary > 0 && remainder > 0) {
remainder = _transferPercentage(
remainder,
paymentSide,
bundle.priceDistribution.erc721Prices[i][j],
defaultFeeReceiver,
feePrimary,
PROTOCOL_FEE_MULTIPLIER
);
}
} else {
remainder = _transferFeesAndRoyaltiesForBundledAsset(
paymentSide,
token,
nftSideRecipient,
remainder,
bundle.bundledERC721[i].ids[j],
bundle.priceDistribution.erc721Prices[i][j],
feeSecondary
);
}
remainder = _processSingleAsset(
paymentSide,
nftSide,
nftSideRecipient,
remainder,
feePrimary,
feeSecondary,
token,
bundle.bundledERC721[i].ids[j],
bundle.priceDistribution.erc721Prices[i][j]
);
}
}
return remainder;
}

function _processERC1155Bundles(
DealSide memory paymentSide,
DealSide memory nftSide,
address nftSideRecipient,
uint256 remainder,
uint256 feePrimary,
uint256 feeSecondary,
LibAsset.Bundle memory bundle
) internal returns (uint256) {
for (uint256 i; i < bundle.bundledERC1155.length; i++) {
address token = bundle.bundledERC1155[i].erc1155Address;
uint256 idLength = bundle.bundledERC1155[i].ids.length;
require(idLength == bundle.bundledERC1155[i].supplies.length, "ERC1155 array error");

for (uint256 j; j < idLength; j++) {
if (_isPrimaryMarketForBundledAsset(nftSide.account, token, bundle.bundledERC1155[i].ids[j])) {
// No royalties

if (feePrimary > 0 && remainder > 0) {
remainder = _transferPercentage(
remainder,
paymentSide,
bundle.priceDistribution.erc1155Prices[i][j],
defaultFeeReceiver,
feePrimary,
PROTOCOL_FEE_MULTIPLIER
);
}
} else {
// for exchanging one or more than one bundle of ERC1155s
for (uint256 k = 0; k < nftSide.asset.value; k++) {
remainder = _transferFeesAndRoyaltiesForBundledAsset(
paymentSide,
token,
nftSideRecipient,
remainder,
bundle.bundledERC1155[i].ids[j],
bundle.priceDistribution.erc1155Prices[i][j],
feeSecondary
);
}
for (uint256 k = 0; k < nftSide.asset.value; k++) {
remainder = _processSingleAsset(
paymentSide,
nftSide,
nftSideRecipient,
remainder,
feePrimary,
feeSecondary,
token,
bundle.bundledERC1155[i].ids[j],
bundle.priceDistribution.erc1155Prices[i][j]
);
}
}
}
return remainder;
}

function _processQuadBundles(
DealSide memory paymentSide,
address nftSideRecipient,
uint256 remainder,
uint256 feeSecondary,
LibAsset.Bundle memory bundle
) internal returns (uint256) {
uint256 quadSize = bundle.quads.xs.length;
if (quadSize > 0) {
for (uint256 i = 0; i < quadSize; i++) {
uint256 size = bundle.quads.sizes[i];
uint256 x = bundle.quads.xs[i];
uint256 y = bundle.quads.ys[i];

uint256 tokenId = idInPath(0, size, x, y);
remainder = _transferFeesAndRoyaltiesForBundledAsset(
paymentSide,
address(landContract),
nftSideRecipient,
for (uint256 i = 0; i < quadSize; i++) {
uint256 size = bundle.quads.sizes[i];
uint256 x = bundle.quads.xs[i];
uint256 y = bundle.quads.ys[i];

uint256 tokenId = idInPath(0, size, x, y);
remainder = _transferFeesAndRoyaltiesForBundledAsset(
paymentSide,
address(landContract),
nftSideRecipient,
remainder,
tokenId,
bundle.priceDistribution.quadPrices[i],
feeSecondary
);
}
return remainder;
}

function _processSingleAsset(
DealSide memory paymentSide,
DealSide memory nftSide,
address nftSideRecipient,
uint256 remainder,
uint256 feePrimary,
uint256 feeSecondary,
address token,
uint256 tokenId,
uint256 assetPrice
) internal returns (uint256) {
if (_isPrimaryMarketForBundledAsset(nftSide.account, token, tokenId)) {
if (feePrimary > 0 && remainder > 0) {
remainder = _transferPercentage(
remainder,
tokenId,
bundle.priceDistribution.quadPrices[i],
feeSecondary
paymentSide,
assetPrice,
defaultFeeReceiver,
feePrimary,
PROTOCOL_FEE_MULTIPLIER
);
}
} else {
remainder = _transferFeesAndRoyaltiesForBundledAsset(
paymentSide,
token,
nftSideRecipient,
remainder,
tokenId,
assetPrice,
feeSecondary
);
}

return remainder;
}

Expand Down

2 comments on commit 2cd712d

@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

98.22%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts
   Exchange.sol94.52%93.33%94.44%96%126, 194, 72
   ExchangeCore.sol98.84%96.67%100%100%85
   OrderValidator.sol98.44%96.15%100%100%36
   RoyaltiesRegistry.sol96.32%88.89%100%98.78%194, 216–217, 263, 65
   TransferManager.sol95.42%85.71%100%99.42%163, 225, 247, 333, 393, 430, 580, 585, 596, 608–609, 620, 93
   Whitelist.sol75.81%60%85.71%82.14%104, 108–109, 122, 125, 141–142, 54, 66, 66–67, 71, 76
packages/marketplace/contracts/interfaces
   IOrderValidator.sol100%100%100%100%
   IRoyaltiesProvider.sol100%100%100%100%
   ITransferManager.sol100%100%100%100%
   IWhitelist.sol100%100%100%100%
packages/marketplace/contracts/libraries
   LibAsset.sol100%100%100%100%
   LibMath.sol100%100%100%100%
   LibOrder.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

98.22%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts
   Exchange.sol94.52%93.33%94.44%96%126, 194, 72
   ExchangeCore.sol98.84%96.67%100%100%85
   OrderValidator.sol98.44%96.15%100%100%36
   RoyaltiesRegistry.sol96.32%88.89%100%98.78%194, 216–217, 263, 65
   TransferManager.sol95.42%85.71%100%99.42%163, 225, 247, 333, 393, 430, 580, 585, 596, 608–609, 620, 93
   Whitelist.sol75.81%60%85.71%82.14%104, 108–109, 122, 125, 141–142, 54, 66, 66–67, 71, 76
packages/marketplace/contracts/interfaces
   IOrderValidator.sol100%100%100%100%
   IRoyaltiesProvider.sol100%100%100%100%
   ITransferManager.sol100%100%100%100%
   IWhitelist.sol100%100%100%100%
packages/marketplace/contracts/libraries
   LibAsset.sol100%100%100%100%
   LibMath.sol100%100%100%100%
   LibOrder.sol100%100%100%100%

Please sign in to comment.