Skip to content

Commit

Permalink
test: matchOrder function
Browse files Browse the repository at this point in the history
  • Loading branch information
capedcrusader21 committed Sep 26, 2023
1 parent aed513e commit 0c04ae9
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 8 deletions.
299 changes: 298 additions & 1 deletion packages/marketplace/test/exchange/Exchange.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {expect} from 'chai';
import {deployFixtures} from '../fixtures';
import {loadFixture} from '@nomicfoundation/hardhat-network-helpers';
import {AssetERC20, AssetERC721, AssetETH} from '../utils/assets.ts';
import {
AssetERC20,
AssetERC721,
AssetERC1155,
AssetETH,
} from '../utils/assets.ts';

import {
hashKey,
Expand Down Expand Up @@ -207,4 +212,296 @@ describe('Exchange.sol', function () {
)
).to.be.equal(UINT256_MAX_VALUE);
});

it('should execute matchOrders', async function () {
const {
ExchangeContractAsUser,
OrderValidatorAsDeployer,
ERC20Contract,
ERC721Contract,
user1,
user2,
} = await loadFixture(deployFixtures);
await ERC721Contract.mint(user1.address, 1);
await ERC721Contract.connect(user1).approve(
await ExchangeContractAsUser.getAddress(),
1
);
await ERC20Contract.mint(user2.address, 100);
await ERC20Contract.connect(user2).approve(
await ExchangeContractAsUser.getAddress(),
100
);

expect(await ERC721Contract.ownerOf(1)).to.be.equal(user1.address);
expect(await ERC20Contract.balanceOf(user2.address)).to.be.equal(100);
const makerAsset = await AssetERC721(ERC721Contract, 1);
const takerAsset = await AssetERC20(ERC20Contract, 100);
const leftOrder = await OrderDefault(
user1,
makerAsset,
ZeroAddress,
takerAsset,
1,
0,
0
);
const rightOrder = await OrderDefault(
user2,
takerAsset,
ZeroAddress,
makerAsset,
1,
0,
0
);
const makerSig = await signOrder(
leftOrder,
user1,
OrderValidatorAsDeployer
);
const takerSig = await signOrder(
rightOrder,
user2,
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(
100
);
expect(await ExchangeContractAsUser.fills(hashKey(rightOrder))).to.be.equal(
1
);
expect(await ERC721Contract.ownerOf(1)).to.be.equal(user2.address);
// 98 = 100 - originFee
expect(await ERC20Contract.balanceOf(user1.address)).to.be.equal(98);
});

it('should revert matchOrders on mismatched asset types', async function () {
const {
ExchangeContractAsUser,
OrderValidatorAsDeployer,
ERC20Contract,
ERC721Contract,
ERC1155Contract,
user1,
user2,
} = await loadFixture(deployFixtures);
await ERC721Contract.mint(user1.address, 1);
await ERC721Contract.connect(user1).approve(
await ExchangeContractAsUser.getAddress(),
1
);
await ERC20Contract.mint(user2.address, 100);
await ERC20Contract.connect(user2).approve(
await ExchangeContractAsUser.getAddress(),
100
);

expect(await ERC721Contract.ownerOf(1)).to.be.equal(user1.address);
expect(await ERC20Contract.balanceOf(user2.address)).to.be.equal(100);

const makerAssetForLeftOrder = await AssetERC721(ERC721Contract, 1);
const takerAssetForLeftOrder = await AssetERC20(ERC20Contract, 100);
const takerAssetForRightOrder = await AssetERC20(ERC20Contract, 50);
const makerAssetForRightOrder = await AssetERC1155(ERC1155Contract, 1, 5);
const leftOrder = await OrderDefault(
user1,
makerAssetForLeftOrder,
ZeroAddress,
takerAssetForLeftOrder,
1,
0,
0
);
const rightOrder = await OrderDefault(
user2,
takerAssetForRightOrder,
ZeroAddress,
makerAssetForRightOrder,
1,
0,
0
);
const makerSig = await signOrder(
leftOrder,
user1,
OrderValidatorAsDeployer
);
const takerSig = await signOrder(
rightOrder,
user2,
OrderValidatorAsDeployer
);

await expect(
ExchangeContractAsUser.matchOrders(
leftOrder,
makerSig,
rightOrder,
takerSig
)
).to.be.revertedWith("assets don't match");
});

it('should partially fill orders using matchOrders', async function () {
const {
ExchangeContractAsUser,
OrderValidatorAsDeployer,
ERC20Contract,
ERC1155Contract,
user1,
user2,
} = await loadFixture(deployFixtures);

await ERC1155Contract.mint(user1.address, 1, 10);
await ERC1155Contract.connect(user1).setApprovalForAll(
await ExchangeContractAsUser.getAddress(),
true
);

await ERC20Contract.mint(user2.address, 100);
await ERC20Contract.connect(user2).approve(
await ExchangeContractAsUser.getAddress(),
100
);

expect(await ERC1155Contract.balanceOf(user1.address, 1)).to.be.equal(10);
expect(await ERC20Contract.balanceOf(user2.address)).to.be.equal(100);

const makerAssetForLeftOrder = await AssetERC1155(ERC1155Contract, 1, 10);
const takerAssetForLeftOrder = await AssetERC20(ERC20Contract, 100);
const takerAssetForRightOrder = await AssetERC20(ERC20Contract, 50);
const makerAssetForRightOrder = await AssetERC1155(ERC1155Contract, 1, 5);
const leftOrder = await OrderDefault(
user1,
makerAssetForLeftOrder,
ZeroAddress,
takerAssetForLeftOrder,
1,
0,
0
);
const rightOrder = await OrderDefault(
user2,
takerAssetForRightOrder,
ZeroAddress,
makerAssetForRightOrder,
1,
0,
0
);
const makerSig = await signOrder(
leftOrder,
user1,
OrderValidatorAsDeployer
);
const takerSig = await signOrder(
rightOrder,
user2,
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(
50
);
expect(await ExchangeContractAsUser.fills(hashKey(rightOrder))).to.be.equal(
5
);
expect(await ERC1155Contract.balanceOf(user1.address, 1)).to.be.equal(5);
expect(await ERC1155Contract.balanceOf(user2.address, 1)).to.be.equal(5);
expect(await ERC20Contract.balanceOf(user2.address)).to.be.equal(50);
// 49 = 50 -originFee
expect(await ERC20Contract.balanceOf(user1.address)).to.be.equal(49);
});

it('should revert for matching a cancelled order', async function () {
const {
ExchangeContractAsUser,
OrderValidatorAsDeployer,
ERC20Contract,
ERC721Contract,
user1,
user2,
} = await loadFixture(deployFixtures);
await ERC721Contract.mint(user1.address, 1);
await ERC721Contract.connect(user1).approve(
await ExchangeContractAsUser.getAddress(),
1
);
await ERC20Contract.mint(user2.address, 100);
await ERC20Contract.connect(user2).approve(
await ExchangeContractAsUser.getAddress(),
100
);

expect(await ERC721Contract.ownerOf(1)).to.be.equal(user1.address);
expect(await ERC20Contract.balanceOf(user2.address)).to.be.equal(100);
const makerAsset = await AssetERC721(ERC721Contract, 1);
const takerAsset = await AssetERC20(ERC20Contract, 100);
const leftOrder = await OrderDefault(
user1,
makerAsset,
ZeroAddress,
takerAsset,
1,
0,
0
);
const rightOrder = await OrderDefault(
user2,
takerAsset,
ZeroAddress,
makerAsset,
1,
0,
0
);
const makerSig = await signOrder(
leftOrder,
user1,
OrderValidatorAsDeployer
);
const takerSig = await signOrder(
rightOrder,
user2,
OrderValidatorAsDeployer
);
await ExchangeContractAsUser.connect(user1).cancel(
leftOrder,
hashKey(leftOrder)
);
await expect(
ExchangeContractAsUser.matchOrders(
leftOrder,
makerSig,
rightOrder,
takerSig
)
).to.be.reverted;
});
});
4 changes: 2 additions & 2 deletions packages/marketplace/test/exchange/OrderValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('OrderValidator.sol', function () {
user1,
} = await loadFixture(deployFixtures);

const makerAsset = await AssetERC721(ERC721Contract, 1, 1);
const makerAsset = await AssetERC721(ERC721Contract, 1);
const takerAsset = await AssetERC20(ERC20Contract, 100);
const order = await OrderBack(
user2,
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('OrderValidator.sol', function () {
user1,
} = await loadFixture(deployFixtures);

const makerAsset = await AssetERC721(ERC721Contract, 1, 1);
const makerAsset = await AssetERC721(ERC721Contract, 1);
const takerAsset = await AssetERC20(ERC20Contract, 100);
const order = await OrderBack(
user2,
Expand Down
5 changes: 5 additions & 0 deletions packages/marketplace/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export async function deployFixtures() {
const ERC721Contract = await ERC721ContractFactory.deploy();
await ERC721Contract.waitForDeployment();

const ERC1155ContractFactory = await ethers.getContractFactory('TestERC1155');
const ERC1155Contract = await ERC1155ContractFactory.deploy();
await ERC1155Contract.waitForDeployment();

// TODO: Do we always want this?
await ExchangeContractAsDeployer.setAssetMatcherContract(
await assetMatcherAsDeployer.getAddress()
Expand All @@ -69,6 +73,7 @@ export async function deployFixtures() {
TrustedForwarder,
ERC20Contract,
ERC721Contract,
ERC1155Contract,
OrderValidatorAsDeployer,
OrderValidatorAsUser,
deployer,
Expand Down
9 changes: 4 additions & 5 deletions packages/marketplace/test/utils/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ export const AssetERC20 = async (

export const AssetERC721 = async (
tokenContract: Contract,
tokenId: number,
value: number
tokenId: number
): Promise<Asset> => ({
assetType: {
assetClass: ERC721_ASSET_CLASS,
Expand All @@ -73,7 +72,7 @@ export const AssetERC721 = async (
[await tokenContract.getAddress(), tokenId]
),
},
value: value,
value: 1,
});

export const AssetERC1155 = async (
Expand Down Expand Up @@ -103,12 +102,12 @@ export const AssetBundle = async (
value: x.value,
});
}
const erc721Details: {token: string; id: number; value: number}[] = [];
const erc721Details: {token: string; id: number}[] = [];
for (const x of erc721) {
erc20Details.push({
token: await x.tokenContract.getAddress(),
id: x.tokenId,
value: x.value,
value: 1,
});
}
const erc1155Details: {token: string; id: number; value: number}[] = [];
Expand Down

1 comment on commit 0c04ae9

@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

30.11%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/marketplace/contracts/exchange
   AssetMatcher.sol78.08%73.33%100%79.49%49–50, 50, 50–51, 53, 56, 59, 73–74, 74, 74–75, 77, 80–81
   Exchange.sol83.33%83.33%75%87.50%33, 56
   ExchangeCore.sol46.78%32.56%72.73%52%129–132, 144, 146, 159, 159, 159, 161, 173, 184–186, 192, 194, 206, 218–219, 251–252, 252, 252, 252, 252, 254–255, 255, 255, 255, 255, 297, 297, 297–298, 298, 298–299, 299, 299, 301–302, 302, 302–303, 303, 303–304, 304, 304–305, 307–308, 308, 308–309, 309, 309–310, 336–337, 339–340, 396, 405–407, 407, 407–409, 409, 409–410, 410–412, 412, 412, 414, 416, 416, 416, 418, 429, 432–433, 437–438, 441, 469, 471–473, 479–481, 496–497, 514, 530–531, 531, 531–532, 534–535, 537, 544, 544, 544–545, 547, 547, 547–548, 550, 73
   ExchangeMeta.sol0%0%0%0%33, 33–36, 42, 52, 56, 61, 61–62, 62, 62, 64
   OrderValidator.sol77.14%64.71%100%86.21%108–109, 114, 116, 116, 116, 116–117, 119, 37, 80, 87–88, 88, 88, 98
   WhiteList.sol97.62%93.75%100%100%50
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.sol21.67%22.22%40%18.92%18–28, 30–31, 33, 46, 48, 48, 48–49, 51–52, 55, 55, 55–56, 58, 61, 61, 61–62, 64, 67, 71, 73, 73, 73–75, 78, 87, 87, 87–89
packages/marketplace/contracts/exchange/mocks
   ERC1155LazyMintTest.sol100%100%100%100%
   ERC721LazyMintTest.sol100%100%100%100%
   ExchangeSimple.sol0%0%0%0%15, 15–17, 27, 31
   ExchangeSimple1.sol0%100%0%0%9
   ExchangeTestImports.sol100%100%100%100%
   LibFillTest.sol0%100%0%0%17
   LibOrderTest.sol0%100%0%0%13, 17, 21, 25, 35, 47
   MockTrustedForwarder.sol0%0%0%0%14, 17–18, 18, 18–19
   OrderValidatorTest.sol0%0%0%0%13, 8, 8–9
   RaribleTestHelper.sol0%100%0%0%11, 15, 19, 23, 33
   SimpleTransferManager.sol0%100%0%0%13–16
   TestAssetMatcher.sol0%0%0%0%12, 12, 12–15, 15, 15–16, 19
   TestERC1155WithRoyaltyV2981.sol100%100%100%100%
   TestERC1271.sol0%0%0%0%24, 28, 28, 28
   TestERC20.sol100%100%100%100%
   TestERC721.sol100%100%100%100%
   TestERC721WithRoyaltyV2981.sol100%100%100%100%
   TestERC721WithRoyaltyV2981Multi.sol100%100%100%100%
   TestMinimalForwarder.sol0%0%0%0%12–15, 15, 15–16
   TestRoyaltiesRegistry.sol18%16.67%25%17.86%21–23, 23, 23–24, 24, 24–26, 28, 28, 28–29, 33, 38–39, 42–46, 50, 62–65, 65, 65–66, 66, 66–68, 70, 70, 70–71
   TransferManagerTest.sol0%0%0%0%107, 110–111, 115–116, 119, 126–127, 129, 21, 21–23, 35, 46–47, 72, 72, 72, 78, 81–83, 83, 83–85, 85, 85–86, 86–88, 88, 88, 90, 92, 92, 92, 94
packages/marketplace/contracts/exchange/mocks/tokens
   ERC2981.sol0%0%0%0%106, 38, 38, 38, 45, 47, 47, 47–48, 51, 53, 62, 74, 74, 74–75, 75, 75, 77, 84, 96, 96, 96–97, 97, 97, 99
   MintableERC1155.sol0%100%0%0%10, 14
   MintableERC1155WithRoyalties.sol0%0%0%0%13, 17, 21, 25, 9, 9, 9
   MintableERC20.sol0%100%0%0%10
   MintableERC721.sol0%100%0%0%10
   MintableERC721WithRoyalties.sol0%0%0%0%13, 17, 21, 25, 9, 9, 9
packages/marketplace/contracts/interfaces
   

Please sign in to comment.