Skip to content

Commit

Permalink
test: added test for primary market fee
Browse files Browse the repository at this point in the history
  • Loading branch information
capedcrusader21 committed Sep 30, 2023
1 parent e44441f commit 9c058cd
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ abstract contract TransferManager is ERC165Upgradeable, ITransferManager {
LibFeeSide.FeeSide feeSide
) internal override {
if (feeSide == LibFeeSide.FeeSide.LEFT) {
doTransfersWithRoyalties(left, right);
doTransfersWithFees(left, right);
transferPayouts(right.asset.assetType, right.asset.value, right.from, left.payouts);
} else if (feeSide == LibFeeSide.FeeSide.RIGHT) {
doTransfersWithRoyalties(right, left);
doTransfersWithFees(right, left);
transferPayouts(left.asset.assetType, left.asset.value, left.from, right.payouts);
} else {
transferPayouts(left.asset.assetType, left.asset.value, left.from, right.payouts);
Expand All @@ -120,7 +120,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 doTransfersWithRoyalties(LibDeal.DealSide memory paymentSide, LibDeal.DealSide memory nftSide) internal {
function doTransfersWithFees(LibDeal.DealSide memory paymentSide, LibDeal.DealSide memory nftSide) internal {
uint256 rest = paymentSide.asset.value;

rest = transferRoyalties(
Expand Down
90 changes: 90 additions & 0 deletions packages/marketplace/test/exchange/Exchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,96 @@ describe('Exchange.sol', function () {
);
});

it('should execute a complete match order between ERC721 and ERC20 tokens in primary market', async function () {
const {
ExchangeContractAsUser,
OrderValidatorAsDeployer,
ERC20Contract,
ERC721WithRoyaltyV2981,
defaultFeeReceiver,
deployer: maker, // making deployer the maker to sell in primary market
user2: taker,
} = await loadFixture(deployFixtures);

await ERC721WithRoyaltyV2981.mint(maker.address, 1, [
await FeeRecipientsData(maker.address, 10000),
]);

await ERC721WithRoyaltyV2981.connect(maker).approve(
await ExchangeContractAsUser.getAddress(),
1
);
await ERC20Contract.mint(taker.address, 100000000000);
await ERC20Contract.connect(taker).approve(
await ExchangeContractAsUser.getAddress(),
100000000000
);

expect(await ERC721WithRoyaltyV2981.ownerOf(1)).to.be.equal(maker.address);
expect(await ERC20Contract.balanceOf(taker.address)).to.be.equal(
100000000000
);
const makerAsset = await AssetERC721(ERC721WithRoyaltyV2981, 1);
const takerAsset = await AssetERC20(ERC20Contract, 100000000000);
const leftOrder = await OrderDefault(
maker,
makerAsset,
ZeroAddress,
takerAsset,
1,
0,
0
);
const rightOrder = await OrderDefault(
taker,
takerAsset,
ZeroAddress,
makerAsset,
1,
0,
0
);
const makerSig = await signOrder(
leftOrder,
maker,
OrderValidatorAsDeployer
);
const takerSig = await signOrder(
rightOrder,
taker,
OrderValidatorAsDeployer
);

expect(await ExchangeContractAsUser.fills(hashKey(leftOrder))).to.be.equal(
0
);
expect(await ExchangeContractAsUser.fills(hashKey(rightOrder))).to.be.equal(
0
);

await ExchangeContractAsUser.matchOrders(
leftOrder,
makerSig,
rightOrder,
takerSig
);
expect(await ExchangeContractAsUser.fills(hashKey(leftOrder))).to.be.equal(
100000000000
);
expect(await ExchangeContractAsUser.fills(hashKey(rightOrder))).to.be.equal(
1
);
expect(await ERC721WithRoyaltyV2981.ownerOf(1)).to.be.equal(taker.address);
expect(await ERC20Contract.balanceOf(maker.address)).to.be.equal(
98770000000
);

// check primary market protocol fee
expect(
await ERC20Contract.balanceOf(defaultFeeReceiver.address)
).to.be.equal(1230000000); // 123 * 100000000000 / 10000 = 1230000000
});

it('should execute a complete match order between ERC721 and ERC20 tokens', async function () {
const {
ExchangeContractAsUser,
Expand Down
3 changes: 2 additions & 1 deletion packages/marketplace/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function deploy() {
);

const TestERC721WithRoyaltyV2981Factory = await ethers.getContractFactory(
'TestERC721WithRoyaltyV2981'
'TestERC721WithRoyaltyV2981Multi'
);
const ERC721WithRoyaltyV2981 = await upgrades.deployProxy(
TestERC721WithRoyaltyV2981Factory,
Expand All @@ -78,6 +78,7 @@ async function deploy() {
initializer: 'initialize',
}
);
await ERC721WithRoyaltyV2981.waitForDeployment();

const ERC20ContractFactory = await ethers.getContractFactory('TestERC20');
const ERC20Contract = await ERC20ContractFactory.deploy();
Expand Down
14 changes: 14 additions & 0 deletions packages/marketplace/test/utils/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Numeric,
BytesLike,
Contract,
AddressLike,
keccak256,
solidityPackedKeccak256,
} from 'ethers';
Expand All @@ -30,6 +31,11 @@ export const ASSET_TYPEHASH = keccak256(
)
);

export type FeeRecipients = {
recipient: AddressLike;
bps: Numeric;
};

export type AssetType = {
assetClass: HashSignature;
data: BytesLike;
Expand All @@ -48,6 +54,14 @@ export const AssetETH = (value: Numeric): Asset => ({
value,
});

export const FeeRecipientsData = async (
recipient: AddressLike,
bps: Numeric
): Promise<FeeRecipients> => ({
recipient,
bps,
});

export const AssetERC20 = async (
tokenContract: Contract,
value: Numeric
Expand Down

1 comment on commit 9c058cd

@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

61.52%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts/exchange
   AssetMatcher.sol83.10%76.67%100%86.49%49–50, 52, 55, 69–70, 70, 70–71, 73, 76–77
   Exchange.sol75%85%73.33%68%105–108, 182, 49, 90, 90–92, 98
   ExchangeCore.sol57.66%45.65%73.33%61.84%106–107, 122, 136–138, 145–146, 158, 173–174, 192–193, 193, 193, 193, 193, 195–196, 196, 196, 196, 196, 257–258, 260–261, 298, 300–302, 308–310, 333–334, 351, 368–369, 369, 369–370, 372–373, 375, 382, 382, 382–383, 385, 385, 385–386, 388
   OrderValidator.sol69.81%57.14%100%80.95%32, 48, 55–56, 56, 56, 66, 76–77, 82, 84, 84, 84, 84–85, 87
   WhiteList.sol97.62%93.75%100%100%51
packages/marketplace/contracts/exchange/libraries
   LibDirectTransfer.sol100%100%100%100%
   LibFill.sol55%33.33%66.67%63.64%48–49, 61, 71–72, 72, 72–73
   LibOrderDataGeneric.sol22.81%22.22%40%20.59%16–23, 25–26, 28, 41, 43, 43, 43–44, 46–47, 50, 50, 50–51, 53, 56, 56, 56–57, 59, 62, 66, 68, 68, 68–70, 73, 82, 82, 82–84
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/lazy-mint/erc-1155
   IERC1155LazyMint.sol100%100%100%100%
   LibERC1155LazyMint.sol0%100%0%0%34–36, 38–40, 42
packages/marketplace/contracts/lazy-mint/erc-721
   IERC721LazyMint.sol100%100%100%100%
   LibERC721LazyMint.sol0%100%0%0%33–35, 37–39, 41
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
   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
   LibOrder.sol66.67%50%100%72.73%105, 105, 107, 107, 41–43, 54, 66
   LibOrderData.sol100%100%100%100%
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.sol69.92%67.65%92.86%66.67%154, 160, 160, 160–161, 164, 164, 164–165, 168, 196–197, 206, 210, 226–227, 230–238, 241, 241, 241–242, 244, 250, 253, 267–268, 270, 54
packages/marketplace/contracts/transfer-manager
   TransferExecutor.sol27.18%21.74%80%26.92%102, 105–106, 111–112, 112, 112–113, 113, 113–114, 116, 119–120, 125–126, 126, 126–127, 135, 145–146, 150, 160, 194, 194, 194–195, 195, 195–196, 200–201, 201–202, 206, 52–54, 61–62, 62, 62, 66, 69–70, 74, 74–75, 75, 75–76, 78, 78, 80–82, 84, 90, 90, 90–91, 91, 91–92, 92, 92, 95–96, 96, 96–97, 97, 97
   TransferManager.sol78.75%67.24%100%83.33%111, 115–116, 149, 195–196, 203–204, 204, 204–205, 209, 223–224, 228–230, 234, 261, 278, 282–284, 284, 284–286, 291–292, 315, 319–320, 83–84
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.sol46.15%41.67%100%46.15%15–16, 18–19, 24, 27, 27, 27–28, 30, 30, 30–31, 33
   LibTransfer.sol0%0%0%0%7–8, 8, 8

Please sign in to comment.