From 60bbfd2ea9d4709b914ab3b0764485dcf3c6149b Mon Sep 17 00:00:00 2001 From: Andres Adjimann Date: Tue, 26 Sep 2023 17:09:03 -0300 Subject: [PATCH] test: ported some tests from the old repo --- .../libraries => lib-order}/LibOrder.md | 0 .../test/exchange/Exchange.test.ts | 8 +- .../test/exchange/MatchOrders.test.ts | 528 ++++++++++++++++++ .../test/exchange/OrderValidator.test.ts | 15 +- packages/marketplace/test/fixtures.ts | 85 ++- packages/marketplace/test/utils/assets.ts | 62 +- packages/marketplace/test/utils/order.ts | 38 +- 7 files changed, 675 insertions(+), 61 deletions(-) rename packages/marketplace/contracts/{exchange/libraries => lib-order}/LibOrder.md (100%) create mode 100644 packages/marketplace/test/exchange/MatchOrders.test.ts diff --git a/packages/marketplace/contracts/exchange/libraries/LibOrder.md b/packages/marketplace/contracts/lib-order/LibOrder.md similarity index 100% rename from packages/marketplace/contracts/exchange/libraries/LibOrder.md rename to packages/marketplace/contracts/lib-order/LibOrder.md diff --git a/packages/marketplace/test/exchange/Exchange.test.ts b/packages/marketplace/test/exchange/Exchange.test.ts index 5599decafc..5b131688c5 100644 --- a/packages/marketplace/test/exchange/Exchange.test.ts +++ b/packages/marketplace/test/exchange/Exchange.test.ts @@ -2,9 +2,9 @@ import {expect} from 'chai'; import {deployFixtures} from '../fixtures'; import {loadFixture} from '@nomicfoundation/hardhat-network-helpers'; import { + AssetERC1155, AssetERC20, AssetERC721, - AssetERC1155, AssetETH, } from '../utils/assets.ts'; @@ -12,12 +12,10 @@ import { hashKey, hashOrder, OrderDefault, - OrderBack, UINT256_MAX_VALUE, - DEFAULT_ORDER_TYPE, } from '../utils/order.ts'; -import {getBytes, ZeroAddress} from 'ethers'; -import {signOrder, signOrderBack} from '../utils/signature'; +import {ZeroAddress} from 'ethers'; +import {signOrder} from '../utils/signature'; describe('Exchange.sol', function () { // TODO: Erase diff --git a/packages/marketplace/test/exchange/MatchOrders.test.ts b/packages/marketplace/test/exchange/MatchOrders.test.ts new file mode 100644 index 0000000000..a8a7f5544b --- /dev/null +++ b/packages/marketplace/test/exchange/MatchOrders.test.ts @@ -0,0 +1,528 @@ +import {deployFixturesWithExtraTokens} from '../fixtures'; +import {loadFixture} from '@nomicfoundation/hardhat-network-helpers'; +import {expect} from 'chai'; +import { + AssetBundle, + AssetERC1155, + AssetERC20, + AssetERC721, +} from '../utils/assets.ts'; +import * as crypto from 'crypto'; + +import {OrderDefault} from '../utils/order.ts'; +import {ethers, ZeroAddress} from 'ethers'; +import {signOrder} from '../utils/signature'; +import {latest} from '@nomicfoundation/hardhat-network-helpers/dist/src/helpers/time'; + +describe('Exchange match orders tests', function () { + describe('without royalties', function () { + describe('erc20 x erc20', function () { + it('match order', async function () { + const { + ExchangeContractAsUser, + user1: maker, + user2: taker, + ERC20Contract, + ERC20Contract2, + matchOrders, + } = await loadFixture(deployFixturesWithExtraTokens); + const makerAmount = ethers.parseUnits('12', 'ether'); + const takerAmount = ethers.parseUnits('23', 'ether'); + const t1 = ERC20Contract; + const t2 = ERC20Contract2; + + await t1.mint(maker.address, makerAmount); + await t1 + .connect(maker) + .approve(await ExchangeContractAsUser.getAddress(), makerAmount); + + await t2.mint(taker.address, takerAmount); + await t2 + .connect(taker) + .approve(await ExchangeContractAsUser.getAddress(), takerAmount); + + expect(await t1.balanceOf(maker)).to.be.equal(makerAmount); + expect(await t1.balanceOf(taker)).to.be.equal(0); + expect(await t2.balanceOf(maker)).to.be.equal(0); + expect(await t2.balanceOf(taker)).to.be.equal(takerAmount); + + const makerAsset = await AssetERC20(t1, makerAmount); + const takerAsset = await AssetERC20(t2, takerAmount); + await matchOrders(maker, makerAsset, taker, takerAsset); + + expect(await t1.balanceOf(maker)).to.be.equal(0); + expect(await t1.balanceOf(taker)).to.be.equal(makerAmount); + expect(await t2.balanceOf(maker)).to.be.equal(takerAmount); + expect(await t2.balanceOf(taker)).to.be.equal(0); + }); + + it('directAcceptBid maker accept from anyone, taker is bidMaker', async function () { + const { + ExchangeContractAsUser, + OrderValidatorAsDeployer, + user1: maker, + user2: taker, + ERC20Contract, + ERC20Contract2, + } = await loadFixture(deployFixturesWithExtraTokens); + // This generate a sell order that anybody can take, the first one that runs the right tx get it => front running ? + const bidPaymentAmount = ethers.parseUnits('12', 'ether'); + const bidNftAmount = ethers.parseUnits('23', 'ether'); + const bidMaker = taker; + const t1 = ERC20Contract; + const t2 = ERC20Contract2; + + await t1.mint(maker.address, bidNftAmount); + await t1 + .connect(maker) + .approve(await ExchangeContractAsUser.getAddress(), bidNftAmount); + + await t2.mint(taker.address, bidPaymentAmount); + await t2 + .connect(taker) + .approve(await ExchangeContractAsUser.getAddress(), bidPaymentAmount); + + const bidSalt = BigInt('0x' + crypto.randomBytes(16).toString('hex')); + const timestamp = await latest(); + const bidStart = timestamp - 100000; + const bidEnd = timestamp + 100000; + + const bidPaymentAsset = await AssetERC20(t2, bidPaymentAmount); + const bidNftAsset = await AssetERC20(t1, bidNftAmount); + // This creates a taker order with ERC20 or ETH and a taker order with anything (here we choose t2). + const rightOrder = await OrderDefault( + bidMaker, + bidPaymentAsset, + ZeroAddress, + bidNftAsset, + bidSalt, + bidStart, + bidEnd + ); + + const bidSignature = await signOrder( + rightOrder, + bidMaker, + OrderValidatorAsDeployer + ); + + const acceptBid = { + bidMaker, + bidNftAmount, + nftAssetClass: bidNftAsset.assetType.assetClass, + nftData: bidNftAsset.assetType.data, + bidPaymentAmount, + paymentToken: await t2.getAddress(), + bidSalt, + bidStart, + bidEnd, + bidDataType: rightOrder.dataType, + bidData: rightOrder.data, + bidSignature, + sellOrderPaymentAmount: bidPaymentAmount, + sellOrderNftAmount: bidNftAmount, + sellOrderData: '0x', + }; + + expect(await t1.balanceOf(maker)).to.be.equal(bidNftAmount); + expect(await t1.balanceOf(bidMaker)).to.be.equal(0); + expect(await t2.balanceOf(maker)).to.be.equal(0); + expect(await t2.balanceOf(bidMaker)).to.be.equal(bidPaymentAmount); + + await ExchangeContractAsUser.connect(maker).directAcceptBid(acceptBid); + + expect(await t1.balanceOf(maker)).to.be.equal(0); + expect(await t1.balanceOf(bidMaker)).to.be.equal(bidNftAmount); + expect(await t2.balanceOf(maker)).to.be.equal(bidPaymentAmount); + expect(await t2.balanceOf(bidMaker)).to.be.equal(0); + }); + }); + + // TODO: Doesn't exists anymore + // eslint-disable-next-line mocha/no-skipped-tests + it.skip('directPurchase, maker is sellOrderMaker taker can be anyone', async function () { + // const sellOrderNftAmount = parseUnits('12', 'ether'); + // const sellOrderPaymentAmount = parseUnits('23', 'ether'); + // const sellOrderMaker = maker; + // + // const t1 = await ERC20Contract.new({from: deployer}); + // const nftData = enc(t1.address); + // const nftAssetClass = ERC20; + // const t2 = await ERC20Contract.new({from: deployer}); + // const paymentToken = t2.address; + // const paymentAssetType = ERC20; + // + // await t1.mint(sellOrderMaker, sellOrderNftAmount); + // await t1.approve(await ExchangeContractAsUser.getAddress(), sellOrderNftAmount, { + // from: sellOrderMaker + // }); + // + // await t2.mint(taker, sellOrderPaymentAmount); + // await t2.approve(await ExchangeContractAsUser.getAddress(), sellOrderPaymentAmount, { + // from: taker + // }); + // + // const sellOrderSalt = '0x' + crypto.randomBytes(16).toString('hex'); + // const currentBlock = await web3.eth.getBlock('latest'); + // const sellOrderStart = currentBlock.timestamp - 100000; + // const sellOrderEnd = currentBlock.timestamp + 100000; + // const sellOrderDataType = '0xffffffff'; + // const sellOrderData = '0x'; + // + // // This creates a maker order with ERC20 or ETH and a taker order with anything (here we choose t2). + // const leftOrder = Order( + // sellOrderMaker, + // Asset(ERC20, nftData, sellOrderNftAmount), + // AddressZero, + // Asset(paymentAssetType, enc(paymentToken), sellOrderPaymentAmount), + // sellOrderSalt, + // sellOrderStart, + // sellOrderEnd, + // sellOrderDataType, + // sellOrderData + // ); + // + // const sellOrderSignature = await sign( + // web3, + // leftOrder, + // sellOrderMaker, + // this.exchange.address + // ); + // + // const purchase = { + // sellOrderMaker, + // sellOrderNftAmount, + // nftAssetClass, + // nftData, + // sellOrderPaymentAmount, + // paymentToken, + // sellOrderSalt, + // sellOrderStart, + // sellOrderEnd, + // sellOrderDataType, + // sellOrderData, + // sellOrderSignature, + // buyOrderPaymentAmount: sellOrderPaymentAmount, + // buyOrderNftAmount: sellOrderNftAmount, + // buyOrderData: '0x' + // }; + // + // expect(await t1.balanceOf(sellOrderMaker), sellOrderNftAmount); + // expect(await t1.balanceOf(taker), 0); + // expect(await t2.balanceOf(sellOrderMaker), 0); + // expect(await t2.balanceOf(taker), sellOrderPaymentAmount); + // + // await this.exchange.directPurchase(purchase, {from: taker}); + // + // expect(await t1.balanceOf(sellOrderMaker), 0); + // expect(await t1.balanceOf(taker), sellOrderNftAmount); + // expect(await t2.balanceOf(sellOrderMaker), sellOrderPaymentAmount); + // expect(await t2.balanceOf(taker), 0); + }); + + describe('erc721 x erc721', function () { + it('match order', async function () { + const { + ExchangeContractAsUser, + user1: maker, + user2: taker, + ERC721Contract, + MintableERC721WithRoyalties, + matchOrders, + } = await loadFixture(deployFixturesWithExtraTokens); + + const makerTokenId = 123; + const takerTokenId = 456; + const t1 = ERC721Contract; + const t2 = MintableERC721WithRoyalties; + + await t1.mint(maker.address, makerTokenId); + await t1 + .connect(maker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + await t2.safeMint(taker.address, takerTokenId); + await t2 + .connect(taker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + expect(await t1.ownerOf(makerTokenId)).to.be.equal(maker.address); + expect(await t2.ownerOf(takerTokenId)).to.be.equal(taker.address); + + const makerAsset = await AssetERC721(t1, makerTokenId); + const takerAsset = await AssetERC721(t2, takerTokenId); + await matchOrders(maker, makerAsset, taker, takerAsset); + + expect(await t1.ownerOf(makerTokenId)).to.be.equal(taker.address); + expect(await t2.ownerOf(takerTokenId)).to.be.equal(maker.address); + }); + }); + + describe('erc1155 x erc1155', function () { + it('match order', async function () { + const { + ExchangeContractAsUser, + user1: maker, + user2: taker, + ERC1155Contract, + MintableERC1155WithRoyalties, + matchOrders, + } = await loadFixture(deployFixturesWithExtraTokens); + + const makerTokenId = 123; + const makerQuantity = 10; + const takerTokenId = 456; + const takerQuantity = 30; + const t1 = ERC1155Contract; + const t2 = MintableERC1155WithRoyalties; + + await t1.mint(maker.address, makerTokenId, makerQuantity); + await t1 + .connect(maker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + await t2.mint(taker.address, takerTokenId, takerQuantity, '0x'); + await t2 + .connect(taker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + const makerAsset = await AssetERC1155(t1, makerTokenId, makerQuantity); + const takerAsset = await AssetERC1155(t2, takerTokenId, takerQuantity); + await matchOrders(maker, makerAsset, taker, takerAsset); + + expect(await t1.balanceOf(maker, makerTokenId)).to.be.equal(0); + expect(await t1.balanceOf(taker, makerTokenId)).to.be.equal( + makerQuantity + ); + expect(await t2.balanceOf(maker, takerTokenId)).to.be.equal( + takerQuantity + ); + expect(await t2.balanceOf(taker, takerTokenId)).to.be.equal(0); + }); + }); + + describe('erc721 x erc20', function () { + it('match order', async function () { + const { + ExchangeContractAsUser, + user1: maker, + user2: taker, + ERC20Contract, + ERC721Contract, + matchOrders, + } = await loadFixture(deployFixturesWithExtraTokens); + + const tokenId = 345; + const amount = ethers.parseUnits('12', 'ether'); + + const t1 = ERC721Contract; + const t2 = ERC20Contract; + + await t1.mint(maker.address, tokenId); + await t1 + .connect(maker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + await t2.mint(taker.address, amount); + await t2 + .connect(taker) + .approve(await ExchangeContractAsUser.getAddress(), amount); + expect(await t1.ownerOf(tokenId)).to.be.equal(maker.address); + expect(await t2.balanceOf(maker)).to.be.equal(0); + expect(await t2.balanceOf(taker)).to.be.equal(amount); + + const makerAsset = await AssetERC721(t1, tokenId); + const takerAsset = await AssetERC20(t2, amount); + await matchOrders(maker, makerAsset, taker, takerAsset); + + expect(await t1.ownerOf(tokenId)).to.be.equal(taker.address); + expect(await t2.balanceOf(maker)).to.be.equal(amount); + expect(await t2.balanceOf(taker)).to.be.equal(0); + }); + }); + + describe('bundle', function () { + it('match order', async function () { + const { + ExchangeContractAsUser, + user1: maker, + user2: taker, + ERC20Contract, + ERC721Contract, + ERC1155Contract, + matchOrders, + } = await loadFixture(deployFixturesWithExtraTokens); + + const makerTokenId = 123; + const makerQuantity = 10; + const takerTokenId = 456; + const takerQuantity = 30; + const makerAmount = ethers.parseUnits('10', 'ether'); + const takerAmount = ethers.parseUnits('1', 'ether'); + + const t1 = ERC20Contract; + const t2 = ERC721Contract; + const t3 = ERC1155Contract; + + // Maker + await t1.mint(maker.address, makerAmount); + await t1 + .connect(maker) + .approve(await ExchangeContractAsUser.getAddress(), makerAmount); + + await t2.mint(maker.address, makerTokenId); + await t2 + .connect(maker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + await t3.mint(maker.address, makerTokenId, makerQuantity); + await t3 + .connect(maker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + // taker + await t1.mint(taker.address, takerAmount); + await t1 + .connect(taker) + .approve(await ExchangeContractAsUser.getAddress(), takerAmount); + await t2.mint(taker.address, takerTokenId); + await t2 + .connect(taker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + await t3.mint(taker.address, takerTokenId, takerQuantity); + await t3 + .connect(taker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + const makerAsset = await AssetBundle( + [{token: t1, value: makerAmount}], + [{token: t2, tokenId: makerTokenId}], + [{token: t3, tokenId: makerTokenId, value: makerQuantity}] + ); + const takerAsset = await AssetBundle( + [{token: t1, value: takerAmount}], + [{token: t2, tokenId: takerTokenId}], + [{token: t3, tokenId: takerTokenId, value: takerQuantity}] + ); + await matchOrders(maker, makerAsset, taker, takerAsset); + + expect(await t1.balanceOf(maker)).to.be.equal(takerAmount); + expect(await t1.balanceOf(taker)).to.be.equal(makerAmount); + + expect(await t2.ownerOf(makerTokenId)).to.be.equal(taker.address); + expect(await t2.ownerOf(takerTokenId)).to.be.equal(maker.address); + + expect(await t3.balanceOf(maker, makerTokenId)).to.be.equal(0); + expect(await t3.balanceOf(taker, makerTokenId)).to.be.equal( + makerQuantity + ); + expect(await t3.balanceOf(maker, takerTokenId)).to.be.equal( + takerQuantity + ); + expect(await t3.balanceOf(taker, takerTokenId)).to.be.equal(0); + }); + }); + }); + + describe('with royalties', function () { + describe('erc721 x erc20', function () { + it('match order', async function () { + const { + ExchangeContractAsUser, + deployer, + user1: maker, + user2: taker, + ERC20Contract, + MintableERC721WithRoyalties, + matchOrders, + } = await loadFixture(deployFixturesWithExtraTokens); + + const royaltiesParts = 123n; + const tokenId = 345; + const amount = ethers.parseUnits('12', 'ether'); + const tenTo18 = BigInt('1' + '0'.repeat(18)); + const royalties = + (((amount * royaltiesParts) / tenTo18) * tenTo18) / 10000n; + + const t1 = MintableERC721WithRoyalties; + await t1.setDefaultRoyalty(deployer.address, royaltiesParts); + const t2 = ERC20Contract; + + await t1.safeMint(maker.address, tokenId); + await t1 + .connect(maker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + await t2.mint(taker, amount); + await t2 + .connect(taker) + .approve(await ExchangeContractAsUser.getAddress(), amount); + + expect(await t1.ownerOf(tokenId)).to.be.equal(maker.address); + expect(await t2.balanceOf(maker)).to.be.equal(0); + expect(await t2.balanceOf(taker)).to.be.equal(amount); + + const makerAsset = await AssetERC721(t1, tokenId); + const takerAsset = await AssetERC20(t2, amount); + await matchOrders(maker, makerAsset, taker, takerAsset); + + expect(await t1.ownerOf(tokenId)).to.be.equal(taker.address); + + expect(await t2.balanceOf(deployer)).to.be.equal(royalties); + expect(await t2.balanceOf(taker)).to.be.equal(0); + expect(await t2.balanceOf(maker)).to.be.equal(amount - royalties); + }); + }); + + describe('erc1155 x erc20', function () { + it('match order', async function () { + const { + ExchangeContractAsUser, + deployer, + user1: maker, + user2: taker, + ERC20Contract, + MintableERC1155WithRoyalties, + matchOrders, + } = await loadFixture(deployFixturesWithExtraTokens); + const royaltiesParts = 123n; + const tokenId = 345; + const quantity = 12; + const amount = ethers.parseUnits('12', 'ether'); + const tenTo18 = BigInt('1' + '0'.repeat(18)); + const royalties = + (((amount * royaltiesParts) / tenTo18) * tenTo18) / 10000n; + + const t1 = MintableERC1155WithRoyalties; + await t1.setDefaultRoyalty(deployer.address, royaltiesParts); + const t2 = ERC20Contract; + + // mint more than needed + await t1.mint(maker, tokenId, quantity * 10, '0x'); + await t1 + .connect(maker) + .setApprovalForAll(await ExchangeContractAsUser.getAddress(), true); + + await t2.mint(taker, amount); + await t2 + .connect(taker) + .approve(await ExchangeContractAsUser.getAddress(), amount); + + expect(await t1.balanceOf(maker, tokenId)).to.be.equal(quantity * 10); + expect(await t1.balanceOf(taker, tokenId)).to.be.equal(0); + expect(await t2.balanceOf(maker)).to.be.equal(0); + expect(await t2.balanceOf(taker)).to.be.equal(amount); + + const makerAsset = await AssetERC1155(t1, tokenId, quantity); + const takerAsset = await AssetERC20(t2, amount); + await matchOrders(maker, makerAsset, taker, takerAsset); + + expect(await t1.balanceOf(maker, tokenId)).to.be.equal(quantity * 9); + expect(await t1.balanceOf(taker, tokenId)).to.be.equal(quantity); + + expect(await t2.balanceOf(deployer)).to.be.equal(royalties); + expect(await t2.balanceOf(taker)).to.be.equal(0); + expect(await t2.balanceOf(maker)).to.be.equal(amount - royalties); + }); + }); + }); +}); diff --git a/packages/marketplace/test/exchange/OrderValidator.test.ts b/packages/marketplace/test/exchange/OrderValidator.test.ts index 898a3446fc..24499aa636 100644 --- a/packages/marketplace/test/exchange/OrderValidator.test.ts +++ b/packages/marketplace/test/exchange/OrderValidator.test.ts @@ -132,8 +132,9 @@ describe('OrderValidator.sol', function () { }); it('should validate when assetClass is not ETH_ASSET_CLASS', async function () { - const {OrderValidatorAsUser, ERC20Contract, user1, user2} = - await loadFixture(deployFixtures); + const {OrderValidatorAsUser, ERC20Contract, user1} = await loadFixture( + deployFixtures + ); const makerAsset = await AssetERC20(ERC20Contract, 100); const takerAsset = await AssetETH(100); const order = await OrderDefault( @@ -173,8 +174,9 @@ describe('OrderValidator.sol', function () { }); it('should validate when assetClass is ETH_ASSET_CLASS, salt is zero and Order maker is sender', async function () { - const {OrderValidatorAsUser, ERC20Contract, user1, user2} = - await loadFixture(deployFixtures); + const {OrderValidatorAsUser, ERC20Contract, user1} = await loadFixture( + deployFixtures + ); const makerAsset = await AssetETH(100); const takerAsset = await AssetERC20(ERC20Contract, 100); const order = await OrderDefault( @@ -234,8 +236,9 @@ describe('OrderValidator.sol', function () { }); it('should verify ERC20 Whitelist', async function () { - const {OrderValidatorAsUser, ERC20Contract, ERC721Contract} = - await loadFixture(deployFixtures); + const {OrderValidatorAsUser, ERC20Contract} = await loadFixture( + deployFixtures + ); await expect( OrderValidatorAsUser.verifyERC20Whitelist( await ERC20Contract.getAddress() diff --git a/packages/marketplace/test/fixtures.ts b/packages/marketplace/test/fixtures.ts index 9bdb6dbd08..c25d771339 100644 --- a/packages/marketplace/test/fixtures.ts +++ b/packages/marketplace/test/fixtures.ts @@ -1,7 +1,12 @@ import {ethers, upgrades} from 'hardhat'; -import {ZeroAddress} from 'ethers'; +import {Signer, ZeroAddress} from 'ethers'; +import {OrderDefault} from './utils/order'; +import {randomInt} from 'crypto'; +import {signOrder} from './utils/signature'; +import {Asset} from './utils/assets'; -export async function deployFixtures() { +// TODO: Split fixtures so we use only what is needed!!! +async function deploy() { const [deployer, user, defaultFeeReceiver, user1, user2] = await ethers.getSigners(); @@ -71,8 +76,11 @@ export async function deployFixtures() { ExchangeContractAsDeployer, ExchangeContractAsUser, TrustedForwarder, + ERC20ContractFactory, ERC20Contract, + ERC721ContractFactory, ERC721Contract, + ERC1155ContractFactory, ERC1155Contract, OrderValidatorAsDeployer, OrderValidatorAsUser, @@ -83,3 +91,76 @@ export async function deployFixtures() { ZERO_ADDRESS: ZeroAddress, }; } + +// TODO: Use only one test token. +export async function deployFixturesWithExtraTokens() { + const ret = await deploy(); + + const ERC20Contract2 = await ret.ERC20ContractFactory.deploy(); + await ERC20Contract2.waitForDeployment(); + + const MintableERC721WithRoyaltiesFactory = await ethers.getContractFactory( + 'MintableERC721WithRoyalties' + ); + const MintableERC721WithRoyalties = + await MintableERC721WithRoyaltiesFactory.deploy(); + await MintableERC721WithRoyalties.waitForDeployment(); + + const MintableERC1155WithRoyaltiesFactory = await ethers.getContractFactory( + 'MintableERC1155WithRoyalties' + ); + const MintableERC1155WithRoyalties = + await MintableERC1155WithRoyaltiesFactory.deploy(); + await MintableERC1155WithRoyalties.waitForDeployment(); + return { + ...ret, + ERC20Contract2, + MintableERC721WithRoyalties, + MintableERC1155WithRoyalties, + matchOrders: async ( + maker: Signer, + makerAsset: Asset, + taker: Signer, + takerAsset: Asset + ) => { + const leftOrder = await OrderDefault( + maker, + makerAsset, + ZeroAddress, + takerAsset, + randomInt(200_000_000_000_000), + 0, + 0 + ); + const rightOrder = await OrderDefault( + taker, + takerAsset, + ZeroAddress, + makerAsset, + randomInt(200_000_000_000_000), + 0, + 0 + ); + const makerSig = await signOrder( + leftOrder, + maker, + ret.OrderValidatorAsDeployer + ); + const takerSig = await signOrder( + rightOrder, + taker, + ret.OrderValidatorAsDeployer + ); + await ret.ExchangeContractAsUser.matchOrders( + leftOrder, + makerSig, + rightOrder, + takerSig + ); + }, + }; +} + +export async function deployFixtures() { + return deploy(); +} diff --git a/packages/marketplace/test/utils/assets.ts b/packages/marketplace/test/utils/assets.ts index 3b7ae76e02..fd42487d78 100644 --- a/packages/marketplace/test/utils/assets.ts +++ b/packages/marketplace/test/utils/assets.ts @@ -4,6 +4,7 @@ // SEE: LibAsset.sol import { AbiCoder, + Numeric, BytesLike, Contract, keccak256, @@ -19,7 +20,7 @@ export const ERC1155_ASSET_CLASS = bytes4Keccak('ERC1155'); // export const ERC1155_TSB_CLASS = bytes4Keccak('ERC1155_TSB'); export const BUNDLE_ASSET_CLASS = bytes4Keccak('BUNDLE'); -//TODO: export const ERC721_LAZY_ASSET_CLASS = bytes4Keccak('ERC721_LAZY'); +// TODO: export const ERC721_LAZY_ASSET_CLASS = bytes4Keccak('ERC721_LAZY'); export const ASSET_TYPE_TYPEHASH = keccak256( Buffer.from('AssetType(bytes4 assetClass,bytes data)') ); @@ -36,20 +37,20 @@ export type AssetType = { export type Asset = { assetType: AssetType; - value: number; + value: Numeric; }; -export const AssetETH = (): Asset => ({ +export const AssetETH = (value: Numeric): Asset => ({ assetType: { assetClass: ETH_ASSET_CLASS, data: '0x', }, - value: 1, + value, }); export const AssetERC20 = async ( tokenContract: Contract, - value: number + value: Numeric ): Promise => ({ assetType: { assetClass: ERC20_ASSET_CLASS, @@ -63,7 +64,7 @@ export const AssetERC20 = async ( export const AssetERC721 = async ( tokenContract: Contract, - tokenId: number + tokenId: Numeric ): Promise => ({ assetType: { assetClass: ERC721_ASSET_CLASS, @@ -72,13 +73,14 @@ export const AssetERC721 = async ( [await tokenContract.getAddress(), tokenId] ), }, + // TODO: Test value !=1 value: 1, }); export const AssetERC1155 = async ( tokenContract: Contract, - tokenId: number, - value: number + tokenId: Numeric, + value: Numeric ): Promise => ({ assetType: { assetClass: ERC1155_ASSET_CLASS, @@ -91,32 +93,33 @@ export const AssetERC1155 = async ( }); export const AssetBundle = async ( - erc20: {tokenContract: Contract; value: number}[], - erc721: {tokenContract: Contract; tokenId: number; value: number}[], - erc1155: {tokenContract: Contract; tokenId: number; value: number}[] + erc20: {token: Contract; value: Numeric}[], + erc721: { + token: Contract; + tokenId: Numeric; + }[], + erc1155: { + token: Contract; + tokenId: Numeric; + value: Numeric; + }[] ): Promise => { const erc20Details = []; for (const x of erc20) { - erc20Details.push({ - token: await x.tokenContract.getAddress(), - value: x.value, - }); + erc20Details.push([await x.token.getAddress(), x.value]); } - const erc721Details: {token: string; id: number}[] = []; + const erc721Details = []; for (const x of erc721) { - erc20Details.push({ - token: await x.tokenContract.getAddress(), - id: x.tokenId, - value: 1, - }); + erc721Details.push([ + await x.token.getAddress(), + x.tokenId, + // TODO: Test value !=1 + 1, + ]); } - const erc1155Details: {token: string; id: number; value: number}[] = []; + const erc1155Details = []; for (const x of erc1155) { - erc20Details.push({ - token: await x.tokenContract.getAddress(), - id: x.tokenId, - value: x.value, - }); + erc1155Details.push([await x.token.getAddress(), x.tokenId, x.value]); } return { assetType: { @@ -124,12 +127,13 @@ export const AssetBundle = async ( data: AbiCoder.defaultAbiCoder().encode( [ 'tuple(address, uint256)[]', - 'tuple(address, uint256,uint256)[]', - 'tuple(address, uint256uint256)[]', + 'tuple(address, uint256, uint256)[]', + 'tuple(address, uint256, uint256)[]', ], [erc20Details, erc721Details, erc1155Details] ), }, + // TODO: It make sense tho have multipler bundles >1 ???? value: 1, }; }; diff --git a/packages/marketplace/test/utils/order.ts b/packages/marketplace/test/utils/order.ts index e1e59864ad..e6e6982752 100644 --- a/packages/marketplace/test/utils/order.ts +++ b/packages/marketplace/test/utils/order.ts @@ -2,7 +2,7 @@ // SEE: LibOrder.sol and LibOrderData.sol import {Asset, hashAsset, hashAssetType} from './assets'; import {bytes4Keccak} from './signature'; -import {AbiCoder, keccak256, Signer, ZeroAddress} from 'ethers'; +import {AbiCoder, Numeric, keccak256, Signer, ZeroAddress} from 'ethers'; import {BytesLike} from 'ethers/src.ts/utils/index'; export const ORDER_TYPEHASH = keccak256( @@ -29,9 +29,9 @@ export type Order = { makeAsset: Asset; taker: string; takeAsset: Asset; - salt: number; - start: number; - end: number; + salt: Numeric; + start: Numeric; + end: Numeric; dataType: string; data: BytesLike; }; @@ -42,9 +42,9 @@ export type OrderBack = { makeAsset: Asset; taker: string; takeAsset: Asset; - salt: number; - start: number; - end: number; + salt: Numeric; + start: Numeric; + end: Numeric; dataType: string; data: BytesLike; }; @@ -55,9 +55,9 @@ export const OrderBack = async ( makeAsset: Asset, taker: Signer | ZeroAddress, takeAsset: Asset, - salt: number, - start: number, - end: number, + salt: Numeric, + start: Numeric, + end: Numeric, dataType: string, data: string ): Promise => ({ @@ -78,9 +78,9 @@ export const OrderDefault = async ( makeAsset: Asset, taker: Signer | ZeroAddress, takeAsset: Asset, - salt: number, - start: number, - end: number + salt: Numeric, + start: Numeric, + end: Numeric ): Promise => ({ maker: await maker.getAddress(), makeAsset, @@ -98,9 +98,9 @@ export const OrderSell = async ( makeAsset: Asset, taker: Signer | ZeroAddress, takeAsset: Asset, - salt: number, - start: number, - end: number, + salt: Numeric, + start: Numeric, + end: Numeric, payouts: string, // TODO: better type originFeeFirst: string, // TODO: better type originFeeSecond: string, // TODO: better type @@ -132,9 +132,9 @@ export const OrderBuy = async ( makeAsset: Asset, taker: Signer | ZeroAddress, takeAsset: Asset, - salt: number, - start: number, - end: number, + salt: Numeric, + start: Numeric, + end: Numeric, payouts: string, // TODO: better type originFeeFirst: string, // TODO: better type originFeeSecond: string, // TODO: better type