/* eslint-disable no-undef */
/* eslint-disable no-underscore-dangle */
const { expect } = require("chai");
const { ethers } = require("hardhat");
const toWei = (num) => ethers.utils.parseEther(num.toString());
const fromWei = (num) => ethers.utils.formatEther(num);
// eslint-disable-next-line no-undef
describe("NFTMarketplace", () => {
let NFTContract;
let MarketplaceContarct;
let deployer;
let addr1;
let addr2;
let addr3;
let addr4;
let addr5;
let addr6;
let companyAddress;
const URI = "sample URI";
// eslint-disable-next-line no-undef
beforeEach(async () => {
[deployer, addr1, addr2, addr3, addr4, addr5, addr6, companyAddress] =
await ethers.getSigners();
const NFT = await ethers.getContractFactory("NFTContract");
NFTContract = await NFT.deploy();
await NFTContract.deployed();
const marketPlace = await ethers.getContractFactory("marketPlace");
MarketplaceContarct = await marketPlace.deploy();
await MarketplaceContarct.deployed();
// set marketplace address
await NFTContract.setMarketPlace(MarketplaceContarct.address);
// set NFTContract address
await MarketplaceContarct.setNFTContractInstance(NFTContract.address);
// set company companyAddress
await MarketplaceContarct.setCompanyAddress(companyAddress.address);
});
// eslint-disable-next-line no-undef
describe("Deployment", () => {
// eslint-disable-next-line no-undef
it("Should track name and symbol of the nft collection", async () => {
// This test expects the owner variable stored in the contract to be equal
// to our Signer's owner.
const nftName = "DAPP";
const nftSymbol = "DAPP";
expect(await NFTContract.name()).to.equal(nftName);
expect(await NFTContract.symbol()).to.equal(nftSymbol);
});
});
// eslint-disable-next-line no-undef
describe("Minting NFTs", () => {
// eslint-disable-next-line no-undef
it("Should track each minted NFT", async () => {
// addr1 mints an nft
await NFTContract.connect(addr1).mintToken("sample URI");
expect(await NFTContract.NFTItemTracker()).to.equal(1);
expect(await NFTContract.balanceOf(addr1.address)).to.equal(1);
expect(await NFTContract.tokenURI(1)).to.equal(URI);
// addr2 mints an nft
await NFTContract.connect(addr2).mintToken("sample URI");
expect(await NFTContract.NFTItemTracker()).to.equal(2);
expect(await NFTContract.balanceOf(addr2.address)).to.equal(1);
expect(await NFTContract.tokenURI(2)).to.equal(URI);
});
});
// eslint-disable-next-line no-undef
describe("Making marketplace items", () => {
const price = 1;
// eslint-disable-next-line no-undef
beforeEach(async () => {
// addr1 mints an nft
await NFTContract.connect(addr1).mintToken(URI);
await NFTContract.connect(addr2).mintToken(URI);
});
// eslint-disable-next-line no-undef
it("Should track newly created item, transfer NFT from seller to marketplace and emit Offered event", async () => {
// addr1 list his NFT in market
await expect(
MarketplaceContarct.connect(addr1).listNFTItem(
NFTContract.address,
1,
toWei(price)
)
)
.to.emit(MarketplaceContarct, "NFTItemListed")
.withArgs(1, NFTContract.address, 1, toWei(price), addr1.address);
// addr2 list his NFT in market
await expect(
MarketplaceContarct.connect(addr2).listNFTItem(
NFTContract.address,
2,
toWei(price)
)
)
.to.emit(MarketplaceContarct, "NFTItemListed")
.withArgs(2, NFTContract.address, 2, toWei(price), addr2.address);
// Owner of NFT should now be the marketplace
expect(await NFTContract.ownerOf(1)).to.equal(
MarketplaceContarct.address
);
expect(await NFTContract.ownerOf(2)).to.equal(
MarketplaceContarct.address
);
// Item count in marketplace should now equal 2 because we listed 2 NFT
// eslint-disable-next-line no-underscore-dangle
expect(await MarketplaceContarct._itemTracker()).to.equal(2);
// now check somethings from NFT item 1
// eslint-disable-next-line no-underscore-dangle
const item = await MarketplaceContarct._NFTItem(1);
expect(item.itemId).to.equal(1);
expect(item.nft).to.equal(NFTContract.address);
expect(item.tokenId).to.equal(1);
expect(item.price).to.equal(toWei(price));
expect(item.sold).to.equal(false);
expect(item.seller).to.equal(addr1.address);
// 2 NFT tokens should listed !
expect(await MarketplaceContarct.getTotalItemsListed()).to.equal(2);
// check company address for items;
expect((await MarketplaceContarct.getNFTItemFromMarket(1))[5]).to.equal(
companyAddress.address
);
expect((await MarketplaceContarct.getNFTItemFromMarket(2))[5]).to.equal(
companyAddress.address
);
});
// eslint-disable-next-line no-undef
it("Should fail", async () => {
// problem with price
await expect(
MarketplaceContarct.connect(addr1).listNFTItem(
NFTContract.address,
1,
0
)
).to.be.revertedWith("Price must be greater than zero");
// problem with owner of NFT
await expect(
MarketplaceContarct.connect(addr2).listNFTItem(
NFTContract.address,
1,
toWei(price)
)
).to.be.revertedWith("You are not owner of this NFT");
});
});
describe("Purchasing marketplace items", () => {
const price = 2;
let totalPriceInWei;
beforeEach(async () => {
// addr1 mints 2 nfts
await NFTContract.connect(addr1).mintToken(URI);
await NFTContract.connect(addr1).mintToken(URI);
// addr1 makes their nft a marketplace item.
await MarketplaceContarct.connect(addr1).listNFTItem(
NFTContract.address,
1,
toWei(price)
);
await MarketplaceContarct.connect(addr1).listNFTItem(
NFTContract.address,
2,
toWei(price)
);
});
it("Should update item as sold, pay seller, transfer NFT to buyer", async () => {
totalPriceInWei = await MarketplaceContarct.getItemTotalPrice(1);
// addr 2 purchases NFT 1.
await expect(
MarketplaceContarct.connect(addr2).buyNFTItem(1, {
value: totalPriceInWei,
})
)
.to.emit(MarketplaceContarct, "Bought")
.withArgs(
1,
NFTContract.address,
1,
toWei(price),
"0x0000000000000000000000000000000000000000",
addr2.address
);
// check item details after sell
const item = await MarketplaceContarct._NFTItem(1);
expect(item.sold).to.equal(true);
expect(item._companyAddress).to.equal(companyAddress.address);
expect(item.lastOwner).to.equal(addr1.address);
expect(item.owner).to.equal(addr2.address);
// check owner of NFT Item 1 by NFT contract
expect(await NFTContract.ownerOf(1)).to.equal(addr2.address);
// sold item counter in marketplace should be 1 now
expect(await MarketplaceContarct.getSoldCounter()).to.equal(1);
});
it("Should pay fee to company address and seller should get payment", async () => {
const companyAddressBalanceBefore = await companyAddress.getBalance();
const sellerBalanceBeforeMakeSell = await addr1.getBalance();
// addr 2 purchases NFT 1.
await expect(
MarketplaceContarct.connect(addr2).buyNFTItem(1, {
value: totalPriceInWei,
})
)
.to.emit(MarketplaceContarct, "Bought")
.withArgs(
1,
NFTContract.address,
1,
toWei(price),
"0x0000000000000000000000000000000000000000",
addr2.address
);
const companyAddressBalanceAfter = await companyAddress.getBalance();
const sellerBalanceAfterMakeSell = await addr1.getBalance();
// seller balance should increase
expect(sellerBalanceAfterMakeSell).to.be.above(
sellerBalanceBeforeMakeSell
);
// company address balance should increase because of sell !
expect(companyAddressBalanceAfter).to.be.above(
companyAddressBalanceBefore
);
});
it("resell NFT item and check details with owners array", async () => {
// get list of owners
let NFTOwnersArray;
// addr 2 buy NFT1
await MarketplaceContarct.connect(addr2).buyNFTItem(1, {
value: totalPriceInWei,
});
// owners of NFT #1 is 1 address , only addr1
NFTOwnersArray = await MarketplaceContarct.getOwners(1);
expect(NFTOwnersArray.length).to.equal(1);
// addr 2 resell NFT1
await NFTContract.connect(addr2).approve(MarketplaceContarct.address, 1);
await MarketplaceContarct.connect(addr2).reSellNFTItem(
NFTContract.address,
1,
toWei(price)
);
// addr3 come and buy NFT #1
await MarketplaceContarct.connect(addr3).buyNFTItem(1, {
value: totalPriceInWei,
});
// owners of NFT #1 is 1 address , only addr1, addr2
NFTOwnersArray = await MarketplaceContarct.getOwners(1);
expect(NFTOwnersArray.length).to.equal(2);
// addr 3 resell NFT1
await NFTContract.connect(addr3).approve(MarketplaceContarct.address, 1);
await MarketplaceContarct.connect(addr3).reSellNFTItem(
NFTContract.address,
1,
toWei(price)
);
// addr 2 buy NFT1
await MarketplaceContarct.connect(addr2).buyNFTItem(1, {
value: totalPriceInWei,
});
// owners of NFT #1 is 1 address , only addr1, addr2, addr3
NFTOwnersArray = await MarketplaceContarct.getOwners(1);
expect(NFTOwnersArray.length).to.equal(3);
// addr 2 resell NFT1
await NFTContract.connect(addr2).approve(MarketplaceContarct.address, 1);
await MarketplaceContarct.connect(addr2).reSellNFTItem(
NFTContract.address,
1,
toWei(price)
);
// addr3 come and buy NFT #1
await MarketplaceContarct.connect(addr3).buyNFTItem(1, {
value: totalPriceInWei,
});
// owners of NFT #1 is 1 address , only addr1, addr2, addr3
NFTOwnersArray = await MarketplaceContarct.getOwners(1);
expect(NFTOwnersArray.length).to.equal(3);
});
});
});