I am facing this issue called “before each” hook
can’t fix this eth test file issue
i have no idea if it is some version issue or something please help me, i tried changing ethers version as well as chai version
test.js
const { expect } = require("chai");
const toWei = (num) => ethers.utils.parseEther(num.toString())
const fromWei = (num) => ethers.utils.formatEther(num)
describe("NFTMarketplace", function () {
let NFT;
let nft;
let Marketplace;
let marketplace
let deployer;
let addr1;
let addr2;
let addrs;
let feePercent = 1;
let URI = "sample URI"
beforeEach(async function () {
NFT = await ethers.getContractFactory("NFT");
Marketplace = await ethers.getContractFactory("Marketplace");
[deployer, addr1, addr2, ...addrs] = await ethers.getSigners();
nft = await NFT.deploy();
marketplace = await Marketplace.deploy(feePercent);
});
describe("Deployment", function () {
it("Should track name and symbol of the nft collection", async function () {
const nftName = "DApp NFT"
const nftSymbol = "DAPP"
expect(await nft.name()).to.equal(nftName);
expect(await nft.symbol()).to.equal(nftSymbol);
});
it("Should track feeAccount and feePercent of the marketplace", async function () {
expect(await marketplace.feeAccount()).to.equal(deployer.address);
expect(await marketplace.feePercent()).to.equal(feePercent);
});
});
describe("Minting NFTs", function () {
it("Should track each minted NFT", async function () {
await nft.connect(addr1).mint(URI)
expect(await nft.tokenCount()).to.equal(1);
expect(await nft.balanceOf(addr1.address)).to.equal(1);
expect(await nft.tokenURI(1)).to.equal(URI);
await nft.connect(addr2).mint(URI)
expect(await nft.tokenCount()).to.equal(2);
expect(await nft.balanceOf(addr2.address)).to.equal(1);
expect(await nft.tokenURI(2)).to.equal(URI);
});
})
describe("Making marketplace items", function () {
let price = 1
let result
beforeEach(async function () {
await nft.connect(addr1).mint(URI)
await nft.connect(addr1).setApprovalForAll(marketplace.address, true)
await nftAddress
await marketplaceAddress
})
it("Should track newly created item, transfer NFT from seller to marketplace and emit Offered event", async function () {
await expect(marketplace.connect(addr1).makeItem(nftAddress, 1 , toWei(price)))
.to.emit(marketplace, "Offered")
.withArgs(
1,
nft.address,
1,
toWei(price),
addr1.address
)
expect(await nft.ownerOf(1)).to.equal(marketplace.address);
expect(await marketplace.itemCount()).to.equal(1)
const item = await marketplace.items(1)
expect(item.itemId).to.equal(1)
expect(item.nft).to.equal(nft.address)
expect(item.tokenId).to.equal(1)
expect(item.price).to.equal(toWei(price))
expect(item.sold).to.equal(false)
});
it("Should fail if price is set to zero", async function () {
await expect(
marketplace.connect(addr1).makeItem(nft.address, 1, 0)
).to.be.revertedWith("Price must be greater than zero");
});
});
describe("Purchasing marketplace items", function () {
let price = 2
let fee = (feePercent/100)*price
let totalPriceInWei
beforeEach(async function () {
await nft.connect(addr1).mint(URI)
await nft.connect(addr1).setApprovalForAll(marketplace.address, true)
await marketplace.connect(addr1).makeItem(nft.address, 1 , toWei(price))
})
it("Should update item as sold, pay seller, transfer NFT to buyer, charge fees and emit a Bought event", async function () {
const sellerInitalEthBal = await addr1.getBalance()
const feeAccountInitialEthBal = await deployer.getBalance()
totalPriceInWei = await marketplace.getTotalPrice(1);
await expect(marketplace.connect(addr2).purchaseItem(1, {value: totalPriceInWei}))
.to.emit(marketplace, "Bought")
.withArgs(
1,
nft.address,
1,
toWei(price),
addr1.address,
addr2.address
)
const sellerFinalEthBal = await addr1.getBalance()
const feeAccountFinalEthBal = await deployer.getBalance()
expect((await marketplace.items(1)).sold).to.equal(true)
expect(+fromWei(sellerFinalEthBal)).to.equal(+price + +fromWei(sellerInitalEthBal))
expect(+fromWei(feeAccountFinalEthBal)).to.equal(+fee + +fromWei(feeAccountInitialEthBal))
expect(await nft.ownerOf(1)).to.equal(addr2.address);
})
it("Should fail for invalid item ids, sold items and when not enough ether is paid", async function () {
await expect(
marketplace.connect(addr2).purchaseItem(2, {value: totalPriceInWei})
).to.be.revertedWith("item doesn't exist");
await expect(
marketplace.connect(addr2).purchaseItem(0, {value: totalPriceInWei})
).to.be.revertedWith("item doesn't exist");
await expect(
marketplace.connect(addr2).purchaseItem(1, {value: toWei(price)})
).to.be.revertedWith("not enough ether to cover item price and market fee");
await marketplace.connect(addr2).purchaseItem(1, {value: totalPriceInWei})
const addr3 = addrs[0]
await expect(
marketplace.connect(addr3).purchaseItem(1, {value: totalPriceInWei})
).to.be.revertedWith("item already sold");
});
})
})
also solidity contructor:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract Marketplace is ReentrancyGuard {
address payable public immutable feeAccount;
uint public immutable feePercent;
uint public itemCount;
struct Item{
uint itemId;
IERC721 nft;
uint tokenId;
uint price;
address payable seller;
bool sold;
}
mapping(uint=>Item) public items;
event offered (
uint itemId,
address indexed nft,
uint tokenId,
uint price,
address indexed seller
);
event Bought(
uint itemId,
address indexed nft,
uint tokenId,
uint price,
address indexed seller,
address indexed buyer
);
constructor(uint _feePercent) {
feeAccount = payable(msg.sender);
feePercent = _feePercent;
}
function makeItem(IERC721 _nft, uint _tokenId, uint _price) external nonReentrant {
require(_price >0,"Price must be greater than 0.");
itemCount++;
_nft.transferFrom(msg.sender,address(this),_tokenId);
items[itemCount] = Item(
itemCount,
_nft,
_tokenId,
_price,
payable(msg.sender),
false
);
emit offered(itemCount, address(_nft), _tokenId, _price, msg.sender);
}
function purchaseItem(uint _itemId) external payable nonReentrant {
uint _totalPrice = getTotalPrice(_itemId);
Item storage item = items[_itemId];
require(_itemId > 0 && _itemId <= itemCount, "item doesn't exist");
require(msg.value >= _totalPrice, "not enough ether to cover item price and market fee");
require(!item.sold, "item already sold");
// pay seller and feeAccount
item.seller.transfer(item.price);
feeAccount.transfer(_totalPrice - item.price);
// update item to sold
item.sold = true;
// transfer nft to buyer
item.nft.transferFrom(address(this), msg.sender, item.tokenId);
// emit Bought event
emit Bought(
_itemId,
address(item.nft),
item.tokenId,
item.price,
item.seller,
msg.sender
);
}
function getTotalPrice(uint _itemId) view public returns(uint){
return((items[_itemId].price*(100 + feePercent))/100);
}
}
error/output
➜ nft-mplace git:(master) ✗ npx hardhat test
Compiled 2 Solidity files successfully (evm target: paris).
NFTMarketplace
Deployment
✔ Should track name and symbol of the nft collection
✔ Should track feeAccount and feePercent of the marketplace
Minting NFTs
✔ Should track each minted NFT
Making marketplace items
1) "before each" hook for "Should track newly created item, transfer NFT from seller to marketplace and emit Offered event"
Purchasing marketplace items
2) "before each" hook for "Should update item as sold, pay seller, transfer NFT to buyer, charge fees and emit a Bought event"
3 passing (611ms)
2 failing
1) NFTMarketplace
Making marketplace items
"before each" hook for "Should track newly created item, transfer NFT from seller to marketplace and emit Offered event":
TypeError: unsupported addressable value (argument="target", value=null, code=INVALID_ARGUMENT, version=6.13.1)
at makeError (node_modules/ethers/src.ts/utils/errors.ts:687:21)
at assert (node_modules/ethers/src.ts/utils/errors.ts:715:25)
at assertArgument (node_modules/ethers/src.ts/utils/errors.ts:727:5)
at resolveAddress (node_modules/ethers/src.ts/address/checks.ts:122:19)
at /home/imanav10/projects/nft-mplace/node_modules/ethers/src.ts/contract/contract.ts:172:60
at ParamType.#walkAsync (node_modules/ethers/src.ts/abi/fragments.ts:779:24)
at ParamType.walkAsync (node_modules/ethers/src.ts/abi/fragments.ts:797:24)
at /home/imanav10/projects/nft-mplace/node_modules/ethers/src.ts/contract/contract.ts:170:22
at Array.map (<anonymous>)
at resolveArgs (node_modules/ethers/src.ts/contract/contract.ts:169:37)
2) NFTMarketplace
Purchasing marketplace items
"before each" hook for "Should update item as sold, pay seller, transfer NFT to buyer, charge fees and emit a Bought event":
TypeError: unsupported addressable value (argument="target", value=null, code=INVALID_ARGUMENT, version=6.13.1)
at makeError (node_modules/ethers/src.ts/utils/errors.ts:687:21)
at assert (node_modules/ethers/src.ts/utils/errors.ts:715:25)
at assertArgument (node_modules/ethers/src.ts/utils/errors.ts:727:5)
at resolveAddress (node_modules/ethers/src.ts/address/checks.ts:122:19)
at /home/imanav10/projects/nft-mplace/node_modules/ethers/src.ts/contract/contract.ts:172:60
at ParamType.#walkAsync (node_modules/ethers/src.ts/abi/fragments.ts:779:24)
at ParamType.walkAsync (node_modules/ethers/src.ts/abi/fragments.ts:797:24)
at /home/imanav10/projects/nft-mplace/node_modules/ethers/src.ts/contract/contract.ts:170:22
at Array.map (<anonymous>)
at resolveArgs (node_modules/ethers/src.ts/contract/contract.ts:169:37)
➜ nft-mplace git:(master) ✗
i tried to add some debugging statement but got the same output
for example i was trying to fix:
const nftAddress = await nft.getAddress();
console.log("NFT Address:", nftAddress);
const marketplaceAddress = await marketplace.getAddress();
console.log("Marketplace Address:", marketplaceAddress);
await nft.connect(addr1).mint(URI);
await nft.connect(addr1).setApprovalForAll(marketplace.address, true);
it("Should track newly created item, transfer NFT from seller to marketplace and emit Offered event", async function () {
await expect(marketplace.connect(addr1).makeItem(nftAddress, 1, toWei(price)))
.to.emit(marketplace, "Offered")
.withArgs(
1,
nftAddress,
1,
toWei(price),
addr1.address
);
expect(await nft.ownerOf(1)).to.equal(marketplaceAddress);
expect(await marketplace.itemCount()).to.equal(1);
const item = await marketplace.items(1);
expect(item.itemId).to.equal(1);
expect(item.nft).to.equal(nftAddress);
expect(item.tokenId).to.equal(1);
expect(item.price).to.equal(toWei(price));
expect(item.sold).to.equal(false);
});
it("Should fail if price is set to zero", async function () {
await expect(
marketplace.connect(addr1).makeItem(nftAddress, 1, 0)
).to.be.revertedWith("Price must be greater than zero");
});
New contributor
Manav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.