Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Add tests for InvalidNativeOfferItem and modify existing tests with an eth offer item
  • Loading branch information
d1ll0n committed Jun 8, 2022
1 parent cedddc7 commit 7aea41d
Showing 1 changed file with 239 additions and 42 deletions.
281 changes: 239 additions & 42 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const {
utils: { parseEther, keccak256, toUtf8Bytes },
} = require("ethers");
const { ethers, network } = require("hardhat");
const { faucet, whileImpersonating } = require("./utils/impersonate");
const {
faucet,
whileImpersonating,
getWalletWithEther,
} = require("./utils/impersonate");
const { merkleTree } = require("./utils/criteria");
const {
randomHex,
Expand Down Expand Up @@ -670,7 +674,19 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function

const offer = [getTestItem721(nftId)];

const consideration = [getItemETH(toBN(1), toBN(1), seller.address)];
const consideration = [
getItemETH(toBN(1), toBN(1), constants.AddressZero),
];
console.log(Object.keys(marketplaceContract.interface.functions));
for (const signature of Object.keys(
marketplaceContract.interface.functions
)) {
console.log(
`${signature.slice(0, signature.indexOf("("))}: ${keccak256(
Buffer.from(signature, "utf8")
).slice(0, 10)}`
);
}

const { order, orderHash, value } = await createOrder(
seller,
Expand Down Expand Up @@ -5587,8 +5603,13 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
await set721ApprovalForAll(buyer, marketplaceContract.address, true);

const { root, proofs } = merkleTree(tokenIds);

const offer = [getItemETH(parseEther("10"), parseEther("10"))];
const tokenAmount = minRandom(100);
await mintAndApproveERC20(
seller,
marketplaceContract.address,
tokenAmount
);
const offer = [getTestItem20(tokenAmount, tokenAmount)];

const consideration = [
getTestItem721WithCriteria(root, toBN(1), toBN(1), seller.address),
Expand Down Expand Up @@ -5651,8 +5672,13 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
await set1155ApprovalForAll(buyer, marketplaceContract.address, true);

const { root, proofs } = merkleTree([nftId]);

const offer = [getItemETH(parseEther("10"), parseEther("10"))];
const tokenAmount = minRandom(100);
await mintAndApproveERC20(
seller,
marketplaceContract.address,
tokenAmount
);
const offer = [getTestItem20(tokenAmount, tokenAmount)];

const consideration = [
getTestItem1155WithCriteria(root, toBN(1), toBN(1), seller.address),
Expand Down Expand Up @@ -5709,12 +5735,17 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
});
it("Criteria-based wildcard consideration item (standard)", async () => {
// buyer mints nft
const nftId = await mint721(buyer);

// Seller approves marketplace contract to transfer NFTs
await set721ApprovalForAll(buyer, marketplaceContract.address, true);

const offer = [getItemETH(parseEther("10"), parseEther("10"))];
const nftId = await mintAndApprove721(
buyer,
marketplaceContract.address
);
const tokenAmount = minRandom(100);
await mintAndApproveERC20(
seller,
marketplaceContract.address,
tokenAmount
);
const offer = [getTestItem20(tokenAmount, tokenAmount)];

const consideration = [
getTestItem721WithCriteria(
Expand Down Expand Up @@ -11797,13 +11828,6 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
);

const offer = [
{
itemType: 0, // ETH
token: constants.AddressZero,
identifierOrCriteria: 0, // ignored for ETH
startAmount: parseEther("1"),
endAmount: parseEther("1"),
},
{
itemType: 2, // ERC721
token: testERC721.address,
Expand All @@ -11827,12 +11851,7 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
0 // FULL_OPEN
);

const offerComponents = [
[
[0, 0],
[0, 1],
],
];
const offerComponents = [[[0, 0]]];

const considerationComponents = [[[0, 0]], [[0, 1]], [[0, 2]]];

Expand Down Expand Up @@ -13464,7 +13483,7 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
return receipt;
});
});
it("Reverts when not enough ether is supplied as offer item (standard)", async () => {
it("Reverts when not enough ether is supplied as offer item (match)", async () => {
// NOTE: this is a ridiculous scenario, buyer is paying the seller's offer

// buyer mints nft
Expand All @@ -13476,36 +13495,50 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
const offer = [getItemETH(parseEther("10"), parseEther("10"))];

const consideration = [
{
itemType: 2, // ERC721
token: testERC721.address,
identifierOrCriteria: nftId,
startAmount: toBN(1),
endAmount: toBN(1),
recipient: seller.address,
},
getItemETH(parseEther("1"), parseEther("1"), seller.address),
getItemETH(parseEther("1"), parseEther("1"), zone.address),
getItemETH(parseEther("1"), parseEther("1"), owner.address),
];

const { order, orderHash } = await createOrder(
const { order, orderHash, value } = await createOrder(
seller,
zone,
offer,
consideration,
0 // FULL_OPEN
);

const { mirrorOrder, mirrorOrderHash } = await createMirrorBuyNowOrder(
buyer,
zone,
order
);

const fulfillments = defaultBuyNowMirrorFulfillment;

const executions = await simulateMatchOrders(
[order, mirrorOrder],
fulfillments,
owner,
value
);

expect(executions.length).to.equal(4);

await expect(
marketplaceContract.connect(buyer).fulfillOrder(order, toKey(false), {
value: toBN(1),
})
marketplaceContract
.connect(buyer)
.matchOrders([order, mirrorOrder], fulfillments, {
value: toBN(1),
})
).to.be.revertedWith("InsufficientEtherSupplied");

await expect(
marketplaceContract.connect(buyer).fulfillOrder(order, toKey(false), {
value: parseEther("9.999999"),
})
marketplaceContract
.connect(buyer)
.matchOrders([order, mirrorOrder], fulfillments, {
value: parseEther("9.999999"),
})
).to.be.revertedWith("InsufficientEtherSupplied");

await withBalanceChecks(
Expand All @@ -13515,7 +13548,7 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
async () => {
const tx = marketplaceContract
.connect(buyer)
.fulfillOrder(order, toKey(false), {
.matchOrders([order, mirrorOrder], fulfillments, {
value: parseEther("12"),
});
const receipt = await (await tx).wait();
Expand Down Expand Up @@ -15367,5 +15400,169 @@ describe(`Consideration (version: ${VERSION}) — initial test suite`, function
}
});
});

describe("ETH offer items", async () => {
let ethAmount;
const tokenAmount = minRandom(100);
let offer;
let consideration;
let seller;
let buyer;

before(async () => {
ethAmount = parseEther("1");
seller = await getWalletWithEther();
buyer = await getWalletWithEther();
zone = new ethers.Wallet(randomHex(32), provider);
offer = [getItemETH(ethAmount, ethAmount)];
consideration = [
getTestItem20(tokenAmount, tokenAmount, seller.address),
];
});

it("fulfillOrder reverts if any offer item is ETH", async () => {
const { order, value } = await createOrder(
seller,
zone,
offer,
consideration,
0 // FULL_OPEN
);

await expect(
marketplaceContract
.connect(buyer)
.fulfillOrder(order, toKey(false), { value })
).to.be.revertedWith("InvalidNativeOfferItem");
});

it("fulfillAdvancedOrder reverts if any offer item is ETH", async () => {
const { order } = await createOrder(
seller,
zone,
offer,
consideration,
0 // FULL_OPEN
);

await expect(
marketplaceContract
.connect(buyer)
.fulfillAdvancedOrder(order, [], toKey(false), buyer.address, {
value: ethAmount,
})
).to.be.revertedWith("InvalidNativeOfferItem");
});

it("fulfillAvailableOrders reverts if any offer item is ETH", async () => {
const { order } = await createOrder(
seller,
zone,
offer,
consideration,
0 // FULL_OPEN
);

await expect(
marketplaceContract
.connect(buyer)
.fulfillAvailableOrders(
[order],
[[[0, 0]]],
[[[0, 0]]],
toKey(false),
100,
{ value: ethAmount }
)
).to.be.revertedWith("InvalidNativeOfferItem");
});

it("fulfillAvailableAdvancedOrders reverts if any offer item is ETH", async () => {
const { order } = await createOrder(
seller,
zone,
offer,
consideration,
0 // FULL_OPEN
);

await expect(
marketplaceContract
.connect(buyer)
.fulfillAvailableAdvancedOrders(
[order],
[],
[[[0, 0]]],
[[[0, 0]]],
toKey(false),
buyer.address,
100,
{ value: ethAmount }
)
).to.be.revertedWith("InvalidNativeOfferItem");
});

it("matchOrders allows fulfilling with native offer items", async () => {
await mintAndApproveERC20(
buyer,
marketplaceContract.address,
tokenAmount
);

const { order } = await createOrder(
seller,
zone,
offer,
consideration,
0 // FULL_OPEN
);
const { mirrorOrder } = await createMirrorBuyNowOrder(
buyer,
zone,
order
);
const fulfillments = [
toFulfillment([[0, 0]], [[1, 0]]),
toFulfillment([[1, 0]], [[0, 0]]),
];

await marketplaceContract
.connect(owner)
.matchOrders([order, mirrorOrder], fulfillments, {
value: ethAmount,
});
});

it("matchAdvancedOrders allows fulfilling with native offer items", async () => {
await mintAndApproveERC20(
buyer,
marketplaceContract.address,
tokenAmount
);

const { order } = await createOrder(
seller,
zone,
offer,
consideration,
0 // FULL_OPEN
);
const { mirrorOrder } = await createMirrorBuyNowOrder(
buyer,
zone,
order
);
const fulfillments = [
toFulfillment([[0, 0]], [[1, 0]]),
toFulfillment([[1, 0]], [[0, 0]]),
];

await marketplaceContract
.connect(owner)
.matchAdvancedOrders([order, mirrorOrder], [], fulfillments, {
value: ethAmount,
});
});
});
});
});

0 comments on commit 7aea41d

Please sign in to comment.