From c2c22667457c6d0117cc81d6bfb61caeca581163 Mon Sep 17 00:00:00 2001 From: Ayush Tiwari Date: Mon, 16 Sep 2024 15:37:11 +0530 Subject: [PATCH] refactor: bundle tests --- packages/marketplace/test/Exchange.test.ts | 4 + .../test/exchange/Bundle.behavior.ts | 1264 +---------------- 2 files changed, 36 insertions(+), 1232 deletions(-) diff --git a/packages/marketplace/test/Exchange.test.ts b/packages/marketplace/test/Exchange.test.ts index fa043bb1c0..b35320c904 100644 --- a/packages/marketplace/test/Exchange.test.ts +++ b/packages/marketplace/test/Exchange.test.ts @@ -27,6 +27,7 @@ import {shouldSupportInterfaces} from './common/SupportsInterface.behavior.ts'; import {shouldCheckForWhitelisting} from './exchange/WhitelistingTokens.behavior.ts'; import {shouldMatchOrdersWithRoyalty} from './exchange/MatchOrdersWithRoyalties.behavior.ts'; import {shouldMatchOrdersForBundle} from './exchange/Bundle.behavior.ts'; +import {shouldMatchOrdersForBundleWithRoyalty} from './exchange/BundleWithRoyalties.behaviour.ts'; describe('Exchange.sol', function () { let AssetMatcherAsUser: Contract, @@ -110,6 +111,9 @@ describe('Exchange.sol', function () { // eslint-disable-next-line mocha/no-setup-in-describe shouldMatchOrdersForBundle(); + // eslint-disable-next-line mocha/no-setup-in-describe + shouldMatchOrdersForBundleWithRoyalty(); + // eslint-disable-next-line mocha/no-setup-in-describe exchangeConfig(); diff --git a/packages/marketplace/test/exchange/Bundle.behavior.ts b/packages/marketplace/test/exchange/Bundle.behavior.ts index 43262dc698..00922bf3ab 100644 --- a/packages/marketplace/test/exchange/Bundle.behavior.ts +++ b/packages/marketplace/test/exchange/Bundle.behavior.ts @@ -5,7 +5,6 @@ import { AssetERC20, Asset, AssetBundle, - LibPartData, BundledERC721, BundledERC1155, Quads, @@ -22,20 +21,15 @@ export function shouldMatchOrdersForBundle() { let ExchangeContractAsUser: Contract, ExchangeContractAsAdmin: Contract, OrderValidatorAsAdmin: Contract, - RoyaltiesRegistryAsDeployer: Contract, ERC20Contract: Contract, ERC721Contract: Contract, ERC1155Contract: Contract, LandContract: Contract, - RoyaltiesProvider: Contract, - QuadHelper: Contract, protocolFeeSecondary: number, defaultFeeReceiver: Signer, maker: Signer, taker: Signer, - royaltyReceiver: Signer, - royaltyReceiver2: Signer, - royaltyReceiver3: Signer, + landAdmin: Signer, LandAsAdmin: Contract, makerAsset: Asset, takerAsset: Asset, @@ -49,15 +43,13 @@ export function shouldMatchOrdersForBundle() { orderLeft: Order, orderRight: Order, makerSig: string, - takerSig: string, - landAdmin: Signer; + takerSig: string; describe('Bundle x ERC20', function () { beforeEach(async function () { ({ ExchangeContractAsUser, OrderValidatorAsAdmin, - RoyaltiesRegistryAsDeployer, ERC20Contract, ERC721Contract, ERC1155Contract, @@ -255,18 +247,22 @@ export function shouldMatchOrdersForBundle() { ).to.be.equal(1); expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9750000000 // 10000000000 - protocolFee + 9885000000 // 10000000000 - protocolFee ); expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( 20000000000 ); - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 + // TODO : check protocol fee expect( await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 + (Number(protocolFeeSecondary) * + Number(priceDistribution.erc721Prices[0][0]) + + Number(protocolFeeSecondary) * + Number(priceDistribution.erc1155Prices[0][0])) / + 10000 ); }); @@ -428,7 +424,7 @@ export function shouldMatchOrdersForBundle() { ).to.be.equal(1); expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9750000000 + 9975000000 ); expect(await ERC20Contract.balanceOf(taker)).to.be.equal(20000000000); @@ -437,7 +433,7 @@ export function shouldMatchOrdersForBundle() { await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) ).to.be.equal( (Number(protocolFeeSecondary) * - Number(ERC20AssetForRightOrder.value)) / + Number(priceDistribution.erc1155Prices[0][0])) / 10000 ); @@ -622,13 +618,16 @@ export function shouldMatchOrdersForBundle() { expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( 10000000000 ); - expect(await ERC20Contract.balanceOf(maker)).to.be.equal(19500000000); + expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( + 19950000000 + ); expect( await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) ).to.be.equal( - (Number(protocolFeeSecondary) * - Number(ERC20AssetForRightOrder.value)) / + (2 * + (Number(protocolFeeSecondary) * + Number(priceDistribution.erc1155Prices[0][0]))) / 10000 ); @@ -723,7 +722,7 @@ export function shouldMatchOrdersForBundle() { ).to.be.equal(1); expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9750000000 + 9975000000 ); expect(await ERC20Contract.balanceOf(taker)).to.be.equal(20000000000); @@ -731,7 +730,7 @@ export function shouldMatchOrdersForBundle() { await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) ).to.be.equal( (Number(protocolFeeSecondary) * - Number(ERC20AssetForRightOrder.value)) / + Number(priceDistribution.erc1155Prices[0][0])) / 10000 ); // 1 * partial fills => 1 * fee taken @@ -776,7 +775,7 @@ export function shouldMatchOrdersForBundle() { ).to.be.equal(1); expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 19500000000 + 19950000000 ); expect(await ERC20Contract.balanceOf(taker)).to.be.equal(10000000000); 0; @@ -785,7 +784,7 @@ export function shouldMatchOrdersForBundle() { await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) ).to.be.equal( (Number(protocolFeeSecondary) * - Number(ERC20AssetForRightOrder.value) * + Number(priceDistribution.erc1155Prices[0][0]) * 2) / 10000 ); // 2 * partial fills => 2 * fee taken @@ -812,7 +811,6 @@ export function shouldMatchOrdersForBundle() { defaultFeeReceiver, user1: maker, user2: taker, - deployer: royaltyReceiver, LandContract, LandAsAdmin, landAdmin, @@ -969,1224 +967,26 @@ export function shouldMatchOrdersForBundle() { ).to.be.equal(1); expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9750000000 // 10000000000 - protocolFee + 9862500000 // 10000000000 - protocolFee ); expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( 20000000000 ); - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 + // check protocol fee expect( await ERC20Contract.balanceOf(await defaultFeeReceiver.getAddress()) ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 - ); - - // check maker received quads - expect(await LandContract.balanceOf(takerAddress)).to.be.equal(18); - expect(await LandContract.balanceOf(makerAddress)).to.be.equal(18); - }); - }); - - describe('Royalty X Bundle', function () { - beforeEach(async function () { - ({ - ExchangeContractAsUser, - ExchangeContractAsAdmin, - OrderValidatorAsAdmin, - RoyaltiesRegistryAsDeployer, - ERC20Contract, - ERC721Contract, - ERC1155Contract, - RoyaltiesProvider, - QuadHelper, - protocolFeeSecondary, - defaultFeeReceiver, - user1: maker, - user2: taker, - deployer: royaltyReceiver, - admin: royaltyReceiver2, - user: royaltyReceiver3, - LandContract, - LandAsAdmin, - landAdmin, - } = await loadFixture(deployFixtures)); - - priceDistribution = { - erc721Prices: [[4000000000]], - erc1155Prices: [[600000000]], - quadPrices: [], - }; - - // Set up ERC721 for maker - await ERC721Contract.mint(await maker.getAddress(), 1); - await ERC721Contract.connect(maker).approve( - await ExchangeContractAsUser.getAddress(), - 1 - ); - await ERC721Contract.mint(await maker.getAddress(), 2); - await ERC721Contract.connect(maker).approve( - await ExchangeContractAsUser.getAddress(), - 2 - ); - - // Set up ERC1155 for maker - await ERC1155Contract.mint(await maker.getAddress(), 1, 50); - await ERC1155Contract.mint(await maker.getAddress(), 2, 50); - - await ERC1155Contract.connect(maker).setApprovalForAll( - await ExchangeContractAsUser.getAddress(), - true - ); - - // Make sure the land contract address is set on the Exchange - const landContractAddress = await LandContract.getAddress(); - await ExchangeContractAsAdmin.setLandContract(landContractAddress); - - // Land contract setup for maker ------------------------------------------------------------- - - // Set a minter - await LandAsAdmin.setMinter(await landAdmin.getAddress(), true); - - // Ensure that the marketplace contract is an approved operator for mock land contract - await LandContract.connect(maker).setApprovalForAllWithOutFilter( - await ExchangeContractAsUser.getAddress(), - true - ); - - await LandAsAdmin.mintQuad(await maker.getAddress(), 3, 0, 0, '0x'); - await LandAsAdmin.mintQuad(await maker.getAddress(), 3, 0, 3, '0x'); - await LandAsAdmin.mintQuad(await maker.getAddress(), 3, 3, 0, '0x'); - await LandAsAdmin.mintQuad(await maker.getAddress(), 3, 3, 3, '0x'); - expect( - await LandContract.balanceOf(await maker.getAddress()) - ).to.be.equal(36); - - // End land setup for maker ------------------------------------------------------------------ - - // Construct makerAsset bundle - bundledERC721 = [ - { - erc721Address: ERC721Contract.target, - ids: [1], - }, - ]; - - bundledERC1155 = [ - { - erc1155Address: ERC1155Contract.target, - ids: [1], - supplies: [10], - }, - ]; - - quads = { - sizes: [], - xs: [], - ys: [], - data: '0x', - }; // empty quads - - // Create bundle for passing as right order - bundleData = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - makerAsset = await AssetBundle(bundleData, 1); // there can only ever be 1 copy of a bundle that contains ERC721 - - // Set up ERC20 for taker - await ERC20Contract.mint(await taker.getAddress(), 30000000000); - await ERC20Contract.connect(taker).approve( - await ExchangeContractAsUser.getAddress(), - 30000000000 - ); - - // Construct takerAsset - takerAsset = await AssetERC20(ERC20Contract, 10000000000); - }); - - it('should not execute match order for bundle if royalties are > 50% for ERC721 token', async function () { - // set up royalties by token - await RoyaltiesRegistryAsDeployer.setRoyaltiesByToken( - await ERC721Contract.getAddress(), - [await LibPartData(royaltyReceiver, 5100)] // royalty is set to 5100% of the amount - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - - await expect( - ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]) - ).to.be.revertedWith('royalties are too high (>50%)'); - }); - - it('should execute complete match order for bundle with royalty on ERC721 token', async function () { - // set up royalties by token - await RoyaltiesRegistryAsDeployer.setRoyaltiesByToken( - await ERC721Contract.getAddress(), - [await LibPartData(royaltyReceiver, 2000)] // 20% royalty for ERC721 token - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - - await ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(takerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 40 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal( - 10 - ); - - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) - ).to.be.equal(10000000000); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) - ).to.be.equal(1); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 8950000000 // 10000000000 - royalty - protocolFee - ); - - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 20000000000 - ); - - // check paid royalty - expect( - await ERC20Contract.balanceOf(royaltyReceiver.getAddress()) - ).to.be.equal(800000000); // 20% of asset price for ERC721 token - - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 - expect( - await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) - ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 - ); - }); - - it('should execute complete match order for bundle with multiple royalty receivers on ERC721 token', async function () { - bundledERC721 = [ - { - erc721Address: ERC721Contract.target, - ids: [1, 2], - }, - ]; - - priceDistribution = { - erc721Prices: [[2000000000, 3000000000]], - erc1155Prices: [[500000000]], - quadPrices: [], - }; - - bundleData = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - makerAsset = await AssetBundle(bundleData, 1); - - // configuring royalties - await RoyaltiesProvider.initializeProvider( - await ERC721Contract.getAddress(), - 1, - [await LibPartData(royaltyReceiver, 1000)] // 10% royalty for ERC721 token with id:1 - ); - await RoyaltiesProvider.initializeProvider( - await ERC721Contract.getAddress(), - 2, - [await LibPartData(royaltyReceiver2, 2000)] // 20% royalty for ERC721 token with id:2 - ); - await RoyaltiesRegistryAsDeployer.setProviderByToken( - await ERC721Contract.getAddress(), - RoyaltiesProvider.getAddress() - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - - await ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(takerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 40 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal( - 10 - ); - - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) - ).to.be.equal(10000000000); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) - ).to.be.equal(1); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 8950000000 // 10000000000 - royalty - protocolFee - ); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 20000000000 - ); - - // check paid royalty - expect( - await ERC20Contract.balanceOf(royaltyReceiver.getAddress()) - ).to.be.equal(200000000); // 10% of asset price for ERC721 token with id:1 - expect( - await ERC20Contract.balanceOf(royaltyReceiver2.getAddress()) - ).to.be.equal(600000000); // 20% of asset price for ERC721 token with id:2 - - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 - expect( - await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) - ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 - ); - }); - - it('should not execute match order for bundle if royalties are > 50% for ERC1155 token', async function () { - bundledERC1155 = [ - { - erc1155Address: ERC1155Contract.target, - ids: [1], - supplies: [10], - }, - ]; - - bundleData = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - makerAsset = await AssetBundle(bundleData, 1); - - // configuring royalties - await RoyaltiesProvider.initializeProvider( - await ERC1155Contract.getAddress(), - 1, - [await LibPartData(royaltyReceiver, 5100)] // royalty is set to 51% of the amount - ); - await RoyaltiesRegistryAsDeployer.setProviderByToken( - await ERC1155Contract.getAddress(), - RoyaltiesProvider.getAddress() - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - - await expect( - ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]) - ).to.be.revertedWith('royalties are too high (>50%)'); - }); - - it('should execute complete match order for bundle with royalty on ERC1155 token', async function () { - bundleData = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - makerAsset = await AssetBundle(bundleData, 1); - - // configuring royalties - await RoyaltiesProvider.initializeProvider( - await ERC1155Contract.getAddress(), - 1, - [await LibPartData(royaltyReceiver, 500)] // 5% royalty for ERC1155 token with id:1 - ); - - await RoyaltiesRegistryAsDeployer.setProviderByToken( - await ERC1155Contract.getAddress(), - RoyaltiesProvider.getAddress() - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - - await ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(takerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 40 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal( - 10 - ); - - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) - ).to.be.equal(10000000000); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) - ).to.be.equal(1); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9720000000 // 10000000000 - royalty - protocolFee - ); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 20000000000 - ); - - // check paid royalty - expect( - await ERC20Contract.balanceOf(royaltyReceiver.getAddress()) - ).to.be.equal(30000000); // 5% of asset price for ERC1155 token with id:1 - - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 - expect( - await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) - ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 - ); - }); - - it('should execute complete match order for bundle with multiple royalty receivers on ERC1155 token', async function () { - bundledERC1155 = [ - { - erc1155Address: ERC1155Contract.target, - ids: [1, 2], - supplies: [10, 5], - }, - ]; - - priceDistribution = { - erc721Prices: [[5000000000]], - erc1155Prices: [[300000000, 400000000]], - quadPrices: [], - }; - - bundleData = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - makerAsset = await AssetBundle(bundleData, 1); - - // configuring royalties - await RoyaltiesProvider.initializeProvider( - await ERC1155Contract.getAddress(), - 1, - [await LibPartData(royaltyReceiver, 500)] // 5% royalty for ERC1155 token with id:1 - ); - await RoyaltiesProvider.initializeProvider( - await ERC1155Contract.getAddress(), - 2, - [await LibPartData(royaltyReceiver2, 1000)] // 10% royalty for ERC1155 token with id:2 - ); - await RoyaltiesRegistryAsDeployer.setProviderByToken( - await ERC1155Contract.getAddress(), - RoyaltiesProvider.getAddress() - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(makerAddress, 2)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - expect(await ERC1155Contract.balanceOf(takerAddress, 2)).to.be.equal(0); - - await ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(takerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 40 - ); - expect(await ERC1155Contract.balanceOf(makerAddress, 2)).to.be.equal( - 45 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal( - 10 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 2)).to.be.equal(5); - - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) - ).to.be.equal(10000000000); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) - ).to.be.equal(1); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9695000000 // 10000000000 - royalty - protocolFee - ); - - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 20000000000 - ); - - // check paid royalty - expect( - await ERC20Contract.balanceOf(royaltyReceiver.getAddress()) - ).to.be.equal(15000000); // 5% of asset price for ERC1155 token with id:1 - expect( - await ERC20Contract.balanceOf(royaltyReceiver2.getAddress()) - ).to.be.equal(40000000); // 10% of asset price for ERC1155 token with id:2 - - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 - expect( - await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) - ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 - ); - }); - - it('should partially fill orders using matchOrders between ERC20 and ERC1155 BUNDLE with order value >1', async function () { - // Seller (taker - right) has 5 copies of a Bundle type; buyer (taker - left) just wants to buy 1 of these - const ERC20AssetForLeftOrder = await AssetERC20( - ERC20Contract, - 50000000000 - ); - - // ERC20Asset for partial fill - const ERC20AssetForRightOrder = await AssetERC20( - ERC20Contract, - 20000000000 - ); - - bundledERC721 = []; - - priceDistribution = { - erc721Prices: [[]], // price distribution without ERC721 - erc1155Prices: [[1000000000]], - quadPrices: [], - }; - - const bundleAsset = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - // configuring royalties - await RoyaltiesProvider.initializeProvider( - await ERC1155Contract.getAddress(), - 1, - [await LibPartData(royaltyReceiver, 1000)] // 10% royalty for ERC1155 token with id:1 - ); - - await RoyaltiesRegistryAsDeployer.setProviderByToken( - await ERC1155Contract.getAddress(), - RoyaltiesProvider.getAddress() - ); - - // ERC1155Asset for partial fill - bundleWithoutERC721Left = await AssetBundle(bundleAsset, 5); - - bundleWithoutERC721Right = await AssetBundle(bundleAsset, 2); - - // left order for partial fill - orderLeft = await OrderDefault( - maker, - bundleWithoutERC721Left, // makeAsset - ZeroAddress, - ERC20AssetForLeftOrder, // takeAsset - 1, - 0, - 0 - ); - // right order for partial fill - orderRight = await OrderDefault( - taker, - ERC20AssetForRightOrder, // makeAsset - ZeroAddress, - bundleWithoutERC721Right, // takeAsset - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) - ).to.be.equal(0); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) - ).to.be.equal(0); - - await ExchangeContractAsUser.matchOrders([ - { - orderLeft, - signatureLeft: makerSig, - orderRight, - signatureRight: takerSig, - }, - ]); - - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) // newFill.rightValue - ).to.be.equal(20000000000); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) // newFill.leftValue - ).to.be.equal(2); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 19300000000 // 10000000000 - royalty - protocolFee - ); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 10000000000 - ); - 100000000; - // check paid royalty - expect( - await ERC20Contract.balanceOf(royaltyReceiver.getAddress()) - ).to.be.equal(200000000); // 10% of asset price for ERC1155 token with id:1 for 2 bundles - - expect( - await ERC20Contract.balanceOf(defaultFeeReceiver.getAddress()) - ).to.be.equal( - (Number(protocolFeeSecondary) * - Number(ERC20AssetForRightOrder.value)) / - 10000 - ); - - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 30 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal( - 20 - ); - }); - - it('should execute complete match order for bundle with royalty on Quads', async function () { - quads = { - sizes: [3, 3], // 3x3, 3x3 = 9+9 lands total - xs: [3, 0], - ys: [0, 3], - data: '0x', - }; - - priceDistribution = { - erc721Prices: [[4000000000]], - erc1155Prices: [[500000000]], - quadPrices: [400000000, 600000000], - }; - - // Create bundle for passing as right order - bundleData = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - makerAsset = await AssetBundle(bundleData, 1); - - // set up royalties by token - await RoyaltiesRegistryAsDeployer.setRoyaltiesByToken( - await LandAsAdmin.getAddress(), - [await LibPartData(royaltyReceiver, 1000)] // 10% royalty for Land token - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - - await ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(takerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 40 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal( - 10 - ); - - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) - ).to.be.equal(10000000000); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) - ).to.be.equal(1); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9650000000 // 10000000000 - protocolFee - royalty - ); - - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 20000000000 - ); - - // check paid royalty - expect( - await ERC20Contract.balanceOf(royaltyReceiver.getAddress()) - ).to.be.equal(100000000); // 10% of asset price for quad (3,0) & (0,3) - - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 - expect( - await ERC20Contract.balanceOf(await defaultFeeReceiver.getAddress()) - ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 - ); - - // check maker received quads - expect(await LandContract.balanceOf(takerAddress)).to.be.equal(18); - expect(await LandContract.balanceOf(makerAddress)).to.be.equal(18); - }); - - it('should execute complete match order for bundle with multiple royalty receivers on Quads', async function () { - quads = { - sizes: [3, 3], // 3x3, 3x3 = 9+9 lands total - xs: [3, 0], - ys: [0, 3], - data: '0x', - }; - - priceDistribution = { - erc721Prices: [[4000000000]], - erc1155Prices: [[500000000]], - quadPrices: [400000000, 600000000], - }; - - // Create bundle for passing as right order - bundleData = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - makerAsset = await AssetBundle(bundleData, 1); - - // configuring royalties - for (let i = 0; i < 9; i++) { - await RoyaltiesProvider.initializeProvider( - await LandAsAdmin.getAddress(), - await QuadHelper.idInPath(i, 3, 3, 0), - [await LibPartData(royaltyReceiver, 1000)] // royalty is set to 10% of the amount for quad(3,0) - ); - } - - for (let i = 0; i < 9; i++) { - await RoyaltiesProvider.initializeProvider( - await LandAsAdmin.getAddress(), - await QuadHelper.idInPath(i, 3, 0, 3), - [await LibPartData(royaltyReceiver2, 2000)] // royalty is set to 20% of the amount for quad(0,3) - ); - } - - await RoyaltiesRegistryAsDeployer.setProviderByToken( - await LandAsAdmin.getAddress(), - RoyaltiesProvider.getAddress() - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - - await ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(takerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 40 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal( - 10 - ); - - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) - ).to.be.equal(10000000000); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) - ).to.be.equal(1); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9590000000 // 10000000000 - protocolFee - royalty - ); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 20000000000 - ); - - // check paid royalty - expect( - await ERC20Contract.balanceOf(royaltyReceiver.getAddress()) - ).to.be.equal(40000000); // 10% of asset price for quad(3,0) - expect( - await ERC20Contract.balanceOf(royaltyReceiver2.getAddress()) - ).to.be.equal(120000000); // 20% of asset price for quad(0,3) - - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 - expect( - await ERC20Contract.balanceOf(await defaultFeeReceiver.getAddress()) - ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 - ); - - // check maker received quads - expect(await LandContract.balanceOf(takerAddress)).to.be.equal(18); - expect(await LandContract.balanceOf(makerAddress)).to.be.equal(18); - }); - - it('should execute complete match order for bundle with multiple external royalty receivers', async function () { - quads = { - sizes: [3, 3], // 3x3, 3x3 = 9+9 lands total - xs: [3, 0], - ys: [0, 3], - data: '0x', - }; - - priceDistribution = { - erc721Prices: [[1000000000]], - erc1155Prices: [[500000000]], - quadPrices: [1000000000, 3000000000], - }; - - // Create bundle for passing as right order - bundleData = { - bundledERC721, - bundledERC1155, - quads, - priceDistribution, - }; - - makerAsset = await AssetBundle(bundleData, 1); - - // set up royalties by token for ERC721 token - await RoyaltiesRegistryAsDeployer.setRoyaltiesByToken( - await ERC721Contract.getAddress(), - [await LibPartData(royaltyReceiver, 1000)] // 10% royalty for ERC721 token - ); - - // configuring royalties for ERC1155 tokens - await RoyaltiesProvider.initializeProvider( - await ERC1155Contract.getAddress(), - 1, - [await LibPartData(royaltyReceiver2, 1000)] // 10% royalty for ERC1155 token with id 1 - ); - await RoyaltiesRegistryAsDeployer.setProviderByToken( - await ERC1155Contract.getAddress(), - RoyaltiesProvider.getAddress() - ); - - // set up royalties by token for quad - await RoyaltiesRegistryAsDeployer.setRoyaltiesByToken( - await LandAsAdmin.getAddress(), - [await LibPartData(royaltyReceiver3, 1000)] // 10% royalty for Land token - ); - - orderLeft = await OrderDefault( - maker, - makerAsset, // Bundle - ZeroAddress, - takerAsset, // ERC20 - 1, - 0, - 0 - ); - orderRight = await OrderDefault( - taker, - takerAsset, // ERC20 - ZeroAddress, - makerAsset, // Bundle - 1, - 0, - 0 - ); - - makerSig = await signOrder(orderLeft, maker, OrderValidatorAsAdmin); - takerSig = await signOrder(orderRight, taker, OrderValidatorAsAdmin); - - const makerAddress = await maker.getAddress(); - const takerAddress = await taker.getAddress(); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal(0); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 30000000000 - ); - expect(await ERC721Contract.ownerOf(1)).to.be.equal(makerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 50 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal(0); - - await ExchangeContractAsUser.matchOrders([ - { - orderLeft, // passing Bundle as left order - signatureLeft: makerSig, - orderRight, // passing ERC20 as right order - signatureRight: takerSig, - }, - ]); - - expect(await ERC721Contract.ownerOf(1)).to.be.equal(takerAddress); - expect(await ERC1155Contract.balanceOf(makerAddress, 1)).to.be.equal( - 40 - ); - expect(await ERC1155Contract.balanceOf(takerAddress, 1)).to.be.equal( - 10 - ); - - expect( - await ExchangeContractAsUser.fills(hashKey(orderLeft)) - ).to.be.equal(10000000000); - expect( - await ExchangeContractAsUser.fills(hashKey(orderRight)) - ).to.be.equal(1); - - expect(await ERC20Contract.balanceOf(makerAddress)).to.be.equal( - 9200000000 // 10000000000 - protocolFee - royalty - ); - expect(await ERC20Contract.balanceOf(takerAddress)).to.be.equal( - 20000000000 - ); - - // check paid royalty - expect( - await ERC20Contract.balanceOf(royaltyReceiver.getAddress()) - ).to.be.equal(100000000); // 10% of the amount - expect( - await ERC20Contract.balanceOf(royaltyReceiver2.getAddress()) - ).to.be.equal(50000000); // 10% of asset price for ERC1155 token with id:1 - expect( - await ERC20Contract.balanceOf(royaltyReceiver3.getAddress()) - ).to.be.equal(400000000); // 10% of asset price for quad (3,0) & (0,3) - - // check protocol fee -> 250 * 10000000000 / 10000 = 250000000 - expect( - await ERC20Contract.balanceOf(await defaultFeeReceiver.getAddress()) - ).to.be.equal( - (Number(protocolFeeSecondary) * Number(takerAsset.value)) / 10000 + (Number(protocolFeeSecondary) * + Number(priceDistribution.erc721Prices[0][0]) + + Number(protocolFeeSecondary) * + Number(priceDistribution.erc1155Prices[0][0]) + + Number(protocolFeeSecondary) * + Number(priceDistribution.quadPrices[0]) + + Number(protocolFeeSecondary) * + Number(priceDistribution.quadPrices[1])) / + 10000 ); // check maker received quads